From f1254338d0a231808369aacc4c3d32ae6268ac45 Mon Sep 17 00:00:00 2001 From: Sooraj Sinha Date: Fri, 7 Jun 2024 20:40:07 +0530 Subject: [PATCH] Refactor checksum logic Signed-off-by: Sooraj Sinha --- .../transfer/BlobStoreTransferService.java | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/server/src/main/java/org/opensearch/index/translog/transfer/BlobStoreTransferService.java b/server/src/main/java/org/opensearch/index/translog/transfer/BlobStoreTransferService.java index 44541a37a5402..d55abb40dec48 100644 --- a/server/src/main/java/org/opensearch/index/translog/transfer/BlobStoreTransferService.java +++ b/server/src/main/java/org/opensearch/index/translog/transfer/BlobStoreTransferService.java @@ -57,6 +57,7 @@ public class BlobStoreTransferService implements TransferService { private final BlobStore blobStore; private final ThreadPool threadPool; + private static final int CHECKSUM_BYTES_LENGTH = 8; private static final Logger logger = LogManager.getLogger(BlobStoreTransferService.class); public BlobStoreTransferService(BlobStore blobStore, ThreadPool threadPool) { @@ -131,18 +132,7 @@ public void uploadBlob( final String resourceDescription = "BlobStoreTransferService.uploadBlob(blob=\"" + fileName + "\")"; byte[] bytes = inputStream.readAllBytes(); try (IndexInput input = new ByteArrayIndexInput(resourceDescription, bytes)) { - long expectedChecksum; - try { - expectedChecksum = checksumOfChecksum(input.clone(), 8); - } catch (Exception e) { - throw new ChecksumCombinationException( - "Potentially corrupted file: Checksum combination failed while combining stored checksum " - + "and calculated checksum of stored checksum", - resourceDescription, - e - ); - } - + long expectedChecksum = computeChecksum(input, resourceDescription); uploadBlobAsyncInternal( fileName, fileName, @@ -345,4 +335,19 @@ public void listAllInSortedOrderAsync( threadPool.executor(threadpoolName).execute(() -> { listAllInSortedOrder(path, filenamePrefix, limit, listener); }); } + private static long computeChecksum(IndexInput indexInput, String resourceDescription) throws ChecksumCombinationException { + long expectedChecksum; + try { + expectedChecksum = checksumOfChecksum(indexInput.clone(), CHECKSUM_BYTES_LENGTH); + } catch (Exception e) { + throw new ChecksumCombinationException( + "Potentially corrupted file: Checksum combination failed while combining stored checksum " + + "and calculated checksum of stored checksum", + resourceDescription, + e + ); + } + return expectedChecksum; + } + }