chungen0126 commented on code in PR #6613:
URL: https://github.com/apache/ozone/pull/6613#discussion_r1804289620
##########
hadoop-hdds/client/dev-support/findbugsExcludeFile.xml:
##########
@@ -32,4 +32,8 @@
<Class
name="org.apache.hadoop.hdds.scm.storage.TestBlockInputStream"></Class>
<Bug pattern="RR_NOT_CHECKED" />
</Match>
+ <Match>
+ <Class
name="org.apache.hadoop.hdds.scm.storage.TestStreamBlockInput"></Class>
+ <Bug pattern="RR_NOT_CHECKED" />
+ </Match>
Review Comment:
done
##########
hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestStreamBlockInput.java:
##########
@@ -0,0 +1,259 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hdds.scm.storage;
+
+import java.io.EOFException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Function;
+
+import com.google.common.primitives.Bytes;
+import org.apache.hadoop.hdds.client.BlockID;
+import org.apache.hadoop.hdds.client.ContainerBlockID;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import
org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ChecksumType;
+import
org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ChunkInfo;
+import org.apache.hadoop.hdds.scm.OzoneClientConfig;
+import org.apache.hadoop.hdds.scm.pipeline.MockPipeline;
+import org.apache.hadoop.hdds.scm.pipeline.Pipeline;
+import org.apache.hadoop.ozone.common.Checksum;
+
+import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+
+/**
+ * Tests for {@link TestStreamBlockInput}'s functionality.
+ */
+public class TestStreamBlockInput {
+ private int blockSize;
+ private static final int CHUNK_SIZE = 100;
+ private static final int BYTES_PER_CHECKSUM = 20;
+ private static final Random RANDOM = new Random();
+ private static final AtomicLong CONTAINER_ID = new AtomicLong();
+ private DummyStreamBlockInput blockStream;
+ private byte[] blockData;
+ private List<ChunkInfo> chunks;
+ private Map<String, byte[]> chunkDataMap;
+ private Checksum checksum;
+ private Function<BlockID, BlockLocationInfo> refreshFunction;
+ private BlockID blockID;
+ private static final String CHUNK_NAME = "chunk-";
+ private OzoneConfiguration conf = new OzoneConfiguration();
+
+
+ @BeforeEach
+ public void setup() throws Exception {
+ OzoneClientConfig clientConfig = conf.getObject(OzoneClientConfig.class);
+ clientConfig.setStreamReadBlock(true);
+ refreshFunction = mock(Function.class);
+ blockID = new BlockID(new ContainerBlockID(1, 1));
+ checksum = new Checksum(ChecksumType.CRC32, BYTES_PER_CHECKSUM);
+ createChunkList(5);
+
+ Pipeline pipeline = MockPipeline.createSingleNodePipeline();
+ blockStream = new DummyStreamBlockInput(blockID, blockSize, pipeline,
+ null, null, refreshFunction, clientConfig, chunks, chunkDataMap);
+ }
+
+ /**
+ * Create a mock list of chunks. The first n-1 chunks of length CHUNK_SIZE
+ * and the last chunk with length CHUNK_SIZE/2.
+ */
+ private void createChunkList(int numChunks)
+ throws Exception {
+
+ chunks = new ArrayList<>(numChunks);
+ chunkDataMap = new HashMap<>();
+ blockData = new byte[0];
+ int i, chunkLen;
+ byte[] byteData;
+ String chunkName;
+
+ for (i = 0; i < numChunks; i++) {
+ chunkName = CHUNK_NAME + i;
+ chunkLen = CHUNK_SIZE;
+ if (i == numChunks - 1) {
+ chunkLen = CHUNK_SIZE / 2;
+ }
+ byteData = generateRandomData(chunkLen);
+ ChunkInfo chunkInfo = ChunkInfo.newBuilder()
+ .setChunkName(chunkName)
+ .setOffset(0)
+ .setLen(chunkLen)
+ .setChecksumData(checksum.computeChecksum(
+ byteData, 0, chunkLen).getProtoBufMessage())
+ .build();
+
+ chunkDataMap.put(chunkName, byteData);
+ chunks.add(chunkInfo);
+
+ blockSize += chunkLen;
+ blockData = Bytes.concat(blockData, byteData);
+ }
+ }
+
+ static byte[] generateRandomData(int length) {
+ byte[] bytes = new byte[length];
+ RANDOM.nextBytes(bytes);
+ return bytes;
+ }
+
+ /**
+ * Match readData with the chunkData byte-wise.
+ * @param readData Data read through ChunkInputStream
+ * @param inputDataStartIndex first index (inclusive) in chunkData to compare
+ * with read data
+ * @param length the number of bytes of data to match starting from
+ * inputDataStartIndex
+ */
+ private void matchWithInputData(byte[] readData, int inputDataStartIndex,
+ int length) {
+ for (int i = inputDataStartIndex; i < inputDataStartIndex + length; i++) {
+ assertEquals(blockData[i], readData[i - inputDataStartIndex], "i: " + i);
+ }
+ }
+
+ private void matchWithInputData(List<ByteString> byteStrings,
+ int inputDataStartIndex, int length) {
+ int offset = inputDataStartIndex;
+ int totalBufferLen = 0;
+ for (ByteString byteString : byteStrings) {
+ int bufferLen = byteString.size();
+ matchWithInputData(byteString.toByteArray(), offset, bufferLen);
+ offset += bufferLen;
+ totalBufferLen += bufferLen;
+ }
+ assertEquals(length, totalBufferLen);
+ }
+
+ /**
+ * Seek to a position and verify through getPos().
+ */
+ private void seekAndVerify(int pos) throws Exception {
+ blockStream.seek(pos);
+ assertEquals(pos, blockStream.getPos(),
+ "Current position of buffer does not match with the sought position");
+ }
+
+ @Test
+ public void testFullChunkRead() throws Exception {
+ byte[] b = new byte[blockSize];
+ blockStream.read(b, 0, blockSize);
+ matchWithInputData(b, 0, blockSize);
+ }
+
+ @Test
+ public void testPartialChunkRead() throws Exception {
+ int len = blockSize / 2;
+ byte[] b = new byte[len];
+
+ blockStream.read(b, 0, len);
Review Comment:
done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]