List of usage examples for java.util.concurrent CompletableFuture complete
public boolean complete(T value)
From source file:org.apache.pulsar.functions.instance.JavaInstanceRunnableProcessTest.java
@Test public void testAtLeastOnceProcessing() throws Exception { FunctionDetails newFunctionDetails = FunctionDetails.newBuilder(functionDetails) .setProcessingGuarantees(ProcessingGuarantees.ATLEAST_ONCE).build(); config.setFunctionDetails(newFunctionDetails); @Cleanup("shutdown") ExecutorService executorService = Executors.newSingleThreadExecutor(); try (JavaInstanceRunnable runnable = new JavaInstanceRunnable(config, fnCache, "test-jar-file", mockClient, null)) {//from w w w. j av a 2 s . co m executorService.submit(runnable); Pair<String, String> consumerId = Pair.of(newFunctionDetails.getInputs(0), FunctionDetailsUtils.getFullyQualifiedName(newFunctionDetails)); ConsumerInstance consumerInstance = mockConsumers.get(consumerId); while (null == consumerInstance) { TimeUnit.MILLISECONDS.sleep(20); consumerInstance = mockConsumers.get(consumerId); } ProducerInstance producerInstance = mockProducers.values().iterator().next(); // once we get consumer id, simulate receiving 10 messages from consumer for (int i = 0; i < 10; i++) { Message msg = mock(Message.class); when(msg.getData()).thenReturn(("message-" + i).getBytes(UTF_8)); when(msg.getMessageId()).thenReturn(new MessageIdImpl(1L, i, 0)); consumerInstance.addMessage(msg); consumerInstance.getConf().getMessageListener().received(consumerInstance.getConsumer(), msg); } // wait until all the messages are published for (int i = 0; i < 10; i++) { Message msg = producerInstance.msgQueue.take(); assertEquals("message-" + i + "!", new String(msg.getData(), UTF_8)); // sequence id is not set for AT_MOST_ONCE processing assertEquals(0L, msg.getSequenceId()); } // verify acknowledge before send completes verify(consumerInstance.getConsumer(), times(0)).acknowledgeAsync(any(Message.class)); assertEquals(10, consumerInstance.getNumMessages()); // complete all the publishes synchronized (producerInstance) { for (CompletableFuture<MessageId> future : producerInstance.sendFutures) { future.complete(mock(MessageId.class)); } } // acknowledges count should remain same verify(consumerInstance.getConsumer(), times(10)).acknowledgeAsync(any(Message.class)); assertEquals(0, consumerInstance.getNumMessages()); } }
From source file:org.apache.pulsar.functions.instance.JavaInstanceRunnableProcessTest.java
@Test public void testEffectivelyOnceProcessing() throws Exception { FunctionDetails newFunctionDetails = FunctionDetails.newBuilder(functionDetails) .setProcessingGuarantees(ProcessingGuarantees.EFFECTIVELY_ONCE).build(); config.setFunctionDetails(newFunctionDetails); @Cleanup("shutdown") ExecutorService executorService = Executors.newSingleThreadExecutor(); try (JavaInstanceRunnable runnable = new JavaInstanceRunnable(config, fnCache, "test-jar-file", mockClient, null)) {//from www . java 2s . com executorService.submit(runnable); Pair<String, String> consumerId = Pair.of(newFunctionDetails.getInputs(0), FunctionDetailsUtils.getFullyQualifiedName(newFunctionDetails)); ConsumerInstance consumerInstance = mockConsumers.get(consumerId); while (null == consumerInstance) { TimeUnit.MILLISECONDS.sleep(20); consumerInstance = mockConsumers.get(consumerId); } // once we get consumer id, simulate receiving 10 messages from consumer for (int i = 0; i < 10; i++) { Message msg = mock(Message.class); when(msg.getData()).thenReturn(("message-" + i).getBytes(UTF_8)); when(msg.getMessageId()).thenReturn(new MessageIdImpl(1L, i, 0)); consumerInstance.addMessage(msg); consumerInstance.getConf().getMessageListener().received(consumerInstance.getConsumer(), msg); } ProducerInstance producerInstance; while (mockProducers.isEmpty()) { TimeUnit.MILLISECONDS.sleep(20); } producerInstance = mockProducers.values().iterator().next(); // wait until all the messages are published for (int i = 0; i < 10; i++) { Message msg = producerInstance.msgQueue.take(); assertEquals("message-" + i + "!", new String(msg.getData(), UTF_8)); // sequence id is not set for AT_MOST_ONCE processing assertEquals(Utils.getSequenceId(new MessageIdImpl(1L, i, 0)), msg.getSequenceId()); } // verify acknowledge before send completes verify(consumerInstance.getConsumer(), times(0)).acknowledgeCumulativeAsync(any(Message.class)); assertEquals(10, consumerInstance.getNumMessages()); // complete all the publishes synchronized (producerInstance) { for (CompletableFuture<MessageId> future : producerInstance.sendFutures) { future.complete(mock(MessageId.class)); } } // acknowledges count should remain same verify(consumerInstance.getConsumer(), times(10)).acknowledgeCumulativeAsync(any(Message.class)); assertEquals(0, consumerInstance.getNumMessages()); } }
From source file:org.apache.pulsar.functions.instance.JavaInstanceRunnableProcessTest.java
@Test public void testAtMostOnceProcessingFailures() throws Exception { FunctionDetails newFunctionDetails = FunctionDetails.newBuilder(functionDetails) .setProcessingGuarantees(ProcessingGuarantees.ATMOST_ONCE) .setClassName(TestFailureFunction.class.getName()).build(); config.setFunctionDetails(newFunctionDetails); @Cleanup("shutdown") ExecutorService executorService = Executors.newSingleThreadExecutor(); try (JavaInstanceRunnable runnable = new JavaInstanceRunnable(config, fnCache, "test-jar-file", mockClient, null)) {/*from w w w. java2 s .c o m*/ executorService.submit(runnable); Pair<String, String> consumerId = Pair.of(newFunctionDetails.getInputs(0), FunctionDetailsUtils.getFullyQualifiedName(newFunctionDetails)); ConsumerInstance consumerInstance = mockConsumers.get(consumerId); while (null == consumerInstance) { TimeUnit.MILLISECONDS.sleep(20); consumerInstance = mockConsumers.get(consumerId); } ProducerInstance producerInstance = mockProducers.values().iterator().next(); // once we get consumer id, simulate receiving 10 messages from consumer for (int i = 0; i < 10; i++) { Message msg = mock(Message.class); when(msg.getData()).thenReturn(("message-" + i).getBytes(UTF_8)); when(msg.getMessageId()).thenReturn(new MessageIdImpl(1L, i, 0)); consumerInstance.addMessage(msg); consumerInstance.getConf().getMessageListener().received(consumerInstance.getConsumer(), msg); } // wait until all the messages are published for (int i = 0; i < 10; i++) { if (i % 2 == 0) { // all messages (i % 2 == 0) will fail to process. continue; } Message msg = producerInstance.msgQueue.take(); assertEquals("message-" + i + "!", new String(msg.getData(), UTF_8)); // sequence id is not set for AT_MOST_ONCE processing assertEquals(0L, msg.getSequenceId()); } // verify acknowledge before send completes verify(consumerInstance.getConsumer(), times(10)).acknowledgeAsync(any(Message.class)); assertEquals(0, consumerInstance.getNumMessages()); // complete all the publishes synchronized (producerInstance) { for (CompletableFuture<MessageId> future : producerInstance.sendFutures) { future.complete(mock(MessageId.class)); } } // acknowledges count should remain same verify(consumerInstance.getConsumer(), times(10)).acknowledgeAsync(any(Message.class)); assertEquals(0, consumerInstance.getNumMessages()); } }
From source file:org.apache.pulsar.client.impl.ClientCnx.java
@Override protected void handleGetTopicsOfNamespaceSuccess(CommandGetTopicsOfNamespaceResponse success) { checkArgument(state == State.Ready); long requestId = success.getRequestId(); List<String> topics = success.getTopicsList(); if (log.isDebugEnabled()) { log.debug("{} Received get topics of namespace success response from server: {} - topics.size: {}", ctx.channel(), success.getRequestId(), topics.size()); }//w ww . j ava 2 s .c o m CompletableFuture<List<String>> requestFuture = pendingGetTopicsRequests.remove(requestId); if (requestFuture != null) { requestFuture.complete(topics); } else { log.warn("{} Received unknown request id from server: {}", ctx.channel(), success.getRequestId()); } }
From source file:org.apache.pulsar.functions.instance.JavaInstanceRunnableProcessTest.java
@Test public void testAtLeastOnceProcessingFailures() throws Exception { FunctionDetails newFunctionDetails = FunctionDetails.newBuilder(functionDetails) .setProcessingGuarantees(ProcessingGuarantees.ATLEAST_ONCE) .setClassName(TestFailureFunction.class.getName()).build(); config.setFunctionDetails(newFunctionDetails); @Cleanup("shutdown") ExecutorService executorService = Executors.newSingleThreadExecutor(); try (JavaInstanceRunnable runnable = new JavaInstanceRunnable(config, fnCache, "test-jar-file", mockClient, null)) {//from www .java 2 s . c om executorService.submit(runnable); Pair<String, String> consumerId = Pair.of(newFunctionDetails.getInputs(0), FunctionDetailsUtils.getFullyQualifiedName(newFunctionDetails)); ConsumerInstance consumerInstance = mockConsumers.get(consumerId); while (null == consumerInstance) { TimeUnit.MILLISECONDS.sleep(20); consumerInstance = mockConsumers.get(consumerId); } ProducerInstance producerInstance = mockProducers.values().iterator().next(); // once we get consumer id, simulate receiving 10 messages from consumer for (int i = 0; i < 10; i++) { Message msg = mock(Message.class); when(msg.getData()).thenReturn(("message-" + i).getBytes(UTF_8)); when(msg.getMessageId()).thenReturn(new MessageIdImpl(1L, i, 0)); consumerInstance.addMessage(msg); consumerInstance.getConf().getMessageListener().received(consumerInstance.getConsumer(), msg); } // wait until all the messages are published for (int i = 0; i < 10; i++) { if (i % 2 == 0) { // all messages (i % 2 == 0) will fail to process. continue; } Message msg = producerInstance.msgQueue.take(); assertEquals("message-" + i + "!", new String(msg.getData(), UTF_8)); // sequence id is not set for AT_MOST_ONCE processing assertEquals(0L, msg.getSequenceId()); } // verify acknowledge before send completes verify(consumerInstance.getConsumer(), times(0)).acknowledgeAsync(any(Message.class)); assertEquals(10, consumerInstance.getNumMessages()); // complete all the publishes synchronized (producerInstance) { for (CompletableFuture<MessageId> future : producerInstance.sendFutures) { future.complete(mock(MessageId.class)); } } // only 5 succeed messages are acknowledged verify(consumerInstance.getConsumer(), times(5)).acknowledgeAsync(any(Message.class)); assertEquals(5, consumerInstance.getNumMessages()); for (int i = 0; i < 10; i++) { assertEquals(i % 2 == 0, consumerInstance.containMessage(new MessageIdImpl(1L, i, 0))); } } }
From source file:org.apache.pulsar.client.impl.ClientCnx.java
@Override protected void handleGetLastMessageIdSuccess(CommandGetLastMessageIdResponse success) { checkArgument(state == State.Ready); if (log.isDebugEnabled()) { log.debug("{} Received success GetLastMessageId response from server: {}", ctx.channel(), success.getRequestId()); }// w w w . ja v a 2 s .co m long requestId = success.getRequestId(); CompletableFuture<MessageIdData> requestFuture = pendingGetLastMessageIdRequests.remove(requestId); if (requestFuture != null) { requestFuture.complete(success.getLastMessageId()); } else { log.warn("{} Received unknown request id from server: {}", ctx.channel(), success.getRequestId()); } }
From source file:io.pravega.client.stream.mock.MockController.java
private CompletableFuture<Void> createSegmentTx(UUID txId, Segment segment) { CompletableFuture<Void> result = new CompletableFuture<>(); FailingReplyProcessor replyProcessor = new FailingReplyProcessor() { @Override// w ww. j a v a2 s . c o m public void connectionDropped() { result.completeExceptionally(new ConnectionClosedException()); } @Override public void wrongHost(WrongHost wrongHost) { result.completeExceptionally(new NotImplementedException()); } @Override public void transactionCreated(TransactionCreated transactionCreated) { result.complete(null); } @Override public void processingFailure(Exception error) { result.completeExceptionally(error); } }; sendRequestOverNewConnection(new CreateTransaction(idGenerator.get(), segment.getScopedName(), txId), replyProcessor, result); return result; }
From source file:org.apache.pulsar.functions.runtime.ProcessRuntime.java
@Override public CompletableFuture<Void> resetMetrics() { CompletableFuture<Void> retval = new CompletableFuture<>(); if (stub == null) { retval.completeExceptionally(new RuntimeException("Not alive")); return retval; }//from www . j a v a2 s . c om ListenableFuture<Empty> response = stub.withDeadlineAfter(GRPC_TIMEOUT_SECS, TimeUnit.SECONDS) .resetMetrics(Empty.newBuilder().build()); Futures.addCallback(response, new FutureCallback<Empty>() { @Override public void onFailure(Throwable throwable) { retval.completeExceptionally(throwable); } @Override public void onSuccess(Empty t) { retval.complete(null); } }); return retval; }
From source file:org.apache.pulsar.functions.instance.JavaInstanceRunnableProcessTest.java
@Test public void testEffectivelyOnceProcessingFailures() throws Exception { FunctionDetails newFunctionDetails = FunctionDetails.newBuilder(functionDetails) .setProcessingGuarantees(ProcessingGuarantees.EFFECTIVELY_ONCE) .setClassName(TestFailureFunction.class.getName()).build(); config.setFunctionDetails(newFunctionDetails); @Cleanup("shutdown") ExecutorService executorService = Executors.newSingleThreadExecutor(); try (JavaInstanceRunnable runnable = new JavaInstanceRunnable(config, fnCache, "test-jar-file", mockClient, null)) {/*from w w w. j a v a2 s. com*/ executorService.submit(runnable); Pair<String, String> consumerId = Pair.of(newFunctionDetails.getInputs(0), FunctionDetailsUtils.getFullyQualifiedName(newFunctionDetails)); ConsumerInstance consumerInstance = mockConsumers.get(consumerId); while (null == consumerInstance) { TimeUnit.MILLISECONDS.sleep(20); consumerInstance = mockConsumers.get(consumerId); } // once we get consumer id, simulate receiving 2 messages from consumer Message[] msgs = new Message[2]; for (int i = 1; i <= 2; i++) { Message msg = mock(Message.class); when(msg.getData()).thenReturn(("message-" + i).getBytes(UTF_8)); when(msg.getMessageId()).thenReturn(new MessageIdImpl(1L, i, 0)); msgs[i - 1] = msg; consumerInstance.addMessage(msg); consumerInstance.getConf().getMessageListener().received(consumerInstance.getConsumer(), msg); } ProducerInstance producerInstance; while (mockProducers.isEmpty()) { TimeUnit.MILLISECONDS.sleep(20); } producerInstance = mockProducers.values().iterator().next(); // only first message is published, the second message is not Message msg = producerInstance.msgQueue.take(); assertEquals("message-1!", new String(msg.getData(), UTF_8)); assertEquals(Utils.getSequenceId(new MessageIdImpl(1L, 1, 0)), msg.getSequenceId()); assertNull(producerInstance.msgQueue.poll()); // the first result message is sent but the send future is not completed yet // so no acknowledge would happen verify(consumerInstance.getConsumer(), times(0)).acknowledgeCumulativeAsync(any(Message.class)); // since the second message failed to process, for correctness, the instance // will close the existing consumer and resubscribe ConsumerInstance secondInstance = mockConsumers.get(consumerId); while (null == secondInstance || secondInstance == consumerInstance) { TimeUnit.MILLISECONDS.sleep(20); secondInstance = mockConsumers.get(consumerId); } Message secondMsg = mock(Message.class); when(secondMsg.getData()).thenReturn("message-2".getBytes(UTF_8)); when(secondMsg.getMessageId()).thenReturn(new MessageIdImpl(1L, 2, 0)); secondInstance.addMessage(secondMsg); secondInstance.getConf().getMessageListener().received(secondInstance.getConsumer(), secondMsg); Message secondReceivedMsg = producerInstance.msgQueue.take(); assertEquals("message-2!", new String(secondReceivedMsg.getData(), UTF_8)); assertEquals(Utils.getSequenceId(new MessageIdImpl(1L, 2, 0)), secondReceivedMsg.getSequenceId()); // the first result message is sent verify(secondInstance.getConsumer(), times(0)).acknowledgeCumulativeAsync(any(Message.class)); // complete all the publishes synchronized (producerInstance) { assertEquals(2, producerInstance.sendFutures.size()); for (CompletableFuture<MessageId> future : producerInstance.sendFutures) { future.complete(mock(MessageId.class)); } } // all 2 messages are sent verify(consumerInstance.getConsumer(), times(1)).acknowledgeCumulativeAsync(same(msgs[0])); verify(consumerInstance.getConsumer(), times(0)).acknowledgeCumulativeAsync(same(msgs[1])); verify(consumerInstance.getConsumer(), times(0)).acknowledgeCumulativeAsync(same(secondMsg)); verify(secondInstance.getConsumer(), times(0)).acknowledgeCumulativeAsync(same(msgs[0])); verify(secondInstance.getConsumer(), times(0)).acknowledgeCumulativeAsync(same(msgs[1])); } }
From source file:io.pravega.client.stream.mock.MockController.java
private CompletableFuture<Void> commitTxSegment(UUID txId, Segment segment) { CompletableFuture<Void> result = new CompletableFuture<>(); FailingReplyProcessor replyProcessor = new FailingReplyProcessor() { @Override//from www. ja v a2s .co m public void connectionDropped() { result.completeExceptionally(new ConnectionClosedException()); } @Override public void wrongHost(WrongHost wrongHost) { result.completeExceptionally(new NotImplementedException()); } @Override public void transactionCommitted(TransactionCommitted transactionCommitted) { result.complete(null); } @Override public void transactionAborted(TransactionAborted transactionAborted) { result.completeExceptionally(new TxnFailedException("Transaction already aborted.")); } @Override public void processingFailure(Exception error) { result.completeExceptionally(error); } }; sendRequestOverNewConnection(new CommitTransaction(idGenerator.get(), segment.getScopedName(), txId), replyProcessor, result); return result; }