use of io.pravega.client.stream.EventWriterConfig in project pravega by pravega.
the class EventStreamWriterTest method testRetryFlushSegmentSealed.
@Test
public void testRetryFlushSegmentSealed() throws EndOfSegmentException, SegmentTruncatedException {
String scope = "scope";
String streamName = "stream";
StreamImpl stream = new StreamImpl(scope, streamName);
Segment segment1 = new Segment(scope, streamName, 0);
Segment segment2 = new Segment(scope, streamName, 1);
EventWriterConfig config = EventWriterConfig.builder().build();
SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
Controller controller = Mockito.mock(Controller.class);
SealedSegmentOutputStream outputStream = new SealedSegmentOutputStream(segment1);
Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment1));
Mockito.when(controller.getSuccessors(segment1)).thenReturn(getReplacement(segment1, segment2));
Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment1), any(), any(), any())).thenAnswer(i -> {
outputStream.callBackForSealed = i.getArgument(1);
return outputStream;
});
JavaSerializer<String> serializer = new JavaSerializer<>();
@Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, controller, streamFactory, serializer, config, new InlineExecutor());
writer.writeEvent("Foo");
Mockito.verify(controller).getCurrentSegments(any(), any());
assertTrue(outputStream.getUnackedEventsOnSeal().size() > 0);
MockSegmentIoStreams outputStream2 = new MockSegmentIoStreams(segment2);
Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment2), any(), any(), any())).thenReturn(outputStream2);
Async.testBlocking(() -> {
// blocking on flush.
writer.flush();
}, () -> {
// trigger release with a segmentSealedException.
outputStream.releaseFlush();
// trigger Sealed Segment call back.
outputStream.invokeSealedCallBack();
});
Mockito.verify(controller, Mockito.times(1)).getCurrentSegments(any(), any());
assertTrue(outputStream2.fetchCurrentSegmentLength() > 0);
assertEquals(serializer.serialize("Foo"), outputStream2.read());
}
use of io.pravega.client.stream.EventWriterConfig in project pravega by pravega.
the class PingerTest method startTxnKeepAliveWithLowLeaseValue.
@Test
public void startTxnKeepAliveWithLowLeaseValue() {
final UUID txnID = UUID.randomUUID();
final EventWriterConfig smallTxnLeaseTime = EventWriterConfig.builder().transactionTimeoutTime(SECONDS.toMillis(10)).build();
Pinger pinger = new Pinger(smallTxnLeaseTime, stream, controller, executor);
pinger.startPing(txnID);
verify(executor, times(1)).scheduleAtFixedRate(any(Runnable.class), anyLong(), eq(SECONDS.toMillis(5)), eq(TimeUnit.MILLISECONDS));
verify(controller, times(1)).pingTransaction(eq(stream), eq(txnID), eq(smallTxnLeaseTime.getTransactionTimeoutTime()));
}
use of io.pravega.client.stream.EventWriterConfig in project pravega by pravega.
the class EndToEndAutoScaleUpWithTxnTest method main.
public static void main(String[] args) throws Exception {
try {
@Cleanup TestingServer zkTestServer = new TestingServerStarter().start();
int port = Config.SERVICE_PORT;
@Cleanup ControllerWrapper controllerWrapper = new ControllerWrapper(zkTestServer.getConnectString(), port);
Controller controller = controllerWrapper.getController();
controllerWrapper.getControllerService().createScope(NameUtils.INTERNAL_SCOPE_NAME).get();
@Cleanup ConnectionFactory connectionFactory = new ConnectionFactoryImpl(ClientConfig.builder().build());
@Cleanup ClientFactory internalCF = new ClientFactoryImpl(NameUtils.INTERNAL_SCOPE_NAME, controller, connectionFactory);
ServiceBuilder serviceBuilder = ServiceBuilder.newInMemoryBuilder(ServiceBuilderConfig.getDefaultConfig());
serviceBuilder.initialize();
StreamSegmentStore store = serviceBuilder.createStreamSegmentService();
@Cleanup SegmentStatsFactory segmentStatsFactory = new SegmentStatsFactory();
SegmentStatsRecorder statsRecorder = segmentStatsFactory.createSegmentStatsRecorder(store, internalCF, AutoScalerConfig.builder().with(AutoScalerConfig.MUTE_IN_SECONDS, 0).with(AutoScalerConfig.COOLDOWN_IN_SECONDS, 0).build());
@Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, "localhost", 12345, store, statsRecorder, null, null, null);
server.startListening();
controllerWrapper.awaitRunning();
controllerWrapper.getControllerService().createScope("test").get();
controller.createStream(CONFIG).get();
@Cleanup MockClientFactory clientFactory = new MockClientFactory("test", controller);
// Mocking pravega service by putting scale up and scale down requests for the stream
EventWriterConfig writerConfig = EventWriterConfig.builder().transactionTimeoutTime(30000).transactionTimeoutScaleGracePeriod(30000).build();
EventStreamWriter<String> test = clientFactory.createEventWriter("test", new JavaSerializer<>(), writerConfig);
// region Successful commit tests
Transaction<String> txn1 = test.beginTxn();
txn1.writeEvent("1");
txn1.flush();
Map<Double, Double> map = new HashMap<>();
map.put(0.0, 1.0 / 3.0);
map.put(1.0 / 3.0, 2.0 / 3.0);
map.put(2.0 / 3.0, 1.0);
Stream stream = new StreamImpl("test", "test");
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
controller.scaleStream(stream, Collections.singletonList(0), map, executor).getFuture().get();
Transaction<String> txn2 = test.beginTxn();
txn2.writeEvent("2");
txn2.flush();
txn2.commit();
txn1.commit();
Thread.sleep(1000);
@Cleanup ReaderGroupManager readerGroupManager = new ReaderGroupManagerImpl("test", controller, clientFactory, connectionFactory);
readerGroupManager.createReaderGroup("readergrp", ReaderGroupConfig.builder().stream("test").build());
final EventStreamReader<String> reader = clientFactory.createReader("1", "readergrp", new JavaSerializer<>(), ReaderConfig.builder().build());
String event1 = reader.readNextEvent(SECONDS.toMillis(60)).getEvent();
String event2 = reader.readNextEvent(SECONDS.toMillis(60)).getEvent();
assert event1.equals("1");
assert event2.equals("2");
final AtomicBoolean done = new AtomicBoolean(false);
startWriter(test, done);
Retry.withExpBackoff(10, 10, 100, 10000).retryingOn(NotDoneException.class).throwingOn(RuntimeException.class).runAsync(() -> controller.getCurrentSegments("test", "test").thenAccept(streamSegments -> {
if (streamSegments.getSegments().size() > 3) {
System.err.println("Success");
log.info("Success");
System.exit(0);
} else {
throw new NotDoneException();
}
}), Executors.newSingleThreadScheduledExecutor()).exceptionally(e -> {
System.err.println("Failure");
log.error("Failure");
System.exit(1);
return null;
}).get();
} catch (Throwable e) {
System.err.print("Test failed with exception: " + e.getMessage());
log.error("Test failed with exception: {}", e);
System.exit(-1);
}
System.exit(0);
}
use of io.pravega.client.stream.EventWriterConfig in project pravega by pravega.
the class EventStreamWriterTest method testFailOnClose.
@Test
public void testFailOnClose() throws SegmentSealedException {
String scope = "scope";
String streamName = "stream";
StreamImpl stream = new StreamImpl(scope, streamName);
Segment segment = new Segment(scope, streamName, 0);
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));
SegmentOutputStream outputStream = Mockito.mock(SegmentOutputStream.class);
Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment), any(), any(), any())).thenReturn(outputStream);
EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, controller, streamFactory, new JavaSerializer<>(), config, new InlineExecutor());
Mockito.doThrow(new RuntimeException("Intentional exception")).when(outputStream).close();
writer.writeEvent("Foo");
writer.writeEvent("Bar");
try {
writer.close();
fail();
} catch (RuntimeException e) {
// expected.
}
try {
writer.writeEvent("fail");
fail();
} catch (IllegalStateException e) {
// expected
}
}
use of io.pravega.client.stream.EventWriterConfig in project pravega by pravega.
the class EventStreamWriterTest method testSegmentSealedInFlush.
@Test
public void testSegmentSealedInFlush() throws EndOfSegmentException, SegmentTruncatedException {
String scope = "scope";
String streamName = "stream";
StreamImpl stream = new StreamImpl(scope, streamName);
Segment segment1 = new Segment(scope, streamName, 0);
Segment segment2 = new Segment(scope, streamName, 1);
EventWriterConfig config = EventWriterConfig.builder().build();
SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
Controller controller = Mockito.mock(Controller.class);
FakeSegmentOutputStream outputStream = new FakeSegmentOutputStream(segment1);
Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment1));
Mockito.when(controller.getSuccessors(segment1)).thenReturn(getReplacement(segment1, segment2));
Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment1), any(), any(), any())).thenAnswer(i -> {
outputStream.callBackForSealed = i.getArgument(1);
return outputStream;
});
JavaSerializer<String> serializer = new JavaSerializer<>();
@Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, controller, streamFactory, serializer, config, new InlineExecutor());
writer.writeEvent("Foo");
Mockito.verify(controller).getCurrentSegments(any(), any());
assertTrue(outputStream.getUnackedEventsOnSeal().size() > 0);
MockSegmentIoStreams outputStream2 = new MockSegmentIoStreams(segment2);
Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment2), any(), any(), any())).thenReturn(outputStream2);
outputStream.invokeSealedCallBack();
writer.flush();
Mockito.verify(controller, Mockito.times(1)).getCurrentSegments(any(), any());
assertTrue(outputStream2.fetchCurrentSegmentLength() > 0);
assertEquals(serializer.serialize("Foo"), outputStream2.read());
}
Aggregations