Skip to content

Commit cdc62ba

Browse files
authored
Merge pull request #46 from dhoard/fix/configuration-validation
fix: Fixed configuration validation
2 parents 581796e + 60488cb commit cdc62ba

5 files changed

Lines changed: 330 additions & 0 deletions

File tree

‎src/main/java/com/tidesdb/ColumnFamilyConfig.java‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,75 @@ public Builder objectPrefetchCompaction(boolean objectPrefetchCompaction) {
695695
* @return a new {@code ColumnFamilyConfig}
696696
*/
697697
public ColumnFamilyConfig build() {
698+
validate();
698699
return new ColumnFamilyConfig(this);
699700
}
701+
702+
private void validate() {
703+
// Nullable-enum checks (do first, before field-level checks)
704+
if (compressionAlgorithm == null) {
705+
throw new IllegalArgumentException("compressionAlgorithm must not be null");
706+
}
707+
if (syncMode == null) {
708+
throw new IllegalArgumentException("syncMode must not be null");
709+
}
710+
if (defaultIsolationLevel == null) {
711+
throw new IllegalArgumentException("defaultIsolationLevel must not be null");
712+
}
713+
714+
// Non-negative (zero sentinel OK)
715+
if (klogValueThreshold < 0) {
716+
throw new IllegalArgumentException("klogValueThreshold must not be negative, was: " + klogValueThreshold);
717+
}
718+
if (syncIntervalUs < 0) {
719+
throw new IllegalArgumentException("syncIntervalUs must not be negative, was: " + syncIntervalUs);
720+
}
721+
if (minDiskSpace < 0) {
722+
throw new IllegalArgumentException("minDiskSpace must not be negative, was: " + minDiskSpace);
723+
}
724+
if (dividingLevelOffset < 0) {
725+
throw new IllegalArgumentException("dividingLevelOffset must not be negative, was: " + dividingLevelOffset);
726+
}
727+
if (blockIndexPrefixLen < 0) {
728+
throw new IllegalArgumentException("blockIndexPrefixLen must not be negative, was: " + blockIndexPrefixLen);
729+
}
730+
if (skipListMaxLevel < 0) {
731+
throw new IllegalArgumentException("skipListMaxLevel must not be negative, was: " + skipListMaxLevel);
732+
}
733+
734+
// Positive-required (zero rejected)
735+
if (writeBufferSize <= 0) {
736+
throw new IllegalArgumentException("writeBufferSize must be positive, was: " + writeBufferSize);
737+
}
738+
if (levelSizeRatio <= 0) {
739+
throw new IllegalArgumentException("levelSizeRatio must be positive, was: " + levelSizeRatio);
740+
}
741+
if (minLevels <= 0) {
742+
throw new IllegalArgumentException("minLevels must be positive, was: " + minLevels);
743+
}
744+
if (indexSampleRatio <= 0) {
745+
throw new IllegalArgumentException("indexSampleRatio must be positive, was: " + indexSampleRatio);
746+
}
747+
if (l1FileCountTrigger <= 0) {
748+
throw new IllegalArgumentException("l1FileCountTrigger must be positive, was: " + l1FileCountTrigger);
749+
}
750+
if (l0QueueStallThreshold <= 0) {
751+
throw new IllegalArgumentException("l0QueueStallThreshold must be positive, was: " + l0QueueStallThreshold);
752+
}
753+
if (tombstoneDensityMinEntries <= 0) {
754+
throw new IllegalArgumentException("tombstoneDensityMinEntries must be positive, was: " + tombstoneDensityMinEntries);
755+
}
756+
757+
// Float/double range and finiteness
758+
if (Double.isNaN(bloomFPR) || Double.isInfinite(bloomFPR) || bloomFPR < 0.0 || bloomFPR > 1.0) {
759+
throw new IllegalArgumentException("bloomFPR must be finite and in [0.0, 1.0], was: " + bloomFPR);
760+
}
761+
if (Float.isNaN(skipListProbability) || Float.isInfinite(skipListProbability) || skipListProbability < 0.0f || skipListProbability > 1.0f) {
762+
throw new IllegalArgumentException("skipListProbability must be finite and in [0.0, 1.0], was: " + skipListProbability);
763+
}
764+
if (Double.isNaN(tombstoneDensityTrigger) || Double.isInfinite(tombstoneDensityTrigger) || tombstoneDensityTrigger < 0.0 || tombstoneDensityTrigger > 1.0) {
765+
throw new IllegalArgumentException("tombstoneDensityTrigger must be finite and in [0.0, 1.0], was: " + tombstoneDensityTrigger);
766+
}
767+
}
700768
}
701769
}

‎src/main/java/com/tidesdb/Config.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,31 @@ private void validate() throws IllegalArgumentException {
458458
if (maxOpenSSTables <= 0) {
459459
throw new IllegalArgumentException("Max open SSTables must be positive");
460460
}
461+
if (logTruncationAt < 0) {
462+
throw new IllegalArgumentException("logTruncationAt must not be negative, was: " + logTruncationAt);
463+
}
464+
if (maxMemoryUsage < 0) {
465+
throw new IllegalArgumentException("maxMemoryUsage must not be negative, was: " + maxMemoryUsage);
466+
}
467+
if (unifiedMemtableWriteBufferSize < 0) {
468+
throw new IllegalArgumentException("unifiedMemtableWriteBufferSize must not be negative, was: " + unifiedMemtableWriteBufferSize);
469+
}
470+
if (unifiedMemtableSyncIntervalUs < 0) {
471+
throw new IllegalArgumentException("unifiedMemtableSyncIntervalUs must not be negative, was: " + unifiedMemtableSyncIntervalUs);
472+
}
473+
if (unifiedMemtableSkipListMaxLevel < 0) {
474+
throw new IllegalArgumentException("unifiedMemtableSkipListMaxLevel must not be negative, was: " + unifiedMemtableSkipListMaxLevel);
475+
}
476+
if (Float.isNaN(unifiedMemtableSkipListProbability) || Float.isInfinite(unifiedMemtableSkipListProbability)
477+
|| unifiedMemtableSkipListProbability < 0.0f || unifiedMemtableSkipListProbability > 1.0f) {
478+
throw new IllegalArgumentException("unifiedMemtableSkipListProbability must be finite and in [0.0, 1.0], was: " + unifiedMemtableSkipListProbability);
479+
}
480+
if (unifiedMemtableSyncMode < 0) {
481+
throw new IllegalArgumentException("unifiedMemtableSyncMode must not be negative, was: " + unifiedMemtableSyncMode);
482+
}
483+
if (maxConcurrentFlushes < 0) {
484+
throw new IllegalArgumentException("maxConcurrentFlushes must not be negative, was: " + maxConcurrentFlushes);
485+
}
461486
}
462487
}
463488
}

‎src/main/java/com/tidesdb/ObjectStoreConfig.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,32 @@ public Builder replicaReplayWal(boolean replicaReplayWal) {
196196
}
197197

198198
public ObjectStoreConfig build() {
199+
validate();
199200
return new ObjectStoreConfig(this);
200201
}
202+
203+
private void validate() {
204+
if (localCacheMaxBytes < 0) {
205+
throw new IllegalArgumentException("localCacheMaxBytes must not be negative, was: " + localCacheMaxBytes);
206+
}
207+
if (maxConcurrentUploads <= 0) {
208+
throw new IllegalArgumentException("maxConcurrentUploads must be positive, was: " + maxConcurrentUploads);
209+
}
210+
if (maxConcurrentDownloads <= 0) {
211+
throw new IllegalArgumentException("maxConcurrentDownloads must be positive, was: " + maxConcurrentDownloads);
212+
}
213+
if (multipartThreshold < 0) {
214+
throw new IllegalArgumentException("multipartThreshold must not be negative, was: " + multipartThreshold);
215+
}
216+
if (multipartPartSize < 0) {
217+
throw new IllegalArgumentException("multipartPartSize must not be negative, was: " + multipartPartSize);
218+
}
219+
if (walSyncThresholdBytes < 0) {
220+
throw new IllegalArgumentException("walSyncThresholdBytes must not be negative, was: " + walSyncThresholdBytes);
221+
}
222+
if (replicaSyncIntervalUs < 0) {
223+
throw new IllegalArgumentException("replicaSyncIntervalUs must not be negative, was: " + replicaSyncIntervalUs);
224+
}
225+
}
201226
}
202227
}

‎src/main/java/com/tidesdb/S3Config.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ public S3Config build() {
157157
if (secretKey == null || secretKey.isEmpty()) {
158158
throw new IllegalArgumentException("S3 secret key is required");
159159
}
160+
if (multipartThreshold < 0) {
161+
throw new IllegalArgumentException("multipartThreshold must not be negative, was: " + multipartThreshold);
162+
}
163+
if (multipartPartSize < 0) {
164+
throw new IllegalArgumentException("multipartPartSize must not be negative, was: " + multipartPartSize);
165+
}
160166
return new S3Config(this);
161167
}
162168
}

‎src/test/java/com/tidesdb/TidesDBTest.java‎

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2699,6 +2699,212 @@ void testOpenWithoutObjectStoreFsPathSucceeds() throws TidesDBException {
26992699
}
27002700
}
27012701

2702+
@Test
2703+
@Order(75)
2704+
void testConfigValidationRejectsNegativeUnsignedFields() {
2705+
// logTruncationAt < 0
2706+
assertThrows(IllegalArgumentException.class,
2707+
() -> Config.builder(tempDir.toString()).logTruncationAt(-1).build());
2708+
try {
2709+
Config.builder(tempDir.toString()).logTruncationAt(-1).build();
2710+
} catch (IllegalArgumentException e) {
2711+
assertTrue(e.getMessage().contains("logTruncationAt"),
2712+
"message should mention field name, was: " + e.getMessage());
2713+
assertTrue(e.getMessage().toLowerCase().contains("negative"),
2714+
"message should mention negative, was: " + e.getMessage());
2715+
}
2716+
2717+
// maxMemoryUsage < 0
2718+
assertThrows(IllegalArgumentException.class,
2719+
() -> Config.builder(tempDir.toString()).maxMemoryUsage(-1).build());
2720+
try {
2721+
Config.builder(tempDir.toString()).maxMemoryUsage(-1).build();
2722+
} catch (IllegalArgumentException e) {
2723+
assertTrue(e.getMessage().contains("maxMemoryUsage"),
2724+
"message should mention field name, was: " + e.getMessage());
2725+
assertTrue(e.getMessage().toLowerCase().contains("negative"),
2726+
"message should mention negative, was: " + e.getMessage());
2727+
}
2728+
2729+
// unifiedMemtableWriteBufferSize < 0
2730+
assertThrows(IllegalArgumentException.class,
2731+
() -> Config.builder(tempDir.toString()).unifiedMemtableWriteBufferSize(-1).build());
2732+
try {
2733+
Config.builder(tempDir.toString()).unifiedMemtableWriteBufferSize(-1).build();
2734+
} catch (IllegalArgumentException e) {
2735+
assertTrue(e.getMessage().contains("unifiedMemtableWriteBufferSize"),
2736+
"message should mention field name, was: " + e.getMessage());
2737+
assertTrue(e.getMessage().toLowerCase().contains("negative"),
2738+
"message should mention negative, was: " + e.getMessage());
2739+
}
2740+
2741+
// unifiedMemtableSyncIntervalUs < 0
2742+
assertThrows(IllegalArgumentException.class,
2743+
() -> Config.builder(tempDir.toString()).unifiedMemtableSyncIntervalUs(-1).build());
2744+
try {
2745+
Config.builder(tempDir.toString()).unifiedMemtableSyncIntervalUs(-1).build();
2746+
} catch (IllegalArgumentException e) {
2747+
assertTrue(e.getMessage().contains("unifiedMemtableSyncIntervalUs"),
2748+
"message should mention field name, was: " + e.getMessage());
2749+
assertTrue(e.getMessage().toLowerCase().contains("negative"),
2750+
"message should mention negative, was: " + e.getMessage());
2751+
}
2752+
}
2753+
2754+
@Test
2755+
@Order(76)
2756+
void testObjectStoreConfigValidation() {
2757+
// Reject negative unsigned-native fields (zero sentinel accepted)
2758+
assertThrows(IllegalArgumentException.class,
2759+
() -> ObjectStoreConfig.builder().localCacheMaxBytes(-1).build());
2760+
assertThrows(IllegalArgumentException.class,
2761+
() -> ObjectStoreConfig.builder().multipartThreshold(-1).build());
2762+
assertThrows(IllegalArgumentException.class,
2763+
() -> ObjectStoreConfig.builder().multipartPartSize(-1).build());
2764+
assertThrows(IllegalArgumentException.class,
2765+
() -> ObjectStoreConfig.builder().walSyncThresholdBytes(-1).build());
2766+
assertThrows(IllegalArgumentException.class,
2767+
() -> ObjectStoreConfig.builder().replicaSyncIntervalUs(-1).build());
2768+
2769+
// Positive-required (<= 0 rejected)
2770+
assertThrows(IllegalArgumentException.class,
2771+
() -> ObjectStoreConfig.builder().maxConcurrentUploads(0).build());
2772+
try {
2773+
ObjectStoreConfig.builder().maxConcurrentUploads(0).build();
2774+
} catch (IllegalArgumentException e) {
2775+
assertTrue(e.getMessage().contains("maxConcurrentUploads"),
2776+
"message should mention field name, was: " + e.getMessage());
2777+
assertTrue(e.getMessage().toLowerCase().contains("positive"),
2778+
"message should mention positive, was: " + e.getMessage());
2779+
}
2780+
2781+
assertThrows(IllegalArgumentException.class,
2782+
() -> ObjectStoreConfig.builder().maxConcurrentDownloads(-1).build());
2783+
try {
2784+
ObjectStoreConfig.builder().maxConcurrentDownloads(-1).build();
2785+
} catch (IllegalArgumentException e) {
2786+
assertTrue(e.getMessage().contains("maxConcurrentDownloads"),
2787+
"message should mention field name, was: " + e.getMessage());
2788+
assertTrue(e.getMessage().toLowerCase().contains("positive"),
2789+
"message should mention positive, was: " + e.getMessage());
2790+
}
2791+
2792+
// Zero sentinel accepted for localCacheMaxBytes
2793+
assertDoesNotThrow(() -> ObjectStoreConfig.builder().localCacheMaxBytes(0).build());
2794+
}
2795+
2796+
@Test
2797+
@Order(77)
2798+
void testS3ConfigValidation() {
2799+
S3Config.Builder baseBuilder = S3Config.builder()
2800+
.endpoint("s3.amazonaws.com")
2801+
.bucket("b")
2802+
.accessKey("ak")
2803+
.secretKey("sk");
2804+
2805+
// Reject negative multipart fields
2806+
assertThrows(IllegalArgumentException.class,
2807+
() -> baseBuilder.multipartThreshold(-1).build());
2808+
assertThrows(IllegalArgumentException.class,
2809+
() -> S3Config.builder()
2810+
.endpoint("s3.amazonaws.com")
2811+
.bucket("b")
2812+
.accessKey("ak")
2813+
.secretKey("sk")
2814+
.multipartPartSize(-1)
2815+
.build());
2816+
2817+
// Zero sentinels accepted
2818+
assertDoesNotThrow(() -> S3Config.builder()
2819+
.endpoint("s3.amazonaws.com")
2820+
.bucket("b")
2821+
.accessKey("ak")
2822+
.secretKey("sk")
2823+
.multipartThreshold(0)
2824+
.multipartPartSize(0)
2825+
.build());
2826+
2827+
// Existing required-string validation still passes
2828+
assertThrows(IllegalArgumentException.class, () -> S3Config.builder().build());
2829+
}
2830+
2831+
@Test
2832+
@Order(78)
2833+
void testColumnFamilyConfigValidation() {
2834+
// Reject negative unsigned-native fields (zero sentinel accepted)
2835+
assertThrows(IllegalArgumentException.class,
2836+
() -> ColumnFamilyConfig.builder().klogValueThreshold(-1).build());
2837+
assertThrows(IllegalArgumentException.class,
2838+
() -> ColumnFamilyConfig.builder().syncIntervalUs(-1).build());
2839+
assertThrows(IllegalArgumentException.class,
2840+
() -> ColumnFamilyConfig.builder().minDiskSpace(-1).build());
2841+
2842+
// Positive-required fields (<= 0 rejected)
2843+
assertThrows(IllegalArgumentException.class,
2844+
() -> ColumnFamilyConfig.builder().writeBufferSize(0).build());
2845+
assertThrows(IllegalArgumentException.class,
2846+
() -> ColumnFamilyConfig.builder().levelSizeRatio(0).build());
2847+
assertThrows(IllegalArgumentException.class,
2848+
() -> ColumnFamilyConfig.builder().minLevels(0).build());
2849+
assertThrows(IllegalArgumentException.class,
2850+
() -> ColumnFamilyConfig.builder().indexSampleRatio(0).build());
2851+
assertThrows(IllegalArgumentException.class,
2852+
() -> ColumnFamilyConfig.builder().l1FileCountTrigger(0).build());
2853+
assertThrows(IllegalArgumentException.class,
2854+
() -> ColumnFamilyConfig.builder().l0QueueStallThreshold(0).build());
2855+
assertThrows(IllegalArgumentException.class,
2856+
() -> ColumnFamilyConfig.builder().tombstoneDensityMinEntries(0).build());
2857+
2858+
// Non-negative-int fields (zero acceptable, negative rejected)
2859+
assertThrows(IllegalArgumentException.class,
2860+
() -> ColumnFamilyConfig.builder().dividingLevelOffset(-1).build());
2861+
assertThrows(IllegalArgumentException.class,
2862+
() -> ColumnFamilyConfig.builder().blockIndexPrefixLen(-1).build());
2863+
assertThrows(IllegalArgumentException.class,
2864+
() -> ColumnFamilyConfig.builder().skipListMaxLevel(-1).build());
2865+
2866+
// Float/double NaN/infinity/range rejection: bloomFPR
2867+
assertThrows(IllegalArgumentException.class,
2868+
() -> ColumnFamilyConfig.builder().bloomFPR(Double.NaN).build());
2869+
assertThrows(IllegalArgumentException.class,
2870+
() -> ColumnFamilyConfig.builder().bloomFPR(Double.POSITIVE_INFINITY).build());
2871+
assertThrows(IllegalArgumentException.class,
2872+
() -> ColumnFamilyConfig.builder().bloomFPR(-0.1).build());
2873+
assertThrows(IllegalArgumentException.class,
2874+
() -> ColumnFamilyConfig.builder().bloomFPR(1.1).build());
2875+
2876+
// Float/double NaN/infinity/range rejection: skipListProbability
2877+
assertThrows(IllegalArgumentException.class,
2878+
() -> ColumnFamilyConfig.builder().skipListProbability(Float.NaN).build());
2879+
assertThrows(IllegalArgumentException.class,
2880+
() -> ColumnFamilyConfig.builder().skipListProbability(Float.POSITIVE_INFINITY).build());
2881+
assertThrows(IllegalArgumentException.class,
2882+
() -> ColumnFamilyConfig.builder().skipListProbability(-0.1f).build());
2883+
assertThrows(IllegalArgumentException.class,
2884+
() -> ColumnFamilyConfig.builder().skipListProbability(1.1f).build());
2885+
2886+
// Float/double NaN/infinity/range rejection: tombstoneDensityTrigger
2887+
assertThrows(IllegalArgumentException.class,
2888+
() -> ColumnFamilyConfig.builder().tombstoneDensityTrigger(Double.NaN).build());
2889+
assertThrows(IllegalArgumentException.class,
2890+
() -> ColumnFamilyConfig.builder().tombstoneDensityTrigger(Double.POSITIVE_INFINITY).build());
2891+
assertThrows(IllegalArgumentException.class,
2892+
() -> ColumnFamilyConfig.builder().tombstoneDensityTrigger(-0.1).build());
2893+
assertThrows(IllegalArgumentException.class,
2894+
() -> ColumnFamilyConfig.builder().tombstoneDensityTrigger(1.1).build());
2895+
2896+
// Nullable-enum null rejection
2897+
assertThrows(IllegalArgumentException.class,
2898+
() -> ColumnFamilyConfig.builder().compressionAlgorithm(null).build());
2899+
assertThrows(IllegalArgumentException.class,
2900+
() -> ColumnFamilyConfig.builder().syncMode(null).build());
2901+
assertThrows(IllegalArgumentException.class,
2902+
() -> ColumnFamilyConfig.builder().defaultIsolationLevel(null).build());
2903+
2904+
// fromNative compatibility: engine-supplied values must be accepted
2905+
assertDoesNotThrow(() -> ColumnFamilyConfig.defaultConfig());
2906+
}
2907+
27022908
@Test
27032909
@Order(74)
27042910
void testS3PrecedenceOverFsPathOnFailure() throws TidesDBException {

0 commit comments

Comments
 (0)