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;
}
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);
}
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);
}
Aggregations