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 @@ -462,8 +462,6 @@ private void validateChunk(

ReadChunkResponseProto readChunkResponse = response.getReadChunk();
List<ByteString> byteStrings;
boolean isV0 = false;

if (readChunkResponse.hasData()) {
ByteString byteString = readChunkResponse.getData();
if (byteString.size() != reqChunkInfo.getLen()) {
Expand All @@ -475,7 +473,6 @@ private void validateChunk(
}
byteStrings = new ArrayList<>();
byteStrings.add(byteString);
isV0 = true;
} else {
byteStrings = readChunkResponse.getDataBuffers().getBuffersList();
long buffersLen = BufferUtils.getBuffersLen(byteStrings);
Expand All @@ -500,8 +497,7 @@ private void validateChunk(
chunkInfo.getOffset();
int bytesPerChecksum = checksumData.getBytesPerChecksum();
int startIndex = (int) (relativeOffset / bytesPerChecksum);
Checksum.verifyChecksum(byteStrings, checksumData, startIndex,
isV0);
Checksum.verifyChecksum(byteStrings, checksumData, startIndex);
}
}

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

package org.apache.hadoop.hdds;

import com.google.common.base.Preconditions;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.apache.ratis.thirdparty.io.netty.buffer.Unpooled;
import org.apache.ratis.util.Preconditions;

/**
* Simple utility class to collection string conversion methods.
Expand Down Expand Up @@ -54,14 +54,22 @@ public static String bytes2String(ByteBuffer bytes, Charset charset) {
}

public static String bytes2Hex(ByteBuffer buffer, int max) {
Preconditions.assertTrue(max > 0, () -> "max = " + max + " <= 0");
buffer = buffer.asReadOnlyBuffer();
final int remaining = buffer.remaining();
final int n = Math.min(max, remaining);
final StringBuilder builder = new StringBuilder(3 * n);
for (int i = 0; i < n; i++) {
builder.append(String.format("%02X ", buffer.get()));
final boolean overflow = max < remaining;
final int n = overflow ? max : remaining;
final StringBuilder builder = new StringBuilder(3 * n + (overflow ? 3 : 0));
if (n > 0) {
for (int i = 0; i < n; i++) {
builder.append(String.format("%02X ", buffer.get()));
}
builder.setLength(builder.length() - 1);
}
return builder + (remaining > max ? "..." : "");
if (overflow) {
builder.append("...");
}
return builder.toString();
}

public static String bytes2Hex(ByteBuffer buffer) {
Expand Down Expand Up @@ -89,9 +97,4 @@ public static String bytes2String(byte[] bytes) {
public static byte[] string2Bytes(String str) {
return str.getBytes(UTF8);
}

public static String appendIfNotPresent(String str, char c) {
Preconditions.checkNotNull(str, "Input string is null");
return str.isEmpty() || str.charAt(str.length() - 1) != c ? str + c : str;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.stream.Collectors;
import javax.net.ssl.TrustManager;
import org.apache.hadoop.hdds.HddsConfigKeys;
import org.apache.hadoop.hdds.StringUtils;
import org.apache.hadoop.hdds.conf.ConfigurationSource;
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
import org.apache.hadoop.hdds.protocol.DatanodeDetails;
Expand Down Expand Up @@ -415,8 +414,7 @@ public static void createRaftServerProperties(ConfigurationSource ozoneConf,

private static Map<String, String> getDatanodeRatisPrefixProps(
ConfigurationSource configuration) {
return configuration.getPropsMatchPrefixAndTrimPrefix(
StringUtils.appendIfNotPresent(HDDS_DATANODE_RATIS_PREFIX_KEY, '.'));
return configuration.getPropsMatchPrefixAndTrimPrefix(HDDS_DATANODE_RATIS_PREFIX_KEY + '.');
}

// For External gRPC client to server with gRPC TLS.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public ChecksumData computeChecksum(ChunkBuffer data, boolean useCache)
try {
function = Algorithm.valueOf(checksumType).newChecksumFunction();
} catch (Exception e) {
throw new OzoneChecksumException(checksumType);
throw new OzoneChecksumException("Failed to get the checksum function for " + checksumType, e);
}

final List<ByteString> checksumList;
Expand Down Expand Up @@ -270,37 +270,16 @@ protected static ByteString computeChecksum(ByteBuffer data,
}
}

/**
* Computes the ChecksumData for the input data and verifies that it
* matches with that of the input checksumData, starting from index
* startIndex.
* @param byteString input data
* @param checksumData checksumData to match with
* @param startIndex index of first checksum in checksumData to match with
* data's computed checksum.
* @throws OzoneChecksumException is thrown if checksums do not match
*/
public static boolean verifyChecksum(ByteString byteString,
ChecksumData checksumData, int startIndex) throws OzoneChecksumException {
final ByteBuffer buffer = byteString.asReadOnlyByteBuffer();
return verifyChecksum(buffer, checksumData, startIndex);
}

/**
* Computes the ChecksumData for the input data and verifies that it
* matches with that of the input checksumData.
* @param data input data
* @param checksumData checksumData to match with
* @throws OzoneChecksumException is thrown if checksums do not match
*/
public static boolean verifyChecksum(byte[] data, ChecksumData checksumData)
throws OzoneChecksumException {
return verifyChecksum(ByteBuffer.wrap(data), checksumData, 0);
}

private static boolean verifyChecksum(ByteBuffer data,
public static void verifyChecksum(ByteBuffer data,
ChecksumData checksumData, int startIndex) throws OzoneChecksumException {
return verifyChecksum(ChunkBuffer.wrap(data), checksumData, startIndex);
verifyChecksum(ChunkBuffer.wrap(data), checksumData, startIndex);
}

/**
Expand All @@ -312,19 +291,19 @@ private static boolean verifyChecksum(ByteBuffer data,
* data's computed checksum.
* @throws OzoneChecksumException is thrown if checksums do not match
*/
public static boolean verifyChecksum(ChunkBuffer data,
public static void verifyChecksum(ChunkBuffer data,
ChecksumData checksumData,
int startIndex) throws OzoneChecksumException {
ChecksumType checksumType = checksumData.getChecksumType();
if (checksumType == ChecksumType.NONE) {
// Checksum is set to NONE. No further verification is required.
return true;
return;
}

int bytesPerChecksum = checksumData.getBytesPerChecksum();
Checksum checksum = new Checksum(checksumType, bytesPerChecksum);
final ChecksumData computed = checksum.computeChecksum(data);
return checksumData.verifyChecksumDataMatches(computed, startIndex);
checksumData.verifyChecksumDataMatches(startIndex, computed);
}

/**
Expand All @@ -335,23 +314,21 @@ public static boolean verifyChecksum(ChunkBuffer data,
* @param checksumData checksumData to match with
* @param startIndex index of first checksum in checksumData to match with
* data's computed checksum.
* @param isSingleByteString if true, there is only one byteString in the
* input list and it should be processes
* accordingly
* @throws OzoneChecksumException is thrown if checksums do not match
*/
public static boolean verifyChecksum(List<ByteString> byteStrings,
ChecksumData checksumData, int startIndex, boolean isSingleByteString)
public static void verifyChecksum(List<ByteString> byteStrings, ChecksumData checksumData, int startIndex)
throws OzoneChecksumException {
ChecksumType checksumType = checksumData.getChecksumType();
if (checksumType == ChecksumType.NONE) {
// Checksum is set to NONE. No further verification is required.
return true;
return;
}

if (isSingleByteString) {
// The data is a single ByteString (old format).
return verifyChecksum(byteStrings.get(0), checksumData, startIndex);
if (byteStrings.size() == 1) {
// Optimization for a single ByteString.
// Note that the old format (V0) also only has a single ByteString.
verifyChecksum(byteStrings.get(0).asReadOnlyByteBuffer(), checksumData, startIndex);
return;
}

// The data is a list of ByteStrings. Each ByteString length should be
Expand All @@ -364,7 +341,7 @@ public static boolean verifyChecksum(List<ByteString> byteStrings,
Checksum checksum = new Checksum(checksumType, bytesPerChecksum);
final ChecksumData computed = checksum.computeChecksum(
ChunkBuffer.wrap(buffers));
return checksumData.verifyChecksumDataMatches(computed, startIndex);
checksumData.verifyChecksumDataMatches(startIndex, computed);
}

/**
Expand Down
Loading