Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 40: Serializers Implementation for Schema Registry #41

Merged
merged 100 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from 82 commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
76d99a3
copying over changes to new fork
shiveshr Jun 8, 2020
b634867
copying over to new fork
shiveshr Jun 8, 2020
a0876cc
serializers copying over
shiveshr Jun 8, 2020
05cc270
copying over
shiveshr Jun 8, 2020
8ee1fef
contract
shiveshr Jun 8, 2020
193776d
Merge branch 'contract' into serializers
shiveshr Jun 8, 2020
600ff74
remove </p>
shiveshr Jun 8, 2020
be5115e
Merge branch 'master' into contract
shiveshr Jun 9, 2020
409c106
merge with master
shiveshr Jun 9, 2020
88af2e7
remove </p>
shiveshr Jun 10, 2020
b1a8894
Merge branch 'contract' into serializers
shiveshr Jun 10, 2020
1961ba1
add name util and hash util
shiveshr Jun 10, 2020
58e7f65
removing all unwanted auto generated swagger files
shiveshr Jun 11, 2020
026b725
Merge branch 'contract' into serializers
shiveshr Jun 11, 2020
7027d88
rename
shiveshr Jun 11, 2020
4be3f40
PR comment
shiveshr Jun 12, 2020
cc223ec
Merge branch 'contract' into serializers
shiveshr Jun 12, 2020
a0a2db7
marking interfaces as beta
shiveshr Jun 15, 2020
8a8c872
Merge branch 'contract' into serializers
shiveshr Jun 15, 2020
3f64c11
validation rules of list
shiveshr Jun 16, 2020
fba77f6
Merge branch 'contract' into serializers
shiveshr Jun 16, 2020
a449b4b
remove Validation rules of list method
shiveshr Jun 16, 2020
22a0a0d
Merge branch 'contract' into serializers
shiveshr Jun 16, 2020
9a50464
PR comment
shiveshr Jun 16, 2020
8cc3091
PR comment
shiveshr Jun 17, 2020
d063e7c
Merge branch 'contract' into serializers
shiveshr Jun 17, 2020
5a7d639
Removing schema validation rules
shiveshr Jun 18, 2020
a9cca3a
Merge branch 'contract' into serializers
shiveshr Jun 18, 2020
0b6230a
license
shiveshr Jun 18, 2020
911f793
merge with contract
shiveshr Jun 18, 2020
7ff74af
Merge branch 'contract' into serializers
shiveshr Jun 18, 2020
c9f9d3f
license
shiveshr Jun 18, 2020
5d91574
javadoc statement
shiveshr Jun 18, 2020
7c30b4f
Merge branch 'contract' into serializers
shiveshr Jun 18, 2020
98a9c6b
javadoc
shiveshr Jun 18, 2020
dcff47c
Merge branch 'contract' into serializers
shiveshr Jun 18, 2020
28660e4
deserializerAsJson name fix
shiveshr Jun 18, 2020
53b8988
compatibility enum reversal
shiveshr Jun 19, 2020
8e2c704
Merge branch 'contract' into serializers
shiveshr Jun 19, 2020
ce1aa98
addschema
shiveshr Jun 19, 2020
151b808
Merge branch 'contract' into serializers
shiveshr Jun 19, 2020
bffd0f7
javadoc
shiveshr Jun 22, 2020
26e34f1
Merge branch 'contract' into serializers
shiveshr Jun 22, 2020
049fb6d
group id generator
shiveshr Jun 22, 2020
910f870
add error message in model helper
shiveshr Jun 22, 2020
d690a21
Merge branch 'contract' into serializers
shiveshr Jun 22, 2020
a44c0fb
required
shiveshr Jun 22, 2020
a82ee56
Merge branch 'contract' into serializers
shiveshr Jun 22, 2020
7b34571
Merge branch 'serializers' of https://github.com/shiveshr/schema-regi…
shiveshr Jun 22, 2020
48da360
add new deserializer factory method
shiveshr Jun 23, 2020
72919bb
PR comments
shiveshr Jun 24, 2020
48e54e9
Merge branch 'contract' into serializers
shiveshr Jun 24, 2020
7c9ba8e
avro reflect deserializer
shiveshr Jun 25, 2020
214ab54
add test for avro reflect deserializer
shiveshr Jun 25, 2020
2241865
javadoc
shiveshr Jun 25, 2020
456e6bd
Merge branch 'contract' into serializers
shiveshr Jun 25, 2020
9741bd5
Merge branch 'master' into contract
shiveshr Jun 25, 2020
6f7f4d5
Merge branch 'contract' into serializers
shiveshr Jun 25, 2020
87c9ebc
make cache package private
shiveshr Jun 25, 2020
f8fd8ad
javadocs
shiveshr Jun 25, 2020
c98ee7b
remove the hashutil from this branch
shiveshr Jun 25, 2020
c3789ca
minor imp
shiveshr Jun 25, 2020
32933ba
use full name
shiveshr Jun 28, 2020
d8b0675
ofRecord in avro schema
shiveshr Jun 28, 2020
322d3c0
exact type to message match for protobuf generic deserializer
shiveshr Jun 29, 2020
a586f4f
generalize the multiformat deserialization
shiveshr Jun 30, 2020
cdc0c0c
revert rename of json generic object
shiveshr Jun 30, 2020
8693357
refactoring serializer factory into multiple classes for readability
shiveshr Jul 2, 2020
223df77
bug fixes
shiveshr Jul 3, 2020
8c69b3f
PR comments
shiveshr Jul 3, 2020
572f34f
javadoc
shiveshr Jul 5, 2020
cf82e07
encode header as a config param
shiveshr Jul 8, 2020
c4a732b
review comments
shiveshr Jul 8, 2020
5daaeb4
temp
shiveshr Jul 8, 2020
eedee88
merge with issue 46
shiveshr Jul 8, 2020
b63a8a4
merge with master
shiveshr Jul 8, 2020
23e6470
rename variable
shiveshr Jul 8, 2020
4925759
remove validate object
shiveshr Jul 8, 2020
1dafddb
ProtobufSchema
shiveshr Jul 8, 2020
53b8f9c
minor refactoring
shiveshr Jul 10, 2020
a744198
PR comments
shiveshr Jul 10, 2020
c6a02c8
fix
shiveshr Jul 10, 2020
bf0dfb4
PR comments
shiveshr Jul 12, 2020
d70ba7e
merge with master
shiveshr Jul 13, 2020
4117557
PR comment
shiveshr Jul 13, 2020
0f770fa
Merge branch 'master' into serializers
shiveshr Jul 13, 2020
9823974
PR comments
shiveshr Jul 13, 2020
7ee6fe0
PR comments
shiveshr Jul 14, 2020
ff47289
PR comments
shiveshr Jul 14, 2020
6c3fd87
PR comments
shiveshr Jul 15, 2020
2d6711d
adding avro protobuf and json creator methods in WithSchema
shiveshr Jul 15, 2020
ade09af
PR comments, javadoc fix, and return schema string instead of JsonSch…
shiveshr Jul 16, 2020
9cbaaae
checkstyle
shiveshr Jul 16, 2020
c0e57be
PR comment
shiveshr Jul 16, 2020
174c4e6
PR comments
shiveshr Jul 16, 2020
fe0974f
revert rename of registry serializer factory
shiveshr Jul 16, 2020
d464b6b
PR comment - generic T on JSONSchema
shiveshr Jul 17, 2020
9137aa6
Merge with master, use everit json schema
shiveshr Jul 17, 2020
ed740ea
Merge with master
shiveshr Jul 17, 2020
a8fb0dd
PR comments, javadoc corrections, test addition
shiveshr Jul 21, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,31 @@ def getProjectVersion() {
return ver
}

project('serializers') {
dependencies {
compile project(':common')
compile project(':client')
compile group: 'org.apache.avro', name: 'avro', version: avroVersion
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should probably be separated into individual targets so that users don't need to depend on more than what they actually need.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created issue #45 to tackle this later as it would require considerable refactoring at this time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it require a change to SerializerFactory? I'm concerned that this could be an API change, and even though we are making this beta, this is an anticipated change, it is not something that we learned later. If we can do it now, I'd say it is better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way i am thinking is as follows:
have format specific serializers implemented in format specific module.
So one for protobuf, one for json, one for avro. Each such module will have a factory with only serializers/deserializers for that format.

Then we will have a consolidated one, which is this library and factory class. This is because here we have serializers which are multi format.

So this way users can take dependency on one common module (all type 1 apps that want to work for multiple formats can depend on this one module).

So the user contract, which is the SerializerFactory, doesnt change.

Only the serializer implementations would move internal to their respective modules and this base module would take dependence on those.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If applications are supposed to use the specific modules, why do we need the base module? If I understand correctly, the base module is a level of indirection to the specific modules.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it serves as two purposes:

  1. level of indirection
  2. allows for multi-format serializers and deserializers

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with this direction, @tkaitchuck needs to resolve this thread if he is satisfied.

compile group: 'com.google.protobuf', name: 'protobuf-java', version: protobufProtocVersion
compile group: 'com.google.protobuf', name: 'protobuf-java-util', version: protobufUtilVersion
compile group: 'io.pravega', name: 'pravega-client', version: pravegaVersion
compile group: 'org.xerial.snappy', name: 'snappy-java', version: snappyVersion
compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-jsonSchema', version: jacksonVersion
testCompile group: 'org.slf4j', name: 'log4j-over-slf4j', version: slf4jApiVersion
testCompile group: 'ch.qos.logback', name: 'logback-classic', version: qosLogbackVersion
testCompile group: 'io.pravega', name: 'pravega-test-testcommon', version: pravegaVersion
}

javadoc {
title = "Serializers"
dependsOn delombok
source = delombok.outputDir
failOnError = false
exclude "**/impl/**";
options.addBooleanOption("Xdoclint:all,-reference", true)
}
}

apply plugin: 'distribution'

task sourceCopy(type: Copy) {
Expand Down Expand Up @@ -297,3 +322,4 @@ class DockerPushTask extends Exec {
}
}
}

3 changes: 3 additions & 0 deletions checkstyle/spotbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<Match> <!-- generated code -->
<Package name="io.pravega.schemaregistry.test.integrationtest.generated" />
</Match>
<Match> <!-- generated code -->
<Package name="io.pravega.schemaregistry.testobjs.generated" />
</Match>
<Match> <!-- does not work well with futures -->
<Bug pattern="NP_NONNULL_PARAM_VIOLATION" />
</Match>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
package io.pravega.schemaregistry.common;

public class NameUtil {
/**
* Extracts the name from the fully qualified type name. Name represents the last token after ".".
* If the qualified name does not contain "." then the name is same as qualified name.
shiveshr marked this conversation as resolved.
Show resolved Hide resolved
*
* Example: io.pravega.MyObject will return MyObject
* Example: MyObject will return MyObject
*
* @param qualifiedName qualified name to extract name from.
* @return extracted name.
*/
public static String extractName(String qualifiedName) {
int nameStart = qualifiedName.lastIndexOf(".");
return qualifiedName.substring(nameStart + 1);
}

/**
* Extracts name and the prefix qualifier before the name. Name represents the last token after ".".
* Qualifier is the prefix before the name.
* If the qualified name does not contain "." then the name is same as qualified name and qualifier is empty string.
*
* Example: io.pravega.MyObject will return ["MyObject", "io.pravega"]
* Example: MyObject will return ["MyObject", ""]
*
* @param qualifiedName qualified name to extract tokens from.
* @return an array containing name at index 0 and qualifier at index 1.
*/
public static String[] extractNameAndQualifier(String qualifiedName) {
int nameStart = qualifiedName.lastIndexOf(".");
String name = qualifiedName.substring(nameStart + 1);
String pckg = nameStart < 0 ? "" : qualifiedName.substring(0, nameStart);
return new String[]{name, pckg};
}
}
11 changes: 7 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,27 @@ gradleLombokPluginVersion=3.2.0
gradleSshPluginVersion=2.9.0
guavaVersion=28.1-jre
javaxServletApiVersion=4.0.0
jacksonVersion=2.9.9.3
jacksonVersion=2.11.1
javaxwsrsApiVersion=2.1
jaxbVersion=2.3.0
javaxAnnotationVersion=1.3.2
jerseyVersion=2.29
jerseyVersion=2.30
junitVersion=4.12
lombokVersion=1.18.4
mockitoVersion=2.23.0
jacocoVersion=0.8.2
protobufGradlePlugin=0.8.9
protobufProtocVersion=3.5.1
protobufProtocVersion=3.11.4
protobufUtilVersion=3.11.4
qosLogbackVersion=1.2.3
swaggerJersey2JaxrsVersion=1.5.22
slf4jApiVersion=1.7.25
gradleGitPluginVersion=2.2.0
avroVersion=1.9.1
avroProtobufVersion=1.7.7
pravegaVersion=0.6.2
snappyVersion=1.1.7.3
pravegaVersion=0.7.0
shiveshr marked this conversation as resolved.
Show resolved Hide resolved
pravegaKeyCloakVersion=0.7.0

# Version and base tags can be overridden at build time
schemaregistryVersion=0.0.1-SNAPSHOT
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
package io.pravega.schemaregistry;

import com.google.common.base.Preconditions;
import io.pravega.shared.NameUtils;

/**
* Defines strategies for generating groupId for stream.
* Currently there is only one naming strategy that uses streams fully qualified scoped stream name.
*/
public class GroupIdGenerator {
private GroupIdGenerator() {
}

public static String getGroupId(Scheme scheme, String... args) {
shiveshr marked this conversation as resolved.
Show resolved Hide resolved
switch (scheme) {
case QualifiedStreamName:
Preconditions.checkNotNull(args);
Preconditions.checkArgument(args.length == 2, "Both scope and stream name should be supplied.");
return NameUtils.getScopedStreamName(args[0], args[1]);
default:
throw new IllegalArgumentException("Unknown Group id generation schema.");
}
}

public enum Scheme {
QualifiedStreamName,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
package io.pravega.schemaregistry.codec;

import io.pravega.schemaregistry.contract.data.CodecType;
import io.pravega.schemaregistry.contract.data.EncodingInfo;

import java.io.IOException;
import java.nio.ByteBuffer;

/**
* Codec interface that defines methods to encode and decoder data for a given codec type.
*/
public interface Codec {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this extend Serializer<ByteBuffer> ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, its not a serializer but a transformer of bytes. Extending the Serializer interface with type byteBuffer will be an unnecessary semantic overload.

/**
* Codec Type object that contains a string name identifying the Codec Type.
* This name should be same as the codecType that is registered for the group in schema registry service.
* The serializers will use this codec to encode the data and deserializers will find
* the decoder for the encoded data from {@link EncodingInfo#getCodecType()}
*
* @return Name of the codec.
*/
CodecType getCodecType();

/**
* Implementation should encode the remaining bytes in the buffer and return a new ByteBuffer that includes
* the encoded data at its current position.
*
* @param data ByteBuffer to encode.
* @return encoded ByteBuffer with position set to the start of encoded data.
* @throws IOException IOException can be thrown while reading from or writing to byte buffers.
*/
ByteBuffer encode(ByteBuffer data) throws IOException;

/**
* Implementation should decode the remaining bytes in the buffer and return a new ByteBuffer that includes
* the decoded data at its current position.
*
* @param data encoded ByteBuffer to decode.
* @return decoded ByteBuffer with position set to the start of decoded data.
* @throws IOException can be thrown while reading from or writing to byte buffers.
*/
ByteBuffer decode(ByteBuffer data) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/**
* Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
package io.pravega.schemaregistry.schemas;

import com.google.common.base.Charsets;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import io.pravega.schemaregistry.contract.data.SchemaInfo;
import io.pravega.schemaregistry.contract.data.SerializationFormat;
import lombok.Getter;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.reflect.ReflectData;
import org.apache.avro.specific.SpecificData;
import org.apache.avro.specific.SpecificRecordBase;

import java.nio.ByteBuffer;

/**
* Container class for Avro Schema.
*
* @param <T> Type of element.
*/
public class AvroSchema<T> implements Schema<T> {
@Getter
private final org.apache.avro.Schema schema;
private final SchemaInfo schemaInfo;
@Getter
private final Class<T> tClass;

private AvroSchema(org.apache.avro.Schema schema, Class<T> tClass) {
this.schema = schema;
this.schemaInfo = new SchemaInfo(schema.getFullName(),
SerializationFormat.Avro, getSchemaBytes(), ImmutableMap.of());
this.tClass = tClass;
}

private AvroSchema(SchemaInfo schemaInfo) {
String schemaString = new String(schemaInfo.getSchemaData().array(), Charsets.UTF_8);
this.schema = new org.apache.avro.Schema.Parser().parse(schemaString);
this.schemaInfo = schemaInfo;
this.tClass = null;
}

/**
* Method to create a typed AvroSchema for the given class. It extracts the avro schema from the class.
* For Avro generated classes, the schema is retrieved from the class.
* For POJOs the schema is extracted using avro's {@link ReflectData}.
*
* @param tClass Class whose object's schema is used.
* @param <T> Type of the Java class.
* @return {@link AvroSchema} with generic type T that extracts and captures the avro schema.
*/
public static <T> AvroSchema<T> of(Class<T> tClass) {
org.apache.avro.Schema schema;
if (SpecificRecordBase.class.isAssignableFrom(tClass)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There appears to already be a method for such a class. We should probably differentiate these methods by name with one for the reflection and one for the specificRecord case.

Copy link
Contributor Author

@shiveshr shiveshr Jul 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That other method creates the Schema of type SpecificRecordBase while this creates the schema of type T.

This matters for multi type handling where we want the schema to be of the common base class as serializer/deserializer is created with that specific type.

schema = SpecificData.get().getSchema(tClass);
} else {
schema = ReflectData.get().getSchema(tClass);
}
return new AvroSchema<>(schema, tClass);
}

/**
* Method to create a typed AvroSchema of type {@link Object} from the given schema.
* This schema can be used to express any non record schema.
*
* @param schema Schema to use.
* @return Returns an AvroSchema with {@link GenericRecord} type.
*/
public static AvroSchema<Object> of(org.apache.avro.Schema schema) {
return new AvroSchema<>(schema, Object.class);
}

/**
* Method to create a typed AvroSchema of type {@link GenericRecord} from the given schema.
*
* @param schema Schema to use.
* @return Returns an AvroSchema with {@link GenericRecord} type.
*/
public static AvroSchema<GenericRecord> ofRecord(org.apache.avro.Schema schema) {
shrids marked this conversation as resolved.
Show resolved Hide resolved
Preconditions.checkArgument(schema.getType().equals(org.apache.avro.Schema.Type.RECORD));
return new AvroSchema<>(schema, GenericRecord.class);
}

/**
* It is same as {@link #of(Class)} except that it generates an AvroSchema typed as T.
*
* This is useful for supplying a map of Avro schemas for multiplexed serializers and deserializers.
*
* @param tDerived Class whose schema should be used.
* @param tBase Base class for the typed AvroSchema object.
* @param <T> Type of base class.
* @return Returns an AvroSchema with T type.
*/
public static <T> AvroSchema<T> ofBaseType(Class<? extends T> tDerived, Class<T> tBase) {
Preconditions.checkArgument(tBase.isAssignableFrom(tDerived));

return new AvroSchema<>(ReflectData.get().getSchema(tDerived), tBase);
}

/**
* It is same as {@link #of(Class)} except that it generates an AvroSchema typed as {@link SpecificRecordBase}.
*
* This is useful for supplying a map of Avro schemas for multiplexed serializers and deserializers.
*
* @param tClass Class whose schema should be used.
* @param <T> Type of class whose schema is to be used.
* @return Returns an AvroSchema with {@link SpecificRecordBase} type.
*/
public static <T extends SpecificRecordBase> AvroSchema<SpecificRecordBase> ofSpecificRecord(Class<T> tClass) {
Preconditions.checkArgument(SpecificRecordBase.class.isAssignableFrom(tClass));

return new AvroSchema<>(SpecificData.get().getSchema(tClass), SpecificRecordBase.class);
}

/**
* Method to create a typed AvroSchema of type {@link GenericRecord} from schema info.
*
* @param schemaInfo Schema info object that has schema data in binary form.
* @return Returns an AvroSchema with {@link GenericRecord} type.
*/
public static AvroSchema<Object> from(SchemaInfo schemaInfo) {
return new AvroSchema<>(schemaInfo);
}

private ByteBuffer getSchemaBytes() {
return ByteBuffer.wrap(schema.toString().getBytes(Charsets.UTF_8));
}

@Override
public SchemaInfo getSchemaInfo() {
return schemaInfo;
}
}
Loading