Skip to content

Commit 076f61d

Browse files
authored
Combine cluster and project tasks in xcontent for single project (#124613)
This PR combines both cluster and project tasks under persistent_tasks for XContent output of Metadata when it contains only a single project, i.e. there will be no cluster_persistent_tasks in such output. This is to maintain the existing output format when the cluster is not multi-project enabled. Relates: MP-1945
1 parent 7e746ce commit 076f61d

File tree

3 files changed

+219
-55
lines changed

3 files changed

+219
-55
lines changed

server/src/main/java/org/elasticsearch/cluster/metadata/Metadata.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,26 @@ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params p) {
744744
// and not include it in the project xcontent output (through the lack of multi-project params)
745745
clusterReservedState.putAll(project.reservedStateMetadata());
746746

747-
@FixForMultiProject(description = "consider include cluster-scoped persistent tasks")
747+
// Similarly, combine cluster and project persistent tasks and report them under a single key
748+
Iterator<ToXContent> customs = Iterators.flatMap(customs().entrySet().iterator(), entry -> {
749+
if (entry.getValue().context().contains(context)
750+
&& ClusterPersistentTasksCustomMetadata.TYPE.equals(entry.getKey()) == false) {
751+
return ChunkedToXContentHelper.object(entry.getKey(), entry.getValue().toXContentChunked(p));
752+
} else {
753+
return Collections.emptyIterator();
754+
}
755+
});
756+
final var combinedTasks = PersistentTasksCustomMetadata.combine(
757+
ClusterPersistentTasksCustomMetadata.get(this),
758+
PersistentTasksCustomMetadata.get(project)
759+
);
760+
if (combinedTasks != null) {
761+
customs = Iterators.concat(
762+
customs,
763+
ChunkedToXContentHelper.object(PersistentTasksCustomMetadata.TYPE, combinedTasks.toXContentChunked(p))
764+
);
765+
}
766+
748767
final var iterators = Iterators.concat(start, Iterators.single((builder, params) -> {
749768
builder.field("cluster_uuid", clusterUUID);
750769
builder.field("cluster_uuid_committed", clusterUUIDCommitted);
@@ -754,12 +773,7 @@ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params p) {
754773
}),
755774
persistentSettings,
756775
project.toXContentChunked(p),
757-
Iterators.flatMap(
758-
customs.entrySet().iterator(),
759-
entry -> entry.getValue().context().contains(context)
760-
? ChunkedToXContentHelper.object(entry.getKey(), entry.getValue().toXContentChunked(p))
761-
: Collections.emptyIterator()
762-
),
776+
customs,
763777
ChunkedToXContentHelper.object("reserved_state", clusterReservedState.values().iterator()),
764778
ChunkedToXContentHelper.endObject()
765779
);

server/src/main/java/org/elasticsearch/cluster/metadata/ProjectMetadata.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.elasticsearch.index.IndexNotFoundException;
3838
import org.elasticsearch.index.IndexSettings;
3939
import org.elasticsearch.index.IndexVersion;
40+
import org.elasticsearch.persistent.PersistentTasksCustomMetadata;
4041
import org.elasticsearch.plugins.FieldPredicate;
4142
import org.elasticsearch.plugins.MapperPlugin;
4243
import org.elasticsearch.transport.Transports;
@@ -2111,14 +2112,18 @@ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params p) {
21112112
? ChunkedToXContentHelper.object("indices", indices().values().iterator())
21122113
: Collections.emptyIterator();
21132114

2114-
Iterator<ToXContent> customs = Iterators.flatMap(
2115-
customs().entrySet().iterator(),
2116-
entry -> entry.getValue().context().contains(context)
2117-
? ChunkedToXContentHelper.object(entry.getKey(), entry.getValue().toXContentChunked(p))
2118-
: Collections.emptyIterator()
2119-
);
2120-
21212115
final var multiProject = p.paramAsBoolean("multi-project", false);
2116+
Iterator<ToXContent> customs = Iterators.flatMap(customs().entrySet().iterator(), entry -> {
2117+
if (entry.getValue().context().contains(context)
2118+
// Include persistent tasks in the output only when multi-project=true.
2119+
// In single-project-mode (multi-project=false), we already output them in Metadata.
2120+
&& (multiProject || PersistentTasksCustomMetadata.TYPE.equals(entry.getKey()) == false)) {
2121+
return ChunkedToXContentHelper.object(entry.getKey(), entry.getValue().toXContentChunked(p));
2122+
} else {
2123+
return Collections.emptyIterator();
2124+
}
2125+
});
2126+
21222127
return Iterators.concat(
21232128
ChunkedToXContentHelper.object(
21242129
"templates",

0 commit comments

Comments
 (0)