You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2022/10/08 07:58:07 UTC

[GitHub] [druid] abhishekagarwal87 commented on a diff in pull request #13192: Add inline descriptor Protobuf bytes decoder

abhishekagarwal87 commented on code in PR #13192:
URL: https://github.com/apache/druid/pull/13192#discussion_r990607935


##########
extensions-core/protobuf-extensions/src/main/java/org/apache/druid/data/input/protobuf/InlineDescriptorProtobufBytesDecoder.java:
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.druid.data.input.protobuf;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.github.os72.protobuf.dynamic.DynamicSchema;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.protobuf.ByteString;
+import com.google.protobuf.Descriptors;
+import com.google.protobuf.DynamicMessage;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.common.parsers.ParseException;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Objects;
+import java.util.Set;
+
+public class InlineDescriptorProtobufBytesDecoder implements ProtobufBytesDecoder
+{
+  private final String descriptorString;
+  private final String protoMessageType;
+  private Descriptors.Descriptor descriptor;
+
+
+  @JsonCreator
+  public InlineDescriptorProtobufBytesDecoder(
+      @JsonProperty("descriptorString") String descriptorString,
+      @JsonProperty("protoMessageType") String protoMessageType
+  )
+  {
+    this.descriptorString = descriptorString;
+    this.protoMessageType = protoMessageType;
+    initDescriptor();
+  }
+
+  @JsonProperty
+  public String getDescriptorString()
+  {
+    return descriptorString;
+  }
+
+  @JsonProperty
+  public String getProtoMessageType()
+  {
+    return protoMessageType;
+  }
+
+  @VisibleForTesting
+  void initDescriptor()
+  {
+    if (this.descriptor == null) {
+      this.descriptor = getDescriptor(descriptorString);
+    }
+  }
+
+  @Override
+  public DynamicMessage parse(ByteBuffer bytes)
+  {
+    try {
+      DynamicMessage message = DynamicMessage.parseFrom(descriptor, ByteString.copyFrom(bytes));
+      return message;
+    }
+    catch (Exception e) {
+      throw new ParseException(null, e, "Fail to decode protobuf message!");
+    }
+  }
+
+  private Descriptors.Descriptor getDescriptor(String descriptorString)
+  {
+    DynamicSchema dynamicSchema;
+    try {
+      byte[] decodedDesc = StringUtils.decodeBase64String(descriptorString);

Review Comment:
   I agree that it is not a kind of exception that we can just move over. Though @FrankChen021  makes a good point of surfacing a nicer user-friendly error and throwing an IAE isn't a good way of getting that done. 
   @jon-wei - What error does the user get with this change if the descriptor string is not valid / can't be decoded? 



-- 
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: commits-unsubscribe@druid.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org