|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.repositories.blobstore; |
| 10 | + |
| 11 | +import org.elasticsearch.common.bytes.BytesReference; |
| 12 | +import org.elasticsearch.common.settings.Settings; |
| 13 | +import org.elasticsearch.common.util.BigArrays; |
| 14 | +import org.elasticsearch.common.util.MockBigArrays; |
| 15 | +import org.elasticsearch.common.util.MockPageCacheRecycler; |
| 16 | +import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; |
| 17 | +import org.elasticsearch.test.ESTestCase; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.OutputStream; |
| 21 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 22 | +import java.util.concurrent.atomic.AtomicInteger; |
| 23 | +import java.util.concurrent.atomic.AtomicLong; |
| 24 | +import java.util.zip.CRC32; |
| 25 | +import java.util.zip.CheckedOutputStream; |
| 26 | + |
| 27 | +public class ChunkedBlobOutputStreamTests extends ESTestCase { |
| 28 | + |
| 29 | + private BigArrays bigArrays; |
| 30 | + |
| 31 | + @Override |
| 32 | + public void setUp() throws Exception { |
| 33 | + bigArrays = new MockBigArrays(new MockPageCacheRecycler(Settings.EMPTY), new NoneCircuitBreakerService()); |
| 34 | + super.setUp(); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void tearDown() throws Exception { |
| 39 | + super.tearDown(); |
| 40 | + } |
| 41 | + |
| 42 | + public void testSuccessfulChunkedWrite() throws IOException { |
| 43 | + final long chunkSize = randomLongBetween(10, 1024); |
| 44 | + final CRC32 checksumIn = new CRC32(); |
| 45 | + final CRC32 checksumOut = new CRC32(); |
| 46 | + final CheckedOutputStream out = new CheckedOutputStream(OutputStream.nullOutputStream(), checksumOut); |
| 47 | + final AtomicLong writtenBytesCounter = new AtomicLong(0L); |
| 48 | + final long bytesToWrite = randomLongBetween(chunkSize - 5, 1000 * chunkSize); |
| 49 | + long written = 0; |
| 50 | + try (ChunkedBlobOutputStream<Integer> stream = new ChunkedBlobOutputStream<>(bigArrays, chunkSize) { |
| 51 | + |
| 52 | + private final AtomicInteger partIdSupplier = new AtomicInteger(); |
| 53 | + |
| 54 | + @Override |
| 55 | + protected void flushBuffer() throws IOException { |
| 56 | + final BytesReference bytes = buffer.bytes(); |
| 57 | + bytes.writeTo(out); |
| 58 | + writtenBytesCounter.addAndGet(bytes.length()); |
| 59 | + finishPart(partIdSupplier.incrementAndGet()); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected void onCompletion() throws IOException { |
| 64 | + if (buffer.size() > 0) { |
| 65 | + flushBuffer(); |
| 66 | + } |
| 67 | + out.flush(); |
| 68 | + for (int i = 0; i < partIdSupplier.get(); i++) { |
| 69 | + assertEquals((long) i + 1, (long) parts.get(i)); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + protected void onFailure() { |
| 75 | + fail("not supposed to fail"); |
| 76 | + } |
| 77 | + }) { |
| 78 | + final byte[] buffer = new byte[randomInt(Math.toIntExact(2 * chunkSize)) + 1]; |
| 79 | + while (written < bytesToWrite) { |
| 80 | + if (randomBoolean()) { |
| 81 | + random().nextBytes(buffer); |
| 82 | + final int offset = randomInt(buffer.length - 2) + 1; |
| 83 | + final int length = Math.toIntExact(Math.min(bytesToWrite - written, buffer.length - offset)); |
| 84 | + stream.write(buffer, offset, length); |
| 85 | + checksumIn.update(buffer, offset, length); |
| 86 | + written += length; |
| 87 | + } else { |
| 88 | + int oneByte = randomByte(); |
| 89 | + stream.write(oneByte); |
| 90 | + checksumIn.update(oneByte); |
| 91 | + written++; |
| 92 | + } |
| 93 | + } |
| 94 | + stream.markSuccess(); |
| 95 | + } |
| 96 | + assertEquals(bytesToWrite, written); |
| 97 | + assertEquals(bytesToWrite, writtenBytesCounter.get()); |
| 98 | + assertEquals(checksumIn.getValue(), checksumOut.getValue()); |
| 99 | + } |
| 100 | + |
| 101 | + public void testExceptionDuringChunkedWrite() throws IOException { |
| 102 | + final long chunkSize = randomLongBetween(10, 1024); |
| 103 | + final AtomicLong writtenBytesCounter = new AtomicLong(0L); |
| 104 | + final long bytesToWrite = randomLongBetween(chunkSize - 5, 1000 * chunkSize); |
| 105 | + long written = 0; |
| 106 | + final AtomicBoolean onFailureCalled = new AtomicBoolean(false); |
| 107 | + try (ChunkedBlobOutputStream<Integer> stream = new ChunkedBlobOutputStream<>(bigArrays, chunkSize) { |
| 108 | + |
| 109 | + private final AtomicInteger partIdSupplier = new AtomicInteger(); |
| 110 | + |
| 111 | + @Override |
| 112 | + protected void flushBuffer() { |
| 113 | + writtenBytesCounter.addAndGet(buffer.size()); |
| 114 | + finishPart(partIdSupplier.incrementAndGet()); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + protected void onCompletion() { |
| 119 | + fail("supposed to fail"); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + protected void onFailure() { |
| 124 | + for (int i = 0; i < partIdSupplier.get(); i++) { |
| 125 | + assertEquals((long) i + 1, (long) parts.get(i)); |
| 126 | + } |
| 127 | + assertTrue(onFailureCalled.compareAndSet(false, true)); |
| 128 | + } |
| 129 | + }) { |
| 130 | + final byte[] buffer = new byte[randomInt(Math.toIntExact(2 * chunkSize)) + 1]; |
| 131 | + while (written < bytesToWrite) { |
| 132 | + if (rarely()) { |
| 133 | + break; |
| 134 | + } else if (randomBoolean()) { |
| 135 | + random().nextBytes(buffer); |
| 136 | + final int offset = randomInt(buffer.length - 2) + 1; |
| 137 | + final int length = Math.toIntExact(Math.min(bytesToWrite - written, buffer.length - offset)); |
| 138 | + stream.write(buffer, offset, length); |
| 139 | + written += length; |
| 140 | + } else { |
| 141 | + int oneByte = randomByte(); |
| 142 | + stream.write(oneByte); |
| 143 | + written++; |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + assertTrue(onFailureCalled.get()); |
| 148 | + } |
| 149 | +} |
0 commit comments