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

[GitHub] [incubator-inlong] luchunliang commented on a change in pull request #2392: [INLONG-2376] add sdk-common module for supporting PB compression protocol format based on Protobuffer protocol.

luchunliang commented on a change in pull request #2392:
URL: https://github.com/apache/incubator-inlong/pull/2392#discussion_r803256194



##########
File path: inlong-sdk/sdk-common/src/main/java/org/apache/inlong/sdk/commons/protocol/EventUtils.java
##########
@@ -0,0 +1,215 @@
+/**
+ * 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.inlong.sdk.commons.protocol;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.inlong.sdk.commons.protocol.ProxySdk.INLONG_COMPRESSED_TYPE;
+import org.apache.inlong.sdk.commons.protocol.ProxySdk.MapFieldEntry;
+import org.apache.inlong.sdk.commons.protocol.ProxySdk.MessageObj;
+import org.apache.inlong.sdk.commons.protocol.ProxySdk.MessageObjs;
+import org.apache.inlong.sdk.commons.protocol.ProxySdk.MessagePack;
+import org.apache.inlong.sdk.commons.protocol.ProxySdk.MessagePackHeader;
+import org.apache.inlong.sdk.commons.utils.GzipUtils;
+import org.xerial.snappy.Snappy;
+
+import com.google.protobuf.ByteString;
+
+/**
+ * EventUtils
+ */
+public class EventUtils {
+
+    /**
+     * encode
+     * 
+     * @param  inlongGroupId
+     * @param  inlongStreamId
+     * @param  compressedType
+     * @param  events
+     * @return                MessagePack

Review comment:
       fixed

##########
File path: inlong-sdk/sdk-common/src/main/java/org/apache/inlong/sdk/commons/protocol/EventUtils.java
##########
@@ -0,0 +1,215 @@
+/**
+ * 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.inlong.sdk.commons.protocol;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.inlong.sdk.commons.protocol.ProxySdk.INLONG_COMPRESSED_TYPE;
+import org.apache.inlong.sdk.commons.protocol.ProxySdk.MapFieldEntry;
+import org.apache.inlong.sdk.commons.protocol.ProxySdk.MessageObj;
+import org.apache.inlong.sdk.commons.protocol.ProxySdk.MessageObjs;
+import org.apache.inlong.sdk.commons.protocol.ProxySdk.MessagePack;
+import org.apache.inlong.sdk.commons.protocol.ProxySdk.MessagePackHeader;
+import org.apache.inlong.sdk.commons.utils.GzipUtils;
+import org.xerial.snappy.Snappy;
+
+import com.google.protobuf.ByteString;
+
+/**
+ * EventUtils
+ */
+public class EventUtils {
+
+    /**
+     * encode
+     * 
+     * @param  inlongGroupId
+     * @param  inlongStreamId
+     * @param  compressedType
+     * @param  events
+     * @return                MessagePack
+     * @throws IOException
+     */
+    public static MessagePack encodeSdkEvents(String inlongGroupId, String inlongStreamId,
+            INLONG_COMPRESSED_TYPE compressedType, List<SdkEvent> events) throws IOException {
+        // MessageObjs
+        MessageObjs.Builder objsBuilder = MessageObjs.newBuilder();
+        for (SdkEvent event : events) {
+            MessageObj.Builder objBuilder = MessageObj.newBuilder();
+            objBuilder.setMsgTime(event.getMsgTime());
+            objBuilder.setSourceIp(event.getSourceIp());
+            objBuilder.setBody(ByteString.copyFrom(event.getBody()));
+            objsBuilder.addMsgs(objBuilder.build());
+        }
+        MessageObjs objs = objsBuilder.build();
+        byte[] srcBytes = objs.toByteArray();
+        byte[] compressedBytes = null;
+        switch (compressedType) {
+            case INLONG_SNAPPY :
+                compressedBytes = Snappy.compress(srcBytes);
+                break;
+            case INLONG_GZ :
+                compressedBytes = GzipUtils.compress(srcBytes);
+                break;
+            case INLONG_NO_COMPRESS :
+            default :
+                compressedBytes = srcBytes;
+                break;
+        }
+        // MessagePack
+        MessagePack.Builder packBuilder = MessagePack.newBuilder();
+        packBuilder.setCompressBytes(ByteString.copyFrom(compressedBytes));
+        // MessagePackHeader
+        MessagePackHeader.Builder headerBuilder = MessagePackHeader.newBuilder();
+        // string inlongGroupId = 1; //inlongGroupId
+        headerBuilder.setInlongGroupId(inlongGroupId);
+        // string inlongStreamId = 2; //inlongStreamId
+        headerBuilder.setInlongStreamId(inlongStreamId);
+        // int64 packTime = 3; //pack time, milliseconds
+        headerBuilder.setPackTime(System.currentTimeMillis());
+        // int32 msgCount = 4; //message count
+        headerBuilder.setMsgCount(events.size());
+        // int32 srcLength = 5; //total length of raw messages body
+        headerBuilder.setSrcLength(srcBytes.length);
+        // int32 compressLen = 6; //compress length of messages
+        headerBuilder.setCompressLen(compressedBytes.length);
+        // INLONG_COMPRESSED_TYPE compressType = 7; //compress type
+        headerBuilder.setCompressType(compressedType);
+        // map<string, string> params = 8; //additional parameters
+        packBuilder.setHeader(headerBuilder.build());
+        return packBuilder.build();
+    }
+
+    /**
+     * decodeSdkPack
+     * 
+     * @param  packBytes
+     * @param  offset
+     * @param  length
+     * @return             List,ProxyEvent

Review comment:
       fixed




-- 
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@inlong.apache.org

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