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 @@ -137,10 +137,8 @@ public static String getPEMEncodedString(X509Certificate certificate)
try {
return writePEMEncoded(certificate, new StringWriter()).toString();
} catch (IOException e) {
LOG.error("Error in encoding certificate." + certificate
.getSubjectDN().toString(), e);
throw new SCMSecurityException("PEM Encoding failed for certificate." +
certificate.getSubjectDN().toString(), e, PEM_ENCODE_FAILED);
throw new SCMSecurityException("Failed to getPEMEncodedString for certificate with subject "
+ certificate.getSubjectDN(), e, PEM_ENCODE_FAILED);
}
}

Expand All @@ -155,9 +153,13 @@ public static String getPEMEncodedString(X509Certificate certificate)
*/
public static X509Certificate getX509Certificate(String pemEncoded)
throws CertificateException {
return getX509Certificate(pemEncoded.getBytes(DEFAULT_CHARSET));
}

public static X509Certificate getX509Certificate(byte[] pemEncoded)
throws CertificateException {
// ByteArrayInputStream.close(), which is a noop, can be safely ignored.
final ByteArrayInputStream input = new ByteArrayInputStream(
pemEncoded.getBytes(DEFAULT_CHARSET));
final ByteArrayInputStream input = new ByteArrayInputStream(pemEncoded);
return readX509Certificate(input);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.hadoop.hdds.utils.db;

import jakarta.annotation.Nonnull;
import java.io.IOException;
import java.util.Objects;

/**
* Codec interface to serialize/deserialize objects to/from bytes.
Expand Down Expand Up @@ -90,14 +90,46 @@ default T fromCodecBuffer(@Nonnull CodecBuffer buffer) throws CodecException {
* Convert object to raw persisted format.
* @param object The original java object. Should not be null.
*/
byte[] toPersistedFormat(T object) throws IOException;
default byte[] toPersistedFormat(T object) throws CodecException {
Objects.requireNonNull(object, "object == null");
try {
return toPersistedFormatImpl(object);
} catch (Exception e) {
throw new CodecException("Failed to serialize " + object
+ " for " + object.getClass(), e);
}
}

/**
* The same as {@link #toPersistedFormat} except that this method throws {@link Exception}.
* A subclass must implement either {@link #toPersistedFormat} or this method.
*/
default byte[] toPersistedFormatImpl(T object) throws Exception {
throw new UnsupportedOperationException();
}

/**
* Convert object from raw persisted format.
*
* @param rawData Byte array from the key/value store. Should not be null.
*/
T fromPersistedFormat(byte[] rawData) throws IOException;
default T fromPersistedFormat(byte[] rawData) throws CodecException {
Objects.requireNonNull(rawData, "rawData == null");
try {
return fromPersistedFormatImpl(rawData);
} catch (Exception e) {
throw new CodecException("Failed to deserialize rawData (length=" + rawData.length
+ ") for " + getTypeClass(), e);
}
}

/**
* The same as {@link #fromPersistedFormat} except that this method throws {@link Exception}.
* A subclass must implement either {@link #fromPersistedFormat} or this method.
*/
default T fromPersistedFormatImpl(byte[] rawData) throws Exception {
throw new UnsupportedOperationException();
}

/**
* Copy the given object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.apache.hadoop.hdds.utils.db;

import jakarta.annotation.Nonnull;
import java.io.IOException;
import org.apache.ratis.util.JavaUtils;
import org.apache.ratis.util.function.CheckedFunction;

Expand Down Expand Up @@ -85,12 +84,12 @@ public final T fromCodecBuffer(@Nonnull CodecBuffer buffer) throws CodecExceptio
}

@Override
public final byte[] toPersistedFormat(T message) throws IOException {
public final byte[] toPersistedFormat(T message) throws CodecException {
return delegate.toPersistedFormat(backward.apply(message));
}

@Override
public final T fromPersistedFormat(byte[] bytes) throws IOException {
public final T fromPersistedFormat(byte[] bytes) throws CodecException {
return forward.apply(delegate.fromPersistedFormat(bytes));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public byte[] toPersistedFormat(M message) {
}

@Override
public M fromPersistedFormat(byte[] bytes)
public M fromPersistedFormatImpl(byte[] bytes)
throws InvalidProtocolBufferException {
return parser.parseFrom(bytes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public byte[] toPersistedFormat(M message) {
}

@Override
public M fromPersistedFormat(byte[] bytes)
public M fromPersistedFormatImpl(byte[] bytes)
throws InvalidProtocolBufferException {
return parser.parseFrom(bytes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.apache.hadoop.hdds.utils.db;

import jakarta.annotation.Nonnull;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -183,8 +182,8 @@ public String fromCodecBuffer(@Nonnull CodecBuffer buffer) {
}

@Override
public byte[] toPersistedFormat(String object) throws IOException {
return string2Bytes(object, IOException::new);
public byte[] toPersistedFormat(String object) throws CodecException {
return string2Bytes(object, CodecException::new);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.google.protobuf.InvalidProtocolBufferException;
Expand All @@ -31,11 +32,13 @@ public abstract class Proto2CodecTestBase<T> {
public abstract Codec<T> getCodec();

@Test
public void testInvalidProtocolBuffer() throws Exception {
InvalidProtocolBufferException exception =
assertThrows(InvalidProtocolBufferException.class,
public void testInvalidProtocolBuffer() {
final CodecException exception =
assertThrows(CodecException.class,
() -> getCodec().fromPersistedFormat("random".getBytes(UTF_8)));
assertThat(exception.getMessage())
final InvalidProtocolBufferException cause = assertInstanceOf(
InvalidProtocolBufferException.class, exception.getCause());
assertThat(cause.getMessage())
.contains("the input ended unexpectedly");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

package org.apache.hadoop.ozone.container.metadata;

import java.io.IOException;
import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos;
import org.apache.hadoop.hdds.utils.db.Codec;
import org.apache.hadoop.hdds.utils.db.CodecException;
import org.apache.hadoop.ozone.container.common.helpers.ChunkInfoList;
import org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException;

Expand Down Expand Up @@ -67,12 +67,12 @@ public byte[] toPersistedFormat(ChunkInfoList chunkList) {
}

@Override
public ChunkInfoList fromPersistedFormat(byte[] rawData) throws IOException {
public ChunkInfoList fromPersistedFormat(byte[] rawData) throws CodecException {
try {
return ChunkInfoList.getFromProtoBuf(
ContainerProtos.ChunkInfoList.parseFrom(rawData));
} catch (InvalidProtocolBufferException ex) {
throw new IOException("Invalid chunk information. " +
throw new CodecException("Invalid chunk information. " +
"This data may have been written using datanode " +
"schema version one, which did not save chunk information.", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

package org.apache.hadoop.ozone.container.metadata;

import java.io.IOException;
import org.apache.hadoop.hdds.utils.db.Codec;
import org.apache.hadoop.hdds.utils.db.CodecException;
import org.apache.hadoop.hdds.utils.db.LongCodec;
import org.apache.hadoop.hdds.utils.db.StringCodec;
import org.slf4j.Logger;
Expand Down Expand Up @@ -52,7 +52,7 @@ public Class<String> getTypeClass() {
}

@Override
public byte[] toPersistedFormat(String stringObject) throws IOException {
public byte[] toPersistedFormat(String stringObject) throws CodecException {
try {
// If the caller's string has no prefix, it should be stored as a long
// to be encoded as a long to be consistent with the schema one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.apache.hadoop.hdds.scm.metadata;

import java.io.IOException;
import java.math.BigInteger;
import org.apache.hadoop.hdds.utils.db.Codec;

Expand All @@ -42,12 +41,12 @@ public Class<BigInteger> getTypeClass() {
}

@Override
public byte[] toPersistedFormat(BigInteger object) throws IOException {
public byte[] toPersistedFormat(BigInteger object) {
return object.toByteArray();
}

@Override
public BigInteger fromPersistedFormat(byte[] rawData) throws IOException {
public BigInteger fromPersistedFormat(byte[] rawData) {
return new BigInteger(rawData);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

package org.apache.hadoop.hdds.scm.metadata;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.UUID;
import org.apache.hadoop.hdds.scm.pipeline.PipelineID;
import org.apache.hadoop.hdds.utils.db.Codec;
Expand All @@ -34,7 +32,7 @@ public Class<PipelineID> getTypeClass() {
}

@Override
public byte[] toPersistedFormat(PipelineID object) throws IOException {
public byte[] toPersistedFormat(PipelineID object) {
byte[] bytes = new byte[16];
System.arraycopy(
asByteArray(object.getId().getMostSignificantBits()), 0, bytes, 0, 8);
Expand All @@ -50,23 +48,18 @@ private byte[] asByteArray(long bits) {
}

@Override
public PipelineID fromPersistedFormat(byte[] rawData) throws IOException {
public PipelineID fromPersistedFormatImpl(byte[] rawData) {
long mostSiginificantBits = toLong(rawData, 0);
long leastSignificantBits = toLong(rawData, 8);

UUID id = new UUID(mostSiginificantBits, leastSignificantBits);
return PipelineID.valueOf(id);
}

private long toLong(byte[] arr, int startIdx) throws IOException {
private long toLong(byte[] arr, int startIdx) {
if (arr.length < startIdx + 8) {
throw new IOException("Key conversion error.",
new ArrayIndexOutOfBoundsException(
"Key does not have the least expected amount of bytes,"
+ "and does not contain a UUID. Key: "
+ Arrays.toString(arr)
)
);
throw new ArrayIndexOutOfBoundsException(
"Not enough bytes: length (=" + arr.length + ") - startIdx (=" + startIdx + ") < 8");
}
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.put(arr, startIdx, 8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.apache.hadoop.hdds.scm.metadata;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
Expand Down Expand Up @@ -48,24 +47,14 @@ public Class<X509Certificate> getTypeClass() {
}

@Override
public byte[] toPersistedFormat(X509Certificate object) throws IOException {
try {
return CertificateCodec.getPEMEncodedString(object)
.getBytes(StandardCharsets.UTF_8);
} catch (SCMSecurityException exp) {
throw new IOException(exp);
}
public byte[] toPersistedFormatImpl(X509Certificate object) throws SCMSecurityException {
return CertificateCodec.getPEMEncodedString(object)
.getBytes(StandardCharsets.UTF_8);
}

@Override
public X509Certificate fromPersistedFormat(byte[] rawData)
throws IOException {
try {
String s = new String(rawData, StandardCharsets.UTF_8);
return CertificateCodec.getX509Certificate(s);
} catch (CertificateException exp) {
throw new IOException(exp);
}
public X509Certificate fromPersistedFormatImpl(byte[] rawData) throws CertificateException {
return CertificateCodec.getX509Certificate(rawData);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,12 @@ public Class<SnapshotDiffJob> getTypeClass() {
}

@Override
public byte[] toPersistedFormat(SnapshotDiffJob object)
throws IOException {
public byte[] toPersistedFormatImpl(SnapshotDiffJob object) throws IOException {
return MAPPER.writeValueAsBytes(object);
}

@Override
public SnapshotDiffJob fromPersistedFormat(byte[] rawData)
throws IOException {
public SnapshotDiffJob fromPersistedFormatImpl(byte[] rawData) throws IOException {
return MAPPER.readValue(rawData, SnapshotDiffJob.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public OzoneTokenIdentifier fromUniqueSerializedKey(byte[] rawData)
return this;
}

public OMTokenProto toProtoBuf() throws IOException {
public OMTokenProto toProtoBuf() {
OMTokenProto.Builder builder = OMTokenProto.newBuilder()
.setMaxDate(getMaxDate())
.setType(getTokenType())
Expand Down
Loading