-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[schemaregistry]ProtobufNative Schema Support (#8372)
Fixes #7642 Fixes #7674 ### Motivation Protobuf Native Schema Support : [PIP-Add ProtobufNative Schema Support ](https://docs.google.com/document/d/1XR_MNOuSXyig-CKsdVhr6IXvFwziBRdSoS3oEUiLFe8/edit?usp=sharing) This PR proposes to import a new protobuf-v3 schema based on protobuf native `Descriptor`, Current `ProtobufSchema` is based on AVRO schema, this Causes the following restrictions : - Current [ProtobufSchema](https://github.com/apache/pulsar/blob/master/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/ProtobufSchema.java) based on avro-protobuf only works when JVM classloader can load `Java implementation class of GeneratedMessageV3` . this is unfriendly for long-running server ( e.g. Presto ), restart server for update class is unfriendly. - Describe `protobuf` schema by `avro` schema will cause losses of information, so based current [ProtobufSchema](https://github.com/apache/pulsar/blob/master/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/ProtobufSchema.java) can’t support `AutoConsume` by `DynamicMessage`. - The amount of support avro language is less than support protobuf language. In consideration of backward compatibility , we add a new schema type `SchemaType.PROTOBUF_NATIVE` base on protobuf-v3 native Descriptor instead of modify SchemaType.PROTOBUF, aim to support `GenericProtobufNativeSchema` and `AutoConsumeSchema` for PROTOBUF. ### Modifications Describe in [PIP-Add ProtobufNative Schema Support ](https://docs.google.com/document/d/1XR_MNOuSXyig-CKsdVhr6IXvFwziBRdSoS3oEUiLFe8/edit?usp=sharing)
- Loading branch information
Showing
31 changed files
with
1,413 additions
and
26 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
73 changes: 73 additions & 0 deletions
73
.../java/org/apache/pulsar/broker/service/schema/ProtobufNativeSchemaCompatibilityCheck.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,73 @@ | ||
/** | ||
* 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.broker.service.schema; | ||
|
||
import org.apache.pulsar.broker.service.schema.exceptions.IncompatibleSchemaException; | ||
import org.apache.pulsar.client.impl.schema.ProtobufNativeSchemaUtils; | ||
import org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy; | ||
import org.apache.pulsar.common.protocol.schema.SchemaData; | ||
import org.apache.pulsar.common.schema.SchemaType; | ||
|
||
import static com.google.protobuf.Descriptors.Descriptor; | ||
|
||
/** | ||
* The {@link SchemaCompatibilityCheck} implementation for {@link SchemaType#PROTOBUF_NATIVE}. | ||
*/ | ||
public class ProtobufNativeSchemaCompatibilityCheck implements SchemaCompatibilityCheck { | ||
|
||
@Override | ||
public SchemaType getSchemaType() { | ||
return SchemaType.PROTOBUF_NATIVE; | ||
} | ||
|
||
@Override | ||
public void checkCompatible(SchemaData from, SchemaData to, SchemaCompatibilityStrategy strategy) throws IncompatibleSchemaException { | ||
Descriptor fromDescriptor = ProtobufNativeSchemaUtils.deserialize(from.getData()); | ||
Descriptor toDescriptor = ProtobufNativeSchemaUtils.deserialize(to.getData()); | ||
switch (strategy) { | ||
case BACKWARD_TRANSITIVE: | ||
case BACKWARD: | ||
case FORWARD_TRANSITIVE: | ||
case FORWARD: | ||
case FULL_TRANSITIVE: | ||
case FULL: | ||
checkRootMessageChange(fromDescriptor, toDescriptor, strategy); | ||
return; | ||
case ALWAYS_COMPATIBLE: | ||
return; | ||
default: | ||
throw new IncompatibleSchemaException("Unknown SchemaCompatibilityStrategy."); | ||
} | ||
} | ||
|
||
@Override | ||
public void checkCompatible(Iterable<SchemaData> from, SchemaData to, SchemaCompatibilityStrategy strategy) throws IncompatibleSchemaException { | ||
for (SchemaData schemaData : from) { | ||
checkCompatible(schemaData, to, strategy); | ||
} | ||
} | ||
|
||
private void checkRootMessageChange(Descriptor fromDescriptor, Descriptor toDescriptor, | ||
SchemaCompatibilityStrategy strategy) throws IncompatibleSchemaException { | ||
if (!fromDescriptor.getFullName().equals(toDescriptor.getFullName())) { | ||
throw new IncompatibleSchemaException("Protobuf root message isn't allow change!"); | ||
} | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
.../org/apache/pulsar/broker/service/schema/validator/ProtobufNativeSchemaDataValidator.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.broker.service.schema.validator; | ||
|
||
import com.google.protobuf.Descriptors; | ||
import org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException; | ||
import org.apache.pulsar.client.impl.schema.ProtobufNativeSchemaUtils; | ||
import org.apache.pulsar.common.protocol.schema.SchemaData; | ||
|
||
public class ProtobufNativeSchemaDataValidator implements SchemaDataValidator { | ||
|
||
@Override | ||
public void validate(SchemaData schemaData) throws InvalidSchemaDataException { | ||
Descriptors.Descriptor descriptor; | ||
try { | ||
descriptor = ProtobufNativeSchemaUtils.deserialize(schemaData.getData()); | ||
} catch (Exception e) { | ||
throw new InvalidSchemaDataException("deserialize ProtobufNative Schema failed", e); | ||
} | ||
if (descriptor == null) { | ||
throw new InvalidSchemaDataException("protobuf root message descriptor is null , please recheck rootMessageTypeName or rootFileDescriptorName conf. "); | ||
} | ||
} | ||
|
||
public static ProtobufNativeSchemaDataValidator of() { | ||
return INSTANCE; | ||
} | ||
|
||
private static final ProtobufNativeSchemaDataValidator INSTANCE = new ProtobufNativeSchemaDataValidator(); | ||
|
||
private ProtobufNativeSchemaDataValidator() { | ||
} | ||
} |
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
50 changes: 50 additions & 0 deletions
50
...a/org/apache/pulsar/broker/service/schema/ProtobufNativeSchemaCompatibilityCheckTest.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,50 @@ | ||
/** | ||
* 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.broker.service.schema; | ||
|
||
import org.apache.pulsar.client.impl.schema.ProtobufNativeSchemaUtils; | ||
import org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy; | ||
import org.apache.pulsar.common.protocol.schema.SchemaData; | ||
import org.apache.pulsar.common.schema.SchemaType; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import static com.google.protobuf.Descriptors.Descriptor; | ||
|
||
public class ProtobufNativeSchemaCompatibilityCheckTest { | ||
|
||
private static final SchemaData schemaData1 = getSchemaData(org.apache.pulsar.client.api.schema.proto.Test.TestMessage.getDescriptor()); | ||
|
||
private static final SchemaData schemaData2 = getSchemaData(org.apache.pulsar.client.api.schema.proto.Test.SubMessage.getDescriptor()); | ||
|
||
/** | ||
* make sure protobuf root message isn't allow change | ||
*/ | ||
@Test | ||
public void testRootMessageChange() { | ||
ProtobufNativeSchemaCompatibilityCheck compatibilityCheck = new ProtobufNativeSchemaCompatibilityCheck(); | ||
Assert.assertFalse(compatibilityCheck.isCompatible(schemaData2, schemaData1, | ||
SchemaCompatibilityStrategy.FULL), | ||
"Protobuf root message isn't allow change"); | ||
} | ||
|
||
private static SchemaData getSchemaData(Descriptor descriptor) { | ||
return SchemaData.builder().data(ProtobufNativeSchemaUtils.serialize(descriptor)).type(SchemaType.PROTOBUF_NATIVE).build(); | ||
} | ||
} |
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
Oops, something went wrong.