Skip to content

Commit b18ab05

Browse files
authored
[fc] Removing the unnecessary reader schema param in DeserializerFactory (#2281)
1 parent 2f07f73 commit b18ab05

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

clients/venice-client/src/main/java/com/linkedin/venice/fastclient/DeserializerFactory.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
*
1717
* <p>Example usage for Protocol Buffers:
1818
* <pre>{@code
19-
* DeserializerFactory<MyValueProto> protoValueFactory =
20-
* (writerSchema, readerSchema) -> new RecordDeserializerToProto<>(writerSchema, MyValueProto.class);
19+
* DeserializerFactory<MyValueProto> protoValueFactory =
20+
* (writerSchema) -> new RecordDeserializerToProto<>(writerSchema, MyValueProto.class);
2121
*
2222
* ClientConfig config = new ClientConfig.ClientConfigBuilder()
2323
* .setValueDeserializerFactory(protoValueFactory)
@@ -36,9 +36,7 @@ public interface DeserializerFactory<V> {
3636
* this schema evolution appropriately.
3737
*
3838
* @param writerSchema the schema that was used when the value was written
39-
* @param readerSchema the schema to use when reading the value (may be same as writer schema)
4039
* @return a record deserializer that can deserialize Avro bytes to the value type; must not return null
41-
* @throws IllegalArgumentException if the schemas are not compatible
4240
*/
43-
RecordDeserializer<V> createDeserializer(Schema writerSchema, Schema readerSchema);
41+
RecordDeserializer<V> createDeserializer(Schema writerSchema);
4442
}

clients/venice-client/src/test/java/com/linkedin/venice/fastclient/ClientConfigTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ public void testClientConfigWithCustomValueDeserializerFactory() {
121121
DeserializerFactory mockDeserializerFactory = mock(DeserializerFactory.class);
122122
RecordDeserializer mockDeserializer = mock(RecordDeserializer.class);
123123
Schema writerSchema = mock(Schema.class);
124-
Schema readerSchema = mock(Schema.class);
125-
when(mockDeserializerFactory.createDeserializer(writerSchema, readerSchema)).thenReturn(mockDeserializer);
124+
when(mockDeserializerFactory.createDeserializer(writerSchema)).thenReturn(mockDeserializer);
126125

127126
ClientConfig.ClientConfigBuilder clientConfigBuilder = getClientConfigWithMinimumRequiredInputs();
128127
clientConfigBuilder.setValueDeserializerFactory(mockDeserializerFactory);
@@ -135,7 +134,7 @@ public void testClientConfigWithCustomValueDeserializerFactory() {
135134
// Verify the factory works
136135
Optional<DeserializerFactory> factoryOptional = clientConfig.getValueDeserializerFactory();
137136
assertTrue(factoryOptional.isPresent());
138-
RecordDeserializer deserializer = factoryOptional.get().createDeserializer(writerSchema, readerSchema);
137+
RecordDeserializer deserializer = factoryOptional.get().createDeserializer(writerSchema);
139138
assertEquals(deserializer, mockDeserializer);
140139
}
141140

clients/venice-client/src/test/java/com/linkedin/venice/fastclient/CustomSerializerFactoryTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ public void testCustomValueDeserializerFactoryIsUsed() {
8383

8484
// Create a custom deserializer factory
8585
DeserializerFactory customDeserializerFactory = mock(DeserializerFactory.class);
86-
when(customDeserializerFactory.createDeserializer(any(Schema.class), any(Schema.class)))
87-
.thenReturn(customValueDeserializer);
86+
when(customDeserializerFactory.createDeserializer(any(Schema.class))).thenReturn(customValueDeserializer);
8887

8988
// Build client config with custom value deserializer factory
9089
ClientConfig.ClientConfigBuilder configBuilder = getClientConfigWithMinimumRequiredInputs();
@@ -99,7 +98,7 @@ public void testCustomValueDeserializerFactoryIsUsed() {
9998
// Verify the factory creates the custom deserializer
10099
Optional<DeserializerFactory> factoryOptional = clientConfig.getValueDeserializerFactory();
101100
assertTrue(factoryOptional.isPresent());
102-
RecordDeserializer deserializer = factoryOptional.get().createDeserializer(valueSchema, valueSchema);
101+
RecordDeserializer deserializer = factoryOptional.get().createDeserializer(valueSchema);
103102
assertEquals(deserializer, customValueDeserializer);
104103

105104
// Verify the custom deserializer is used
@@ -123,8 +122,7 @@ public void testBothCustomFactoriesCanBeUsedTogether() {
123122
when(customSerializerFactory.createSerializer(any(Schema.class))).thenReturn(customKeySerializer);
124123

125124
DeserializerFactory customDeserializerFactory = mock(DeserializerFactory.class);
126-
when(customDeserializerFactory.createDeserializer(any(Schema.class), any(Schema.class)))
127-
.thenReturn(customValueDeserializer);
125+
when(customDeserializerFactory.createDeserializer(any(Schema.class))).thenReturn(customValueDeserializer);
128126

129127
// Build client config with both custom factories
130128
ClientConfig.ClientConfigBuilder configBuilder = getClientConfigWithMinimumRequiredInputs();
@@ -145,7 +143,7 @@ public void testBothCustomFactoriesCanBeUsedTogether() {
145143

146144
Optional<DeserializerFactory> deserializerFactoryOptional = clientConfig.getValueDeserializerFactory();
147145
assertTrue(deserializerFactoryOptional.isPresent());
148-
RecordDeserializer deserializer = deserializerFactoryOptional.get().createDeserializer(valueSchema, valueSchema);
146+
RecordDeserializer deserializer = deserializerFactoryOptional.get().createDeserializer(valueSchema);
149147
assertEquals(deserializer, customValueDeserializer);
150148
}
151149

internal/venice-common/src/main/java/com/linkedin/venice/serialization/AvroStoreDeserializerCache.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.linkedin.venice.serializer.SerializerDeserializerFactory;
88
import com.linkedin.venice.utils.collections.BiIntKeyCache;
99
import java.util.function.BiFunction;
10+
import java.util.function.Function;
1011
import java.util.function.IntFunction;
1112
import org.apache.avro.Schema;
1213

@@ -47,6 +48,20 @@ public AvroStoreDeserializerCache(
4748
(writerId, readerId) -> deserializerGetter.apply(schemaGetter.apply(writerId), schemaGetter.apply(readerId)));
4849
}
4950

51+
/**
52+
* Constructor that allows custom deserializer factory to be provided.
53+
* This enables support for custom serialization formats (e.g., Protocol Buffers).
54+
* This variant assumes that the deserializer only needs the writer schema which is true for the case Protocol Buffers.
55+
*
56+
* @param schemaGetter Function to retrieve schema by schema ID
57+
* @param deserializerGetter Function to create deserializer given the writer schema
58+
*/
59+
public AvroStoreDeserializerCache(
60+
IntFunction<Schema> schemaGetter,
61+
Function<Schema, RecordDeserializer<T>> deserializerGetter) {
62+
this.cache = new BiIntKeyCache<>((writerId, readerId) -> deserializerGetter.apply(schemaGetter.apply(writerId)));
63+
}
64+
5065
public RecordDeserializer<T> getDeserializer(int writerSchemaId, int readerSchemaId) {
5166
return this.cache.get(writerSchemaId, readerSchemaId);
5267
}

0 commit comments

Comments
 (0)