Class SemanticDetector
java.lang.Object
com.linkedin.venice.controller.kafka.protocol.serializer.SemanticDetector
SemanticDetector is used to detect the semantic changes between two schemas.
It traverses the object based on the current schema and target schema.
When we detect new semantic change, we compare object with default value.
If the object is NOT default value, we throw @code{VeniceProtocolException}.
Semantic changes include:
1. Changing the type of a field
2. Adding a new field
3. Changing the fixed size of a fixed type
4. Adding a new enum value
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
compareObjectToDefaultValue
(Object object, org.apache.avro.Schema currentSchema, String name, Object defaultValue) Validate the object with default value.static org.apache.avro.Schema
getObjectSchema
(Object object, org.apache.avro.Schema unionSchema) static void
traverseAndValidate
(Object object, org.apache.avro.Schema currentSchema, org.apache.avro.Schema targetSchema, String name, Object defaultValue) Traverse the object based on the current schema and target schema.static void
traverseCollections
(List<Object> array, org.apache.avro.Schema currentSchema, org.apache.avro.Schema targetSchema, String name, Object defaultArrayEntry) Traverse the array to make sure the array elements are not using new semantic.static void
traverseFields
(org.apache.avro.generic.GenericRecord record, org.apache.avro.Schema currentSchema, org.apache.avro.Schema targetSchema, String recordName, Object defaultValue) Traverse the fields of the record.static void
traverseMap
(Map<String, Object> map, org.apache.avro.Schema currentSchema, org.apache.avro.Schema targetSchema, String name, Object defaultValueEntry) Traverse the map to make sure the map values are not using new semantic.static void
traverseUnion
(Object object, org.apache.avro.Schema currentSchema, org.apache.avro.Schema targetSchema, String name, Object defaultValue) Traverse the union to make sure the object is not using new semantic.static void
validateEnum
(Object enumValue, org.apache.avro.Schema currentSchema, org.apache.avro.Schema targetSchema, String name) Validate the enum value.
-
Constructor Details
-
SemanticDetector
public SemanticDetector()
-
-
Method Details
-
traverseAndValidate
public static void traverseAndValidate(Object object, org.apache.avro.Schema currentSchema, org.apache.avro.Schema targetSchema, String name, Object defaultValue) Traverse the object based on the current schema and target schema. Stop traversing if found a semantic change & using not default value.- Parameters:
object
- the object containing valuescurrentSchema
- the object schematargetSchema
- the target schema we want to compare withname
- The name of the object from the parent objectdefaultValue
- The default value of the object- Throws:
VeniceProtocolException
- if there is a semantic change between the two schemas & value from
-
traverseFields
public static void traverseFields(org.apache.avro.generic.GenericRecord record, org.apache.avro.Schema currentSchema, org.apache.avro.Schema targetSchema, String recordName, Object defaultValue) Traverse the fields of the record. Stop traversing if found a semantic change & using not default value. We want to loop through fields to find the mismatch field between the current schema and target schema.For example,
- Current schema: field1, field2, field3
- Target schema: field1, field2
- Parameters:
record
- the record containing valuescurrentSchema
- the record schematargetSchema
- the target schema we want to compare withrecordName
- The name of the record from the parent objectdefaultValue
- The default value of the record- Throws:
VeniceProtocolException
- if there is a semantic change between the two schemas & field value from @code is non-default value.
-
traverseCollections
public static void traverseCollections(List<Object> array, org.apache.avro.Schema currentSchema, org.apache.avro.Schema targetSchema, String name, Object defaultArrayEntry) Traverse the array to make sure the array elements are not using new semantic.For example,
- Current schema: List (RecordA - field1, field2, field3)
- Target schema: List (RecordA - field1, field2)
- Parameters:
array
- the array containing valuescurrentSchema
- the array schematargetSchema
- the target schema we want to compare withname
- The name of the array from the parent objectdefaultArrayEntry
- The default value of the array- Throws:
VeniceProtocolException
- if there is a semantic change between the two schemas & element from @code is non-default value.
-
traverseMap
public static void traverseMap(Map<String, Object> map, org.apache.avro.Schema currentSchema, org.apache.avro.Schema targetSchema, String name, Object defaultValueEntry) Traverse the map to make sure the map values are not using new semantic.For example,
- Current schema: Map (String, RecordA - field1, field2, field3)
- Target schema: Map (String, RecordA - field1, field2)
- Parameters:
map
- the map containing valuescurrentSchema
- the map schematargetSchema
- the target schema we want to compare withname
- The name of the map from the parent objectdefaultValueEntry
- The default value of the map- Throws:
VeniceProtocolException
- if there is a semantic change between the two schemas & value from @code is non-default value.
-
traverseUnion
public static void traverseUnion(Object object, org.apache.avro.Schema currentSchema, org.apache.avro.Schema targetSchema, String name, Object defaultValue) Traverse the union to make sure the object is not using new semantic.For example,
- Current schema: Union (RecordA - field1, field2, field3, RecordB - field1, field2)
- Target schema: Union (RecordA - field1, field2, field3)
- Parameters:
object
- the object containing valuescurrentSchema
- the union schematargetSchema
- the target schema we want to compare withname
- The name of the object from the parent objectdefaultValue
- The default value of the object- Throws:
VeniceProtocolException
- if there is a semantic change between the two schemas & value from @code is non-default value OR if the object does not match any schema within the union schemas.
-
getObjectSchema
public static org.apache.avro.Schema getObjectSchema(Object object, org.apache.avro.Schema unionSchema) -
compareObjectToDefaultValue
public static void compareObjectToDefaultValue(Object object, org.apache.avro.Schema currentSchema, String name, Object defaultValue) Validate the object with default value. There are two default values:- Default value derived from schema as @code}
- Default value derived from the schema type of the object
- Parameters:
object
- the object to validatecurrentSchema
- the schema of the objectname
- the name of the objectdefaultValue
- the default value of the object
-
validateEnum
public static void validateEnum(Object enumValue, org.apache.avro.Schema currentSchema, org.apache.avro.Schema targetSchema, String name) Validate the enum value. If the enum value is in current schema but NOT in target schema, it is a new enum value.- Parameters:
enumValue
- the enum value to validatecurrentSchema
- the current schematargetSchema
- the target schemaname
- the name of the enum value- Throws:
VeniceProtocolException
- if the enum value is not in the target schema OR if the enum value is not a string
-