private void createMissingWriters(Consumer<Segment> segmentSealedCallBack, String delegationToken) { for (Segment segment : currentSegments.getSegments()) { if (!writers.containsKey(segment)) { log.debug("Creating writer for segment {}", segment); SegmentOutputStream out = outputStreamFactory.createOutputStreamForSegment(segment, segmentSealedCallBack, config, delegationToken); writers.put(segment, out); } } }
/** * Moved to {@link TransactionalEventStreamWriterImpl}. * @deprecated Moved to {@link TransactionalEventStreamWriterImpl} */ @Override @Deprecated public Transaction<Type> getTxn(UUID txId) { StreamSegments segments = getAndHandleExceptions( controller.getCurrentSegments(stream.getScope(), stream.getStreamName()), RuntimeException::new); Status status = getAndHandleExceptions(controller.checkTransactionStatus(stream, txId), RuntimeException::new); if (status != Status.OPEN) { return new TransactionImpl<>(txId, controller, stream); } Map<Segment, SegmentTransaction<Type>> transactions = new HashMap<>(); for (Segment s : segments.getSegments()) { SegmentOutputStream out = outputStreamFactory.createOutputStreamForTransaction(s, txId, config, segments.getDelegationToken()); SegmentTransactionImpl<Type> impl = new SegmentTransactionImpl<>(txId, out, serializer); transactions.put(s, impl); } return new TransactionImpl<Type>(txId, transactions, segments, controller, stream, pinger); }
Mockito.when(controller.createTransaction(stream, 0)) .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);
Mockito.when(controller.createTransaction(stream, 0)) .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);
@Override public ByteStreamWriter createByteStreamWriter(String streamName) { StreamSegments segments = Futures.getThrowingException(controller.getCurrentSegments(scope, streamName)); Preconditions.checkArgument(segments.getSegments().size() == 1, "Stream is configured with more than one segment"); Segment segment = segments.getSegments().iterator().next(); EventWriterConfig config = EventWriterConfig.builder().build(); String delegationToken = segments.getDelegationToken(); return new BufferedByteStreamWriterImpl(new ByteStreamWriterImpl(outputStreamFactory.createOutputStreamForSegment(segment, config, delegationToken), metaStreamFactory.createSegmentMetadataClient(segment, delegationToken))); }
Mockito.when(controller.createTransaction(stream, 0)) .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);
@Override public Transaction<Type> beginTxn() { TxnSegments txnSegments = getAndHandleExceptions(controller.createTransaction(stream, config.getTransactionTimeoutTime()), RuntimeException::new); UUID txnId = txnSegments.getTxnId(); Map<Segment, SegmentTransaction<Type>> transactions = new HashMap<>(); for (Segment s : txnSegments.getSteamSegments().getSegments()) { SegmentOutputStream out = outputStreamFactory.createOutputStreamForTransaction(s, txnId, config, txnSegments.getSteamSegments().getDelegationToken()); SegmentTransactionImpl<Type> impl = new SegmentTransactionImpl<>(txnId, out, serializer); transactions.put(s, impl); } pinger.startPing(txnId); return new TransactionImpl<Type>(txnId, transactions, txnSegments.getSteamSegments(), controller, stream, pinger); }
@Override @SuppressWarnings("deprecation") public <T> RevisionedStreamClient<T> createRevisionedStreamClient(String streamName, Serializer<T> serializer, SynchronizerConfig config) { log.info("Creating revisioned stream client for stream: {} with synchronizer configuration: {}", streamName, config); Segment segment = new Segment(scope, streamName, 0); EventSegmentReader in = inFactory.createEventReaderForSegment(segment); // Segment sealed is not expected for Revisioned Stream Client. Consumer<Segment> segmentSealedCallBack = s -> { throw new IllegalStateException("RevisionedClient: Segmentsealed exception observed for segment:" + s); }; String delegationToken = Futures.getAndHandleExceptions(controller.getOrRefreshDelegationTokenFor(segment.getScope(), segment.getStreamName()), RuntimeException::new); SegmentOutputStream out = outFactory.createOutputStreamForSegment(segment, segmentSealedCallBack, config.getEventWriterConfig(), delegationToken); ConditionalOutputStream cond = condFactory.createConditionalOutputStream(segment, delegationToken, config.getEventWriterConfig()); SegmentMetadataClient meta = metaFactory.createSegmentMetadataClient(segment, delegationToken); return new RevisionedStreamClientImpl<>(segment, in, out, cond, meta, serializer); }
Mockito.when(controller.createTransaction(stream, 0)) .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);
/** * Moved to {@link TransactionalEventStreamWriterImpl}. * @deprecated Moved to {@link TransactionalEventStreamWriterImpl} */ @Override @Deprecated public Transaction<Type> beginTxn() { TxnSegments txnSegments = getAndHandleExceptions(controller.createTransaction(stream, config.getTransactionTimeoutTime()), RuntimeException::new); UUID txnId = txnSegments.getTxnId(); Map<Segment, SegmentTransaction<Type>> transactions = new HashMap<>(); for (Segment s : txnSegments.getSteamSegments().getSegments()) { SegmentOutputStream out = outputStreamFactory.createOutputStreamForTransaction(s, txnId, config, txnSegments.getSteamSegments().getDelegationToken()); SegmentTransactionImpl<Type> impl = new SegmentTransactionImpl<>(txnId, out, serializer); transactions.put(s, impl); } pinger.startPing(txnId); return new TransactionImpl<Type>(txnId, transactions, txnSegments.getSteamSegments(), controller, stream, pinger); }
when(factory.createOutputStreamForSegment(eq(segment0), ArgumentMatchers.<Consumer<Segment>>any(), any(EventWriterConfig.class), anyString())) .thenReturn(s0Writer); when(factory.createOutputStreamForSegment(eq(segment1), ArgumentMatchers.<Consumer<Segment>>any(), any(EventWriterConfig.class), anyString())) .thenReturn(s1Writer);
@Override public Transaction<Type> getTxn(UUID txId) { StreamSegments segments = getAndHandleExceptions( controller.getCurrentSegments(stream.getScope(), stream.getStreamName()), RuntimeException::new); Status status = getAndHandleExceptions(controller.checkTransactionStatus(stream, txId), RuntimeException::new); if (status != Status.OPEN) { return new TransactionImpl<>(txId, controller, stream); } Map<Segment, SegmentTransaction<Type>> transactions = new HashMap<>(); for (Segment s : segments.getSegments()) { SegmentOutputStream out = outputStreamFactory.createOutputStreamForTransaction(s, txId, config, segments.getDelegationToken()); SegmentTransactionImpl<Type> impl = new SegmentTransactionImpl<>(txId, out, serializer); transactions.put(s, impl); } return new TransactionImpl<Type>(txId, transactions, segments, controller, stream, pinger); }
@Test public void testFlush() { 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); FakeSegmentOutputStream outputStream = new FakeSegmentOutputStream(segment); Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment)); Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment), any(), any(), any())).thenReturn(outputStream); JavaSerializer<String> serializer = new JavaSerializer<>(); @Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, controller, streamFactory, serializer, config, executorService()); writer.writeEvent("Foo"); Mockito.verify(controller).getCurrentSegments(any(), any()); assertTrue(outputStream.unacked.size() > 0); writer.flush(); assertTrue(outputStream.unacked.isEmpty()); }
.thenReturn(getSegmentsFuture(segment1)); Mockito.when(controller.getSuccessors(segment1)).thenReturn(getReplacement(segment1, segment2)); Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment1), any(), any(), any())).thenAnswer(i -> { outputStream1.callBackForSealed = i.getArgument(1); return outputStream1; }); Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment2), any(), any(), any())).thenAnswer(i -> { outputStream2.callBackForSealed = i.getArgument(1); return outputStream2;
.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); Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment2), any(), any(), any())).thenReturn(outputStream2); outputStream.invokeSealedCallBack();
.thenReturn(getSegmentsFuture(segment1)); Mockito.when(controller.getSuccessors(segment1)).thenReturn(getReplacement(segment1, segment2)); Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment1), any(), any(), any())) .thenAnswer(i -> { outputStream1.callBackForSealed = i.getArgument(1); Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment2), any(), any(), any())).thenReturn(outputStream2); outputStream1.invokeSealedCallBack();
.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); Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment2), any(), any(), any())).thenReturn(outputStream2);
.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); Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment2), any(), any(), any())).thenReturn(outputStream2);
FakeSegmentOutputStream outputStream2 = new FakeSegmentOutputStream(segment2); Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment1), any(), any(), any())).thenAnswer(i -> { outputStream1.callBackForSealed = i.getArgument(1); return outputStream1; }); Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment2), any(), any(), any())).thenAnswer(i -> { outputStream2.callBackForSealed = i.getArgument(1); return outputStream2;
@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, executorService()); 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 } }