-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-8449: Restart tasks on reconfiguration under incremental cooperative rebalancing #6850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,6 +80,8 @@ | |
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static org.apache.kafka.connect.runtime.distributed.ConnectProtocol.CONNECT_PROTOCOL_V0; | ||
|
|
||
|
|
@@ -141,6 +143,7 @@ public class DistributedHerder extends AbstractHerder implements Runnable { | |
| // and the from other nodes are safe to process | ||
| private boolean rebalanceResolved; | ||
| private ExtendedAssignment runningAssignment = ExtendedAssignment.empty(); | ||
| private Set<ConnectorTaskId> tasksToRestart = new HashSet<>(); | ||
| private ExtendedAssignment assignment; | ||
| private boolean canReadConfigs; | ||
| private ClusterConfigState configState; | ||
|
|
@@ -151,6 +154,7 @@ public class DistributedHerder extends AbstractHerder implements Runnable { | |
| // Config updates can be collected and applied together when possible. Also, we need to take care to rebalance when | ||
| // needed (e.g. task reconfiguration, which requires everyone to coordinate offset commits). | ||
| private Set<String> connectorConfigUpdates = new HashSet<>(); | ||
| private Set<ConnectorTaskId> taskConfigUpdates = new HashSet<>(); | ||
| // Similarly collect target state changes (when observed by the config storage listener) for handling in the | ||
| // herder's main thread. | ||
| private Set<String> connectorTargetStateChanges = new HashSet<>(); | ||
|
|
@@ -304,51 +308,47 @@ public void tick() { | |
| } | ||
|
|
||
| // Process any configuration updates | ||
| Set<String> connectorConfigUpdatesCopy = null; | ||
| Set<String> connectorTargetStateChangesCopy = null; | ||
| synchronized (this) { | ||
| if (needsReconfigRebalance || !connectorConfigUpdates.isEmpty() || !connectorTargetStateChanges.isEmpty()) { | ||
| // Connector reconfigs only need local updates since there is no coordination between workers required. | ||
| // However, if connectors were added or removed, work needs to be rebalanced since we have more work | ||
| // items to distribute among workers. | ||
| configState = configBackingStore.snapshot(); | ||
|
|
||
| if (needsReconfigRebalance) { | ||
| // Task reconfigs require a rebalance. Request the rebalance, clean out state, and then restart | ||
| // this loop, which will then ensure the rebalance occurs without any other requests being | ||
| // processed until it completes. | ||
| member.requestRejoin(); | ||
| // Any connector config updates or target state changes will be addressed during the rebalance too | ||
| connectorConfigUpdates.clear(); | ||
| connectorTargetStateChanges.clear(); | ||
| needsReconfigRebalance = false; | ||
| log.debug("Requesting rebalance due to reconfiguration of tasks (needsReconfigRebalance: {})", | ||
| needsReconfigRebalance); | ||
| return; | ||
| } else { | ||
| if (!connectorConfigUpdates.isEmpty()) { | ||
| // We can't start/stop while locked since starting connectors can cause task updates that will | ||
| // require writing configs, which in turn make callbacks into this class from another thread that | ||
| // require acquiring a lock. This leads to deadlock. Instead, just copy the info we need and process | ||
| // the updates after unlocking. | ||
| connectorConfigUpdatesCopy = connectorConfigUpdates; | ||
| connectorConfigUpdates = new HashSet<>(); | ||
| } | ||
| AtomicReference<Set<String>> connectorConfigUpdatesCopy = new AtomicReference<>(); | ||
| AtomicReference<Set<String>> connectorTargetStateChangesCopy = new AtomicReference<>(); | ||
| AtomicReference<Set<ConnectorTaskId>> taskConfigUpdatesCopy = new AtomicReference<>(); | ||
|
|
||
| boolean shouldReturn; | ||
| if (member.currentProtocolVersion() == CONNECT_PROTOCOL_V0) { | ||
| shouldReturn = updateConfigsWithEager(connectorConfigUpdatesCopy, | ||
| connectorTargetStateChangesCopy); | ||
| // With eager protocol we should return immediately if needsReconfigRebalance has | ||
| // been set to retain the old workflow | ||
| if (shouldReturn) { | ||
| return; | ||
| } | ||
|
|
||
| if (!connectorTargetStateChanges.isEmpty()) { | ||
| // Similarly for target state changes which can cause connectors to be restarted | ||
| connectorTargetStateChangesCopy = connectorTargetStateChanges; | ||
| connectorTargetStateChanges = new HashSet<>(); | ||
| } | ||
| } | ||
| if (connectorConfigUpdatesCopy.get() != null) { | ||
| processConnectorConfigUpdates(connectorConfigUpdatesCopy.get()); | ||
| } | ||
| } | ||
|
|
||
| if (connectorConfigUpdatesCopy != null) | ||
| processConnectorConfigUpdates(connectorConfigUpdatesCopy); | ||
| if (connectorTargetStateChangesCopy.get() != null) { | ||
| processTargetStateChanges(connectorTargetStateChangesCopy.get()); | ||
| } | ||
| } else { | ||
| shouldReturn = updateConfigsWithIncrementalCooperative(connectorConfigUpdatesCopy, | ||
| connectorTargetStateChangesCopy, taskConfigUpdatesCopy); | ||
|
|
||
| if (connectorConfigUpdatesCopy.get() != null) { | ||
| processConnectorConfigUpdates(connectorConfigUpdatesCopy.get()); | ||
| } | ||
|
|
||
| if (connectorTargetStateChangesCopy.get() != null) { | ||
| processTargetStateChanges(connectorTargetStateChangesCopy.get()); | ||
| } | ||
|
|
||
| if (connectorTargetStateChangesCopy != null) | ||
| processTargetStateChanges(connectorTargetStateChangesCopy); | ||
| if (taskConfigUpdatesCopy.get() != null) { | ||
| processTaskConfigUpdatesWithIncrementalCooperative(taskConfigUpdatesCopy.get()); | ||
| } | ||
|
|
||
| if (shouldReturn) { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // Let the group take any actions it needs to | ||
| try { | ||
|
|
@@ -360,6 +360,95 @@ public void tick() { | |
| } | ||
| } | ||
|
|
||
| private synchronized boolean updateConfigsWithEager(AtomicReference<Set<String>> connectorConfigUpdatesCopy, | ||
| AtomicReference<Set<String>> connectorTargetStateChangesCopy) { | ||
| // This branch is here to avoid creating a snapshot if not needed | ||
| if (needsReconfigRebalance | ||
| || !connectorConfigUpdates.isEmpty() | ||
| || !connectorTargetStateChanges.isEmpty()) { | ||
| // Connector reconfigs only need local updates since there is no coordination between workers required. | ||
| // However, if connectors were added or removed, work needs to be rebalanced since we have more work | ||
| // items to distribute among workers. | ||
| configState = configBackingStore.snapshot(); | ||
|
|
||
| if (needsReconfigRebalance) { | ||
| // Task reconfigs require a rebalance. Request the rebalance, clean out state, and then restart | ||
| // this loop, which will then ensure the rebalance occurs without any other requests being | ||
| // processed until it completes. | ||
| log.debug("Requesting rebalance due to reconfiguration of tasks (needsReconfigRebalance: {})", | ||
| needsReconfigRebalance); | ||
| member.requestRejoin(); | ||
| needsReconfigRebalance = false; | ||
| // Any connector config updates or target state changes will be addressed during the rebalance too | ||
| connectorConfigUpdates.clear(); | ||
| connectorTargetStateChanges.clear(); | ||
| return true; | ||
| } else { | ||
| if (!connectorConfigUpdates.isEmpty()) { | ||
| // We can't start/stop while locked since starting connectors can cause task updates that will | ||
| // require writing configs, which in turn make callbacks into this class from another thread that | ||
| // require acquiring a lock. This leads to deadlock. Instead, just copy the info we need and process | ||
| // the updates after unlocking. | ||
| connectorConfigUpdatesCopy.set(connectorConfigUpdates); | ||
| connectorConfigUpdates = new HashSet<>(); | ||
| } | ||
|
|
||
| if (!connectorTargetStateChanges.isEmpty()) { | ||
| // Similarly for target state changes which can cause connectors to be restarted | ||
| connectorTargetStateChangesCopy.set(connectorTargetStateChanges); | ||
| connectorTargetStateChanges = new HashSet<>(); | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private synchronized boolean updateConfigsWithIncrementalCooperative(AtomicReference<Set<String>> connectorConfigUpdatesCopy, | ||
| AtomicReference<Set<String>> connectorTargetStateChangesCopy, | ||
| AtomicReference<Set<ConnectorTaskId>> taskConfigUpdatesCopy) { | ||
| boolean retValue = false; | ||
| // This branch is here to avoid creating a snapshot if not needed | ||
| if (needsReconfigRebalance | ||
| || !connectorConfigUpdates.isEmpty() | ||
| || !connectorTargetStateChanges.isEmpty() | ||
| || !taskConfigUpdates.isEmpty()) { | ||
| // Connector reconfigs only need local updates since there is no coordination between workers required. | ||
| // However, if connectors were added or removed, work needs to be rebalanced since we have more work | ||
| // items to distribute among workers. | ||
| configState = configBackingStore.snapshot(); | ||
|
|
||
| if (needsReconfigRebalance) { | ||
| log.debug("Requesting rebalance due to reconfiguration of tasks (needsReconfigRebalance: {})", | ||
| needsReconfigRebalance); | ||
| member.requestRejoin(); | ||
| needsReconfigRebalance = false; | ||
| retValue = true; | ||
| } | ||
|
|
||
| if (!connectorConfigUpdates.isEmpty()) { | ||
| // We can't start/stop while locked since starting connectors can cause task updates that will | ||
| // require writing configs, which in turn make callbacks into this class from another thread that | ||
| // require acquiring a lock. This leads to deadlock. Instead, just copy the info we need and process | ||
| // the updates after unlocking. | ||
| connectorConfigUpdatesCopy.set(connectorConfigUpdates); | ||
| connectorConfigUpdates = new HashSet<>(); | ||
| } | ||
|
|
||
| if (!connectorTargetStateChanges.isEmpty()) { | ||
| // Similarly for target state changes which can cause connectors to be restarted | ||
| connectorTargetStateChangesCopy.set(connectorTargetStateChanges); | ||
| connectorTargetStateChanges = new HashSet<>(); | ||
| } | ||
|
|
||
| if (!taskConfigUpdates.isEmpty()) { | ||
| // Similarly for task config updates | ||
| taskConfigUpdatesCopy.set(taskConfigUpdates); | ||
| taskConfigUpdates = new HashSet<>(); | ||
| } | ||
| } | ||
| return retValue; | ||
| } | ||
|
|
||
| private void processConnectorConfigUpdates(Set<String> connectorConfigUpdates) { | ||
| // If we only have connector config updates, we can just bounce the updated connectors that are | ||
| // currently assigned to this worker. | ||
|
|
@@ -396,6 +485,21 @@ private void processTargetStateChanges(Set<String> connectorTargetStateChanges) | |
| } | ||
| } | ||
|
|
||
| private void processTaskConfigUpdatesWithIncrementalCooperative(Set<ConnectorTaskId> taskConfigUpdates) { | ||
| Set<ConnectorTaskId> localTasks = assignment == null | ||
| ? Collections.emptySet() | ||
| : new HashSet<>(assignment.tasks()); | ||
| Set<String> connectorsWhoseTasksToStop = taskConfigUpdates.stream() | ||
| .map(ConnectorTaskId::connector).collect(Collectors.toSet()); | ||
|
|
||
| List<ConnectorTaskId> tasksToStop = localTasks.stream() | ||
| .filter(taskId -> connectorsWhoseTasksToStop.contains(taskId.connector())) | ||
| .collect(Collectors.toList()); | ||
| log.info("Handling task config update by restarting tasks {}", tasksToStop); | ||
| worker.stopAndAwaitTasks(tasksToStop); | ||
| tasksToRestart.addAll(tasksToStop); | ||
| } | ||
|
|
||
| // public for testing | ||
| public void halt() { | ||
| synchronized (this) { | ||
|
|
@@ -900,6 +1004,12 @@ private void startWork() { | |
| callables.add(getConnectorStartingCallable(connectorName)); | ||
| } | ||
|
|
||
| // These tasks have been stopped by this worker due to task reconfiguration. In order to | ||
| // restart them, they are removed just before the overall task startup from the set of | ||
| // currently running tasks. Therefore, they'll be restarted only if they are included in | ||
| // the assignment that was just received after rebalancing. | ||
| runningAssignment.tasks().removeAll(tasksToRestart); | ||
| tasksToRestart.clear(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: it would help to have a comment here to explain why the tasks to restart are removed from the running tasks.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| for (ConnectorTaskId taskId : assignmentDifference(assignment.tasks(), runningAssignment.tasks())) { | ||
| callables.add(getTaskStartingCallable(taskId)); | ||
| } | ||
|
|
@@ -1172,12 +1282,17 @@ public void onConnectorConfigUpdate(String connector) { | |
| public void onTaskConfigUpdate(Collection<ConnectorTaskId> tasks) { | ||
| log.info("Tasks {} configs updated", tasks); | ||
|
|
||
| // Stage the update and wake up the work thread. No need to record the set of tasks here because task reconfigs | ||
| // always need a rebalance to ensure offsets get committed. | ||
| // Stage the update and wake up the work thread. | ||
| // The set of tasks is recorder for incremental cooperative rebalancing, in which | ||
| // tasks don't get restarted unless they are balanced between workers. | ||
| // With eager rebalancing there's no need to record the set of tasks because task reconfigs | ||
| // always need a rebalance to ensure offsets get committed. In eager rebalancing the | ||
| // recorded set of tasks remains unused. | ||
| // TODO: As an optimization, some task config updates could avoid a rebalance. In particular, single-task | ||
| // connectors clearly don't need any coordination. | ||
| synchronized (DistributedHerder.this) { | ||
| needsReconfigRebalance = true; | ||
| taskConfigUpdates.addAll(tasks); | ||
| } | ||
| member.wakeup(); | ||
| } | ||
|
|
@@ -1279,7 +1394,7 @@ public void onAssigned(ExtendedAssignment assignment, int generation) { | |
| // catch up (or backoff if we fail) not executed in a callback, and so we'll be able to invoke other | ||
| // group membership actions (e.g., we may need to explicitly leave the group if we cannot handle the | ||
| // assigned tasks). | ||
| log.info("Joined group and got assignment: {}", assignment); | ||
| log.info("Joined group at generation {} and got assignment: {}", generation, assignment); | ||
| synchronized (DistributedHerder.this) { | ||
| DistributedHerder.this.assignment = assignment; | ||
| DistributedHerder.this.generation = generation; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: this log line is in a different location in the similar
updateConfigsWithEager(...)method. Would it make sense to move this log message to just before themember.requestRejoin()call?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved it to both before
member.requestRejoin. I changed it because it was even after we set this variable to false, which made the log message a bit confusing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(done)