Class SubscriptionState
java.lang.Object
org.apache.kafka.clients.consumer.internals.SubscriptionState
A class for tracking the topics, partitions, and offsets for the consumer. A partition
is "assigned" either directly with
assignFromUser(Set) (manual assignment)
or with assignFromSubscribed(Collection) (automatic assignment from subscription).
Once assigned, the partition is not considered "fetchable" until its initial position has
been set with seekValidated(TopicPartition, FetchPosition). Fetchable partitions
track a position which is the last offset that has been returned to the user. You can
suspend fetching from a partition through pause(TopicPartition) without affecting the consumed
position. The partition will remain unfetchable until the resume(TopicPartition) is
used. You can also query the pause state independently with isPaused(TopicPartition).
Note that pause state as well as the consumed positions are not preserved when partition assignment is changed whether directly by the user or through a group rebalance.
Thread Safety: this class is thread-safe.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRepresents the position of a partition subscription.static class -
Constructor Summary
ConstructorsConstructorDescriptionSubscriptionState(org.apache.kafka.common.utils.LogContext logContext, OffsetResetStrategy defaultResetStrategy) -
Method Summary
Modifier and TypeMethodDescriptionMap<org.apache.kafka.common.TopicPartition, OffsetAndMetadata> Set<org.apache.kafka.common.TopicPartition> List<org.apache.kafka.common.TopicPartition> voidassignFromSubscribed(Collection<org.apache.kafka.common.TopicPartition> assignments) Change the assignment to the specified partitions returned from the coordinator, note this is different fromassignFromUser(Set)which directly set the assignment from user inputs.booleanassignFromUser(Set<org.apache.kafka.common.TopicPartition> partitions) Change the assignment to the specified partitions provided by the user, note this is different fromassignFromSubscribed(Collection)whose input partitions are provided from the subscribed topics.booleanawaitingValidation(org.apache.kafka.common.TopicPartition tp) booleancheckAssignmentMatchedSubscription(Collection<org.apache.kafka.common.TopicPartition> assignments) clearPreferredReadReplica(org.apache.kafka.common.TopicPartition tp) Unset the preferred read replica.voidcompleteValidation(org.apache.kafka.common.TopicPartition tp) List<org.apache.kafka.common.TopicPartition> fetchablePartitions(Predicate<org.apache.kafka.common.TopicPartition> isAvailable) booleanbooleanbooleanbooleanhasValidPosition(org.apache.kafka.common.TopicPartition tp) Set<org.apache.kafka.common.TopicPartition> booleanisAssigned(org.apache.kafka.common.TopicPartition tp) booleanisOffsetResetNeeded(org.apache.kafka.common.TopicPartition partition) booleanisPaused(org.apache.kafka.common.TopicPartition tp) voidmarkPendingRevocation(Set<org.apache.kafka.common.TopicPartition> tps) maybeCompleteValidation(org.apache.kafka.common.TopicPartition tp, SubscriptionState.FetchPosition requestPosition, org.apache.kafka.common.message.OffsetForLeaderEpochResponseData.EpochEndOffset epochEndOffset) Attempt to complete validation with the end offset returned from the OffsetForLeaderEpoch request.booleanmaybeValidatePositionForCurrentLeader(org.apache.kafka.clients.ApiVersions apiVersions, org.apache.kafka.common.TopicPartition tp, org.apache.kafka.clients.Metadata.LeaderAndEpoch leaderAndEpoch) Enter the offset validation state if the leader for this partition is known to support a usable version of the OffsetsForLeaderEpoch API.partitionEndOffset(org.apache.kafka.common.TopicPartition tp, org.apache.kafka.common.IsolationLevel isolationLevel) booleanpartitionEndOffsetRequested(org.apache.kafka.common.TopicPartition tp) partitionLag(org.apache.kafka.common.TopicPartition tp, org.apache.kafka.common.IsolationLevel isolationLevel) Set<org.apache.kafka.common.TopicPartition> partitionsNeedingReset(long nowMs) Set<org.apache.kafka.common.TopicPartition> partitionsNeedingValidation(long nowMs) voidpause(org.apache.kafka.common.TopicPartition tp) Set<org.apache.kafka.common.TopicPartition> position(org.apache.kafka.common.TopicPartition tp) voidposition(org.apache.kafka.common.TopicPartition tp, SubscriptionState.FetchPosition position) preferredReadReplica(org.apache.kafka.common.TopicPartition tp, long timeMs) Get the preferred read replicavoidrequestOffsetReset(Collection<org.apache.kafka.common.TopicPartition> partitions, OffsetResetStrategy offsetResetStrategy) voidrequestOffsetReset(org.apache.kafka.common.TopicPartition partition) voidrequestOffsetReset(org.apache.kafka.common.TopicPartition partition, OffsetResetStrategy offsetResetStrategy) voidrequestPartitionEndOffset(org.apache.kafka.common.TopicPartition tp) voidresetStrategy(org.apache.kafka.common.TopicPartition partition) voidresume(org.apache.kafka.common.TopicPartition tp) voidseek(org.apache.kafka.common.TopicPartition tp, long offset) voidseekUnvalidated(org.apache.kafka.common.TopicPartition tp, SubscriptionState.FetchPosition position) voidseekValidated(org.apache.kafka.common.TopicPartition tp, SubscriptionState.FetchPosition position) voidsubscribe(Pattern pattern, Optional<ConsumerRebalanceListener> listener) booleansubscribe(Set<String> topics, Optional<ConsumerRebalanceListener> listener) booleansubscribeFromPattern(Set<String> topics) toString()voidvoidupdatePreferredReadReplica(org.apache.kafka.common.TopicPartition tp, int preferredReadReplicaId, LongSupplier timeMs) Set the preferred read replica with a lease timeout.validPosition(org.apache.kafka.common.TopicPartition tp)
-
Constructor Details
-
SubscriptionState
public SubscriptionState(org.apache.kafka.common.utils.LogContext logContext, OffsetResetStrategy defaultResetStrategy)
-
-
Method Details
-
toString
-
prettyString
-
subscribe
-
subscribe
-
subscribeFromPattern
-
assignFromUser
Change the assignment to the specified partitions provided by the user, note this is different fromassignFromSubscribed(Collection)whose input partitions are provided from the subscribed topics. -
checkAssignmentMatchedSubscription
public boolean checkAssignmentMatchedSubscription(Collection<org.apache.kafka.common.TopicPartition> assignments) - Returns:
- true if assignments matches subscription, otherwise false
-
assignFromSubscribed
Change the assignment to the specified partitions returned from the coordinator, note this is different fromassignFromUser(Set)which directly set the assignment from user inputs. -
hasNoSubscriptionOrUserAssignment
public boolean hasNoSubscriptionOrUserAssignment() -
unsubscribe
public void unsubscribe() -
subscription
-
pausedPartitions
-
seekValidated
public void seekValidated(org.apache.kafka.common.TopicPartition tp, SubscriptionState.FetchPosition position) -
seek
public void seek(org.apache.kafka.common.TopicPartition tp, long offset) -
seekUnvalidated
public void seekUnvalidated(org.apache.kafka.common.TopicPartition tp, SubscriptionState.FetchPosition position) -
assignedPartitions
- Returns:
- a modifiable copy of the currently assigned partitions
-
assignedPartitionsList
- Returns:
- a modifiable copy of the currently assigned partitions as a list
-
fetchablePartitions
-
hasAutoAssignedPartitions
public boolean hasAutoAssignedPartitions() -
position
public void position(org.apache.kafka.common.TopicPartition tp, SubscriptionState.FetchPosition position) -
maybeValidatePositionForCurrentLeader
public boolean maybeValidatePositionForCurrentLeader(org.apache.kafka.clients.ApiVersions apiVersions, org.apache.kafka.common.TopicPartition tp, org.apache.kafka.clients.Metadata.LeaderAndEpoch leaderAndEpoch) Enter the offset validation state if the leader for this partition is known to support a usable version of the OffsetsForLeaderEpoch API. If the leader node does not support the API, simply complete the offset validation.- Parameters:
apiVersions- supported API versionstp- topic partition to validateleaderAndEpoch- leader epoch of the topic partition- Returns:
- true if we enter the offset validation state
-
maybeCompleteValidation
public Optional<SubscriptionState.LogTruncation> maybeCompleteValidation(org.apache.kafka.common.TopicPartition tp, SubscriptionState.FetchPosition requestPosition, org.apache.kafka.common.message.OffsetForLeaderEpochResponseData.EpochEndOffset epochEndOffset) Attempt to complete validation with the end offset returned from the OffsetForLeaderEpoch request.- Returns:
- Log truncation details if detected and no reset policy is defined.
-
awaitingValidation
public boolean awaitingValidation(org.apache.kafka.common.TopicPartition tp) -
completeValidation
public void completeValidation(org.apache.kafka.common.TopicPartition tp) -
validPosition
-
position
-
partitionLag
public Long partitionLag(org.apache.kafka.common.TopicPartition tp, org.apache.kafka.common.IsolationLevel isolationLevel) -
partitionEndOffset
public Long partitionEndOffset(org.apache.kafka.common.TopicPartition tp, org.apache.kafka.common.IsolationLevel isolationLevel) -
requestPartitionEndOffset
public void requestPartitionEndOffset(org.apache.kafka.common.TopicPartition tp) -
partitionEndOffsetRequested
public boolean partitionEndOffsetRequested(org.apache.kafka.common.TopicPartition tp) -
updatePreferredReadReplica
public void updatePreferredReadReplica(org.apache.kafka.common.TopicPartition tp, int preferredReadReplicaId, LongSupplier timeMs) Set the preferred read replica with a lease timeout. After this time, the replica will no longer be valid andpreferredReadReplica(TopicPartition, long)will return an empty result.- Parameters:
tp- The topic partitionpreferredReadReplicaId- The preferred read replicatimeMs- The time at which this preferred replica is no longer valid
-
preferredReadReplica
public Optional<Integer> preferredReadReplica(org.apache.kafka.common.TopicPartition tp, long timeMs) Get the preferred read replica- Parameters:
tp- The topic partitiontimeMs- The current time- Returns:
- Returns the current preferred read replica, if it has been set and if it has not expired.
-
clearPreferredReadReplica
Unset the preferred read replica. This causes the fetcher to go back to the leader for fetches.- Parameters:
tp- The topic partition- Returns:
- the removed preferred read replica if set, None otherwise.
-
allConsumed
-
requestOffsetReset
public void requestOffsetReset(org.apache.kafka.common.TopicPartition partition, OffsetResetStrategy offsetResetStrategy) -
requestOffsetReset
public void requestOffsetReset(Collection<org.apache.kafka.common.TopicPartition> partitions, OffsetResetStrategy offsetResetStrategy) -
requestOffsetReset
public void requestOffsetReset(org.apache.kafka.common.TopicPartition partition) -
isOffsetResetNeeded
public boolean isOffsetResetNeeded(org.apache.kafka.common.TopicPartition partition) -
resetStrategy
-
hasAllFetchPositions
public boolean hasAllFetchPositions() -
initializingPartitions
-
resetInitializingPositions
public void resetInitializingPositions() -
partitionsNeedingReset
-
partitionsNeedingValidation
-
isAssigned
public boolean isAssigned(org.apache.kafka.common.TopicPartition tp) -
isPaused
public boolean isPaused(org.apache.kafka.common.TopicPartition tp) -
hasValidPosition
public boolean hasValidPosition(org.apache.kafka.common.TopicPartition tp) -
pause
public void pause(org.apache.kafka.common.TopicPartition tp) -
markPendingRevocation
-
resume
public void resume(org.apache.kafka.common.TopicPartition tp) -
rebalanceListener
-