public boolean isPaused(TopicPartition tp) { return isAssigned(tp) && assignedState(tp).paused; }
public boolean hasValidPosition(TopicPartition tp) { return isAssigned(tp) && assignedState(tp).hasValidPosition(); }
public boolean isFetchable(TopicPartition tp) { return isAssigned(tp) && assignedState(tp).isFetchable(); }
@Override public synchronized OffsetAndMetadata committed(TopicPartition partition) { ensureNotClosed(); if (subscriptions.isAssigned(partition)) { return committed.get(partition); } return new OffsetAndMetadata(0); }
private void resetOffsetIfNeeded(TopicPartition partition, Long requestedResetTimestamp, OffsetData offsetData) { // we might lose the assignment while fetching the offset, or the user might seek to a different offset, // so verify it is still assigned and still in need of the requested reset if (!subscriptions.isAssigned(partition)) { log.debug("Skipping reset of partition {} since it is no longer assigned", partition); } else if (!subscriptions.isOffsetResetNeeded(partition)) { log.debug("Skipping reset of partition {} since reset is no longer needed", partition); } else if (!requestedResetTimestamp.equals(offsetResetStrategyTimestamp(partition))) { log.debug("Skipping reset of partition {} since an alternative reset has been requested", partition); } else { log.info("Resetting offset for partition {} to offset {}.", partition, offsetData.offset); offsetData.leaderEpoch.ifPresent(epoch -> metadata.updateLastSeenEpochIfNewer(partition, epoch)); subscriptions.seek(partition, offsetData.offset); } }
@Override public synchronized long position(TopicPartition partition) { ensureNotClosed(); if (!this.subscriptions.isAssigned(partition)) throw new IllegalArgumentException("You can only check the position for partitions assigned to this consumer."); Long offset = this.subscriptions.position(partition); if (offset == null) { updateFetchPosition(partition); offset = this.subscriptions.position(partition); } return offset; }
private List<ConsumerRecord<K, V>> fetchRecords(PartitionRecords partitionRecords, int maxRecords) { if (!subscriptions.isAssigned(partitionRecords.partition)) {
acquireAndEnsureOpen(); try { if (!this.subscriptions.isAssigned(partition)) throw new IllegalStateException("You can only check the position for partitions assigned to this consumer.");
@Test public void topicSubscription() { state.subscribe(singleton(topic), rebalanceListener); assertEquals(1, state.subscription().size()); assertTrue(state.assignedPartitions().isEmpty()); assertEquals(0, state.numAssignedPartitions()); assertTrue(state.partitionsAutoAssigned()); state.assignFromSubscribed(singleton(tp0)); state.seek(tp0, 1); assertEquals(1L, state.position(tp0).longValue()); state.assignFromSubscribed(singleton(tp1)); assertTrue(state.isAssigned(tp1)); assertFalse(state.isAssigned(tp0)); assertFalse(state.isFetchable(tp1)); assertEquals(singleton(tp1), state.assignedPartitions()); assertEquals(1, state.numAssignedPartitions()); }
@Test public void partitionAssignment() { state.assignFromUser(singleton(tp0)); assertEquals(singleton(tp0), state.assignedPartitions()); assertEquals(1, state.numAssignedPartitions()); assertFalse(state.hasAllFetchPositions()); state.seek(tp0, 1); assertTrue(state.isFetchable(tp0)); assertEquals(1L, state.position(tp0).longValue()); state.assignFromUser(Collections.<TopicPartition>emptySet()); assertTrue(state.assignedPartitions().isEmpty()); assertEquals(0, state.numAssignedPartitions()); assertFalse(state.isAssigned(tp0)); assertFalse(state.isFetchable(tp0)); }
@Test public void testAssignmentChangeWithInFlightReset() { subscriptions.assignFromUser(singleton(tp0)); subscriptions.requestOffsetReset(tp0, OffsetResetStrategy.LATEST); // Send the ListOffsets request to reset the position fetcher.resetOffsetsIfNeeded(); consumerClient.pollNoWakeup(); assertFalse(subscriptions.hasValidPosition(tp0)); assertTrue(client.hasInFlightRequests()); // Now we have an assignment change subscriptions.assignFromUser(singleton(tp1)); // The response returns and is discarded client.respond(listOffsetResponse(Errors.NONE, 1L, 5L)); consumerClient.pollNoWakeup(); assertFalse(client.hasPendingResponses()); assertFalse(client.hasInFlightRequests()); assertFalse(subscriptions.isAssigned(tp0)); }