Skip to content

Commit 2ad3121

Browse files
nielsbaumanywangd
authored andcommitted
Make the health node a cluster-scoped persistent task (MP-1969)
Now that the persistent task framework supports cluster-scoped persistent tasks, we can make the health node persistent task run with cluster scope.
1 parent ad8e22d commit 2ad3121

File tree

6 files changed

+47
-16
lines changed

6 files changed

+47
-16
lines changed

server/src/main/java/org/elasticsearch/health/node/selection/HealthNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.cluster.node.DiscoveryNode;
1414
import org.elasticsearch.core.Nullable;
1515
import org.elasticsearch.persistent.AllocatedPersistentTask;
16+
import org.elasticsearch.persistent.ClusterPersistentTasksCustomMetadata;
1617
import org.elasticsearch.persistent.PersistentTasksCustomMetadata;
1718
import org.elasticsearch.tasks.TaskId;
1819

@@ -36,8 +37,7 @@ protected void onCancelled() {
3637

3738
@Nullable
3839
public static PersistentTasksCustomMetadata.PersistentTask<?> findTask(ClusterState clusterState) {
39-
PersistentTasksCustomMetadata taskMetadata = clusterState.getMetadata().getProject().custom(PersistentTasksCustomMetadata.TYPE);
40-
return taskMetadata == null ? null : taskMetadata.getTask(TASK_NAME);
40+
return ClusterPersistentTasksCustomMetadata.getTaskWithId(clusterState, TASK_NAME);
4141
}
4242

4343
@Nullable

server/src/main/java/org/elasticsearch/health/node/selection/HealthNodeTaskExecutor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ private HealthNodeTaskExecutor(ClusterService clusterService, PersistentTasksSer
7373
this.enabled = ENABLED_SETTING.get(settings);
7474
}
7575

76+
@Override
77+
public Scope scope() {
78+
return Scope.CLUSTER;
79+
}
80+
7681
public static HealthNodeTaskExecutor create(
7782
ClusterService clusterService,
7883
PersistentTasksService persistentTasksService,

server/src/test/java/org/elasticsearch/cluster/metadata/MetadataTests.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@
3838
import org.elasticsearch.common.util.set.Sets;
3939
import org.elasticsearch.common.xcontent.ChunkedToXContent;
4040
import org.elasticsearch.common.xcontent.XContentHelper;
41-
import org.elasticsearch.core.FixForMultiProject;
4241
import org.elasticsearch.core.Nullable;
4342
import org.elasticsearch.core.Predicates;
4443
import org.elasticsearch.core.SuppressForbidden;
44+
import org.elasticsearch.health.node.selection.HealthNode;
4545
import org.elasticsearch.health.node.selection.HealthNodeTaskExecutor;
46+
import org.elasticsearch.health.node.selection.HealthNodeTaskParams;
4647
import org.elasticsearch.index.Index;
4748
import org.elasticsearch.index.IndexMode;
4849
import org.elasticsearch.index.IndexNotFoundException;
@@ -843,17 +844,20 @@ public void testParseXContentFormatBeforeMultiProject() throws IOException {
843844
assertThat(metatdata, notNullValue());
844845
assertThat(metatdata.clusterUUID(), is("aba1aa1ababbbaabaabaab"));
845846
assertThat(metatdata.customs().keySet(), containsInAnyOrder("desired_nodes", "cluster_persistent_tasks"));
846-
@FixForMultiProject(description = "adjust the assertion once health-node becomes cluster-scoped")
847847
final var clusterTasks = ClusterPersistentTasksCustomMetadata.get(metatdata);
848-
assertThat(clusterTasks.tasks(), hasSize(0));
848+
assertThat(clusterTasks.tasks(), hasSize(1));
849+
assertThat(
850+
clusterTasks.tasks().stream().map(PersistentTasksCustomMetadata.PersistentTask::getTaskName).toList(),
851+
containsInAnyOrder("health-node")
852+
);
849853
assertThat(
850854
metatdata.getProject().customs().keySet(),
851855
containsInAnyOrder("persistent_tasks", "index-graveyard", "component_template")
852856
);
853857
final var projectTasks = PersistentTasksCustomMetadata.get(metatdata.getProject());
854858
assertThat(
855859
projectTasks.tasks().stream().map(PersistentTasksCustomMetadata.PersistentTask::getTaskName).toList(),
856-
containsInAnyOrder("health-node", "upgrade-system-indices")
860+
containsInAnyOrder("upgrade-system-indices")
857861
);
858862
assertThat(clusterTasks.getLastAllocationId(), equalTo(projectTasks.getLastAllocationId()));
859863
}
@@ -2607,8 +2611,24 @@ public void testMultiProjectXContent() throws IOException {
26072611
)
26082612
.toList();
26092613

2610-
@FixForMultiProject(description = "considering adding health-node into metadata customs once health-node becomes a cluster task")
2611-
final Metadata originalMeta = randomMetadata(projects);
2614+
final Metadata originalMeta = Metadata.builder(randomMetadata(projects))
2615+
.putCustom(
2616+
ClusterPersistentTasksCustomMetadata.TYPE,
2617+
new ClusterPersistentTasksCustomMetadata(
2618+
lastAllocationId + 1,
2619+
Map.of(
2620+
HealthNode.TASK_NAME,
2621+
new PersistentTasksCustomMetadata.PersistentTask<>(
2622+
HealthNode.TASK_NAME,
2623+
HealthNode.TASK_NAME,
2624+
HealthNodeTaskParams.INSTANCE,
2625+
lastAllocationId + 1,
2626+
PersistentTasks.INITIAL_ASSIGNMENT
2627+
)
2628+
)
2629+
)
2630+
)
2631+
.build();
26122632

26132633
ToXContent.Params p = new ToXContent.MapParams(
26142634
Map.of("multi-project", "true", Metadata.CONTEXT_MODE_PARAM, Metadata.CONTEXT_MODE_GATEWAY)
@@ -2618,6 +2638,7 @@ public void testMultiProjectXContent() throws IOException {
26182638
final List<NamedXContentRegistry.Entry> registry = new ArrayList<>();
26192639
registry.addAll(ClusterModule.getNamedXWriteables());
26202640
registry.addAll(SystemIndexMigrationExecutor.getNamedXContentParsers());
2641+
registry.addAll(HealthNodeTaskExecutor.getNamedXContentParsers());
26212642
final var config = XContentParserConfiguration.EMPTY.withRegistry(new NamedXContentRegistry(registry));
26222643

26232644
try (XContentParser parser = createParser(config, JsonXContent.jsonXContent, bytes)) {
@@ -2628,6 +2649,9 @@ public void testMultiProjectXContent() throws IOException {
26282649
assertThat(projectTasks.getLastAllocationId(), equalTo(lastAllocationId));
26292650
assertThat(projectTasks.taskMap().keySet(), equalTo(Set.of(SystemIndexMigrationTaskParams.SYSTEM_INDEX_UPGRADE_TASK_NAME)));
26302651
}
2652+
final var clusterTasks = ClusterPersistentTasksCustomMetadata.get(fromXContentMeta);
2653+
assertThat(clusterTasks.getLastAllocationId(), equalTo(lastAllocationId + 1));
2654+
assertThat(clusterTasks.taskMap().keySet(), equalTo(Set.of(HealthNode.TASK_NAME)));
26312655
}
26322656
}
26332657

server/src/test/java/org/elasticsearch/health/node/selection/HealthNodeTaskExecutorTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import org.elasticsearch.common.settings.ClusterSettings;
2323
import org.elasticsearch.common.settings.Settings;
2424
import org.elasticsearch.features.FeatureService;
25+
import org.elasticsearch.persistent.ClusterPersistentTasksCustomMetadata;
2526
import org.elasticsearch.persistent.PersistentTaskState;
26-
import org.elasticsearch.persistent.PersistentTasksCustomMetadata;
2727
import org.elasticsearch.persistent.PersistentTasksService;
2828
import org.elasticsearch.test.ESTestCase;
2929
import org.elasticsearch.threadpool.TestThreadPool;
@@ -198,10 +198,11 @@ private ClusterState stateWithNodeShuttingDown(ClusterState clusterState, Single
198198

199199
private ClusterState stateWithHealthNodeSelectorTask(ClusterState clusterState) {
200200
ClusterState.Builder builder = ClusterState.builder(clusterState);
201-
PersistentTasksCustomMetadata.Builder tasks = PersistentTasksCustomMetadata.builder();
201+
ClusterPersistentTasksCustomMetadata.Builder tasks = ClusterPersistentTasksCustomMetadata.builder();
202202
tasks.addTask(HealthNode.TASK_NAME, HealthNode.TASK_NAME, new HealthNodeTaskParams(), NO_NODE_FOUND);
203203

204-
Metadata.Builder metadata = Metadata.builder(clusterState.metadata()).putCustom(PersistentTasksCustomMetadata.TYPE, tasks.build());
204+
Metadata.Builder metadata = Metadata.builder(clusterState.metadata())
205+
.putCustom(ClusterPersistentTasksCustomMetadata.TYPE, tasks.build());
205206
return builder.metadata(metadata).build();
206207
}
207208
}

server/src/test/java/org/elasticsearch/health/node/selection/HealthNodeTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import org.elasticsearch.cluster.node.DiscoveryNode;
1515
import org.elasticsearch.cluster.node.DiscoveryNodeRole;
1616
import org.elasticsearch.cluster.node.DiscoveryNodeUtils;
17-
import org.elasticsearch.persistent.PersistentTasksCustomMetadata;
17+
import org.elasticsearch.persistent.ClusterPersistentTasksCustomMetadata;
1818
import org.elasticsearch.test.ESTestCase;
1919

2020
import java.util.Set;
@@ -55,10 +55,10 @@ public void testFindHealthNodeNoTask() {
5555
}
5656

5757
public void testfindHealthNodeNoAssignment() {
58-
PersistentTasksCustomMetadata.Builder tasks = PersistentTasksCustomMetadata.builder();
58+
ClusterPersistentTasksCustomMetadata.Builder tasks = ClusterPersistentTasksCustomMetadata.builder();
5959
tasks.addTask(HealthNode.TASK_NAME, HealthNode.TASK_NAME, HealthNodeTaskParams.INSTANCE, NO_NODE_FOUND);
6060
ClusterState state = ClusterStateCreationUtils.state(node1, node1, allNodes)
61-
.copyAndUpdateMetadata(b -> b.putCustom(PersistentTasksCustomMetadata.TYPE, tasks.build()));
61+
.copyAndUpdateMetadata(b -> b.putCustom(ClusterPersistentTasksCustomMetadata.TYPE, tasks.build()));
6262
assertThat(HealthNode.findHealthNode(state), nullValue());
6363
}
6464

test/framework/src/main/java/org/elasticsearch/action/support/replication/ClusterStateCreationUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.elasticsearch.index.shard.IndexLongFieldRange;
3838
import org.elasticsearch.index.shard.ShardId;
3939
import org.elasticsearch.indices.SystemIndices;
40+
import org.elasticsearch.persistent.ClusterPersistentTasksCustomMetadata;
4041
import org.elasticsearch.persistent.PersistentTasksCustomMetadata;
4142

4243
import java.util.ArrayList;
@@ -802,12 +803,12 @@ private static String selectAndRemove(Set<String> strings) {
802803
}
803804

804805
private static Metadata.Builder addHealthNode(Metadata.Builder metadataBuilder, DiscoveryNode healthNode) {
805-
PersistentTasksCustomMetadata.Builder tasks = PersistentTasksCustomMetadata.builder();
806+
ClusterPersistentTasksCustomMetadata.Builder tasks = ClusterPersistentTasksCustomMetadata.builder();
806807
PersistentTasksCustomMetadata.Assignment assignment = new PersistentTasksCustomMetadata.Assignment(
807808
healthNode.getId(),
808809
randomAlphaOfLength(10)
809810
);
810811
tasks.addTask(HealthNode.TASK_NAME, HealthNode.TASK_NAME, HealthNodeTaskParams.INSTANCE, assignment);
811-
return metadataBuilder.putCustom(PersistentTasksCustomMetadata.TYPE, tasks.build());
812+
return metadataBuilder.putCustom(ClusterPersistentTasksCustomMetadata.TYPE, tasks.build());
812813
}
813814
}

0 commit comments

Comments
 (0)