use of io.pravega.client.stream.EventWriterConfig in project pravega by pravega.
the class AutoScaleProcessor method bootstrapOnce.
@VisibleForTesting
void bootstrapOnce(EventStreamClientFactory clientFactory, AtomicReference<EventStreamWriter<AutoScaleEvent>> writerRef) {
if (!writer.isDone()) {
if (!startInitWriter.get()) {
throw new RuntimeException("Init not requested");
}
// Ensure the writer tries indefinitely to establish connection. This retry will continue in the background
// until the AutoScaleProcessor is closed.
EventWriterConfig writerConfig = EventWriterConfig.builder().retryAttempts(Integer.MAX_VALUE).build();
writerRef.set(clientFactory.createEventWriter(configuration.getInternalRequestStream(), SERIALIZER, writerConfig));
log.info("AutoScale Processor Initialized. RequestStream={}", configuration.getInternalRequestStream());
}
}
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, 0L).get();
@Cleanup ConnectionFactory connectionFactory = new SocketConnectionFactoryImpl(ClientConfig.builder().build());
@Cleanup ClientFactoryImpl internalCF = new ClientFactoryImpl(NameUtils.INTERNAL_SCOPE_NAME, controller, connectionFactory);
@Cleanup("shutdownNow") val executor = ExecutorServiceHelpers.newScheduledThreadPool(1, "test");
@Cleanup ServiceBuilder serviceBuilder = ServiceBuilder.newInMemoryBuilder(ServiceBuilderConfig.getDefaultConfig());
serviceBuilder.initialize();
StreamSegmentStore store = serviceBuilder.createStreamSegmentService();
TableStore tableStore = serviceBuilder.createTableStoreService();
@Cleanup AutoScaleMonitor autoScaleMonitor = new AutoScaleMonitor(store, internalCF, AutoScalerConfig.builder().with(AutoScalerConfig.MUTE_IN_SECONDS, 0).with(AutoScalerConfig.COOLDOWN_IN_SECONDS, 0).build());
@Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, false, "localhost", 12345, store, tableStore, autoScaleMonitor.getStatsRecorder(), autoScaleMonitor.getTableSegmentStatsRecorder(), null, null, null, true, serviceBuilder.getLowPriorityExecutor(), Config.TLS_PROTOCOL_VERSION.toArray(new String[Config.TLS_PROTOCOL_VERSION.size()]));
server.startListening();
controllerWrapper.awaitRunning();
controllerWrapper.getControllerService().createScope("test", 0L).get();
controller.createStream("test", "test", CONFIG).get();
@Cleanup MockClientFactory clientFactory = new MockClientFactory("test", controller, internalCF.getConnectionPool());
// Mocking pravega service by putting scale up and scale down requests for the stream
EventWriterConfig writerConfig = EventWriterConfig.builder().transactionTimeoutTime(30000).build();
TransactionalEventStreamWriter<String> test = clientFactory.createTransactionalEventWriter("writer", "test", new UTF8StringSerializer(), 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");
controller.startScale(stream, Collections.singletonList(0L), map).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);
readerGroupManager.createReaderGroup("readergrp", ReaderGroupConfig.builder().stream("test/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().stream().anyMatch(x -> NameUtils.getEpoch(x.getSegmentId()) > 5)) {
System.err.println("Success");
log.info("Success");
System.exit(0);
} else {
throw new NotDoneException();
}
}), executor).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 RestoreBackUpDataRecoveryTest method writeTransactionalEvents.
// Writes the required number of events to the given stream with using transactions.
private void writeTransactionalEvents(String streamName, ClientFactoryImpl clientFactory) throws TxnFailedException {
EventWriterConfig writerConfig = EventWriterConfig.builder().transactionTimeoutTime(TRANSACTION_TIMEOUT.toMillis()).build();
@Cleanup TransactionalEventStreamWriter<String> txnWriter = clientFactory.createTransactionalEventWriter(streamName, new UTF8StringSerializer(), writerConfig);
Transaction<String> transaction = txnWriter.beginTxn();
for (int i = 0; i < TOTAL_NUM_EVENTS; i++) {
transaction.writeEvent("0", EVENT);
}
transaction.commit();
txnWriter.close();
}
use of io.pravega.client.stream.EventWriterConfig in project pravega by pravega.
the class TransactionTest method testTransactionalWritesOrderedCorrectly.
@Test(timeout = 10000)
@SuppressWarnings("deprecation")
public void testTransactionalWritesOrderedCorrectly() throws TxnFailedException, ReinitializationRequiredException {
int readTimeout = 5000;
String readerName = "reader";
String groupName = "testTransactionalWritesOrderedCorrectly-group";
String endpoint = "localhost";
String streamName = "testTransactionalWritesOrderedCorrectly";
int port = TestUtils.getAvailableListenPort();
String txnEvent = "TXN Event\n";
String nonTxEvent = "Non-TX Event\n";
String routingKey = "RoutingKey";
StreamSegmentStore store = SERVICE_BUILDER.createStreamSegmentService();
TableStore tableStore = SERVICE_BUILDER.createTableStoreService();
@Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store, tableStore, SERVICE_BUILDER.getLowPriorityExecutor());
server.startListening();
@Cleanup MockStreamManager streamManager = new MockStreamManager("scope", endpoint, port);
streamManager.createScope("scope");
streamManager.createStream("scope", streamName, StreamConfiguration.builder().build());
streamManager.createReaderGroup(groupName, ReaderGroupConfig.builder().stream(Stream.of("scope", streamName)).disableAutomaticCheckpoints().build());
@Cleanup MockClientFactory clientFactory = streamManager.getClientFactory();
EventWriterConfig eventWriterConfig = EventWriterConfig.builder().transactionTimeoutTime(60000).build();
@Cleanup EventStreamWriter<String> producer = clientFactory.createEventWriter(streamName, new JavaSerializer<>(), eventWriterConfig);
@Cleanup TransactionalEventStreamWriter<String> txnProducer = clientFactory.createTransactionalEventWriter(streamName, new JavaSerializer<>(), eventWriterConfig);
producer.writeEvent(routingKey, nonTxEvent);
Transaction<String> transaction = txnProducer.beginTxn();
producer.writeEvent(routingKey, nonTxEvent);
transaction.writeEvent(routingKey, txnEvent);
producer.writeEvent(routingKey, nonTxEvent);
transaction.writeEvent(routingKey, txnEvent);
producer.flush();
producer.writeEvent(routingKey, nonTxEvent);
transaction.writeEvent(routingKey, txnEvent);
producer.writeEvent(routingKey, nonTxEvent);
transaction.writeEvent(routingKey, txnEvent);
transaction.flush();
producer.writeEvent(routingKey, nonTxEvent);
transaction.writeEvent(routingKey, txnEvent);
producer.flush();
transaction.writeEvent(routingKey, txnEvent);
transaction.commit();
producer.writeEvent(routingKey, nonTxEvent);
AssertExtensions.assertThrows(TxnFailedException.class, () -> transaction.writeEvent(routingKey, txnEvent));
@Cleanup EventStreamReader<Serializable> consumer = streamManager.getClientFactory().createReader(readerName, groupName, new JavaSerializer<>(), ReaderConfig.builder().build());
assertEquals(nonTxEvent, consumer.readNextEvent(readTimeout).getEvent());
assertEquals(nonTxEvent, consumer.readNextEvent(readTimeout).getEvent());
assertEquals(nonTxEvent, consumer.readNextEvent(readTimeout).getEvent());
assertEquals(nonTxEvent, consumer.readNextEvent(readTimeout).getEvent());
assertEquals(nonTxEvent, consumer.readNextEvent(readTimeout).getEvent());
assertEquals(nonTxEvent, consumer.readNextEvent(readTimeout).getEvent());
assertEquals(txnEvent, consumer.readNextEvent(readTimeout).getEvent());
assertEquals(txnEvent, consumer.readNextEvent(readTimeout).getEvent());
assertEquals(txnEvent, consumer.readNextEvent(readTimeout).getEvent());
assertEquals(txnEvent, consumer.readNextEvent(readTimeout).getEvent());
assertEquals(txnEvent, consumer.readNextEvent(readTimeout).getEvent());
assertEquals(txnEvent, consumer.readNextEvent(readTimeout).getEvent());
assertEquals(nonTxEvent, consumer.readNextEvent(readTimeout).getEvent());
}
use of io.pravega.client.stream.EventWriterConfig in project pravega by pravega.
the class TransactionTest method testDrop.
@Test(timeout = 10000)
@SuppressWarnings("deprecation")
public void testDrop() throws TxnFailedException, ReinitializationRequiredException {
String endpoint = "localhost";
String groupName = "testDrop-group";
String streamName = "testDrop";
int port = TestUtils.getAvailableListenPort();
String txnEvent = "TXN Event\n";
String nonTxEvent = "Non-TX Event\n";
String routingKey = "RoutingKey";
StreamSegmentStore store = SERVICE_BUILDER.createStreamSegmentService();
TableStore tableStore = SERVICE_BUILDER.createTableStoreService();
@Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store, tableStore, SERVICE_BUILDER.getLowPriorityExecutor());
server.startListening();
@Cleanup MockStreamManager streamManager = new MockStreamManager("scope", endpoint, port);
streamManager.createScope("scope");
streamManager.createStream("scope", streamName, StreamConfiguration.builder().build());
streamManager.createReaderGroup(groupName, ReaderGroupConfig.builder().stream(Stream.of("scope", streamName)).disableAutomaticCheckpoints().build());
@Cleanup MockClientFactory clientFactory = streamManager.getClientFactory();
EventWriterConfig eventWriterConfig = EventWriterConfig.builder().transactionTimeoutTime(60000).build();
@Cleanup TransactionalEventStreamWriter<String> txnProducer = clientFactory.createTransactionalEventWriter(streamName, new JavaSerializer<>(), eventWriterConfig);
Transaction<String> transaction = txnProducer.beginTxn();
transaction.writeEvent(routingKey, txnEvent);
transaction.flush();
transaction.abort();
transaction.abort();
AssertExtensions.assertThrows(TxnFailedException.class, () -> transaction.writeEvent(routingKey, txnEvent));
AssertExtensions.assertThrows(TxnFailedException.class, () -> transaction.commit());
@Cleanup EventStreamReader<Serializable> consumer = streamManager.getClientFactory().createReader("reader", groupName, new JavaSerializer<>(), ReaderConfig.builder().build());
@Cleanup EventStreamWriter<String> producer = clientFactory.createEventWriter(streamName, new JavaSerializer<>(), eventWriterConfig);
producer.writeEvent(routingKey, nonTxEvent);
producer.flush();
assertEquals(nonTxEvent, consumer.readNextEvent(1500).getEvent());
}
Aggregations