forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[schema] Introduce multi version generic record schema (apache#3670)
*Motivation* Currently AUTO_CONSUME only supports decoding records from latest schema. All the schema versions are lost. It makes AUTO_CONSUME less useful in some use cases, such as CDC. Because there is no way for the applications to know which version of schema that a message is using. In order to support multi-version schema, we need to propagate schema version from message header through schema#decode method to the decoded record. *Modifications* - Introduce a new decode method `decode(byte[] data, byte[] schemaVersion)`. This allows the implementation to leverage the schema version. - Introduce a method `supportSchemaVersioning` to tell which decode methods to use. Because most of the schema implementations such as primitive schemas and POJO based schema doesn't make any sense to use schema version. - Introduce a SchemaProvider which returns a specific schema instance for a given schema version - Implement a MultiVersionGenericRecordSchema which decode the messages based on schema version. All the records decoded by this schema will have schema version and its corresponding schema definitions. *NOTES This implementation only introduce the mechanism. But it doesn't wire the multi-version schema with auto_consume schema. There will be a subsequent pull request on implementing a schema provider that fetches and caches schemas from brokers.
- Loading branch information
Showing
13 changed files
with
312 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...src/main/java/org/apache/pulsar/client/impl/schema/generic/MultiVersionGenericSchema.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.client.impl.schema.generic; | ||
|
||
import org.apache.pulsar.client.api.Schema; | ||
import org.apache.pulsar.client.api.schema.GenericRecord; | ||
import org.apache.pulsar.client.impl.schema.BytesSchema; | ||
import org.apache.pulsar.common.schema.SchemaInfo; | ||
|
||
/** | ||
* A schema implementation that handles schema versioning. | ||
*/ | ||
public class MultiVersionGenericSchema implements Schema<GenericRecord> { | ||
|
||
private final SchemaProvider<GenericRecord> provider; | ||
|
||
MultiVersionGenericSchema(SchemaProvider<GenericRecord> provider) { | ||
this.provider = provider; | ||
} | ||
|
||
@Override | ||
public byte[] encode(GenericRecord message) { | ||
throw new UnsupportedOperationException("This schema implementation is only used for AUTO_CONSUME"); | ||
} | ||
|
||
@Override | ||
public boolean supportSchemaVersioning() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public GenericRecord decode(byte[] bytes) { | ||
return provider.getSchema(null).decode(bytes); | ||
} | ||
|
||
@Override | ||
public GenericRecord decode(byte[] bytes, byte[] schemaVersion) { | ||
return provider.getSchema(schemaVersion).decode(bytes, schemaVersion); | ||
} | ||
|
||
@Override | ||
public SchemaInfo getSchemaInfo() { | ||
// simulate it is a bytes schema | ||
return BytesSchema.of().getSchemaInfo(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/generic/SchemaProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.client.impl.schema.generic; | ||
|
||
import org.apache.pulsar.client.api.Schema; | ||
|
||
/** | ||
* Schema Provider. | ||
*/ | ||
public interface SchemaProvider<T> { | ||
|
||
/** | ||
* Retrieve the schema instance of a given <tt>schemaVersion</tt>. | ||
* | ||
* @param schemaVersion schema version | ||
* @return schema instance of the provided <tt>schemaVersion</tt> | ||
*/ | ||
Schema<T> getSchema(byte[] schemaVersion); | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
...nt/src/main/java/org/apache/pulsar/client/impl/schema/generic/VersionedGenericRecord.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.client.impl.schema.generic; | ||
|
||
import java.util.List; | ||
import org.apache.pulsar.client.api.schema.Field; | ||
import org.apache.pulsar.client.api.schema.GenericRecord; | ||
|
||
/** | ||
* A generic record carrying schema version. | ||
*/ | ||
abstract class VersionedGenericRecord implements GenericRecord { | ||
|
||
protected final byte[] schemaVersion; | ||
protected final List<Field> fields; | ||
|
||
protected VersionedGenericRecord(byte[] schemaVersion, | ||
List<Field> fields) { | ||
this.schemaVersion = schemaVersion; | ||
this.fields = fields; | ||
} | ||
|
||
@Override | ||
public byte[] getSchemaVersion() { | ||
return schemaVersion; | ||
} | ||
|
||
@Override | ||
public List<Field> getFields() { | ||
return fields; | ||
} | ||
|
||
} |
Oops, something went wrong.