You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ozone.apache.org by ru...@apache.org on 2021/01/21 09:26:01 UTC

[ozone] branch HDDS-2823 updated: HDDS-4695. Support encode and decode ArrayList and Long. (#1831)

This is an automated email from the ASF dual-hosted git repository.

runzhiwang pushed a commit to branch HDDS-2823
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/HDDS-2823 by this push:
     new 8ce33f2  HDDS-4695. Support encode and decode ArrayList and Long. (#1831)
8ce33f2 is described below

commit 8ce33f275870f4db0754c37544fe2f8a3124bc8f
Author: Nandakumar <na...@apache.org>
AuthorDate: Thu Jan 21 14:55:42 2021 +0530

    HDDS-4695. Support encode and decode ArrayList and Long. (#1831)
---
 .../src/main/proto/SCMRatisProtocol.proto          |  5 ++
 .../apache/hadoop/hdds/scm/ha/SCMRatisRequest.java | 34 ++--------
 .../hadoop/hdds/scm/ha/SCMRatisResponse.java       | 72 +++++++---------------
 .../org/apache/hadoop/hdds/scm/ha/io/Codec.java    | 30 +++++++++
 .../apache/hadoop/hdds/scm/ha/io/CodecFactory.java | 57 +++++++++++++++++
 .../apache/hadoop/hdds/scm/ha/io/EnumCodec.java    | 50 +++++++++++++++
 .../hdds/scm/ha/io/GeneratedMessageCodec.java      | 48 +++++++++++++++
 .../apache/hadoop/hdds/scm/ha/io/ListCodec.java    | 69 +++++++++++++++++++++
 .../apache/hadoop/hdds/scm/ha/io/LongCodec.java    | 38 ++++++++++++
 .../hadoop/hdds/scm/ha/io/package-info.java}       | 33 ++--------
 .../hadoop/hdds/scm/ha/TestSCMRatisRequest.java    | 32 +++++++++-
 11 files changed, 362 insertions(+), 106 deletions(-)

diff --git a/hadoop-hdds/interface-server/src/main/proto/SCMRatisProtocol.proto b/hadoop-hdds/interface-server/src/main/proto/SCMRatisProtocol.proto
index 1107016..8818a83 100644
--- a/hadoop-hdds/interface-server/src/main/proto/SCMRatisProtocol.proto
+++ b/hadoop-hdds/interface-server/src/main/proto/SCMRatisProtocol.proto
@@ -35,6 +35,11 @@ message MethodArgument {
     required bytes value = 2;
 }
 
+message ListArgument {
+    required string type = 1;
+    repeated bytes value = 2;
+}
+
 message SCMRatisRequestProto {
     required RequestType type = 1;
     required Method method = 2;
diff --git a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMRatisRequest.java b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMRatisRequest.java
index fbba4d0..4277bb0 100644
--- a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMRatisRequest.java
+++ b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMRatisRequest.java
@@ -17,17 +17,12 @@
 
 package org.apache.hadoop.hdds.scm.ha;
 
-import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
 import java.util.List;
 
-import com.google.common.primitives.Ints;
-import com.google.protobuf.GeneratedMessage;
 import com.google.protobuf.InvalidProtocolBufferException;
 
-import com.google.protobuf.ByteString;
-import com.google.protobuf.ProtocolMessageEnum;
-
+import org.apache.hadoop.hdds.scm.ha.io.CodecFactory;
 import org.apache.ratis.protocol.Message;
 
 import org.apache.hadoop.hdds.protocol.proto.SCMRatisProtocol.Method;
@@ -94,15 +89,8 @@ public final class SCMRatisRequest {
     for (Object argument : arguments) {
       final MethodArgument.Builder argBuilder = MethodArgument.newBuilder();
       argBuilder.setType(argument.getClass().getName());
-      if (argument instanceof GeneratedMessage) {
-        argBuilder.setValue(((GeneratedMessage) argument).toByteString());
-      } else if (argument instanceof ProtocolMessageEnum) {
-        argBuilder.setValue(ByteString.copyFrom(Ints.toByteArray(
-            ((ProtocolMessageEnum) argument).getNumber())));
-      } else {
-        throw new InvalidProtocolBufferException(argument.getClass() +
-            " is not a protobuf object!");
-      }
+      argBuilder.setValue(CodecFactory.getCodec(argument.getClass())
+          .serialize(argument));
       args.add(argBuilder.build());
     }
     methodBuilder.addAllArgs(args);
@@ -124,19 +112,9 @@ public final class SCMRatisRequest {
     for (MethodArgument argument : method.getArgsList()) {
       try {
         final Class<?> clazz = ReflectionUtil.getClass(argument.getType());
-        if (GeneratedMessage.class.isAssignableFrom(clazz)) {
-          args.add(ReflectionUtil.getMethod(clazz, "parseFrom", byte[].class)
-              .invoke(null, (Object) argument.getValue().toByteArray()));
-        } else if (Enum.class.isAssignableFrom(clazz)) {
-          args.add(ReflectionUtil.getMethod(clazz, "valueOf", int.class)
-              .invoke(null, Ints.fromByteArray(
-                  argument.getValue().toByteArray())));
-        } else {
-          throw new InvalidProtocolBufferException(argument.getType() +
-              " is not a protobuf object!");
-        }
-      } catch (ClassNotFoundException | NoSuchMethodException |
-          IllegalAccessException | InvocationTargetException ex) {
+        args.add(CodecFactory.getCodec(clazz)
+            .deserialize(clazz, argument.getValue()));
+      } catch (ClassNotFoundException ex) {
         throw new InvalidProtocolBufferException(argument.getType() +
             " cannot be decoded!" + ex.getMessage());
       }
diff --git a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMRatisResponse.java b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMRatisResponse.java
index b3ec543..15163bf 100644
--- a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMRatisResponse.java
+++ b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMRatisResponse.java
@@ -17,14 +17,10 @@
 
 package org.apache.hadoop.hdds.scm.ha;
 
-import java.lang.reflect.InvocationTargetException;
-import java.math.BigInteger;
-
 import com.google.protobuf.ByteString;
-import com.google.protobuf.GeneratedMessage;
 import com.google.protobuf.InvalidProtocolBufferException;
-import com.google.protobuf.ProtocolMessageEnum;
 import org.apache.hadoop.hdds.protocol.proto.SCMRatisProtocol.SCMRatisResponseProto;
+import org.apache.hadoop.hdds.scm.ha.io.CodecFactory;
 import org.apache.ratis.protocol.Message;
 import org.apache.ratis.protocol.RaftClientReply;
 
@@ -37,6 +33,10 @@ public final class SCMRatisResponse {
   private final Object result;
   private final Exception exception;
 
+  private SCMRatisResponse() {
+    this(true, null, null);
+  }
+
   private SCMRatisResponse(final Object result) {
     this(true, result, null);
   }
@@ -71,22 +71,11 @@ public final class SCMRatisResponse {
       return Message.EMPTY;
     }
 
-    final ByteString value;
-    if (result instanceof GeneratedMessage) {
-      value = ((GeneratedMessage) result).toByteString();
-    } else if (result instanceof ProtocolMessageEnum) {
-      value = ByteString.copyFrom(BigInteger.valueOf(
-          ((ProtocolMessageEnum) result).getNumber()).toByteArray());
-    } else {
-      throw new InvalidProtocolBufferException(result.getClass() +
-          " is not a protobuf object!");
-    }
+    final Class<?> type = result.getClass();
+    final ByteString value = CodecFactory.getCodec(type).serialize(result);
 
-    final SCMRatisResponseProto response =
-        SCMRatisResponseProto.newBuilder()
-            .setType(result.getClass().getName())
-            .setValue(value)
-        .build();
+    final SCMRatisResponseProto response = SCMRatisResponseProto.newBuilder()
+        .setType(type.getName()).setValue(value).build();
     return Message.valueOf(
         org.apache.ratis.thirdparty.com.google.protobuf.ByteString.copyFrom(
             response.toByteArray()));
@@ -94,42 +83,27 @@ public final class SCMRatisResponse {
 
   public static SCMRatisResponse decode(RaftClientReply reply)
       throws InvalidProtocolBufferException {
-    return reply.isSuccess() ?
-        new SCMRatisResponse(
-            deserializeResult(reply.getMessage().getContent().toByteArray())) :
-        new SCMRatisResponse(reply.getException());
-  }
+    if (!reply.isSuccess()) {
+      return new SCMRatisResponse(reply.getException());
+    }
+
+    final byte[] response = reply.getMessage().getContent().toByteArray();
 
-  private static Object deserializeResult(byte[] response)
-      throws InvalidProtocolBufferException {
     if (response.length == 0) {
-      return null;
+      return new SCMRatisResponse();
     }
 
-    final SCMRatisResponseProto responseProto =
-        SCMRatisResponseProto.parseFrom(response);
-    try {
-      final Class<?> clazz = ReflectionUtil.getClass(responseProto.getType());
-      if (GeneratedMessage.class.isAssignableFrom(clazz)) {
-        return ReflectionUtil.getMethod(clazz, "parseFrom", byte[].class)
-            .invoke(null, (Object) responseProto.getValue().toByteArray());
-      }
-
-      if (Enum.class.isAssignableFrom(clazz)) {
-        return ReflectionUtil.getMethod(clazz, "valueOf", int.class)
-            .invoke(null, new BigInteger(
-                responseProto.getValue().toByteArray()).intValue());
-      }
-
-      throw new InvalidProtocolBufferException(responseProto.getType() +
-            " is not a protobuf object!");
+    final SCMRatisResponseProto responseProto = SCMRatisResponseProto
+        .parseFrom(response);
 
-    } catch (ClassNotFoundException | NoSuchMethodException |
-        IllegalAccessException | InvocationTargetException ex) {
+    try {
+      final Class<?> type = ReflectionUtil.getClass(responseProto.getType());
+      return new SCMRatisResponse(CodecFactory.getCodec(type)
+          .deserialize(type, responseProto.getValue()));
+    } catch (ClassNotFoundException e) {
       throw new InvalidProtocolBufferException(responseProto.getType() +
-          " cannot be decoded!" + ex.getMessage());
+          " cannot be decoded!" + e.getMessage());
     }
-
   }
 
 }
diff --git a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/Codec.java b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/Codec.java
new file mode 100644
index 0000000..2e16376
--- /dev/null
+++ b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/Codec.java
@@ -0,0 +1,30 @@
+/*
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.hadoop.hdds.scm.ha.io;
+
+import com.google.protobuf.ByteString;
+import com.google.protobuf.InvalidProtocolBufferException;
+
+public interface Codec {
+
+  ByteString serialize(Object object) throws InvalidProtocolBufferException;
+
+  Object deserialize(Class<?> type, ByteString value)
+      throws InvalidProtocolBufferException;
+
+}
diff --git a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/CodecFactory.java b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/CodecFactory.java
new file mode 100644
index 0000000..5a4a491
--- /dev/null
+++ b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/CodecFactory.java
@@ -0,0 +1,57 @@
+/*
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.hadoop.hdds.scm.ha.io;
+
+import com.google.protobuf.GeneratedMessage;
+import com.google.protobuf.InvalidProtocolBufferException;
+import com.google.protobuf.ProtocolMessageEnum;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public final class CodecFactory {
+
+  private static Map<Class<?>, Codec> codecs = new HashMap<>();
+
+  static {
+    codecs.put(GeneratedMessage.class, new GeneratedMessageCodec());
+    codecs.put(ProtocolMessageEnum.class, new EnumCodec());
+    codecs.put(List.class, new ListCodec());
+    codecs.put(Long.class, new LongCodec());
+  }
+
+  private CodecFactory() {}
+
+  public static Codec getCodec(Class<?> type)
+      throws InvalidProtocolBufferException {
+    final List<Class<?>> classes = new ArrayList<>();
+    classes.add(type);
+    classes.add(type.getSuperclass());
+    classes.addAll(Arrays.asList(type.getInterfaces()));
+    for (Class<?> clazz : classes) {
+      if (codecs.containsKey(clazz)) {
+        return codecs.get(clazz);
+      }
+    }
+    throw new InvalidProtocolBufferException(
+        "Codec for " + type + " not found!");
+  }
+}
diff --git a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/EnumCodec.java b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/EnumCodec.java
new file mode 100644
index 0000000..bca71ed
--- /dev/null
+++ b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/EnumCodec.java
@@ -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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.hadoop.hdds.scm.ha.io;
+
+import com.google.common.primitives.Ints;
+import com.google.protobuf.ByteString;
+import com.google.protobuf.InvalidProtocolBufferException;
+import com.google.protobuf.ProtocolMessageEnum;
+import org.apache.hadoop.hdds.scm.ha.ReflectionUtil;
+
+import java.lang.reflect.InvocationTargetException;
+
+public class EnumCodec implements Codec {
+
+  @Override
+  public ByteString serialize(Object object)
+      throws InvalidProtocolBufferException {
+    return ByteString.copyFrom(Ints.toByteArray(
+        ((ProtocolMessageEnum) object).getNumber()));
+  }
+
+  @Override
+  public Object deserialize(Class<?> type, ByteString value)
+      throws InvalidProtocolBufferException {
+    try {
+      return ReflectionUtil.getMethod(type, "valueOf", int.class)
+          .invoke(null, Ints.fromByteArray(
+              value.toByteArray()));
+    } catch (NoSuchMethodException | IllegalAccessException
+        | InvocationTargetException ex) {
+      throw new InvalidProtocolBufferException(
+          "GeneratedMessage cannot be decoded!" + ex.getMessage());
+    }
+  }
+}
diff --git a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/GeneratedMessageCodec.java b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/GeneratedMessageCodec.java
new file mode 100644
index 0000000..acfc719
--- /dev/null
+++ b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/GeneratedMessageCodec.java
@@ -0,0 +1,48 @@
+/*
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.hadoop.hdds.scm.ha.io;
+
+import com.google.protobuf.ByteString;
+import com.google.protobuf.GeneratedMessage;
+import com.google.protobuf.InvalidProtocolBufferException;
+import org.apache.hadoop.hdds.scm.ha.ReflectionUtil;
+
+import java.lang.reflect.InvocationTargetException;
+
+public class GeneratedMessageCodec implements Codec {
+
+  @Override
+  public ByteString serialize(Object object) {
+    return ((GeneratedMessage)object).toByteString();
+  }
+
+  @Override
+  public GeneratedMessage deserialize(Class<?> type, ByteString value)
+      throws InvalidProtocolBufferException {
+    try {
+      return (GeneratedMessage) ReflectionUtil.getMethod(type,
+          "parseFrom", byte[].class)
+          .invoke(null, (Object) value.toByteArray());
+    } catch (NoSuchMethodException | IllegalAccessException
+        | InvocationTargetException ex) {
+      ex.printStackTrace();
+      throw new InvalidProtocolBufferException(
+          "GeneratedMessage cannot be decoded: " + ex.getMessage());
+    }
+  }
+}
diff --git a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/ListCodec.java b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/ListCodec.java
new file mode 100644
index 0000000..0dbb1c0
--- /dev/null
+++ b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/ListCodec.java
@@ -0,0 +1,69 @@
+/*
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.hadoop.hdds.scm.ha.io;
+
+import com.google.protobuf.ByteString;
+import com.google.protobuf.InvalidProtocolBufferException;
+import org.apache.hadoop.hdds.protocol.proto.SCMRatisProtocol.ListArgument;
+import org.apache.hadoop.hdds.scm.ha.ReflectionUtil;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.List;
+
+public class ListCodec implements Codec {
+
+  @Override
+  public ByteString serialize(Object object)
+      throws InvalidProtocolBufferException {
+    final ListArgument.Builder listArgs = ListArgument.newBuilder();
+    final List<?> values = (List<?>) object;
+    if (!values.isEmpty()) {
+      Class<?> type = values.get(0).getClass();
+      listArgs.setType(type.getName());
+      for (Object value : values) {
+        listArgs.addValue(CodecFactory.getCodec(type).serialize(value));
+      }
+    } else {
+      listArgs.setType(Object.class.getName());
+    }
+    return listArgs.build().toByteString();
+  }
+
+  @Override
+  public Object deserialize(Class<?> type, ByteString value)
+      throws InvalidProtocolBufferException {
+    try {
+
+      List<Object> result = (List<Object>) type.newInstance();
+      final ListArgument listArgs = (ListArgument) ReflectionUtil
+          .getMethod(ListArgument.class, "parseFrom", byte[].class)
+          .invoke(null, (Object) value.toByteArray());
+      final Class<?> dataType = ReflectionUtil.getClass(listArgs.getType());
+      for (ByteString element : listArgs.getValueList()) {
+        result.add(CodecFactory.getCodec(dataType)
+            .deserialize(dataType, element));
+      }
+      return result;
+    } catch (InstantiationException | NoSuchMethodException |
+        IllegalAccessException | InvocationTargetException |
+        ClassNotFoundException ex) {
+      throw new InvalidProtocolBufferException(
+          "GeneratedMessage cannot be decoded: " + ex.getMessage());
+    }
+  }
+}
diff --git a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/LongCodec.java b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/LongCodec.java
new file mode 100644
index 0000000..1a02dbd
--- /dev/null
+++ b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/LongCodec.java
@@ -0,0 +1,38 @@
+/*
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.hadoop.hdds.scm.ha.io;
+
+import com.google.common.primitives.Longs;
+import com.google.protobuf.ByteString;
+import com.google.protobuf.InvalidProtocolBufferException;
+
+public class LongCodec implements Codec {
+
+  @Override
+  public ByteString serialize(Object object)
+      throws InvalidProtocolBufferException {
+    return ByteString.copyFrom(Longs.toByteArray((Long) object));
+  }
+
+  @Override
+  public Object deserialize(Class<?> type, ByteString value)
+      throws InvalidProtocolBufferException {
+    return Longs.fromByteArray(value.toByteArray());
+  }
+
+}
diff --git a/hadoop-hdds/interface-server/src/main/proto/SCMRatisProtocol.proto b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/package-info.java
similarity index 57%
copy from hadoop-hdds/interface-server/src/main/proto/SCMRatisProtocol.proto
copy to hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/package-info.java
index 1107016..718b76c 100644
--- a/hadoop-hdds/interface-server/src/main/proto/SCMRatisProtocol.proto
+++ b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/package-info.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -16,31 +16,8 @@
  * limitations under the License.
  */
 
-option java_package = "org.apache.hadoop.hdds.protocol.proto";
-option java_outer_classname = "SCMRatisProtocol";
-option java_generate_equals_and_hash = true;
-
-enum RequestType {
-    PIPELINE = 1;
-    CONTAINER = 2;
-}
-
-message Method {
-    required string name = 1;
-    repeated MethodArgument args = 2;
-}
-
-message MethodArgument {
-    required string type = 1;
-    required bytes value = 2;
-}
-
-message SCMRatisRequestProto {
-    required RequestType type = 1;
-    required Method method = 2;
-}
+/**
+ * This package contains classes related to SCM HA Serialization.
+ */
+package org.apache.hadoop.hdds.scm.ha.io;
 
-message SCMRatisResponseProto {
-    required string type = 2;
-    required bytes value = 3;
-}
diff --git a/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestSCMRatisRequest.java b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestSCMRatisRequest.java
index 52d2ff3..5295e0f 100644
--- a/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestSCMRatisRequest.java
+++ b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestSCMRatisRequest.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -19,11 +19,15 @@
 package org.apache.hadoop.hdds.scm.ha;
 
 import com.google.protobuf.InvalidProtocolBufferException;
+import org.apache.hadoop.hdds.protocol.proto.HddsProtos;
 import org.apache.hadoop.hdds.scm.pipeline.PipelineID;
 import org.apache.ratis.protocol.Message;
 import org.junit.Assert;
 import org.junit.Test;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import static org.apache.hadoop.hdds.protocol.proto.SCMRatisProtocol.RequestType.PIPELINE;
 
 /**
@@ -60,4 +64,30 @@ public class TestSCMRatisRequest {
     // Should throw exception there.
     SCMRatisRequest.decode(message);
   }
+
+  @Test
+  public void testEncodeAndDecodeWithList() throws Exception {
+    List<HddsProtos.PipelineID> pids = new ArrayList<>();
+    pids.add(PipelineID.randomId().getProtobuf());
+    pids.add(PipelineID.randomId().getProtobuf());
+    pids.add(PipelineID.randomId().getProtobuf());
+    Object[] args = new Object[] {pids};
+    String operation = "test";
+    SCMRatisRequest request = SCMRatisRequest.of(PIPELINE, operation, args);
+    Assert.assertEquals(operation,
+        SCMRatisRequest.decode(request.encode()).getOperation());
+    Assert.assertEquals(args[0],
+        SCMRatisRequest.decode(request.encode()).getArguments()[0]);
+  }
+
+  @Test
+  public void testEncodeAndDecodeOfLong() throws Exception {
+    final Long value = 10L;
+    String operation = "test";
+    SCMRatisRequest request = SCMRatisRequest.of(PIPELINE, operation, value);
+    Assert.assertEquals(operation,
+        SCMRatisRequest.decode(request.encode()).getOperation());
+    Assert.assertEquals(value,
+        SCMRatisRequest.decode(request.encode()).getArguments()[0]);
+  }
 }


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