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.
@@ -36,6 +37,9 @@ public class RecordCountVarianceValidator implements ValidatorListener {
36
37
"Record count validation for type %s has failed as actual change percent %s "
37
38
+ "is greater than allowed change percent %s." ;
38
39
40
+ private static final String NULL_THRESHOLD =
41
+ "Record count validation for type %s has failed because the variance threshold was null." ;
42
+
39
43
private static final String DATA_TYPE_NAME = "Typename" ;
40
44
private static final String ALLOWABLE_VARIANCE_PERCENT_NAME = "AllowableVariancePercent" ;
41
45
private static final String LATEST_CARDINALITY_NAME = "LatestRecordCount" ;
@@ -46,7 +50,7 @@ public class RecordCountVarianceValidator implements ValidatorListener {
46
50
47
51
private final String typeName ;
48
52
49
- private final float allowableVariancePercent ;
53
+ private final Supplier < Float > allowableVariancePercentSupplier ;
50
54
51
55
/**
52
56
* @param typeName type name
@@ -58,13 +62,27 @@ public class RecordCountVarianceValidator implements ValidatorListener {
58
62
* Anything more results in failure of validation.
59
63
*/
60
64
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 ) {
61
78
this .typeName = typeName ;
62
- if (allowableVariancePercent < 0 ) {
79
+ this .allowableVariancePercentSupplier = allowableVariancePercentSupplier ;
80
+ Float allowableVariancePercent = allowableVariancePercentSupplier .get ();
81
+ if (allowableVariancePercent == null || allowableVariancePercent < 0 ) {
63
82
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: "
65
84
+ allowableVariancePercent );
66
85
}
67
- this .allowableVariancePercent = allowableVariancePercent ;
68
86
}
69
87
70
88
@ Override
@@ -75,6 +93,13 @@ public String getName() {
75
93
@ Override
76
94
public ValidationResult onValidate (ReadState readState ) {
77
95
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
+
78
103
vrb .detail (ALLOWABLE_VARIANCE_PERCENT_NAME , allowableVariancePercent )
79
104
.detail (DATA_TYPE_NAME , typeName );
80
105
0 commit comments