Skip to content

Combine join task and listener #82645

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.LongSupplier;
import java.util.function.Supplier;
Expand Down Expand Up @@ -396,15 +397,13 @@ default void close(Mode newMode) {}
class LeaderJoinAccumulator implements JoinAccumulator {
@Override
public void handleJoinRequest(DiscoveryNode sender, ActionListener<Void> joinListener) {
final JoinTaskExecutor.Task task = new JoinTaskExecutor.Task(sender, joinReasonService.getJoinReason(sender, Mode.LEADER));
assert joinTaskExecutor != null;
masterService.submitStateUpdateTask(
"node-join",
task,
ClusterStateTaskConfig.build(Priority.URGENT),
joinTaskExecutor,
new JoinTaskListener(task, joinListener)
final JoinTaskExecutor.Task task = new JoinTaskExecutor.Task(
sender,
joinReasonService.getJoinReason(sender, Mode.LEADER),
joinListener
);
assert joinTaskExecutor != null;
masterService.submitStateUpdateTask("node-join", task, ClusterStateTaskConfig.build(Priority.URGENT), joinTaskExecutor, task);
}

@Override
Expand Down Expand Up @@ -458,18 +457,17 @@ public void close(Mode newMode) {
closed = true;
if (newMode == Mode.LEADER) {
final Map<JoinTaskExecutor.Task, ClusterStateTaskListener> pendingAsTasks = new LinkedHashMap<>();
joinRequestAccumulator.forEach((node, listener) -> {
final JoinTaskExecutor.Task task = new JoinTaskExecutor.Task(
node,
joinReasonService.getJoinReason(node, Mode.CANDIDATE)
);
pendingAsTasks.put(task, new JoinTaskListener(task, listener));
});
final Consumer<JoinTaskExecutor.Task> pendingTaskAdder = task -> pendingAsTasks.put(task, task);
joinRequestAccumulator.forEach(
(node, listener) -> pendingTaskAdder.accept(
new JoinTaskExecutor.Task(node, joinReasonService.getJoinReason(node, Mode.CANDIDATE), listener)
)
);

final String stateUpdateSource = "elected-as-master ([" + pendingAsTasks.size() + "] nodes joined)";

pendingAsTasks.put(JoinTaskExecutor.newBecomeMasterTask(), (e) -> {});
pendingAsTasks.put(JoinTaskExecutor.newFinishElectionTask(), (e) -> {});
pendingTaskAdder.accept(JoinTaskExecutor.newBecomeMasterTask());
pendingTaskAdder.accept(JoinTaskExecutor.newFinishElectionTask());
joinTaskExecutor = joinTaskExecutorGenerator.get();
masterService.submitStateUpdateTasks(
stateUpdateSource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateTaskExecutor;
import org.elasticsearch.cluster.ClusterStateTaskListener;
import org.elasticsearch.cluster.NotMasterException;
import org.elasticsearch.cluster.block.ClusterBlocks;
import org.elasticsearch.cluster.metadata.IndexMetadata;
Expand Down Expand Up @@ -42,14 +43,16 @@ public class JoinTaskExecutor implements ClusterStateTaskExecutor<JoinTaskExecut
private final Logger logger;
private final RerouteService rerouteService;

public static class Task {
public static class Task implements ClusterStateTaskListener {

private final DiscoveryNode node;
private final String reason;
private final ActionListener<Void> listener;

public Task(DiscoveryNode node, String reason) {
public Task(DiscoveryNode node, String reason, ActionListener<Void> listener) {
this.node = node;
this.reason = reason;
this.listener = listener;
}

public DiscoveryNode node() {
Expand Down Expand Up @@ -82,6 +85,17 @@ public boolean isFinishElectionTask() {

private static final String BECOME_MASTER_TASK_REASON = "_BECOME_MASTER_TASK_";
private static final String FINISH_ELECTION_TASK_REASON = "_FINISH_ELECTION_";

@Override
public void onFailure(Exception e) {
listener.onFailure(e);
}

@Override
public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
listener.onResponse(null);
}

}

public JoinTaskExecutor(AllocationService allocationService, Logger logger, RerouteService rerouteService) {
Expand Down Expand Up @@ -253,15 +267,15 @@ public boolean runOnlyOnMaster() {
}

public static Task newBecomeMasterTask() {
return new Task(null, Task.BECOME_MASTER_TASK_REASON);
return new Task(null, Task.BECOME_MASTER_TASK_REASON, ActionListener.wrap(() -> {}));
}

/**
* a task that is used to signal the election is stopped and we should process pending joins.
* it may be used in combination with {@link JoinTaskExecutor#newBecomeMasterTask()}
*/
public static Task newFinishElectionTask() {
return new Task(null, Task.FINISH_ELECTION_TASK_REASON);
return new Task(null, Task.FINISH_ELECTION_TASK_REASON, ActionListener.wrap(() -> {}));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package org.elasticsearch.cluster.coordination;

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateTaskExecutor;
Expand Down Expand Up @@ -159,7 +160,13 @@ public void testUpdatesNodeWithNewRoles() throws Exception {

final ClusterStateTaskExecutor.ClusterTasksResult<JoinTaskExecutor.Task> result = joinTaskExecutor.execute(
clusterState,
List.of(new JoinTaskExecutor.Task(actualNode, "test"))
List.of(
new JoinTaskExecutor.Task(
actualNode,
"test",
ActionListener.wrap(() -> { throw new AssertionError("should not complete publication"); })
)
)
);
assertThat(result.executionResults.entrySet(), hasSize(1));
final ClusterStateTaskExecutor.TaskResult taskResult = result.executionResults.values().iterator().next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.apache.logging.log4j.Logger;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.ActionType;
Expand Down Expand Up @@ -364,15 +365,33 @@ public ClusterState addNodes(ClusterState clusterState, List<DiscoveryNode> node
return runTasks(
joinTaskExecutor,
clusterState,
nodes.stream().map(node -> new JoinTaskExecutor.Task(node, "dummy reason")).collect(Collectors.toList())
nodes.stream()
.map(
node -> new JoinTaskExecutor.Task(
node,
"dummy reason",
ActionListener.wrap(() -> { throw new AssertionError("should not complete publication"); })
)
)
.collect(Collectors.toList())
);
}

public ClusterState joinNodesAndBecomeMaster(ClusterState clusterState, List<DiscoveryNode> nodes) {
List<JoinTaskExecutor.Task> joinNodes = new ArrayList<>();
joinNodes.add(JoinTaskExecutor.newBecomeMasterTask());
joinNodes.add(JoinTaskExecutor.newFinishElectionTask());
joinNodes.addAll(nodes.stream().map(node -> new JoinTaskExecutor.Task(node, "dummy reason")).collect(Collectors.toList()));
joinNodes.addAll(
nodes.stream()
.map(
node -> new JoinTaskExecutor.Task(
node,
"dummy reason",
ActionListener.wrap(() -> { throw new AssertionError("should not complete publication"); })
)
)
.collect(Collectors.toList())
);

return runTasks(joinTaskExecutor, clusterState, joinNodes);
}
Expand Down