18
18
19
19
import com .netflix .hollow .api .producer .HollowProducer .ReadState ;
20
20
import com .netflix .hollow .core .read .engine .HollowTypeReadState ;
21
+ import java .util .function .Supplier ;
21
22
22
23
/**
23
24
* 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 {
46
47
47
48
private final String typeName ;
48
49
49
- private final float allowableVariancePercent ;
50
+ private final Supplier < Float > allowableVariancePercentSupplier ;
50
51
51
52
/**
52
53
* @param typeName type name
@@ -58,13 +59,27 @@ public class RecordCountVarianceValidator implements ValidatorListener {
58
59
* Anything more results in failure of validation.
59
60
*/
60
61
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 ) {
61
75
this .typeName = typeName ;
62
- if (allowableVariancePercent < 0 ) {
76
+ this .allowableVariancePercentSupplier = allowableVariancePercentSupplier ;
77
+ Float allowableVariancePercent = allowableVariancePercentSupplier .get ();
78
+ if (allowableVariancePercent == null || allowableVariancePercent < 0 ) {
63
79
throw new IllegalArgumentException ("RecordCountVarianceValidator for type " + typeName
64
80
+ ": cannot have allowableVariancePercent less than 0. Value provided: "
65
81
+ allowableVariancePercent );
66
82
}
67
- this .allowableVariancePercent = allowableVariancePercent ;
68
83
}
69
84
70
85
@ Override
@@ -74,6 +89,7 @@ public String getName() {
74
89
75
90
@ Override
76
91
public ValidationResult onValidate (ReadState readState ) {
92
+ Float allowableVariancePercent = allowableVariancePercentSupplier .get ();
77
93
ValidationResult .ValidationResultBuilder vrb = ValidationResult .from (this );
78
94
vrb .detail (ALLOWABLE_VARIANCE_PERCENT_NAME , allowableVariancePercent )
79
95
.detail (DATA_TYPE_NAME , typeName );
@@ -92,7 +108,7 @@ public ValidationResult onValidate(ReadState readState) {
92
108
float actualChangePercent = getChangePercent (latestCardinality , previousCardinality );
93
109
vrb .detail (ACTUAL_CHANGE_PERCENT_NAME , actualChangePercent );
94
110
95
- if (Float .compare (actualChangePercent , allowableVariancePercent ) > 0 ) {
111
+ if (allowableVariancePercent == null || Float .compare (actualChangePercent , allowableVariancePercent ) > 0 ) {
96
112
String message = String .format (FAILED_RECORD_COUNT_VALIDATION , typeName , actualChangePercent ,
97
113
allowableVariancePercent );
98
114
return vrb .failed (message );
0 commit comments