You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2019/05/18 14:14:04 UTC

[GitHub] [pulsar] tuteng commented on a change in pull request #4192: [pulsar-broker]Support key value schema compatibility checker

tuteng commented on a change in pull request #4192: [pulsar-broker]Support key value schema compatibility checker
URL: https://github.com/apache/pulsar/pull/4192#discussion_r285343500
 
 

 ##########
 File path: pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/KeyValueSchemaCompatibilityCheck.java
 ##########
 @@ -0,0 +1,138 @@
+/**
+ * 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 com.google.gson.Gson;
+import org.apache.pulsar.common.schema.KeyValue;
+import org.apache.pulsar.common.schema.SchemaData;
+import org.apache.pulsar.common.schema.SchemaType;
+
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.Map;
+
+/**
+ * {@link KeyValueSchemaCompatibilityCheck} for {@link SchemaType#KEY_VALUE}.
+ */
+public class KeyValueSchemaCompatibilityCheck implements SchemaCompatibilityCheck {
+
+    private final Map<SchemaType, SchemaCompatibilityCheck> checkers;
+
+    public KeyValueSchemaCompatibilityCheck(Map<SchemaType, SchemaCompatibilityCheck> checkers) {
+        this.checkers = checkers;
+    }
+
+    private KeyValue<byte[], byte[]> splitKeyValueSchemaData(byte[] bytes) {
+        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
+        int keyLength = byteBuffer.getInt();
+        byte[] keySchema = new byte[keyLength];
+        byteBuffer.get(keySchema);
+
+        int valueLength = byteBuffer.getInt();
+        byte[] valueSchema = new byte[valueLength];
+        byteBuffer.get(valueSchema);
+        return new KeyValue<>(keySchema, valueSchema);
+    }
+
+    private SchemaType fetchSchemaType(Map<String, String> properties, String key) {
+        if (properties.get(key) != null) {
+            return SchemaType.valueOf(properties.get(key));
+        }
+        return SchemaType.BYTES;
+    }
+
+    private SchemaData fetchSchemaData(
+            byte[] keyValue, SchemaType schemaType, Gson schemaGson, Map<String, String> properties, String key) {
+        if (properties.get(key) != null) {
+            return SchemaData.builder().data(keyValue)
+                    .type(schemaType)
+                    .props(schemaGson.fromJson(properties.get(key), Map.class)).build();
+        }
+        return SchemaData.builder().data(keyValue)
+                .type(schemaType)
+                .props(Collections.emptyMap()).build();
+    }
+
+    private KeyValue<SchemaData, SchemaData> parseSchemaData(SchemaData schemaData) {
+        KeyValue<byte[], byte[]> keyValue = this.splitKeyValueSchemaData(schemaData.getData());
+        Map<String, String> properties = schemaData.getProps();
+        SchemaType keyType = fetchSchemaType(properties, "key.schema.type");
+        SchemaType valueType = fetchSchemaType(properties, "value.schema.type");
+        Gson schemaGson = new Gson();
+        SchemaData keySchemaData = fetchSchemaData(
+                keyValue.getKey(), keyType, schemaGson, properties, "key.schema.properties");
+        SchemaData valueSchemaData = fetchSchemaData(
+                keyValue.getValue(), valueType, schemaGson, properties, "value.schema.properties");
+        return new KeyValue<>(keySchemaData, valueSchemaData);
+    }
+
+    @Override
+    public SchemaType getSchemaType() {
+        return SchemaType.KEY_VALUE;
+    }
+
+    @Override
+    public boolean isWellFormed(SchemaData to) {
+        KeyValue<SchemaData, SchemaData> keyValue = parseSchemaData(to);
+        SchemaCompatibilityCheck valueCheck;
+        if (checkers.get(keyValue.getValue().getType()) != null) {
+            valueCheck = checkers.get(keyValue.getValue().getType());
+        } else {
+            valueCheck = SchemaCompatibilityCheck.DEFAULT;
+        }
+        return valueCheck.isWellFormed(keyValue.getKey()) && valueCheck.isWellFormed(keyValue.getValue());
+    }
+
+    @Override
+    public boolean isCompatible(SchemaData from, SchemaData to, SchemaCompatibilityStrategy strategy) {
+        if (from.getType() != SchemaType.KEY_VALUE || to.getType() != SchemaType.KEY_VALUE) {
+            if (strategy == SchemaCompatibilityStrategy.FULL || strategy == SchemaCompatibilityStrategy.ALWAYS_COMPATIBLE) {
 
 Review comment:
   When I debug integration test of debezium, I found its default policy is FULL, schemaData of from is String and schemaData of to is KeyValue,  i am not sure whether this judgment condition should be added here or whether the strategy should be updated to ALWAYS_COMPATIBLE in the integration test:
   ```
   10:03:34.332 [ForkJoinPool.commonPool-worker-2] INFO  org.apache.pulsar.broker.service.schema.KeyValueSchemaCompatibilityCheck - from SchemaData(type=STRING, isDeleted=false, timestamp=0, user=, data=[], props={})
   10:03:34.332 [ForkJoinPool.commonPool-worker-2] INFO  org.apache.pulsar.broker.service.schema.KeyValueSchemaCompatibilityCheck - to SchemaData(type=KEY_VALUE, isDeleted=false, timestamp=1558087414170, user=, data=[0, 0, 0, 0, 0, 0, 0, 0], props={key.schema.properties={}, value.schema.properties={}, value.schema.type=BYTES, key.schema.name=Bytes, value.schema.name=Bytes, kv.encoding.type=INLINE, key.schema.type=BYTES})
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services