@Override public void write(byte[] b, int off, int len) throws IOException { ByteBuffer data = ByteBuffer.wrap(b, off, len); out.write(PendingEvent.withoutHeader(null, data, null)); }
@Override public void write(ByteBuffer src) throws IOException { out.write(PendingEvent.withoutHeader(null, src, null)); }
@GuardedBy("writeSealLock") private void resend(List<PendingEvent> toResend) { while (!toResend.isEmpty()) { List<PendingEvent> unsent = new ArrayList<>(); boolean sendFailed = false; log.info("Resending {} events", toResend.size()); for (PendingEvent event : toResend) { if (sendFailed) { unsent.add(event); } else { SegmentOutputStream segmentWriter = selector.getSegmentOutputStreamForKey(event.getRoutingKey()); if (segmentWriter == null) { log.info("No writer for segment during resend."); unsent.addAll(selector.refreshSegmentEventWriters(segmentSealedCallBack)); sendFailed = true; } else { segmentWriter.write(event); } } } toResend = unsent; } }
private ByteBuffer writeInt(SegmentOutputStream stream, int value) throws SegmentSealedException { ByteBuffer buffer = ByteBuffer.allocate(4).putInt(value); buffer.flip(); stream.write(PendingEvent.withHeader(null, buffer, new CompletableFuture<Void>())); return buffer; }
@Override public void writeEvent(Type event) throws TxnFailedException { checkFailed(); ByteBuffer buffer = serializer.serialize(event); CompletableFuture<Void> ack = new CompletableFuture<Void>(); PendingEvent pendingEvent = PendingEvent.withHeader(null, buffer, ack); synchronized (lock) { out.write(pendingEvent); outstanding.addLast(ack); removeCompleted(); } checkFailed(); }
private CompletableFuture<Void> writeEventInternal(String routingKey, Type event) { Preconditions.checkNotNull(event); Exceptions.checkNotClosed(closed.get(), this); ByteBuffer data = serializer.serialize(event); CompletableFuture<Void> ackFuture = new CompletableFuture<Void>(); synchronized (writeFlushLock) { synchronized (writeSealLock) { 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(PendingEvent.withHeader(routingKey, data, ackFuture)); } } return ackFuture; }
@Override public void writeUnconditionally(T value) { CompletableFuture<Void> ack = new CompletableFuture<>(); ByteBuffer serialized = serializer.serialize(value); try { PendingEvent event = PendingEvent.withHeader(null, serialized, ack); log.trace("Unconditionally writing: {}", value); synchronized (lock) { out.write(event); out.flush(); } } catch (SegmentSealedException e) { throw new CorruptedStateException("Unexpected end of segment ", e); } Futures.getAndHandleExceptions(ack, RuntimeException::new); }
private void sendData(String data, SegmentOutputStream outputStream) { outputStream.write(PendingEvent.withHeader("routingKey", stringSerializer.serialize(data), new CompletableFuture<>())); }
@Test(timeout = 5000) public void testSegmentDoesNotExist() { UUID uuid = UUID.randomUUID(); SegmentOutputStream outputStream = Mockito.mock(SegmentOutputStream.class); SegmentTransactionImpl<String> txn = new SegmentTransactionImpl<>(uuid, outputStream, new JavaSerializer<String>()); Mockito.doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { PendingEvent event = (PendingEvent) invocation.getArgument(0); event.getAckFuture().completeExceptionally(new NoSuchSegmentException("segment")); return null; } }).when(outputStream).write(Mockito.any(PendingEvent.class)); AssertExtensions.assertThrows(TxnFailedException.class, () -> txn.writeEvent("hi")); verify(outputStream).write(Mockito.any(PendingEvent.class)); AssertExtensions.assertThrows(TxnFailedException.class, () -> txn.flush()); Mockito.verifyNoMoreInteractions(outputStream); } }
@Test(timeout = 5000) public void testFlush() throws TxnFailedException, SegmentSealedException { UUID uuid = UUID.randomUUID(); SegmentOutputStream outputStream = Mockito.mock(SegmentOutputStream.class); @Cleanup SegmentTransactionImpl<String> txn = new SegmentTransactionImpl<>(uuid, outputStream, new JavaSerializer<String>()); Mockito.doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { PendingEvent event = (PendingEvent) invocation.getArgument(0); event.getAckFuture().complete(null); return null; } }).when(outputStream).write(Mockito.any(PendingEvent.class)); txn.writeEvent("hi"); verify(outputStream).write(Mockito.any(PendingEvent.class)); txn.flush(); verify(outputStream).flush(); Mockito.verifyNoMoreInteractions(outputStream); }
out.write(PendingEvent.withHeader(null, ByteBuffer.wrap(testString.getBytes()), new CompletableFuture<>())); out.flush(); out.write(PendingEvent.withHeader(null, ByteBuffer.wrap(new byte[15]), new CompletableFuture<>())); out.write(PendingEvent.withHeader(null, ByteBuffer.wrap(new byte[15]), new CompletableFuture<>())); out.write(PendingEvent.withHeader(null, ByteBuffer.wrap(new byte[150000]), new CompletableFuture<>())); assertEquals(in.read().capacity(), 15); assertEquals(in.read().capacity(), 15);
SegmentOutputStream out = segmentClient.createOutputStreamForSegment(segment, segmentSealedCallback, EventWriterConfig.builder().build(), ""); CompletableFuture<Void> ack = new CompletableFuture<>(); out.write(PendingEvent.withoutHeader(null, ByteBuffer.wrap(payload), ack)); for (Channel c : clientCF.getActiveChannels()) { c.close(); out.write(PendingEvent.withoutHeader(null, ByteBuffer.wrap(payload), ack2)); ack.get(5, TimeUnit.SECONDS); ack2.get(5, TimeUnit.SECONDS);
@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(); TableStore tableStore = serviceBuilder.createTableStoreService(); @Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store, tableStore); server.startListening(); @Cleanup ConnectionFactory clientCF = new ConnectionFactoryImpl(ClientConfig.builder().build()); Controller controller = new MockController(endpoint, port, clientCF); controller.createScope(scope); controller.createStream(scope, stream, StreamConfiguration.builder().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<Void> ack = new CompletableFuture<>(); out.write(PendingEvent.withHeader(null, ByteBuffer.wrap(testString.getBytes()), ack)); ack.get(5, TimeUnit.SECONDS); }