Skip to content

Commit 6648db0

Browse files
committed
RecordCountVarianceValidator- support dynamic threshold
1 parent a55ca71 commit 6648db0

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

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

Lines changed: 20 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.
@@ -46,7 +47,7 @@ public class RecordCountVarianceValidator implements ValidatorListener {
4647

4748
private final String typeName;
4849

49-
private final float allowableVariancePercent;
50+
private final Supplier<Float> allowableVariancePercentSupplier;
5051

5152
/**
5253
* @param typeName type name
@@ -58,13 +59,27 @@ public class RecordCountVarianceValidator implements ValidatorListener {
5859
* Anything more results in failure of validation.
5960
*/
6061
public RecordCountVarianceValidator(String typeName, float allowableVariancePercent) {
62+
this(typeName, () -> allowableVariancePercent);
63+
}
64+
65+
/**
66+
* @param typeName type name
67+
* @param allowableVariancePercentSupplier: Used to validate if the cardinality change in current cycle is with in the
68+
* allowed percent, and changes to this threshold are applied in the next invocation of validation.
69+
* Ex: 0% allowableVariancePercent ensures type cardinality does not vary at all for cycle to cycle.
70+
* Ex: Number of state in United States.
71+
* 10% allowableVariancePercent: from previous cycle any addition or removal within 10% cardinality is valid.
72+
* Anything more results in failure of validation.
73+
*/
74+
public RecordCountVarianceValidator(String typeName, Supplier<Float> allowableVariancePercentSupplier) {
6175
this.typeName = typeName;
62-
if (allowableVariancePercent < 0) {
76+
this.allowableVariancePercentSupplier = allowableVariancePercentSupplier;
77+
Float allowableVariancePercent = allowableVariancePercentSupplier.get();
78+
if (allowableVariancePercent == null || allowableVariancePercent < 0) {
6379
throw new IllegalArgumentException("RecordCountVarianceValidator for type " + typeName
6480
+ ": cannot have allowableVariancePercent less than 0. Value provided: "
6581
+ allowableVariancePercent);
6682
}
67-
this.allowableVariancePercent = allowableVariancePercent;
6883
}
6984

7085
@Override
@@ -74,6 +89,7 @@ public String getName() {
7489

7590
@Override
7691
public ValidationResult onValidate(ReadState readState) {
92+
Float allowableVariancePercent = allowableVariancePercentSupplier.get();
7793
ValidationResult.ValidationResultBuilder vrb = ValidationResult.from(this);
7894
vrb.detail(ALLOWABLE_VARIANCE_PERCENT_NAME, allowableVariancePercent)
7995
.detail(DATA_TYPE_NAME, typeName);
@@ -92,7 +108,7 @@ public ValidationResult onValidate(ReadState readState) {
92108
float actualChangePercent = getChangePercent(latestCardinality, previousCardinality);
93109
vrb.detail(ACTUAL_CHANGE_PERCENT_NAME, actualChangePercent);
94110

95-
if (Float.compare(actualChangePercent, allowableVariancePercent) > 0) {
111+
if (allowableVariancePercent == null || Float.compare(actualChangePercent, allowableVariancePercent) > 0) {
96112
String message = String.format(FAILED_RECORD_COUNT_VALIDATION, typeName, actualChangePercent,
97113
allowableVariancePercent);
98114
return vrb.failed(message);

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)