use of io.pravega.segmentstore.contracts.ReadResultEntryType in project pravega by pravega.
the class ReadTest method testReadDirectlyFromStore.
@Test(timeout = 10000)
public void testReadDirectlyFromStore() throws Exception {
String segmentName = "testReadFromStore";
final int entries = 10;
final byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
UUID clientId = UUID.randomUUID();
StreamSegmentStore segmentStore = SERVICE_BUILDER.createStreamSegmentService();
fillStoreForSegment(segmentName, data, entries, segmentStore);
@Cleanup ReadResult result = segmentStore.read(segmentName, 0, entries * data.length, Duration.ZERO).get();
int index = 0;
while (result.hasNext()) {
ReadResultEntry entry = result.next();
ReadResultEntryType type = entry.getType();
assertTrue(type == ReadResultEntryType.Cache || type == ReadResultEntryType.Future);
// Each ReadResultEntryContents may be of an arbitrary length - we should make no assumptions.
// Also put a timeout when fetching the response in case we get back a Future read and it never completes.
BufferView contents = entry.getContent().get(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
@Cleanup InputStream contentStream = contents.getReader();
byte next;
while ((next = (byte) contentStream.read()) != -1) {
byte expected = data[index % data.length];
assertEquals(expected, next);
index++;
}
}
assertEquals(entries * data.length, index);
}
use of io.pravega.segmentstore.contracts.ReadResultEntryType in project pravega by pravega.
the class ContainerReadIndexTests method testStorageFailedCacheInsert.
/**
* Tests the ability to handle Cache/Index Update failures post a successful Storage Read.
*/
@Test
public void testStorageFailedCacheInsert() throws Exception {
final int segmentLength = 1024;
// Create a segment and write some data in Storage for it.
@Cleanup TestContext context = new TestContext();
ArrayList<Long> segmentIds = createSegments(context);
createSegmentsInStorage(context);
val testSegmentId = segmentIds.get(0);
UpdateableSegmentMetadata sm = context.metadata.getStreamSegmentMetadata(testSegmentId);
sm.setStorageLength(segmentLength);
sm.setLength(segmentLength);
context.storage.openWrite(sm.getName()).thenCompose(handle -> context.storage.write(handle, 0, new ByteArrayInputStream(new byte[segmentLength]), segmentLength, TIMEOUT)).join();
// Keep track of inserted/deleted calls to the Cache, and "fail" the insert call.
val inserted = new ReusableLatch();
val insertedAddress = new AtomicInteger(CacheStorage.NO_ADDRESS);
val deletedAddress = new AtomicInteger(Integer.MAX_VALUE);
context.cacheStorage.insertCallback = address -> {
// Immediately delete this data (prevent leaks).
context.cacheStorage.delete(address);
Assert.assertTrue(insertedAddress.compareAndSet(CacheStorage.NO_ADDRESS, address));
inserted.release();
throw new IntentionalException();
};
context.cacheStorage.deleteCallback = deletedAddress::set;
// Trigger a read. The first read call will be served with data directly from Storage, so we expect it to be successful.
@Cleanup ReadResult readResult = context.readIndex.read(testSegmentId, 0, segmentLength, TIMEOUT);
ReadResultEntry entry = readResult.next();
Assert.assertEquals("Unexpected ReadResultEntryType.", ReadResultEntryType.Storage, entry.getType());
entry.requestContent(TIMEOUT);
// This should complete without issues.
entry.getContent().get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
// Verify that the cache insert attempt has been made
inserted.await();
Assert.assertNotEquals("Expected an insert attempt to have been made.", CacheStorage.NO_ADDRESS, insertedAddress.get());
AssertExtensions.assertEventuallyEquals(CacheStorage.NO_ADDRESS, deletedAddress::get, TIMEOUT.toMillis());
}
Aggregations