Skip to content

Commit 9a9a05d

Browse files
committed
RecordCountVarianceValidator- support dynamic threshold
1 parent a55ca71 commit 9a9a05d

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

hollow/src/main/java/com/netflix/hollow/api/producer/validation/RecordCountVarianceValidator.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.netflix.hollow.api.producer.HollowProducer.ReadState;
2020
import com.netflix.hollow.core.read.engine.HollowTypeReadState;
21+
import java.util.function.Supplier;
2122

2223
/**
2324
* Used to validate if the cardinality change in current cycle is with in the allowed percent for a given typeName.
@@ -36,6 +37,9 @@ public class RecordCountVarianceValidator implements ValidatorListener {
3637
"Record count validation for type %s has failed as actual change percent %s "
3738
+ "is greater than allowed change percent %s.";
3839

40+
private static final String NULL_THRESHOLD =
41+
"Record count validation for type %s has failed because the variance threshold was null.";
42+
3943
private static final String DATA_TYPE_NAME = "Typename";
4044
private static final String ALLOWABLE_VARIANCE_PERCENT_NAME = "AllowableVariancePercent";
4145
private static final String LATEST_CARDINALITY_NAME = "LatestRecordCount";
@@ -46,7 +50,7 @@ public class RecordCountVarianceValidator implements ValidatorListener {
4650

4751
private final String typeName;
4852

49-
private final float allowableVariancePercent;
53+
private final Supplier<Float> allowableVariancePercentSupplier;
5054

5155
/**
5256
* @param typeName type name
@@ -58,13 +62,27 @@ public class RecordCountVarianceValidator implements ValidatorListener {
5862
* Anything more results in failure of validation.
5963
*/
6064
public RecordCountVarianceValidator(String typeName, float allowableVariancePercent) {
65+
this(typeName, () -> allowableVariancePercent);
66+
}
67+
68+
/**
69+
* @param typeName type name
70+
* @param allowableVariancePercentSupplier: Used to validate if the cardinality change in current cycle is with in the
71+
* allowed percent, and changes to this threshold are applied in the next invocation of validation.
72+
* Ex: 0% allowableVariancePercent ensures type cardinality does not vary at all for cycle to cycle.
73+
* Ex: Number of state in United States.
74+
* 10% allowableVariancePercent: from previous cycle any addition or removal within 10% cardinality is valid.
75+
* Anything more results in failure of validation.
76+
*/
77+
public RecordCountVarianceValidator(String typeName, Supplier<Float> allowableVariancePercentSupplier) {
6178
this.typeName = typeName;
62-
if (allowableVariancePercent < 0) {
79+
this.allowableVariancePercentSupplier = allowableVariancePercentSupplier;
80+
Float allowableVariancePercent = allowableVariancePercentSupplier.get();
81+
if (allowableVariancePercent == null || allowableVariancePercent < 0) {
6382
throw new IllegalArgumentException("RecordCountVarianceValidator for type " + typeName
64-
+ ": cannot have allowableVariancePercent less than 0. Value provided: "
83+
+ ": cannot have allowableVariancePercent be null or less than 0. Value provided: "
6584
+ allowableVariancePercent);
6685
}
67-
this.allowableVariancePercent = allowableVariancePercent;
6886
}
6987

7088
@Override
@@ -75,6 +93,13 @@ public String getName() {
7593
@Override
7694
public ValidationResult onValidate(ReadState readState) {
7795
ValidationResult.ValidationResultBuilder vrb = ValidationResult.from(this);
96+
97+
Float allowableVariancePercent = allowableVariancePercentSupplier.get();
98+
if (allowableVariancePercent == null) {
99+
String message = String.format(NULL_THRESHOLD, typeName);
100+
return vrb.failed(message);
101+
}
102+
78103
vrb.detail(ALLOWABLE_VARIANCE_PERCENT_NAME, allowableVariancePercent)
79104
.detail(DATA_TYPE_NAME, typeName);
80105

hollow/src/test/java/com/netflix/hollow/api/producer/validation/HollowProducerValidationListenerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void testValidationListenerWithOnlyRecordCountValidator() {
9090

9191
private void createHollowProducerAndRunCycle(final String typeName, boolean addPrimaryKeyValidator) {
9292
ValidatorListener dupeValidator = new DuplicateDataDetectionValidator(typeName);
93-
ValidatorListener countValidator = new RecordCountVarianceValidator(typeName, 3.0f);
93+
ValidatorListener countValidator = new RecordCountVarianceValidator(typeName, () -> 3.0f);
9494
validationListener = new TestValidationStatusListener();
9595
cycleAndValidationListener = new TestCycleAndValidationStatusListener();
9696
Builder builder = HollowProducer.withPublisher(publisher).withAnnouncer(announcer)

0 commit comments

Comments
 (0)