use of io.pravega.client.segment.impl.Segment in project pravega by pravega.
the class SynchronizerTest method testLocking.
@Test(timeout = 20000)
public void testLocking() {
String streamName = "streamName";
String scope = "scope";
Segment segment = new Segment(scope, streamName, 0);
BlockingUpdate[] updates = new BlockingUpdate[] { new BlockingUpdate(1), new BlockingUpdate(2), new BlockingUpdate(3), new BlockingUpdate(4) };
MockRevisionedStreamClient client = new MockRevisionedStreamClient();
client.segment = segment;
@Cleanup StateSynchronizerImpl<RevisionedImpl> sync = new StateSynchronizerImpl<RevisionedImpl>(segment, client);
client.init = new BlockingUpdate(0);
client.updates = updates;
client.visableLength = 2;
client.init.latch.release();
updates[0].latch.release();
updates[1].latch.release();
sync.fetchUpdates();
RevisionedImpl state1 = sync.getState();
assertEquals(new RevisionImpl(segment, 2, 2), state1.getRevision());
client.visableLength = 3;
AssertExtensions.assertBlocks(() -> {
sync.fetchUpdates();
}, () -> updates[2].latch.release());
RevisionedImpl state2 = sync.getState();
assertEquals(new RevisionImpl(segment, 3, 3), state2.getRevision());
client.visableLength = 4;
AssertExtensions.assertBlocks(() -> {
sync.fetchUpdates();
}, () -> {
client.visableLength = 3;
sync.getState();
updates[3].latch.release();
});
RevisionedImpl state3 = sync.getState();
assertEquals(new RevisionImpl(segment, 4, 4), state3.getRevision());
}
use of io.pravega.client.segment.impl.Segment in project pravega by pravega.
the class SynchronizerTest method testFetchUpdatesWithMultipleTruncation.
@Test(timeout = 20000)
@SuppressWarnings("unchecked")
public void testFetchUpdatesWithMultipleTruncation() {
String streamName = "streamName";
String scope = "scope";
RevisionedStreamClient<UpdateOrInit<RevisionedImpl>> revisionedClient = mock(RevisionedStreamClient.class);
final Segment segment = new Segment(scope, streamName, 0L);
@Cleanup StateSynchronizerImpl<RevisionedImpl> syncA = new StateSynchronizerImpl<>(segment, revisionedClient);
Revision firstMark = new RevisionImpl(segment, 10L, 1);
Revision secondMark = new RevisionImpl(segment, 20L, 2);
final AbstractMap.SimpleImmutableEntry<Revision, UpdateOrInit<RevisionedImpl>> entry = new AbstractMap.SimpleImmutableEntry<>(secondMark, new UpdateOrInit<>(new RegularUpdate("x")));
Iterator<Entry<Revision, UpdateOrInit<RevisionedImpl>>> iterator = Collections.<Entry<Revision, UpdateOrInit<RevisionedImpl>>>singletonList(entry).iterator();
// Setup Mock
when(revisionedClient.getMark()).thenReturn(firstMark);
when(revisionedClient.readFrom(firstMark)).thenThrow(new TruncatedDataException()).thenThrow(new TruncatedDataException()).thenReturn(iterator);
when(revisionedClient.readFrom(secondMark)).thenReturn(iterator);
// invoke fetchUpdates which will encounter TruncatedDataException from RevisionedStreamClient.
syncA.fetchUpdates();
assertEquals("x", syncA.getState().getValue());
}
use of io.pravega.client.segment.impl.Segment in project pravega by pravega.
the class StreamCutTest method testStreamCutSerialization.
@Test
public void testStreamCutSerialization() throws Exception {
ImmutableMap<Segment, Long> segmentOffsetMap = ImmutableMap.<Segment, Long>builder().put(new Segment("scope", "stream", computeSegmentId(1, 1)), 10L).put(new Segment("scope", "stream", computeSegmentId(2, 1)), 20L).put(new Segment("scope", "stream", computeSegmentId(3, 1)), 30L).put(new Segment("scope", "stream", computeSegmentId(4, 1)), 20L).put(new Segment("scope", "stream", computeSegmentId(5, 2)), 50L).put(new Segment("scope", "stream", computeSegmentId(8, 2)), 50L).put(new Segment("scope", "stream", computeSegmentId(9, 2)), 60L).put(new Segment("scope", "stream", computeSegmentId(10, 2)), -1L).build();
StreamCut sc = new StreamCutImpl(Stream.of("scope", "stream"), segmentOffsetMap);
byte[] buf = serialize(sc);
String base64 = sc.asText();
assertEquals(sc, deSerializeStreamCut(buf));
assertEquals(sc, StreamCut.fromBytes(sc.toBytes()));
assertEquals(sc, StreamCut.from(base64));
}
use of io.pravega.client.segment.impl.Segment in project pravega by pravega.
the class TransactionalEventStreamWriterTest method testGetTxnAborted.
@Test(expected = TxnFailedException.class)
public void testGetTxnAborted() throws TxnFailedException {
String scope = "scope";
String streamName = "stream";
StreamImpl stream = new StreamImpl(scope, streamName);
Segment segment = new Segment(scope, streamName, 0);
UUID txnId = UUID.randomUUID();
EventWriterConfig config = EventWriterConfig.builder().build();
JavaSerializer<String> serializer = new JavaSerializer<>();
// Setup mocks
SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
Controller controller = Mockito.mock(Controller.class);
Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment));
Mockito.when(controller.getEpochSegments(scope, streamName, NameUtils.getEpoch(txnId))).thenReturn(getSegmentsFuture(segment));
Mockito.when(controller.checkTransactionStatus(Stream.of(scope, streamName), txnId)).thenReturn(CompletableFuture.completedFuture(Transaction.Status.ABORTED));
Mockito.when(controller.pingTransaction(eq(Stream.of(scope, streamName)), eq(txnId), anyLong())).thenReturn(CompletableFuture.completedFuture(Transaction.PingStatus.OPEN));
FakeSegmentOutputStream outputStream = new FakeSegmentOutputStream(segment);
FakeSegmentOutputStream bad = new FakeSegmentOutputStream(segment);
Mockito.when(controller.createTransaction(eq(stream), anyLong())).thenReturn(CompletableFuture.completedFuture(new TxnSegments(getSegments(segment), txnId)));
Mockito.when(streamFactory.createOutputStreamForTransaction(eq(segment), eq(txnId), any(), any())).thenReturn(outputStream);
Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment), any(), any(), any())).thenReturn(bad);
// Create a transactional event Writer
@Cleanup TransactionalEventStreamWriter<String> writer = new TransactionalEventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, config, executorService());
writer.beginTxn();
// fetch the txn when it is already aborted
Transaction<String> txn = writer.getTxn(txnId);
txn.writeEvent("Foo");
}
use of io.pravega.client.segment.impl.Segment in project pravega by pravega.
the class TransactionalEventStreamWriterTest method testTxnCommit.
@Test
public void testTxnCommit() throws TxnFailedException, SegmentSealedException {
String scope = "scope";
String streamName = "stream";
StreamImpl stream = new StreamImpl(scope, streamName);
Segment segment = new Segment(scope, streamName, 0);
UUID txid = UUID.randomUUID();
EventWriterConfig config = EventWriterConfig.builder().build();
SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
Controller controller = Mockito.mock(Controller.class);
Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment));
FakeSegmentOutputStream outputStream = spy(new FakeSegmentOutputStream(segment));
FakeSegmentOutputStream bad = new FakeSegmentOutputStream(segment);
Mockito.when(controller.createTransaction(eq(stream), anyLong())).thenReturn(CompletableFuture.completedFuture(new TxnSegments(getSegments(segment), txid)));
Mockito.when(controller.commitTransaction(eq(stream), anyString(), isNull(), eq(txid))).thenReturn(CompletableFuture.completedFuture(null));
Mockito.when(controller.pingTransaction(eq(stream), eq(txid), anyLong())).thenReturn(CompletableFuture.completedFuture(Transaction.PingStatus.OPEN));
Mockito.when(controller.checkTransactionStatus(eq(stream), eq(txid))).thenReturn(CompletableFuture.completedFuture(Transaction.Status.OPEN));
Mockito.when(streamFactory.createOutputStreamForTransaction(eq(segment), eq(txid), any(), any())).thenReturn(outputStream);
Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment), any(), any(), any())).thenReturn(bad);
JavaSerializer<String> serializer = new JavaSerializer<>();
@Cleanup TransactionalEventStreamWriter<String> writer = new TransactionalEventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, config, executorService());
Transaction<String> txn = writer.beginTxn();
txn.writeEvent("Foo");
assertTrue(bad.unacked.isEmpty());
assertEquals(1, outputStream.unacked.size());
outputStream.unacked.get(0).getAckFuture().complete(null);
txn.checkStatus();
Mockito.verify(controller, Mockito.times(1)).checkTransactionStatus(eq(stream), eq(txid));
// invoke commit of transaction.
txn.commit();
// verify if segments are flushed and closed.
Mockito.verify(outputStream, Mockito.times(1)).close();
Mockito.verify(controller, Mockito.times(1)).commitTransaction(eq(stream), anyString(), isNull(), eq(txid));
assertTrue(bad.unacked.isEmpty());
assertTrue(outputStream.unacked.isEmpty());
}
Aggregations