use of io.pravega.client.segment.impl.SegmentOutputStream in project pravega by pravega.
the class TransactionalEventStreamWriterImpl method getTxn.
@Override
public Transaction<Type> getTxn(UUID txId) {
// check if the transaction is open.
Status status = Futures.getThrowingException(controller.checkTransactionStatus(stream, txId));
if (status != Status.OPEN) {
return new TransactionImpl<>(writerId, txId, controller, stream);
}
// get the segments corresponding to the transaction.
StreamSegments segments = Futures.getThrowingException(controller.getEpochSegments(stream.getScope(), stream.getStreamName(), getEpoch(txId)));
assert segments != null : "Epoch segments returned is null";
Preconditions.checkState(segments.getSegments().size() > 0, "There should be at least 1 epoch segment");
// Create OutputStream for every segment.
Map<Segment, SegmentTransaction<Type>> transactions = new HashMap<>();
DelegationTokenProvider tokenProvider = null;
for (Segment s : segments.getSegments()) {
if (tokenProvider == null) {
tokenProvider = DelegationTokenProviderFactory.create(controller, s, AccessOperation.WRITE);
}
SegmentOutputStream out = outputStreamFactory.createOutputStreamForTransaction(s, txId, config, tokenProvider);
SegmentTransactionImpl<Type> impl = new SegmentTransactionImpl<>(txId, out, serializer);
transactions.put(s, impl);
}
pinger.startPing(txId);
return new TransactionImpl<Type>(writerId, txId, transactions, segments, controller, stream, pinger);
}
use of io.pravega.client.segment.impl.SegmentOutputStream in project pravega by pravega.
the class AppendReconnectTest method reconnectOnSegmentClient.
@Test(timeout = 30000)
public void reconnectOnSegmentClient() throws Exception {
String endpoint = "localhost";
int port = TestUtils.getAvailableListenPort();
byte[] payload = "Hello world\n".getBytes();
String scope = "scope";
String stream = "stream";
StreamSegmentStore store = this.serviceBuilder.createStreamSegmentService();
@Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store, mock(TableStore.class), serviceBuilder.getLowPriorityExecutor());
server.startListening();
@Cleanup SocketConnectionFactoryImpl clientCF = new SocketConnectionFactoryImpl(ClientConfig.builder().build());
@Cleanup ConnectionPoolImpl connectionPool = new ConnectionPoolImpl(ClientConfig.builder().build(), clientCF);
Controller controller = new MockController(endpoint, port, connectionPool, true);
controller.createScope(scope);
controller.createStream(scope, stream, StreamConfiguration.builder().build());
SegmentOutputStreamFactoryImpl segmentClient = new SegmentOutputStreamFactoryImpl(controller, connectionPool);
Segment segment = Futures.getAndHandleExceptions(controller.getCurrentSegments(scope, stream), RuntimeException::new).getSegments().iterator().next();
@Cleanup SegmentOutputStream out = segmentClient.createOutputStreamForSegment(segment, segmentSealedCallback, EventWriterConfig.builder().build(), DelegationTokenProviderFactory.createWithEmptyToken());
CompletableFuture<Void> ack = new CompletableFuture<>();
out.write(PendingEvent.withoutHeader(null, ByteBuffer.wrap(payload), ack));
for (AutoCloseable c : connectionPool.getActiveChannels()) {
c.close();
}
CompletableFuture<Void> ack2 = new CompletableFuture<>();
out.write(PendingEvent.withoutHeader(null, ByteBuffer.wrap(payload), ack2));
ack.get(5, TimeUnit.SECONDS);
ack2.get(5, TimeUnit.SECONDS);
@Cleanup SegmentMetadataClient metadataClient = new SegmentMetadataClientFactoryImpl(controller, connectionPool).createSegmentMetadataClient(segment, DelegationTokenProviderFactory.createWithEmptyToken());
assertEquals(payload.length * 2, metadataClient.fetchCurrentSegmentLength().join().longValue());
}
Aggregations