Skip to content

Commit f7d3e05

Browse files
committed
added validation
1 parent a97ecb7 commit f7d3e05

File tree

3 files changed

+94
-79
lines changed

3 files changed

+94
-79
lines changed

src/java/org/apache/cassandra/config/DatabaseDescriptor.java

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,12 @@ else if (conf.max_value_size.toMebibytes() >= 2048)
12201220
// run audit logging options through sanitation and validation
12211221
if (conf.audit_logging_options != null)
12221222
setAuditLoggingOptions(conf.audit_logging_options);
1223+
1224+
// just run through the validation by setting current values back to their setters, so we are sure that their values are valid
1225+
DatabaseDescriptor.setGCLogThreshold((int) DatabaseDescriptor.getGCLogThreshold());
1226+
DatabaseDescriptor.setGCWarnThreshold((int) DatabaseDescriptor.getGCWarnThreshold());
1227+
DatabaseDescriptor.setGCConcurrentPhaseLogThreshold(DatabaseDescriptor.getGCConcurrentPhaseLogThreshold());
1228+
DatabaseDescriptor.setGCConcurrentPhaseWarnThreshold(DatabaseDescriptor.getGCConcurrentPhaseWarnThreshold());
12231229
}
12241230

12251231
@VisibleForTesting
@@ -4719,14 +4725,17 @@ public static long getGCLogThreshold()
47194725
return conf.gc_log_threshold.toMilliseconds();
47204726
}
47214727

4722-
public static void setGCLogThreshold(int gcLogThreshold)
4728+
public static void setGCLogThreshold(int threshold)
47234729
{
4724-
conf.gc_log_threshold = new DurationSpec.IntMillisecondsBound(gcLogThreshold);
4725-
}
4730+
if (threshold <= 0)
4731+
throw new ConfigurationException("Threshold value for gc_log_threshold must be greater than 0");
47264732

4727-
public static EncryptionContext getEncryptionContext()
4728-
{
4729-
return encryptionContext;
4733+
long gcWarnThresholdInMs = getGCWarnThreshold();
4734+
if (gcWarnThresholdInMs != 0 && threshold > gcWarnThresholdInMs)
4735+
throw new ConfigurationException("Threshold value for gc_log_threshold (" + threshold + ") must be less than gc_warn_threshold which is currently "
4736+
+ gcWarnThresholdInMs);
4737+
4738+
conf.gc_log_threshold = new DurationSpec.IntMillisecondsBound(threshold);
47304739
}
47314740

47324741
public static long getGCWarnThreshold()
@@ -4736,6 +4745,14 @@ public static long getGCWarnThreshold()
47364745

47374746
public static void setGCWarnThreshold(int threshold)
47384747
{
4748+
if (threshold < 0)
4749+
throw new ConfigurationException("Threshold value for gc_warn_threshold must be greater than or equal to 0");
4750+
4751+
long gcLogThresholdInMs = getGCLogThreshold();
4752+
if (threshold != 0 && threshold <= gcLogThresholdInMs)
4753+
throw new ConfigurationException("Threshold value for gc_warn_threshold (" + threshold + ") must be greater than gc_log_threshold which is currently "
4754+
+ gcLogThresholdInMs);
4755+
47394756
conf.gc_warn_threshold = new DurationSpec.IntMillisecondsBound(threshold);
47404757
}
47414758

@@ -4746,20 +4763,40 @@ public static int getGCConcurrentPhaseLogThreshold()
47464763

47474764
public static void setGCConcurrentPhaseLogThreshold(int threshold)
47484765
{
4766+
if (threshold <= 0)
4767+
throw new ConfigurationException("Threshold must be greater than 0");
4768+
4769+
long gcConcurrentPhaseWarnThresholdInMs = getGCConcurrentPhaseWarnThreshold();
4770+
if (gcConcurrentPhaseWarnThresholdInMs != 0 && threshold > gcConcurrentPhaseWarnThresholdInMs)
4771+
throw new ConfigurationException("Threshold value for gc_concurrent_phase_log_threshold (" + threshold + ") must be less than gc_concurrent_phase_warn_threshold which is currently "
4772+
+ gcConcurrentPhaseWarnThresholdInMs);
4773+
47494774
conf.gc_concurrent_phase_log_threshold = new DurationSpec.IntMillisecondsBound(threshold);
47504775
}
47514776

4752-
47534777
public static int getGCConcurrentPhaseWarnThreshold()
47544778
{
47554779
return conf.gc_concurrent_phase_warn_threshold.toMilliseconds();
47564780
}
47574781

47584782
public static void setGCConcurrentPhaseWarnThreshold(int threshold)
47594783
{
4784+
if (threshold < 0)
4785+
throw new ConfigurationException("Threshold value for gc_concurrent_phase_warn_threshold must be greater than or equal to 0");
4786+
4787+
long gcConcurrentPhaseLogThresholdInMs = getGCConcurrentPhaseLogThreshold();
4788+
if (threshold != 0 && threshold <= gcConcurrentPhaseLogThresholdInMs)
4789+
throw new ConfigurationException("Threshold value for gc_concurrent_phase_warn_threshold (" + threshold + ") must be greater than gc_concurrent_phase_log_threshold which is currently "
4790+
+ gcConcurrentPhaseLogThresholdInMs);
4791+
47604792
conf.gc_concurrent_phase_warn_threshold = new DurationSpec.IntMillisecondsBound(threshold);
47614793
}
47624794

4795+
public static EncryptionContext getEncryptionContext()
4796+
{
4797+
return encryptionContext;
4798+
}
4799+
47634800
public static boolean isCDCEnabled()
47644801
{
47654802
return conf.cdc_enabled;

src/java/org/apache/cassandra/service/GCInspector.java

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -408,15 +408,14 @@ private static long getFieldValue(Field field, boolean isAtomicLong)
408408

409409
public void setGcWarnThresholdInMs(long threshold)
410410
{
411-
long gcLogThresholdInMs = getGcLogThresholdInMs();
412-
if (threshold < 0)
413-
throw new IllegalArgumentException("Threshold must be greater than or equal to 0");
414-
if (threshold != 0 && threshold <= gcLogThresholdInMs)
415-
throw new IllegalArgumentException("Threshold must be greater than gcLogThresholdInMs which is currently "
416-
+ gcLogThresholdInMs);
417-
if (threshold > Integer.MAX_VALUE)
418-
throw new IllegalArgumentException("Threshold must be less than Integer.MAX_VALUE");
419-
DatabaseDescriptor.setGCWarnThreshold((int)threshold);
411+
try
412+
{
413+
DatabaseDescriptor.setGCWarnThreshold((int)threshold);
414+
}
415+
catch (Throwable t)
416+
{
417+
throw new IllegalArgumentException(t.getMessage());
418+
}
420419
}
421420

422421
public long getGcWarnThresholdInMs()
@@ -426,15 +425,14 @@ public long getGcWarnThresholdInMs()
426425

427426
public void setGcLogThresholdInMs(long threshold)
428427
{
429-
if (threshold <= 0)
430-
throw new IllegalArgumentException("Threshold must be greater than 0");
431-
432-
long gcWarnThresholdInMs = getGcWarnThresholdInMs();
433-
if (gcWarnThresholdInMs != 0 && threshold > gcWarnThresholdInMs)
434-
throw new IllegalArgumentException("Threshold must be less than gcWarnThresholdInMs which is currently "
435-
+ gcWarnThresholdInMs);
436-
437-
DatabaseDescriptor.setGCLogThreshold((int) threshold);
428+
try
429+
{
430+
DatabaseDescriptor.setGCLogThreshold((int) threshold);
431+
}
432+
catch (Throwable t)
433+
{
434+
throw new IllegalArgumentException(t.getMessage());
435+
}
438436
}
439437

440438
public int getGcConcurrentPhaseWarnThresholdInMs()
@@ -444,13 +442,14 @@ public int getGcConcurrentPhaseWarnThresholdInMs()
444442

445443
public void setGcConcurrentPhaseWarnThresholdInMs(int threshold)
446444
{
447-
long gcConcurrentPhaseLogThresholdInMs = getGcConcurrentPhaseLogThresholdInMs();
448-
if (threshold < 0)
449-
throw new IllegalArgumentException("Threshold must be greater than or equal to 0");
450-
if (threshold != 0 && threshold <= gcConcurrentPhaseLogThresholdInMs)
451-
throw new IllegalArgumentException("Threshold must be greater than gcConcurrentPhaseLogThresholdInMs which is currently "
452-
+ gcConcurrentPhaseLogThresholdInMs);
453-
DatabaseDescriptor.setGCConcurrentPhaseWarnThreshold(threshold);
445+
try
446+
{
447+
DatabaseDescriptor.setGCConcurrentPhaseWarnThreshold(threshold);
448+
}
449+
catch (Throwable t)
450+
{
451+
throw new IllegalArgumentException(t.getMessage());
452+
}
454453
}
455454

456455
public int getGcConcurrentPhaseLogThresholdInMs()
@@ -460,15 +459,14 @@ public int getGcConcurrentPhaseLogThresholdInMs()
460459

461460
public void setGcConcurrentPhaseLogThresholdInMs(int threshold)
462461
{
463-
if (threshold <= 0)
464-
throw new IllegalArgumentException("Threshold must be greater than 0");
465-
466-
long gcConcurrentPhaseWarnThresholdInMs = getGcConcurrentPhaseWarnThresholdInMs();
467-
if (gcConcurrentPhaseWarnThresholdInMs != 0 && threshold > gcConcurrentPhaseWarnThresholdInMs)
468-
throw new IllegalArgumentException("Threshold must be less than gcConcurrentPhaseWarnThresholdInMs which is currently "
469-
+ gcConcurrentPhaseWarnThresholdInMs);
470-
471-
DatabaseDescriptor.setGCConcurrentPhaseLogThreshold(threshold);
462+
try
463+
{
464+
DatabaseDescriptor.setGCConcurrentPhaseLogThreshold(threshold);
465+
}
466+
catch (Throwable t)
467+
{
468+
throw new IllegalArgumentException(t.getMessage());
469+
}
472470
}
473471

474472
public long getGcLogThresholdInMs()

test/unit/org/apache/cassandra/service/GCInspectorTest.java

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
package org.apache.cassandra.service;
1919

2020
import org.apache.cassandra.config.DatabaseDescriptor;
21+
2122
import org.junit.Before;
2223
import org.junit.BeforeClass;
2324
import org.junit.Test;
2425

26+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2527
import static org.junit.Assert.assertEquals;
2628
import static org.junit.Assert.assertFalse;
2729
import static org.junit.Assert.assertTrue;
28-
import static org.junit.Assert.fail;
29-
3030

3131
public class GCInspectorTest
3232
{
@@ -63,24 +63,13 @@ public void ensureStatusIsCalculated()
6363
@Test
6464
public void ensureWarnGreaterThanLog()
6565
{
66-
try
67-
{
68-
gcInspector.setGcWarnThresholdInMs(gcInspector.getGcLogThresholdInMs());
69-
fail("Expect IllegalArgumentException");
70-
}
71-
catch (IllegalArgumentException e)
72-
{
73-
// expected
74-
}
75-
try
76-
{
77-
gcInspector.setGcConcurrentPhaseWarnThresholdInMs(gcInspector.getGcConcurrentPhaseLogThresholdInMs());
78-
fail("Expect IllegalArgumentException");
79-
}
80-
catch (IllegalArgumentException e)
81-
{
82-
// expected
83-
}
66+
assertThatThrownBy(() -> gcInspector.setGcWarnThresholdInMs(gcInspector.getGcLogThresholdInMs()))
67+
.isInstanceOf(IllegalArgumentException.class)
68+
.hasMessage("Threshold value for gc_warn_threshold (200) must be greater than gc_log_threshold which is currently 200");
69+
70+
assertThatThrownBy(() -> gcInspector.setGcConcurrentPhaseWarnThresholdInMs(gcInspector.getGcConcurrentPhaseLogThresholdInMs()))
71+
.isInstanceOf(IllegalArgumentException.class)
72+
.hasMessage("Threshold value for gc_concurrent_phase_warn_threshold (1000) must be greater than gc_concurrent_phase_log_threshold which is currently 1000");
8473
}
8574

8675
@Test
@@ -102,27 +91,18 @@ public void ensureLogLessThanWarn()
10291
assertEquals(200, gcInspector.getGcLogThresholdInMs());
10392
gcInspector.setGcWarnThresholdInMs(1000);
10493
assertEquals(1000, gcInspector.getGcWarnThresholdInMs());
105-
try
106-
{
107-
gcInspector.setGcLogThresholdInMs(gcInspector.getGcWarnThresholdInMs() + 1);
108-
fail("Expect IllegalArgumentException");
109-
}
110-
catch (IllegalArgumentException e)
111-
{
112-
// expected
113-
}
94+
95+
assertThatThrownBy(() -> gcInspector.setGcLogThresholdInMs(gcInspector.getGcWarnThresholdInMs() + 1))
96+
.isInstanceOf(IllegalArgumentException.class)
97+
.hasMessage("Threshold value for gc_log_threshold (1001) must be less than gc_warn_threshold which is currently 1000");
98+
11499
assertEquals(1000, gcInspector.getGcConcurrentPhaseLogThresholdInMs());
115100
gcInspector.setGcConcurrentPhaseWarnThresholdInMs(2000);
116101
assertEquals(2000, gcInspector.getGcConcurrentPhaseWarnThresholdInMs());
117-
try
118-
{
119-
gcInspector.setGcConcurrentPhaseLogThresholdInMs(gcInspector.getGcConcurrentPhaseWarnThresholdInMs() + 1);
120-
fail("Expect IllegalArgumentException");
121-
}
122-
catch (IllegalArgumentException e)
123-
{
124-
// expected
125-
}
102+
103+
assertThatThrownBy(() -> gcInspector.setGcConcurrentPhaseLogThresholdInMs(gcInspector.getGcConcurrentPhaseWarnThresholdInMs() + 1))
104+
.isInstanceOf(IllegalArgumentException.class)
105+
.hasMessage("Threshold value for gc_concurrent_phase_log_threshold (2001) must be less than gc_concurrent_phase_warn_threshold which is currently 2000");
126106
}
127107

128108
@Test

0 commit comments

Comments
 (0)