/** * Poll for any network IO. * @param timer Timer bounding how long this method can block * @throws WakeupException if {@link #wakeup()} is called from another thread * @throws InterruptException if the calling thread is interrupted */ public void poll(Timer timer) { poll(timer, null); }
/** * Poll for any network IO. * @param timer Timer bounding how long this method can block * @param pollCondition Nullable blocking condition */ public void poll(Timer timer, PollCondition pollCondition) { poll(timer, pollCondition, false); }
/** * Poll for network IO and return immediately. This will not trigger wakeups. */ public void pollNoWakeup() { poll(time.timer(0), null, true); }
@Override public void run() { consumerClient.poll(future); } };
@Override public void run() { consumerClient.poll(future); } };
/** * Block indefinitely until the given request future has finished. * @param future The request future to await. * @throws WakeupException if {@link #wakeup()} is called from another thread * @throws InterruptException if the calling thread is interrupted */ public void poll(RequestFuture<?> future) { while (!future.isDone()) poll(time.timer(Long.MAX_VALUE), future); }
/** * Block until all pending requests from the given node have finished. * @param node The node to await requests from * @param timer Timer bounding how long this method can block * @return true If all requests finished, false if the timeout expired first */ public boolean awaitPendingRequests(Node node, Timer timer) { while (hasPendingRequests(node) && timer.notExpired()) { poll(timer); } return !hasPendingRequests(node); }
@Test public void blockWhenPollConditionNotSatisfied() { long timeout = 4000L; NetworkClient mockNetworkClient = mock(NetworkClient.class); ConsumerNetworkClient consumerClient = new ConsumerNetworkClient(new LogContext(), mockNetworkClient, metadata, time, 100, 1000, Integer.MAX_VALUE); when(mockNetworkClient.inFlightRequestCount()).thenReturn(1); consumerClient.poll(time.timer(timeout), () -> true); verify(mockNetworkClient).poll(eq(timeout), anyLong()); }
@Test public void blockOnlyForRetryBackoffIfNoInflightRequests() { long retryBackoffMs = 100L; NetworkClient mockNetworkClient = mock(NetworkClient.class); ConsumerNetworkClient consumerClient = new ConsumerNetworkClient(new LogContext(), mockNetworkClient, metadata, time, retryBackoffMs, 1000, Integer.MAX_VALUE); when(mockNetworkClient.inFlightRequestCount()).thenReturn(0); consumerClient.poll(time.timer(Long.MAX_VALUE), () -> true); verify(mockNetworkClient).poll(eq(retryBackoffMs), anyLong()); }
@Test public void doNotBlockIfPollConditionIsSatisfied() { NetworkClient mockNetworkClient = mock(NetworkClient.class); ConsumerNetworkClient consumerClient = new ConsumerNetworkClient(new LogContext(), mockNetworkClient, metadata, time, 100, 1000, Integer.MAX_VALUE); // expect poll, but with no timeout consumerClient.poll(time.timer(Long.MAX_VALUE), () -> false); verify(mockNetworkClient).poll(eq(0L), anyLong()); }
@Test public void testJoinGroupRequestMaxTimeout() { // Ensure we can handle the maximum allowed rebalance timeout setupCoordinator(RETRY_BACKOFF_MS, Integer.MAX_VALUE); mockClient.prepareResponse(groupCoordinatorResponse(node, Errors.NONE)); coordinator.ensureCoordinatorReady(mockTime.timer(0)); RequestFuture<ByteBuffer> future = coordinator.sendJoinGroupRequest(); assertFalse(consumerClient.poll(future, mockTime.timer(0))); mockTime.sleep(Integer.MAX_VALUE + 1L); assertTrue(consumerClient.poll(future, mockTime.timer(0))); }
@Test public void testInFlightFetchOnPausedPartition() { subscriptions.assignFromUser(singleton(tp0)); subscriptions.seek(tp0, 0); assertEquals(1, fetcher.sendFetches()); subscriptions.pause(tp0); client.prepareResponse(fullFetchResponse(tp0, this.records, Errors.NONE, 100L, 0)); consumerClient.poll(time.timer(0)); assertNull(fetcher.fetchedRecords().get(tp0)); }
private Map<TopicPartition, List<ConsumerRecord<byte[], byte[]>>> fetchRecords( TopicPartition tp, MemoryRecords records, Errors error, long hw, long lastStableOffset, int throttleTime) { assertEquals(1, fetcher.sendFetches()); client.prepareResponse(fullFetchResponse(tp, records, error, hw, lastStableOffset, throttleTime)); consumerClient.poll(time.timer(0)); return fetcher.fetchedRecords(); }
private Map<TopicPartition, List<ConsumerRecord<byte[], byte[]>>> fetchRecords( TopicPartition tp, MemoryRecords records, Errors error, long hw, long lastStableOffset, long logStartOffset, int throttleTime) { assertEquals(1, fetcher.sendFetches()); client.prepareResponse(fetchResponse(tp, records, error, hw, lastStableOffset, logStartOffset, throttleTime)); consumerClient.poll(time.timer(0)); return fetcher.fetchedRecords(); }
@Test public void testJoinGroupRequestTimeout() { setupCoordinator(RETRY_BACKOFF_MS, REBALANCE_TIMEOUT_MS); mockClient.prepareResponse(groupCoordinatorResponse(node, Errors.NONE)); coordinator.ensureCoordinatorReady(mockTime.timer(0)); RequestFuture<ByteBuffer> future = coordinator.sendJoinGroupRequest(); mockTime.sleep(REQUEST_TIMEOUT_MS + 1); assertFalse(consumerClient.poll(future, mockTime.timer(0))); mockTime.sleep(REBALANCE_TIMEOUT_MS - REQUEST_TIMEOUT_MS + 5000); assertTrue(consumerClient.poll(future, mockTime.timer(0))); }
private void makeFetchRequestWithIncompleteRecord() { subscriptions.assignFromUser(singleton(tp0)); subscriptions.seek(tp0, 0); assertEquals(1, fetcher.sendFetches()); assertFalse(fetcher.hasCompletedFetches()); MemoryRecords partialRecord = MemoryRecords.readableRecords( ByteBuffer.wrap(new byte[]{0, 0, 0, 0, 0, 0, 0, 0})); client.prepareResponse(fullFetchResponse(tp0, partialRecord, Errors.NONE, 100L, 0)); consumerClient.poll(time.timer(0)); assertTrue(fetcher.hasCompletedFetches()); }
@Test public void testFetchNotLeaderForPartition() { subscriptions.assignFromUser(singleton(tp0)); subscriptions.seek(tp0, 0); assertEquals(1, fetcher.sendFetches()); client.prepareResponse(fullFetchResponse(tp0, this.records, Errors.NOT_LEADER_FOR_PARTITION, 100L, 0)); consumerClient.poll(time.timer(0)); assertEquals(0, fetcher.fetchedRecords().size()); assertEquals(0L, metadata.timeToNextUpdate(time.milliseconds())); }
@Test public void testFetchedRecordsAfterSeek() { subscriptionsNoAutoReset.assignFromUser(singleton(tp0)); subscriptionsNoAutoReset.seek(tp0, 0); assertTrue(fetcherNoAutoReset.sendFetches() > 0); client.prepareResponse(fullFetchResponse(tp0, this.records, Errors.OFFSET_OUT_OF_RANGE, 100L, 0)); consumerClient.poll(time.timer(0)); assertFalse(subscriptionsNoAutoReset.isOffsetResetNeeded(tp0)); subscriptionsNoAutoReset.seek(tp0, 2); assertEquals(0, fetcherNoAutoReset.fetchedRecords().size()); }
@Test public void testFetchOffsetOutOfRange() { subscriptions.assignFromUser(singleton(tp0)); subscriptions.seek(tp0, 0); assertEquals(1, fetcher.sendFetches()); client.prepareResponse(fullFetchResponse(tp0, this.records, Errors.OFFSET_OUT_OF_RANGE, 100L, 0)); consumerClient.poll(time.timer(0)); assertEquals(0, fetcher.fetchedRecords().size()); assertTrue(subscriptions.isOffsetResetNeeded(tp0)); assertEquals(null, subscriptions.position(tp0)); }
private void makeCoordinatorUnknown(ConsumerCoordinator coordinator, Errors error) { time.sleep(sessionTimeoutMs); coordinator.sendHeartbeatRequest(); client.prepareResponse(heartbeatResponse(error)); time.sleep(sessionTimeoutMs); consumerClient.poll(time.timer(0)); assertTrue(coordinator.coordinatorUnknown()); }