You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2022/08/23 09:50:21 UTC

[GitHub] [rocketmq-schema-registry] MatrixHB commented on a diff in pull request #47: Support ser/de skip schema registry service.[ISSUE-38]

MatrixHB commented on code in PR #47:
URL: https://github.com/apache/rocketmq-schema-registry/pull/47#discussion_r952204994


##########
client/src/test/java/org/apache/rocketmq/schema/registry/client/serde/SkipSchemaRegistrySerdeTest.java:
##########
@@ -0,0 +1,70 @@
+/*
+ * 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.rocketmq.schema.registry.client.serde;
+
+import org.apache.rocketmq.schema.registry.client.config.AvroSerializerConfig;
+import org.apache.rocketmq.schema.registry.client.config.JsonSerializerConfig;
+import org.apache.rocketmq.schema.registry.client.serde.avro.ReflectionAvroSerde;
+import org.apache.rocketmq.schema.registry.client.serde.json.JsonSerde;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class SkipSchemaRegistrySerdeTest {
+
+    @Test
+    public void testReflectionAvroSerde() {
+        Charge charge = new Charge("specific", 100.0);
+        Map<String, Object> configs = new HashMap<>();
+        configs.put(AvroSerializerConfig.DESERIALIZE_TARGET_TYPE, charge.getClass());
+        try (ReflectionAvroSerde serde = new ReflectionAvroSerde()) {
+            //serialize
+            serde.configure(configs);
+
+            byte[] bytes = serde.serializer().serialize("TopicTest", charge);
+            //deserialize
+            Charge charge1 = (Charge) serde.deserializer().deserialize("TopicTest", bytes);
+            assertThat(charge1).isEqualTo(charge);
+        } catch (IOException e) {
+            System.out.println("serde shutdown failed");
+        }
+    }

Review Comment:
   Could you move this test into another Test class, ReflectionAvroSerdeTest ?



##########
client/src/main/java/org/apache/rocketmq/schema/registry/client/serde/json/JsonDeserializer.java:
##########
@@ -35,31 +36,47 @@
 
 public class JsonDeserializer<T> implements Deserializer<T> {
     Logger log = LoggerFactory.getLogger(JsonDeserializer.class);
-    private final SchemaRegistryClient registryClient;
-    private final ObjectMapper objectMapper;
-    private final Class<T> type;
+    private SchemaRegistryClient registryClient;
+    private final ObjectMapper objectMapper = JacksonMapper.INSTANCE;
+    private boolean skipSchemaRegistry;
+    private Class<T> type;
 
-    public JsonDeserializer(SchemaRegistryClient registryClient, Class<T> type) {
+    public JsonDeserializer() {
+    }
+
+    public JsonDeserializer(SchemaRegistryClient registryClient) {
         this.registryClient = registryClient;
-        objectMapper = JacksonMapper.INSTANCE;
-        this.type = type;
     }
 
     @Override
     public void configure(Map<String, Object> configs) {
-
+        JsonSerializerConfig serializerConfig = new JsonSerializerConfig(configs);
+        this.skipSchemaRegistry = serializerConfig.skipSchemaRegistry();
+        this.type = (Class<T>) serializerConfig.deserializeTargetType();
     }
 
     @Override
     public T deserialize(String subject, byte[] payload) {
-        if (null == registryClient) {
-            throw new SerializationException("please initialize the schema registry client first");
-        }
-
         if (null == payload || payload.length == 0) {
             return null;
         }
 
+        if (null == type) {
+            throw new SerializationException("type cannot be null");
+        }

Review Comment:
   IMO, when skipSchemaRegistry=false,deserializeTargetType can be null. 
   
   ```
   if (skipSchemaRegistry) {
           if (null == type) {
               throw new SerializationException("type cannot be null");
           }
           ....
   } 
   ```
   may be better



##########
client/src/main/java/org/apache/rocketmq/schema/registry/client/serde/json/JsonDeserializer.java:
##########
@@ -35,31 +36,47 @@
 
 public class JsonDeserializer<T> implements Deserializer<T> {
     Logger log = LoggerFactory.getLogger(JsonDeserializer.class);
-    private final SchemaRegistryClient registryClient;
-    private final ObjectMapper objectMapper;
-    private final Class<T> type;
+    private SchemaRegistryClient registryClient;
+    private final ObjectMapper objectMapper = JacksonMapper.INSTANCE;
+    private boolean skipSchemaRegistry;
+    private Class<T> type;
 
-    public JsonDeserializer(SchemaRegistryClient registryClient, Class<T> type) {
+    public JsonDeserializer() {
+    }
+
+    public JsonDeserializer(SchemaRegistryClient registryClient) {
         this.registryClient = registryClient;
-        objectMapper = JacksonMapper.INSTANCE;
-        this.type = type;
     }
 
     @Override
     public void configure(Map<String, Object> configs) {
-
+        JsonSerializerConfig serializerConfig = new JsonSerializerConfig(configs);
+        this.skipSchemaRegistry = serializerConfig.skipSchemaRegistry();
+        this.type = (Class<T>) serializerConfig.deserializeTargetType();
     }
 
     @Override
     public T deserialize(String subject, byte[] payload) {
-        if (null == registryClient) {
-            throw new SerializationException("please initialize the schema registry client first");
-        }
-
         if (null == payload || payload.length == 0) {
             return null;
         }
 
+        if (null == type) {
+            throw new SerializationException("type cannot be null");
+        }
+
+        if (skipSchemaRegistry) {
+            try {
+                return objectMapper.readValue(payload, type);
+            } catch (Exception e) {
+                throw new SerializationException("JSON serialize failed", e);
+            }

Review Comment:
   When producer use skipSchemaRegistry=false, it means that consumer should also be  skipSchemaRegistry=false, or the deserialization will fail because the payload contains schemaRecordId at the head. 
   Does this compatibility need to be considered? @humkum @ferrirW 



##########
client/src/main/java/org/apache/rocketmq/schema/registry/client/serde/avro/ReflectionAvroSerializer.java:
##########
@@ -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.rocketmq.schema.registry.client.serde.avro;
+
+import org.apache.avro.Schema;
+import org.apache.avro.io.BinaryEncoder;
+import org.apache.avro.io.DatumWriter;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.avro.reflect.ReflectData;
+import org.apache.avro.specific.SpecificDatumWriter;
+import org.apache.rocketmq.schema.registry.client.exceptions.SerializationException;
+import org.apache.rocketmq.schema.registry.client.serde.Serializer;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Map;
+
+public class ReflectionAvroSerializer<T> implements Serializer<T> {
+    private final EncoderFactory encoderFactory = EncoderFactory.get();
+
+    public ReflectionAvroSerializer() {
+    }
+
+    @Override
+    public void configure(Map<String, Object> configs) {
+
+    }
+
+    @Override
+    public byte[] serialize(String subject, T record) {
+        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+            BinaryEncoder encoder = encoderFactory.directBinaryEncoder(out, null);
+            Schema schema = ReflectData.get().getSchema(record.getClass());
+
+            DatumWriter<T> datumWriter = new SpecificDatumWriter<>(schema);

Review Comment:
   Should it be ReflectDatumReader?



##########
client/src/main/java/org/apache/rocketmq/schema/registry/client/serde/avro/ReflectionAvroDeserializer.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.rocketmq.schema.registry.client.serde.avro;
+
+import org.apache.avro.Schema;
+import org.apache.avro.io.BinaryDecoder;
+import org.apache.avro.io.DatumReader;
+import org.apache.avro.io.DecoderFactory;
+import org.apache.avro.reflect.ReflectData;
+import org.apache.avro.specific.SpecificDatumReader;
+import org.apache.rocketmq.schema.registry.client.config.AvroSerializerConfig;
+import org.apache.rocketmq.schema.registry.client.exceptions.SerializationException;
+import org.apache.rocketmq.schema.registry.client.serde.Deserializer;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.lang.reflect.Type;
+import java.util.Map;
+
+public class ReflectionAvroDeserializer<T> implements Deserializer<T> {
+    private Type type;
+
+    public ReflectionAvroDeserializer() {
+    }
+
+    @Override
+    public void configure(Map<String, Object> configs) {
+        AvroSerializerConfig avroSerializerConfig = new AvroSerializerConfig(configs);
+        this.type = avroSerializerConfig.deserializeTargetType();
+    }
+
+    @Override
+    public T deserialize(String subject, byte[] bytes) {
+        if (null == bytes) return null;
+
+        if (null == type) {
+            throw new SerializationException("deserialize type can not be null");
+        }
+        Schema schema = ReflectData.get().getSchema(type);
+        try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) {
+            BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(bais, null);
+            DatumReader<T> datumReader = new SpecificDatumReader<>(schema);

Review Comment:
   Should it be ReflectDatumReader?



-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org