|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iotdb.consensus.iot.snapshot; |
| 21 | + |
| 22 | +import org.junit.Assert; |
| 23 | +import org.junit.Rule; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.rules.TemporaryFolder; |
| 26 | + |
| 27 | +import java.io.ByteArrayOutputStream; |
| 28 | +import java.io.IOException; |
| 29 | +import java.nio.ByteBuffer; |
| 30 | +import java.nio.file.Files; |
| 31 | +import java.nio.file.Path; |
| 32 | +import java.util.Random; |
| 33 | + |
| 34 | +/** |
| 35 | + * Regression test for the snapshot-transmission read path. The sending side reuses a single, |
| 36 | + * caller-supplied {@link ByteBuffer} across every file of a snapshot (instead of allocating a fresh |
| 37 | + * 10MB buffer per file), so this verifies that (1) a single buffer reused across many readers still |
| 38 | + * reconstructs every file byte-for-byte, and (2) the buffer is genuinely shared rather than |
| 39 | + * re-allocated per file. |
| 40 | + */ |
| 41 | +public class SnapshotFragmentReaderTest { |
| 42 | + |
| 43 | + @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); |
| 44 | + |
| 45 | + @Test |
| 46 | + public void reusedBufferReadsEveryFileCorrectly() throws IOException { |
| 47 | + final Random random = new Random(42); |
| 48 | + // Deliberately use a tiny buffer so files span multiple fragments, and use file sizes that are |
| 49 | + // not multiples of the buffer size to exercise the partial-final-fragment path. |
| 50 | + final int bufferSize = 16; |
| 51 | + final int[] fileSizes = {0, 1, bufferSize, bufferSize + 1, 5 * bufferSize + 7}; |
| 52 | + |
| 53 | + byte[][] contents = new byte[fileSizes.length][]; |
| 54 | + Path[] paths = new Path[fileSizes.length]; |
| 55 | + for (int i = 0; i < fileSizes.length; i++) { |
| 56 | + contents[i] = new byte[fileSizes[i]]; |
| 57 | + random.nextBytes(contents[i]); |
| 58 | + paths[i] = temporaryFolder.newFile("file-" + i).toPath(); |
| 59 | + Files.write(paths[i], contents[i]); |
| 60 | + } |
| 61 | + |
| 62 | + final ByteBuffer sharedBuffer = ByteBuffer.allocate(bufferSize); |
| 63 | + ByteBuffer firstReusedChunk = null; |
| 64 | + |
| 65 | + for (int i = 0; i < paths.length; i++) { |
| 66 | + SnapshotFragmentReader reader = new SnapshotFragmentReader("snap", paths[i], sharedBuffer); |
| 67 | + try { |
| 68 | + ByteArrayOutputStream reconstructed = new ByteArrayOutputStream(); |
| 69 | + while (reader.hasNext()) { |
| 70 | + SnapshotFragment fragment = reader.next(); |
| 71 | + // Every fragment must be backed by the one shared buffer, proving it is reused and not |
| 72 | + // re-allocated per file. |
| 73 | + Assert.assertSame(sharedBuffer, fragment.getFileChunk()); |
| 74 | + if (firstReusedChunk == null) { |
| 75 | + firstReusedChunk = fragment.getFileChunk(); |
| 76 | + } else { |
| 77 | + Assert.assertSame(firstReusedChunk, fragment.getFileChunk()); |
| 78 | + } |
| 79 | + |
| 80 | + // Drain the fragment immediately, mirroring how the sender serializes each fragment |
| 81 | + // synchronously before the next read overwrites the shared buffer. |
| 82 | + ByteBuffer chunk = fragment.getFileChunk(); |
| 83 | + byte[] bytes = new byte[chunk.remaining()]; |
| 84 | + chunk.get(bytes); |
| 85 | + reconstructed.write(bytes); |
| 86 | + } |
| 87 | + Assert.assertArrayEquals( |
| 88 | + "File " + i + " was not reconstructed correctly", |
| 89 | + contents[i], |
| 90 | + reconstructed.toByteArray()); |
| 91 | + Assert.assertEquals(contents[i].length, reader.getTotalReadSize()); |
| 92 | + } finally { |
| 93 | + reader.close(); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments