Skip to content

Commit 19f43c1

Browse files
authored
HDDS-14561. SCMRatisRequest/ResponseProto should use the shaded protobuf from Ratis (#9733)
1 parent 371a2da commit 19f43c1

24 files changed

Lines changed: 301 additions & 131 deletions

hadoop-hdds/interface-server/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
<includes>
8484
<include>InterSCMProtocol.proto</include>
8585
<include>SCMUpdateProtocol.proto</include>
86+
<include>SCMRatisProtocol.proto</include>
8687
</includes>
8788
<outputDirectory>target/generated-sources/proto-java-for-ratis</outputDirectory>
8889
<clearOutputDirectory>false</clearOutputDirectory>
@@ -101,6 +102,7 @@
101102
<excludes>
102103
<exclude>InterSCMProtocol.proto</exclude>
103104
<exclude>SCMUpdateProtocol.proto</exclude>
105+
<exclude>SCMRatisProtocol.proto</exclude>
104106
</excludes>
105107
<outputDirectory>target/generated-sources/proto-java-for-protobuf-${protobuf.version}</outputDirectory>
106108
<clearOutputDirectory>false</clearOutputDirectory>

hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMRatisRequest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
package org.apache.hadoop.hdds.scm.ha;
1919

2020
import com.google.common.base.Preconditions;
21-
import com.google.protobuf.InvalidProtocolBufferException;
22-
import com.google.protobuf.TextFormat;
2321
import java.util.ArrayList;
2422
import java.util.List;
2523
import org.apache.hadoop.hdds.protocol.proto.SCMRatisProtocol.Method;
@@ -29,6 +27,8 @@
2927
import org.apache.hadoop.hdds.scm.ha.io.CodecFactory;
3028
import org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto;
3129
import org.apache.ratis.protocol.Message;
30+
import org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
31+
import org.apache.ratis.thirdparty.com.google.protobuf.TextFormat;
3232
import org.slf4j.Logger;
3333
import org.slf4j.LoggerFactory;
3434

@@ -111,9 +111,8 @@ public Message encode() throws InvalidProtocolBufferException {
111111
}
112112
methodBuilder.addAllArgs(args);
113113
requestProtoBuilder.setMethod(methodBuilder.build());
114-
return Message.valueOf(
115-
org.apache.ratis.thirdparty.com.google.protobuf.ByteString.copyFrom(
116-
requestProtoBuilder.build().toByteArray()));
114+
final SCMRatisRequestProto requestProto = requestProtoBuilder.build();
115+
return Message.valueOf(requestProto.toByteString());
117116
}
118117

119118
/**
@@ -122,7 +121,7 @@ public Message encode() throws InvalidProtocolBufferException {
122121
public static SCMRatisRequest decode(Message message)
123122
throws InvalidProtocolBufferException {
124123
final SCMRatisRequestProto requestProto =
125-
SCMRatisRequestProto.parseFrom(message.getContent().toByteArray());
124+
SCMRatisRequestProto.parseFrom(message.getContent().asReadOnlyByteBuffer());
126125

127126
// proto2 required-equivalent checks
128127
if (!requestProto.hasType()) {
@@ -173,7 +172,7 @@ public static String smProtoToString(StateMachineLogEntryProto proto) {
173172
StringBuilder builder = new StringBuilder();
174173
try {
175174
builder.append(TextFormat.shortDebugString(
176-
SCMRatisRequestProto.parseFrom(proto.getLogData().toByteArray())));
175+
SCMRatisRequestProto.parseFrom(proto.getLogData().asReadOnlyByteBuffer())));
177176
} catch (Throwable ex) {
178177
LOG.error("smProtoToString failed", ex);
179178
builder.append("smProtoToString failed with");

hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMRatisResponse.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
package org.apache.hadoop.hdds.scm.ha;
1919

20-
import com.google.protobuf.InvalidProtocolBufferException;
2120
import org.apache.hadoop.hdds.protocol.proto.SCMRatisProtocol.SCMRatisResponseProto;
2221
import org.apache.hadoop.hdds.scm.ha.io.CodecFactory;
2322
import org.apache.ratis.protocol.Message;
2423
import org.apache.ratis.protocol.RaftClientReply;
2524
import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
25+
import org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
2626
import org.apache.ratis.thirdparty.com.google.protobuf.UnsafeByteOperations;
2727

2828
/**
@@ -92,7 +92,7 @@ public static SCMRatisResponse decode(RaftClientReply reply)
9292
return new SCMRatisResponse();
9393
}
9494

95-
final SCMRatisResponseProto responseProto = SCMRatisResponseProto.parseFrom(response.toByteArray());
95+
final SCMRatisResponseProto responseProto = SCMRatisResponseProto.parseFrom(response.asReadOnlyByteBuffer());
9696

9797
// proto2 required-equivalent checks
9898
if (!responseProto.hasType()) {

hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/BigIntegerCodec.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
package org.apache.hadoop.hdds.scm.ha.io;
1919

20-
import com.google.protobuf.ByteString;
21-
import com.google.protobuf.ProtoUtils;
2220
import java.math.BigInteger;
21+
import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
22+
import org.apache.ratis.thirdparty.com.google.protobuf.UnsafeByteOperations;
2323

2424
/**
2525
* Codec for type BigInteger.
@@ -29,7 +29,7 @@ public class BigIntegerCodec implements Codec {
2929
@Override
3030
public ByteString serialize(Object object) {
3131
// BigInteger returns a new byte[].
32-
return ProtoUtils.unsafeByteString(((BigInteger)object).toByteArray());
32+
return UnsafeByteOperations.unsafeWrap(((BigInteger) object).toByteArray());
3333
}
3434

3535
@Override

hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/BooleanCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package org.apache.hadoop.hdds.scm.ha.io;
1919

20-
import com.google.protobuf.ByteString;
20+
import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
2121

2222
/**
2323
* {@link Codec} for {@code Boolean} objects.

hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/ByteStringCodec.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,27 @@
1717

1818
package org.apache.hadoop.hdds.scm.ha.io;
1919

20-
import com.google.protobuf.ByteString;
21-
import com.google.protobuf.InvalidProtocolBufferException;
20+
import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
21+
import org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
22+
import org.apache.ratis.thirdparty.com.google.protobuf.UnsafeByteOperations;
2223

2324
/**
24-
* A dummy codec that serializes a ByteString object to ByteString.
25+
* {@link Codec} implementation for non-shaded
26+
* {@link com.google.protobuf.ByteString} objects.
2527
*/
2628
public class ByteStringCodec implements Codec {
2729

2830
@Override
2931
public ByteString serialize(Object object)
3032
throws InvalidProtocolBufferException {
31-
return (ByteString) object;
33+
return UnsafeByteOperations.unsafeWrap(
34+
((com.google.protobuf.ByteString) object).asReadOnlyByteBuffer());
3235
}
3336

3437
@Override
3538
public Object deserialize(Class<?> type, ByteString value)
3639
throws InvalidProtocolBufferException {
37-
return value;
40+
return com.google.protobuf.UnsafeByteOperations.
41+
unsafeWrap(value.asReadOnlyByteBuffer());
3842
}
3943
}

hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/Codec.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
package org.apache.hadoop.hdds.scm.ha.io;
1919

20-
import com.google.protobuf.ByteString;
21-
import com.google.protobuf.InvalidProtocolBufferException;
20+
import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
21+
import org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
2222

2323
/**
2424
* Codec interface to marshall/unmarshall data to/from {@link ByteString}.

hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/CodecFactory.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
package org.apache.hadoop.hdds.scm.ha.io;
1919

20-
import com.google.protobuf.ByteString;
21-
import com.google.protobuf.InvalidProtocolBufferException;
22-
import com.google.protobuf.Message;
2320
import com.google.protobuf.ProtocolMessageEnum;
2421
import java.math.BigInteger;
2522
import java.security.cert.X509Certificate;
@@ -29,6 +26,9 @@
2926
import java.util.Map;
3027
import org.apache.commons.lang3.ClassUtils;
3128
import org.apache.hadoop.hdds.security.symmetric.ManagedSecretKey;
29+
import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
30+
import org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
31+
import org.apache.ratis.thirdparty.com.google.protobuf.Message;
3232

3333
/**
3434
* Maps types to the corresponding {@link Codec} implementation.
@@ -38,7 +38,8 @@ public final class CodecFactory {
3838
private static Map<Class<?>, Codec> codecs = new HashMap<>();
3939

4040
static {
41-
codecs.put(Message.class, new GeneratedMessageCodec());
41+
codecs.put(com.google.protobuf.Message.class, new GeneratedMessageCodec());
42+
codecs.put(Message.class, new ScmGeneratedMessageCodec());
4243
codecs.put(ProtocolMessageEnum.class, new EnumCodec());
4344
codecs.put(List.class, new ListCodec());
4445
codecs.put(Integer.class, new IntegerCodec());
@@ -47,7 +48,8 @@ public final class CodecFactory {
4748
codecs.put(Boolean.class, new BooleanCodec());
4849
codecs.put(BigInteger.class, new BigIntegerCodec());
4950
codecs.put(X509Certificate.class, new X509CertificateCodec());
50-
codecs.put(ByteString.class, new ByteStringCodec());
51+
codecs.put(com.google.protobuf.ByteString.class, new ByteStringCodec());
52+
codecs.put(ByteString.class, new ScmByteStringCodec());
5153
codecs.put(ManagedSecretKey.class, new ManagedSecretKeyCodec());
5254
}
5355

hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/EnumCodec.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
package org.apache.hadoop.hdds.scm.ha.io;
1919

2020
import com.google.common.primitives.Ints;
21-
import com.google.protobuf.ByteString;
22-
import com.google.protobuf.InvalidProtocolBufferException;
23-
import com.google.protobuf.ProtoUtils;
2421
import com.google.protobuf.ProtocolMessageEnum;
2522
import java.lang.reflect.InvocationTargetException;
2623
import org.apache.hadoop.hdds.scm.ha.ReflectionUtil;
24+
import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
25+
import org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
26+
import org.apache.ratis.thirdparty.com.google.protobuf.UnsafeByteOperations;
2727

2828
/**
2929
* {@link Codec} for {@link ProtocolMessageEnum} objects.
@@ -34,8 +34,7 @@ public class EnumCodec implements Codec {
3434
public ByteString serialize(Object object)
3535
throws InvalidProtocolBufferException {
3636
// toByteArray returns a new array
37-
return ProtoUtils.unsafeByteString(Ints.toByteArray(
38-
((ProtocolMessageEnum) object).getNumber()));
37+
return UnsafeByteOperations.unsafeWrap(Ints.toByteArray(((ProtocolMessageEnum) object).getNumber()));
3938
}
4039

4140
@Override

hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/io/GeneratedMessageCodec.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,36 @@
1717

1818
package org.apache.hadoop.hdds.scm.ha.io;
1919

20-
import com.google.protobuf.ByteString;
21-
import com.google.protobuf.InvalidProtocolBufferException;
2220
import com.google.protobuf.Message;
2321
import java.lang.reflect.InvocationTargetException;
2422
import org.apache.hadoop.hdds.scm.ha.ReflectionUtil;
23+
import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
24+
import org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
25+
import org.apache.ratis.thirdparty.com.google.protobuf.UnsafeByteOperations;
2526

2627
/**
27-
* {@link Codec} for {@link Message} objects.
28+
* {@link Codec} implementation for non-shaded
29+
* {@link com.google.protobuf.Message} objects.
2830
*/
2931
public class GeneratedMessageCodec implements Codec {
3032

3133
@Override
32-
public ByteString serialize(Object object) {
33-
return ((Message)object).toByteString();
34+
public ByteString serialize(Object object)
35+
throws InvalidProtocolBufferException {
36+
return UnsafeByteOperations.unsafeWrap(
37+
((Message) object).toByteString().asReadOnlyByteBuffer());
3438
}
3539

3640
@Override
37-
public Message deserialize(Class<?> type, ByteString value)
41+
public Object deserialize(Class<?> type, ByteString value)
3842
throws InvalidProtocolBufferException {
3943
try {
40-
return (Message) ReflectionUtil.getMethod(type,
41-
"parseFrom", byte[].class)
44+
return ReflectionUtil.getMethod(type, "parseFrom", byte[].class)
4245
.invoke(null, (Object) value.toByteArray());
4346
} catch (NoSuchMethodException | IllegalAccessException
44-
| InvocationTargetException ex) {
47+
| InvocationTargetException ex) {
4548
ex.printStackTrace();
46-
throw new InvalidProtocolBufferException(
47-
"Message cannot be decoded: " + ex.getMessage());
49+
throw new InvalidProtocolBufferException("Message cannot be decoded: " + ex.getMessage());
4850
}
4951
}
5052
}

0 commit comments

Comments
 (0)