Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.apache.hadoop.hdds.protocol.proto.SCMRatisProtocol.MethodArgument;
import org.apache.hadoop.hdds.protocol.proto.SCMRatisProtocol.RequestType;
import org.apache.hadoop.hdds.protocol.proto.SCMRatisProtocol.SCMRatisRequestProto;
import org.apache.hadoop.hdds.scm.ha.io.CodecFactory;
import org.apache.hadoop.hdds.scm.ha.io.ScmCodecFactory;
import org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto;
import org.apache.ratis.protocol.Message;
import org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
Expand Down Expand Up @@ -105,7 +105,7 @@ public Message encode() throws InvalidProtocolBufferException {
// This is done to avoid MethodNotFoundException in case if argument is
// subclass type, where as method is defined with super class type.
argBuilder.setType(parameterTypes[paramCounter++].getName());
argBuilder.setValue(CodecFactory.getCodec(argument.getClass())
argBuilder.setValue(ScmCodecFactory.getCodec(argument.getClass())
.serialize(argument));
args.add(argBuilder.build());
}
Expand Down Expand Up @@ -152,7 +152,7 @@ public static SCMRatisRequest decode(Message message)
try {
final Class<?> clazz = ReflectionUtil.getClass(argument.getType());
parameterTypes[paramCounter++] = clazz;
args.add(CodecFactory.getCodec(clazz)
args.add(ScmCodecFactory.getCodec(clazz)
.deserialize(clazz, argument.getValue()));
} catch (ClassNotFoundException ex) {
throw new InvalidProtocolBufferException(argument.getType() +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.hadoop.hdds.scm.ha;

import org.apache.hadoop.hdds.protocol.proto.SCMRatisProtocol.SCMRatisResponseProto;
import org.apache.hadoop.hdds.scm.ha.io.CodecFactory;
import org.apache.hadoop.hdds.scm.ha.io.ScmCodecFactory;
import org.apache.ratis.protocol.Message;
import org.apache.ratis.protocol.RaftClientReply;
import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
Expand Down Expand Up @@ -75,7 +75,7 @@ public static Message encode(final Object result)
final Class<?> type = result.getClass();
final SCMRatisResponseProto response = SCMRatisResponseProto.newBuilder()
.setType(type.getName())
.setValue(CodecFactory.getCodec(type).serialize(result))
.setValue(ScmCodecFactory.getCodec(type).serialize(result))
.build();
return Message.valueOf(UnsafeByteOperations.unsafeWrap(response.toByteString().asReadOnlyByteBuffer()));
}
Expand Down Expand Up @@ -104,7 +104,7 @@ public static SCMRatisResponse decode(RaftClientReply reply)

try {
final Class<?> type = ReflectionUtil.getClass(responseProto.getType());
return new SCMRatisResponse(CodecFactory.getCodec(type)
return new SCMRatisResponse(ScmCodecFactory.getCodec(type)
.deserialize(type, responseProto.getValue()));
} catch (ClassNotFoundException e) {
throw new InvalidProtocolBufferException(responseProto.getType() +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/**
* Codec for type BigInteger.
*/
public class BigIntegerCodec implements Codec {
public class ScmBigIntegerCodec implements ScmCodec {

@Override
public ByteString serialize(Object object) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;

/**
* {@link Codec} for {@code Boolean} objects.
* {@link ScmCodec} for {@code Boolean} objects.
*/
public class BooleanCodec implements Codec {
public class ScmBooleanCodec implements ScmCodec {
static final ByteString TRUE = ByteString.copyFromUtf8(Boolean.TRUE.toString());
static final ByteString FALSE = ByteString.copyFromUtf8(Boolean.FALSE.toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
* A dummy codec that serializes a ByteString object to ByteString.
*/
public class ScmByteStringCodec implements Codec {
public class ScmByteStringCodec implements ScmCodec {

@Override
public ByteString serialize(Object object)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
* Codec interface to marshall/unmarshall data to/from {@link ByteString}.
*/
public interface Codec {
public interface ScmCodec {

ByteString serialize(Object object) throws InvalidProtocolBufferException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,31 @@
import org.apache.ratis.thirdparty.com.google.protobuf.Message;

/**
* Maps types to the corresponding {@link Codec} implementation.
* Maps types to the corresponding {@link ScmCodec} implementation.
*/
public final class CodecFactory {
public final class ScmCodecFactory {

private static Map<Class<?>, Codec> codecs = new HashMap<>();
private static Map<Class<?>, ScmCodec> codecs = new HashMap<>();

static {
codecs.put(com.google.protobuf.Message.class, new GeneratedMessageCodec());
codecs.put(com.google.protobuf.Message.class, new ScmNonShadedGeneratedMessageCodec());
codecs.put(Message.class, new ScmGeneratedMessageCodec());
codecs.put(ProtocolMessageEnum.class, new EnumCodec());
codecs.put(List.class, new ListCodec());
codecs.put(Integer.class, new IntegerCodec());
codecs.put(Long.class, new LongCodec());
codecs.put(String.class, new StringCodec());
codecs.put(Boolean.class, new BooleanCodec());
codecs.put(BigInteger.class, new BigIntegerCodec());
codecs.put(X509Certificate.class, new X509CertificateCodec());
codecs.put(com.google.protobuf.ByteString.class, new ByteStringCodec());
codecs.put(ProtocolMessageEnum.class, new ScmEnumCodec());
codecs.put(List.class, new ScmListCodec());
codecs.put(Integer.class, new ScmIntegerCodec());
codecs.put(Long.class, new ScmLongCodec());
codecs.put(String.class, new ScmStringCodec());
codecs.put(Boolean.class, new ScmBooleanCodec());
codecs.put(BigInteger.class, new ScmBigIntegerCodec());
codecs.put(X509Certificate.class, new ScmX509CertificateCodec());
codecs.put(com.google.protobuf.ByteString.class, new ScmNonShadedByteStringCodec());
codecs.put(ByteString.class, new ScmByteStringCodec());
codecs.put(ManagedSecretKey.class, new ManagedSecretKeyCodec());
codecs.put(ManagedSecretKey.class, new ScmManagedSecretKeyCodec());
}

private CodecFactory() { }
private ScmCodecFactory() { }

public static Codec getCodec(Class<?> type)
public static ScmCodec getCodec(Class<?> type)
throws InvalidProtocolBufferException {
final List<Class<?>> classes = new ArrayList<>();
classes.add(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import org.apache.ratis.thirdparty.com.google.protobuf.UnsafeByteOperations;

/**
* {@link Codec} for {@link ProtocolMessageEnum} objects.
* {@link ScmCodec} for {@link ProtocolMessageEnum} objects.
*/
public class EnumCodec implements Codec {
public class ScmEnumCodec implements ScmCodec {

@Override
public ByteString serialize(Object object)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import org.apache.ratis.thirdparty.com.google.protobuf.Message;

/**
* {@link Codec} for {@link Message} objects.
* {@link ScmCodec} for {@link Message} objects.
*/
public class ScmGeneratedMessageCodec implements Codec {
public class ScmGeneratedMessageCodec implements ScmCodec {

@Override
public ByteString serialize(Object object) throws InvalidProtocolBufferException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* Encodes/decodes an integer to a byte string.
*/
public class IntegerCodec implements Codec {
public class ScmIntegerCodec implements ScmCodec {
@Override
public ByteString serialize(Object object)
throws InvalidProtocolBufferException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException;

/**
* {@link Codec} for {@link List} objects.
* {@link ScmCodec} for {@link List} objects.
*/
public class ListCodec implements Codec {
public class ScmListCodec implements ScmCodec {

@Override
public ByteString serialize(Object object)
Expand All @@ -39,7 +39,7 @@ public ByteString serialize(Object object)
Class<?> type = values.get(0).getClass();
listArgs.setType(type.getName());
for (Object value : values) {
listArgs.addValue(CodecFactory.getCodec(type).serialize(value));
listArgs.addValue(ScmCodecFactory.getCodec(type).serialize(value));
}
} else {
listArgs.setType(Object.class.getName());
Expand Down Expand Up @@ -67,7 +67,7 @@ public Object deserialize(Class<?> type, ByteString value)

final Class<?> dataType = ReflectionUtil.getClass(listArgs.getType());
for (ByteString element : listArgs.getValueList()) {
result.add(CodecFactory.getCodec(dataType)
result.add(ScmCodecFactory.getCodec(dataType)
.deserialize(dataType, element));
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import org.apache.ratis.thirdparty.com.google.protobuf.UnsafeByteOperations;

/**
* {@link Codec} for {@code Long} objects.
* {@link ScmCodec} for {@code Long} objects.
*/
public class LongCodec implements Codec {
public class ScmLongCodec implements ScmCodec {

@Override
public ByteString serialize(Object object)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/**
* A codec for {@link ManagedSecretKey} objects.
*/
public class ManagedSecretKeyCodec implements Codec {
public class ScmManagedSecretKeyCodec implements ScmCodec {
@Override
public ByteString serialize(Object object)
throws InvalidProtocolBufferException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import org.apache.ratis.thirdparty.com.google.protobuf.UnsafeByteOperations;

/**
* {@link Codec} implementation for non-shaded
* {@link ScmCodec} implementation for non-shaded
* {@link com.google.protobuf.ByteString} objects.
*/
public class ByteStringCodec implements Codec {
public class ScmNonShadedByteStringCodec implements ScmCodec {

@Override
public ByteString serialize(Object object)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import org.apache.ratis.thirdparty.com.google.protobuf.UnsafeByteOperations;

/**
* {@link Codec} implementation for non-shaded
* {@link ScmCodec} implementation for non-shaded
* {@link com.google.protobuf.Message} objects.
*/
public class GeneratedMessageCodec implements Codec {
public class ScmNonShadedGeneratedMessageCodec implements ScmCodec {

@Override
public ByteString serialize(Object object)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import org.apache.ratis.thirdparty.com.google.protobuf.UnsafeByteOperations;

/**
* {@link Codec} for {@code String} objects.
* {@link ScmCodec} for {@code String} objects.
*/
public class StringCodec implements Codec {
public class ScmStringCodec implements ScmCodec {
@Override
public ByteString serialize(Object object) {
// getBytes returns a new array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/**
* Codec for type X509Certificate.
*/
public class X509CertificateCodec implements Codec {
public class ScmX509CertificateCodec implements ScmCodec {
@Override
public ByteString serialize(Object object)
throws InvalidProtocolBufferException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.List;
import org.apache.hadoop.hdds.protocol.proto.HddsProtos;
import org.apache.hadoop.hdds.protocol.proto.SCMRatisProtocol;
import org.apache.hadoop.hdds.scm.ha.io.ListCodec;
import org.apache.hadoop.hdds.scm.ha.io.ScmListCodec;
import org.apache.hadoop.hdds.scm.pipeline.PipelineID;
import org.apache.ratis.protocol.Message;
import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
Expand Down Expand Up @@ -216,7 +216,7 @@ public void testListDecodeMissingTypeShouldFail() throws Exception {
.addValue(ByteString.copyFromUtf8("x"))
.build();

ListCodec codec = new ListCodec();
ScmListCodec codec = new ScmListCodec();

InvalidProtocolBufferException ex = assertThrows(
InvalidProtocolBufferException.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
/**
* Class to test BigIntegerCodec serialize and deserialize.
*/
public class TestBigIntegerCodec {
public class TestScmBigIntegerCodec {

@Test
public void testCodec() {
BigIntegerCodec bigIntegerCodec = new BigIntegerCodec();
ScmBigIntegerCodec scmBigIntegerCodec = new ScmBigIntegerCodec();

BigInteger bigInteger = BigInteger.valueOf(100);
ByteString byteString = bigIntegerCodec.serialize(bigInteger);
ByteString byteString = scmBigIntegerCodec.serialize(bigInteger);

BigInteger actual =
(BigInteger) bigIntegerCodec.deserialize(BigInteger.class, byteString);
(BigInteger) scmBigIntegerCodec.deserialize(BigInteger.class, byteString);
assertEquals(bigInteger, actual);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
/**
* Class to test X509CertificateCodec serialize and deserialize.
*/
public class TestX509CertificateCodec {
public class TestScmX509CertificateCodec {

@BeforeAll
public static void initSecurityProvider() {
Expand All @@ -49,11 +49,11 @@ public void codec() throws Exception {
KeyStoreTestUtil.generateCertificate("CN=Test", keyPair, 30,
"SHA256withRSA");

X509CertificateCodec x509CertificateCodec = new X509CertificateCodec();
ByteString byteString = x509CertificateCodec.serialize(x509Certificate);
ScmX509CertificateCodec scmX509CertificateCodec = new ScmX509CertificateCodec();
ByteString byteString = scmX509CertificateCodec.serialize(x509Certificate);

X509Certificate actual = (X509Certificate)
x509CertificateCodec.deserialize(X509Certificate.class, byteString);
scmX509CertificateCodec.deserialize(X509Certificate.class, byteString);

assertEquals(x509Certificate, actual);

Expand All @@ -62,10 +62,10 @@ public void codec() throws Exception {
@Test
public void testCodecError() {

X509CertificateCodec x509CertificateCodec = new X509CertificateCodec();
ScmX509CertificateCodec scmX509CertificateCodec = new ScmX509CertificateCodec();
final ByteString byteString = UnsafeByteOperations.unsafeWrap("dummy".getBytes(UTF_8));

assertThrows(InvalidProtocolBufferException.class, () ->
x509CertificateCodec.deserialize(X509Certificate.class, byteString));
scmX509CertificateCodec.deserialize(X509Certificate.class, byteString));
}
}