use of io.pravega.client.stream.EventWriterConfig in project pravega by pravega.
the class EndToEndTruncationTest method testWriteOnSealedStream.
@Test(timeout = 50000)
public void testWriteOnSealedStream() throws Exception {
JavaSerializer<String> serializer = new JavaSerializer<>();
EventWriterConfig writerConfig = EventWriterConfig.builder().build();
String scope = "testSeal";
String streamName = "testWriteOnSealedStream";
StreamConfiguration config = StreamConfiguration.builder().scalingPolicy(ScalingPolicy.byEventRate(10, 2, 2)).build();
LocalController controller = (LocalController) PRAVEGA.getLocalController();
controller.createScope(scope).get();
controller.createStream(scope, streamName, config).get();
config = StreamConfiguration.builder().scalingPolicy(ScalingPolicy.byEventRate(10, 2, 1)).build();
controller.updateStream(scope, streamName, config).get();
@Cleanup ConnectionFactory connectionFactory = new SocketConnectionFactoryImpl(ClientConfig.builder().controllerURI(PRAVEGA.getControllerURI()).build());
@Cleanup ClientFactoryImpl clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory);
@Cleanup EventStreamWriter<String> writer = clientFactory.createEventWriter(streamName, serializer, writerConfig);
// write an event.
writer.writeEvent("0", "data").get();
// Seal Stream.
assertTrue(controller.sealStream(scope, streamName).get());
// Write by an existing writer to a sealed stream should complete exceptionally.
assertFutureThrows("Should throw IllegalStateException", writer.writeEvent("2", "Write to sealed stream"), e -> IllegalStateException.class.isAssignableFrom(e.getClass()));
// Subsequent writes will throw an exception.
assertThrows(IllegalStateException.class, () -> writer.writeEvent("testEvent"));
// Creating a writer against a sealed stream throws an exception.
assertThrows(IllegalStateException.class, () -> clientFactory.createEventWriter(streamName, serializer, writerConfig));
}
use of io.pravega.client.stream.EventWriterConfig in project pravega by pravega.
the class ByteStreamClientImpl method createByteStreamWriter.
@Override
public ByteStreamWriter createByteStreamWriter(String streamName) {
StreamSegments segments = Futures.getThrowingException(controller.getCurrentSegments(scope, streamName));
Preconditions.checkState(segments.getNumberOfSegments() > 0, "Stream is sealed");
Preconditions.checkState(segments.getNumberOfSegments() == 1, "Stream is configured with more than one segment");
Segment segment = segments.getSegments().iterator().next();
// The writer should not give up connecting to SegmentStore in the background until the ByteStreamWriter is closed.
EventWriterConfig config = EventWriterConfig.builder().retryAttempts(Integer.MAX_VALUE).build();
DelegationTokenProvider tokenProvider = DelegationTokenProviderFactory.create(controller, segment, AccessOperation.WRITE);
return new BufferedByteStreamWriterImpl(new ByteStreamWriterImpl(outputStreamFactory.createOutputStreamForSegment(segment, config, tokenProvider), metaStreamFactory.createSegmentMetadataClient(segment, tokenProvider)));
}
use of io.pravega.client.stream.EventWriterConfig in project pravega by pravega.
the class TransactionalEventStreamWriterTest method testGetTxn.
@Test
public void testGetTxn() 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.OPEN));
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
TransactionalEventStreamWriter<String> writer = new TransactionalEventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, config, executorService());
writer.beginTxn();
writer.close();
// Create a new transactional eventWriter
@Cleanup TransactionalEventStreamWriter<String> writer1 = new TransactionalEventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, EventWriterConfig.builder().transactionTimeoutTime(10000).build(), executorService());
Transaction<String> txn = writer1.getTxn(txnId);
txn.writeEvent("Foo");
assertTrue(bad.unacked.isEmpty());
assertEquals(1, outputStream.unacked.size());
outputStream.unacked.get(0).getAckFuture().complete(null);
txn.flush();
assertTrue(bad.unacked.isEmpty());
assertTrue(outputStream.unacked.isEmpty());
// verify pings was invoked for the transaction with the specified transactionTimeout.
verify(controller, timeout(10_000).atLeastOnce()).pingTransaction(eq(stream), eq(txnId), eq(10000L));
}
use of io.pravega.client.stream.EventWriterConfig in project pravega by pravega.
the class TransactionalEventStreamWriterTest method testTxnFailed.
@Test
public void testTxnFailed() {
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 = 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(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();
outputStream.invokeSealedCallBack();
try {
txn.writeEvent("Foo");
} catch (TxnFailedException e) {
// Expected
}
assertTrue(bad.unacked.isEmpty());
assertEquals(1, outputStream.unacked.size());
}
use of io.pravega.client.stream.EventWriterConfig in project pravega by pravega.
the class TransactionalEventStreamWriterTest method testTxnAbort.
@Test
public void testTxnAbort() 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.pingTransaction(eq(stream), eq(txid), anyLong())).thenReturn(CompletableFuture.completedFuture(Transaction.PingStatus.OPEN));
Mockito.when(controller.abortTransaction(eq(stream), eq(txid))).thenReturn(CompletableFuture.completedFuture(null));
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);
// invoke commit of transaction.
txn.abort();
// verify if segments are flushed and closed.
Mockito.verify(outputStream, Mockito.times(1)).close();
Mockito.verify(controller, Mockito.times(1)).abortTransaction(eq(stream), eq(txid));
}
Aggregations