boolean shouldResume() { // simple logic for now - from the resume docs: "If the partitions were not previously paused, this method is a no-op." return !isPaused(); }
Collection<TopicPartition> partitionsToBePaused() { List<TopicPartition> pausedPartitions = new ArrayList<>(); processors.forEach((key, processor) -> { if (processor.isPaused()) { pausedPartitions.add(key); } }); return pausedPartitions; }
@Test public void basicLifecycle() throws InterruptedException { PartitionProcessor processor = givenAPartionProcessor(); assertFalse(processor.isTerminated()); assertFalse(processor.isPaused()); assertTrue(processor.shouldResume()); assertEquals(0, processor.numberOfUnprocessedMessages()); assertFalse(processor.hasUncommittedMessages()); assertEquals(-1, processor.getCommitOffsetAndClear()); processor.waitForHandlersToTerminate(1); assertTrue(processor.isTerminated()); assertEquals(0, processor.numberOfUnprocessedMessages()); assertFalse(processor.hasUncommittedMessages()); assertEquals(-1, processor.getCommitOffsetAndClear()); }
@Test public void pauseProcessorIfBacklogOfUnconsumedMessagesToBig() throws InterruptedException { PartitionProcessor processor = givenAPartionProcessor(); for (int i = 1; i <= PartitionProcessor.MAX_MESSAGES_IN_FLIGHT + 2; i++) { ConsumerRecord<String, byte[]> record = testRecordWithOffset(i); processor.enqueue(record); } while (processor.numberOfUnprocessedMessages() < PartitionProcessor.MAX_MESSAGES_IN_FLIGHT + 1) { shortSleep(); // allow to fill queue } assertEquals(PartitionProcessor.MAX_MESSAGES_IN_FLIGHT + 1, processor.numberOfUnprocessedMessages()); // 1 message currently in handler assertTrue(processor.isPaused()); // should throttle assertFalse(processor.shouldResume()); (getTestHandler(processor)).onMessageCalled.await(); (getTestHandler(processor)).blockReturnFromOnMessage.countDown(); shortSleep(); // allow handler to continue assertTrue(processor.hasUncommittedMessages()); assertEquals(PartitionProcessor.MAX_MESSAGES_IN_FLIGHT + 3, processor.getCommitOffsetAndClear()); assertFalse(processor.isPaused()); // after emptying the queue, processor should resume assertTrue(processor.shouldResume()); processor.waitForHandlersToTerminate(50); }