Search in sources :

Example 1 with FixedByteArrayOutputStream

use of io.pravega.common.io.FixedByteArrayOutputStream in project pravega by pravega.

the class TestLogItem method getFullSerialization.

@SneakyThrows(IOException.class)
byte[] getFullSerialization() {
    byte[] result = new byte[Long.BYTES + Integer.BYTES + this.data.length];
    serialize(new FixedByteArrayOutputStream(result, 0, result.length));
    return result;
}
Also used : FixedByteArrayOutputStream(io.pravega.common.io.FixedByteArrayOutputStream) SneakyThrows(lombok.SneakyThrows)

Example 2 with FixedByteArrayOutputStream

use of io.pravega.common.io.FixedByteArrayOutputStream in project pravega by pravega.

the class SegmentAggregatorTests method testProgressiveReconcile.

/**
 * Tests the ability of the SegmentAggregator to reconcile operations as they are added to it (it detected a possible
 * data corruption, but it does not yet have all the operations it needs to reconcile - it needs to stay in reconciliation
 * mode until all disagreements have been resolved).
 */
@Test
public void testProgressiveReconcile() throws Exception {
    final WriterConfig config = DEFAULT_CONFIG;
    final int appendCount = 1000;
    final int failEvery = 3;
    final int maxFlushLoopCount = 5;
    @Cleanup TestContext context = new TestContext(config);
    context.storage.create(context.segmentAggregator.getMetadata().getName(), TIMEOUT).join();
    context.segmentAggregator.initialize(TIMEOUT).join();
    @Cleanup ByteArrayOutputStream writtenData = new ByteArrayOutputStream();
    ArrayList<StorageOperation> appendOperations = new ArrayList<>();
    ArrayList<InputStream> appendData = new ArrayList<>();
    for (int i = 0; i < appendCount; i++) {
        // Add another operation and record its length.
        StorageOperation appendOp = generateAppendAndUpdateMetadata(i, SEGMENT_ID, context);
        appendOperations.add(appendOp);
        byte[] ad = new byte[(int) appendOp.getLength()];
        getAppendData(appendOp, new FixedByteArrayOutputStream(ad, 0, ad.length), context);
        appendData.add(new ByteArrayInputStream(ad));
        writtenData.write(ad);
    }
    // Add each operation at at time, and every X appends, write ahead to storage (X-1 appends). This will force a
    // good mix of reconciles and normal appends.
    int errorCount = 0;
    int flushCount = 0;
    for (int i = 0; i < appendOperations.size(); i++) {
        StorageOperation op = appendOperations.get(i);
        context.segmentAggregator.add(op);
        if (i % failEvery == 0) {
            // Corrupt the storage by adding the next failEvery-1 ops to Storage.
            for (int j = i; j < i + failEvery - 1 && j < appendOperations.size(); j++) {
                long offset = context.storage.getStreamSegmentInfo(SEGMENT_NAME, TIMEOUT).join().getLength();
                context.storage.write(writeHandle(SEGMENT_NAME), offset, appendData.get(j), appendData.get(j).available(), TIMEOUT).join();
            }
        }
        // Force a flush by incrementing the time by a lot.
        context.increaseTime(config.getFlushThresholdTime().toMillis() + 1);
        int flushLoopCount = 0;
        while (context.segmentAggregator.mustFlush()) {
            try {
                flushCount++;
                context.segmentAggregator.flush(TIMEOUT).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
            } catch (Exception ex) {
                errorCount++;
                Assert.assertTrue("", Exceptions.unwrap(ex) instanceof BadOffsetException);
            }
            flushLoopCount++;
            AssertExtensions.assertLessThan("Too many flush-loops for a single attempt.", maxFlushLoopCount, flushLoopCount);
        }
    }
    AssertExtensions.assertGreaterThan("At least one flush was expected.", 0, flushCount);
    AssertExtensions.assertGreaterThan("At least one BadOffsetException was expected.", 0, errorCount);
    // Verify data.
    byte[] expectedData = writtenData.toByteArray();
    byte[] actualData = new byte[expectedData.length];
    long storageLength = context.storage.getStreamSegmentInfo(context.segmentAggregator.getMetadata().getName(), TIMEOUT).join().getLength();
    Assert.assertEquals("Unexpected number of bytes flushed to Storage.", expectedData.length, storageLength);
    context.storage.read(readHandle(context.segmentAggregator.getMetadata().getName()), 0, actualData, 0, actualData.length, TIMEOUT).join();
    Assert.assertArrayEquals("Unexpected data written to storage.", expectedData, actualData);
}
Also used : FixedByteArrayOutputStream(io.pravega.common.io.FixedByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FixedByteArrayOutputStream(io.pravega.common.io.FixedByteArrayOutputStream) Cleanup(lombok.Cleanup) StreamSegmentNotExistsException(io.pravega.segmentstore.contracts.StreamSegmentNotExistsException) BadOffsetException(io.pravega.segmentstore.contracts.BadOffsetException) IntentionalException(io.pravega.test.common.IntentionalException) IOException(java.io.IOException) DataCorruptionException(io.pravega.segmentstore.server.DataCorruptionException) ByteArrayInputStream(java.io.ByteArrayInputStream) StorageOperation(io.pravega.segmentstore.server.logs.operations.StorageOperation) BadOffsetException(io.pravega.segmentstore.contracts.BadOffsetException) Test(org.junit.Test)

Example 3 with FixedByteArrayOutputStream

use of io.pravega.common.io.FixedByteArrayOutputStream in project pravega by pravega.

the class RevisionDataOutputStreamTests method testRandomOutputFixed.

/**
 * Tests the RandomRevisionDataOutput class with an fixed-length RandomAccessOutputStream.
 */
@Test
public void testRandomOutputFixed() throws Exception {
    final int bufferSize = 1024 * 1024;
    @Cleanup val s = new FixedByteArrayOutputStream(new byte[bufferSize], 0, bufferSize);
    @Cleanup val impl = RevisionDataOutputStream.wrap(s);
    testImpl(impl, s::getData);
}
Also used : lombok.val(lombok.val) FixedByteArrayOutputStream(io.pravega.common.io.FixedByteArrayOutputStream) Arrays(java.util.Arrays) AssertExtensions(io.pravega.test.common.AssertExtensions) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Aggregations

FixedByteArrayOutputStream (io.pravega.common.io.FixedByteArrayOutputStream)3 Cleanup (lombok.Cleanup)2 Test (org.junit.Test)2 BadOffsetException (io.pravega.segmentstore.contracts.BadOffsetException)1 StreamSegmentNotExistsException (io.pravega.segmentstore.contracts.StreamSegmentNotExistsException)1 DataCorruptionException (io.pravega.segmentstore.server.DataCorruptionException)1 StorageOperation (io.pravega.segmentstore.server.logs.operations.StorageOperation)1 AssertExtensions (io.pravega.test.common.AssertExtensions)1 IntentionalException (io.pravega.test.common.IntentionalException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 SneakyThrows (lombok.SneakyThrows)1 lombok.val (lombok.val)1