use of io.pravega.client.segment.impl.SegmentOutputStream in project pravega by pravega.
the class ReadTest method readThroughSegmentClient.
@Test
public void readThroughSegmentClient() throws SegmentSealedException, EndOfSegmentException, SegmentTruncatedException {
String endpoint = "localhost";
String scope = "scope";
String stream = "stream";
int port = TestUtils.getAvailableListenPort();
String testString = "Hello world\n";
StreamSegmentStore store = this.serviceBuilder.createStreamSegmentService();
@Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store);
server.startListening();
ConnectionFactory clientCF = new ConnectionFactoryImpl(ClientConfig.builder().build());
Controller controller = new MockController(endpoint, port, clientCF);
controller.createScope(scope);
controller.createStream(StreamConfiguration.builder().scope(scope).streamName(stream).build());
SegmentOutputStreamFactoryImpl segmentproducerClient = new SegmentOutputStreamFactoryImpl(controller, clientCF);
SegmentInputStreamFactoryImpl segmentConsumerClient = new SegmentInputStreamFactoryImpl(controller, clientCF);
Segment segment = Futures.getAndHandleExceptions(controller.getCurrentSegments(scope, stream), RuntimeException::new).getSegments().iterator().next();
@Cleanup("close") SegmentOutputStream out = segmentproducerClient.createOutputStreamForSegment(segment, segmentSealedCallback, EventWriterConfig.builder().build(), "");
out.write(new PendingEvent(null, ByteBuffer.wrap(testString.getBytes()), new CompletableFuture<>()));
out.flush();
@Cleanup("close") SegmentInputStream in = segmentConsumerClient.createInputStreamForSegment(segment);
ByteBuffer result = in.read();
assertEquals(ByteBuffer.wrap(testString.getBytes()), result);
// Test large write followed by read
out.write(new PendingEvent(null, ByteBuffer.wrap(new byte[15]), new CompletableFuture<>()));
out.write(new PendingEvent(null, ByteBuffer.wrap(new byte[15]), new CompletableFuture<>()));
out.write(new PendingEvent(null, ByteBuffer.wrap(new byte[150000]), new CompletableFuture<>()));
assertEquals(in.read().capacity(), 15);
assertEquals(in.read().capacity(), 15);
assertEquals(in.read().capacity(), 150000);
}
use of io.pravega.client.segment.impl.SegmentOutputStream in project pravega by pravega.
the class AppendTest method appendThroughSegmentClient.
@Test
public void appendThroughSegmentClient() throws Exception {
String endpoint = "localhost";
int port = TestUtils.getAvailableListenPort();
String testString = "Hello world\n";
String scope = "scope";
String stream = "stream";
StreamSegmentStore store = this.serviceBuilder.createStreamSegmentService();
@Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store);
server.startListening();
@Cleanup ConnectionFactory clientCF = new ConnectionFactoryImpl(ClientConfig.builder().build());
Controller controller = new MockController(endpoint, port, clientCF);
controller.createScope(scope);
controller.createStream(StreamConfiguration.builder().scope(scope).streamName(stream).build());
SegmentOutputStreamFactoryImpl segmentClient = new SegmentOutputStreamFactoryImpl(controller, clientCF);
Segment segment = Futures.getAndHandleExceptions(controller.getCurrentSegments(scope, stream), RuntimeException::new).getSegments().iterator().next();
@Cleanup SegmentOutputStream out = segmentClient.createOutputStreamForSegment(segment, segmentSealedCallback, EventWriterConfig.builder().build(), "");
CompletableFuture<Boolean> ack = new CompletableFuture<>();
out.write(new PendingEvent(null, ByteBuffer.wrap(testString.getBytes()), ack));
assertTrue(ack.get(5, TimeUnit.SECONDS));
}
use of io.pravega.client.segment.impl.SegmentOutputStream in project pravega by pravega.
the class EventStreamWriterImpl method flushInternal.
private void flushInternal() {
writeFlushLock.readLock().lock();
boolean success = false;
try {
while (!success) {
success = true;
for (SegmentOutputStream writer : selector.getWriters()) {
try {
writer.flush();
} catch (SegmentSealedException e) {
// Segment sealed exception observed during a flush. Re-run flush on all the available writers.
success = false;
log.warn("Flush failed due to {}, it will be retried.", e.getMessage());
}
}
}
} finally {
writeFlushLock.readLock().unlock();
}
}
use of io.pravega.client.segment.impl.SegmentOutputStream in project pravega by pravega.
the class EventStreamWriterImpl method resend.
@GuardedBy("writeFlushLock")
private void resend(List<PendingEvent> toResend) {
while (!toResend.isEmpty()) {
List<PendingEvent> unsent = new ArrayList<>();
boolean sendFailed = false;
for (PendingEvent event : toResend) {
if (sendFailed) {
unsent.add(event);
} else {
SegmentOutputStream segmentWriter = selector.getSegmentOutputStreamForKey(event.getRoutingKey());
if (segmentWriter == null) {
unsent.addAll(selector.refreshSegmentEventWriters(segmentSealedCallBack));
sendFailed = true;
} else {
segmentWriter.write(event);
}
}
}
toResend = unsent;
}
}
use of io.pravega.client.segment.impl.SegmentOutputStream in project pravega by pravega.
the class EventStreamWriterImpl method writeEventInternal.
private CompletableFuture<Void> writeEventInternal(String routingKey, Type event) {
Preconditions.checkNotNull(event);
Exceptions.checkNotClosed(closed.get(), this);
ByteBuffer data = serializer.serialize(event);
CompletableFuture<Boolean> ackFuture = new CompletableFuture<Boolean>();
writeFlushLock.writeLock().lock();
try {
SegmentOutputStream segmentWriter = selector.getSegmentOutputStreamForKey(routingKey);
while (segmentWriter == null) {
log.info("Don't have a writer for segment: {}", selector.getSegmentForEvent(routingKey));
handleMissingLog();
segmentWriter = selector.getSegmentOutputStreamForKey(routingKey);
}
segmentWriter.write(new PendingEvent(routingKey, data, ackFuture));
} finally {
writeFlushLock.writeLock().unlock();
}
CompletableFuture<Void> result = new CompletableFuture<>();
ackFuture.whenComplete((bool, exception) -> {
if (exception != null) {
result.completeExceptionally(exception);
} else {
if (bool) {
result.complete(null);
} else {
result.completeExceptionally(new IllegalStateException("Condition failed for non-conditional " + "write!?"));
}
}
});
return result;
}
Aggregations