@Override public boolean rejoinNeededOrPending() { if (!subscriptions.partitionsAutoAssigned()) return false; // we need to rejoin if we performed the assignment and metadata has changed if (assignmentSnapshot != null && !assignmentSnapshot.equals(metadataSnapshot)) return true; // we need to join if our subscription has changed since the last join if (joinedSubscription != null && !joinedSubscription.equals(subscriptions.subscription())) return true; return super.rejoinNeededOrPending(); }
/** * Change the assignment to the specified partitions returned from the coordinator, * note this is different from {@link #assignFromUser(Set)} which directly set the assignment from user inputs */ public void assignFromSubscribed(Collection<TopicPartition> assignments) { if (!this.partitionsAutoAssigned()) throw new IllegalArgumentException("Attempt to dynamically assign partitions while manual assignment in use"); Map<TopicPartition, TopicPartitionState> assignedPartitionStates = partitionToStateMap(assignments); fireOnAssignment(assignedPartitionStates.keySet()); if (this.subscribedPattern != null) { for (TopicPartition tp : assignments) { if (!this.subscribedPattern.matcher(tp.topic()).matches()) throw new IllegalArgumentException("Assigned partition " + tp + " for non-subscribed topic regex pattern; subscription pattern is " + this.subscribedPattern); } } else { for (TopicPartition tp : assignments) if (!this.subscription.contains(tp.topic())) throw new IllegalArgumentException("Assigned partition " + tp + " for non-subscribed topic; subscription is " + this.subscription); } this.assignment.set(assignedPartitionStates); }
@Override public void onMetadataUpdate(Cluster cluster, Set<String> unavailableTopics) { // if we encounter any unauthorized topics, raise an exception to the user if (!cluster.unauthorizedTopics().isEmpty()) throw new TopicAuthorizationException(new HashSet<>(cluster.unauthorizedTopics())); // if we encounter any invalid topics, raise an exception to the user if (!cluster.invalidTopics().isEmpty()) throw new InvalidTopicException(cluster.invalidTopics()); if (subscriptions.hasPatternSubscription()) updatePatternSubscription(cluster); // check if there are any changes to the metadata which should trigger a rebalance if (subscriptions.partitionsAutoAssigned()) { MetadataSnapshot snapshot = new MetadataSnapshot(subscriptions, cluster); if (!snapshot.equals(metadataSnapshot)) metadataSnapshot = snapshot; } if (!Collections.disjoint(metadata.topics(), unavailableTopics)) metadata.requestUpdate(); } });
if (subscriptions.partitionsAutoAssigned()) generation = generation(); else
@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()); }
invokeCompletedOffsetCommitCallbacks(); if (subscriptions.partitionsAutoAssigned()) {