Skip to content

Commit a7f2bd9

Browse files
committed
Reduce use of deprecated Metadata builder method
This removes all non-test usage of Metadata.Builder.put(IndexMetadata, boolean) And replaces it with appropriate calls to the equivalent method on `ProjectMetadata.Builder` In most cases this _does not_ make the code project aware, but does reduce the number of deprecated methods in use.
1 parent fb322dc commit a7f2bd9

File tree

11 files changed

+143
-50
lines changed

11 files changed

+143
-50
lines changed

benchmarks/src/main/java/org/elasticsearch/benchmark/indices/resolution/IndexNameExpressionResolverBenchmark.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
import org.elasticsearch.cluster.metadata.DataStream;
1717
import org.elasticsearch.cluster.metadata.IndexMetadata;
1818
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
19-
import org.elasticsearch.cluster.metadata.Metadata;
19+
import org.elasticsearch.cluster.metadata.ProjectId;
20+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
2021
import org.elasticsearch.cluster.project.DefaultProjectResolver;
2122
import org.elasticsearch.common.settings.Settings;
2223
import org.elasticsearch.common.util.concurrent.ThreadContext;
@@ -63,12 +64,12 @@ public void setUp() {
6364
int numDataStreams = toInt(params[0]);
6465
int numIndices = toInt(params[1]);
6566

66-
Metadata.Builder mb = Metadata.builder();
67+
ProjectMetadata.Builder pmb = ProjectMetadata.builder(ProjectId.DEFAULT);
6768
String[] indices = new String[numIndices + numDataStreams * (numIndices + 1)];
6869
int position = 0;
6970
for (int i = 1; i <= numIndices; i++) {
7071
String indexName = INDEX_PREFIX + i;
71-
createIndexMetadata(indexName, mb);
72+
createIndexMetadata(indexName, pmb);
7273
indices[position++] = indexName;
7374
}
7475

@@ -77,14 +78,14 @@ public void setUp() {
7778
List<Index> backingIndices = new ArrayList<>();
7879
for (int j = 1; j <= numIndices; j++) {
7980
String backingIndexName = DataStream.getDefaultBackingIndexName(dataStreamName, j);
80-
backingIndices.add(createIndexMetadata(backingIndexName, mb).getIndex());
81+
backingIndices.add(createIndexMetadata(backingIndexName, pmb).getIndex());
8182
indices[position++] = backingIndexName;
8283
}
8384
indices[position++] = dataStreamName;
84-
mb.put(DataStream.builder(dataStreamName, backingIndices).build());
85+
pmb.put(DataStream.builder(dataStreamName, backingIndices).build());
8586
}
8687
int mid = indices.length / 2;
87-
clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(mb).build();
88+
clusterState = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(pmb).build();
8889
resolver = new IndexNameExpressionResolver(
8990
new ThreadContext(Settings.EMPTY),
9091
new SystemIndices(List.of()),
@@ -97,13 +98,13 @@ public void setUp() {
9798
mixedRequest = new Request(IndicesOptions.lenientExpandOpenHidden(), mixed);
9899
}
99100

100-
private IndexMetadata createIndexMetadata(String indexName, Metadata.Builder mb) {
101+
private IndexMetadata createIndexMetadata(String indexName, ProjectMetadata.Builder pmb) {
101102
IndexMetadata indexMetadata = IndexMetadata.builder(indexName)
102103
.settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()))
103104
.numberOfShards(1)
104105
.numberOfReplicas(0)
105106
.build();
106-
mb.put(indexMetadata, false);
107+
pmb.put(indexMetadata, false);
107108
return indexMetadata;
108109
}
109110

benchmarks/src/main/java/org/elasticsearch/benchmark/routing/allocation/ShardsAvailabilityHealthIndicatorBenchmark.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
import org.elasticsearch.cluster.ClusterName;
1313
import org.elasticsearch.cluster.ClusterState;
1414
import org.elasticsearch.cluster.metadata.IndexMetadata;
15-
import org.elasticsearch.cluster.metadata.Metadata;
15+
import org.elasticsearch.cluster.metadata.ProjectId;
16+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
1617
import org.elasticsearch.cluster.node.DiscoveryNodes;
1718
import org.elasticsearch.cluster.routing.IndexRoutingTable;
1819
import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
@@ -95,7 +96,7 @@ public void setUp() throws Exception {
9596

9697
AllocationService allocationService = Allocators.createAllocationService(Settings.EMPTY);
9798

98-
Metadata.Builder mb = Metadata.builder();
99+
ProjectMetadata.Builder pmb = ProjectMetadata.builder(ProjectId.DEFAULT);
99100
RoutingTable.Builder rb = RoutingTable.builder();
100101

101102
DiscoveryNodes.Builder nb = DiscoveryNodes.builder();
@@ -159,12 +160,12 @@ public void setUp() throws Exception {
159160
}
160161

161162
routingTable.add(indexRountingTableBuilder);
162-
mb.put(indexMetadata, false);
163+
pmb.put(indexMetadata, false);
163164
}
164165

165166
ClusterState initialClusterState = ClusterState.builder(ClusterName.DEFAULT)
166-
.metadata(mb)
167-
.routingTable(routingTable)
167+
.putProjectMetadata(pmb)
168+
.putRoutingTable(pmb.getId(), routingTable.build())
168169
.nodes(nb)
169170
.build();
170171

modules/data-streams/src/main/java/org/elasticsearch/datastreams/lifecycle/DataStreamLifecycleService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.ResolvedExpression;
5454
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.SelectorResolver;
5555
import org.elasticsearch.cluster.metadata.Metadata;
56+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
5657
import org.elasticsearch.cluster.routing.allocation.AllocationService;
5758
import org.elasticsearch.cluster.service.ClusterService;
5859
import org.elasticsearch.cluster.service.MasterServiceTaskQueue;
@@ -1511,8 +1512,10 @@ ClusterState execute(ClusterState currentState) throws Exception {
15111512
LIFECYCLE_CUSTOM_INDEX_METADATA_KEY,
15121513
newCustomMetadata
15131514
).build();
1514-
Metadata metadata = Metadata.builder(currentState.metadata()).put(updatededIndexMetadata, true).build();
1515-
return ClusterState.builder(currentState).metadata(metadata).build();
1515+
ProjectMetadata project = ProjectMetadata.builder(currentState.metadata().getProject())
1516+
.put(updatededIndexMetadata, true)
1517+
.build();
1518+
return ClusterState.builder(currentState).putProjectMetadata(project).build();
15161519
}
15171520

15181521
@Override

modules/data-streams/src/main/java/org/elasticsearch/datastreams/lifecycle/downsampling/DeleteSourceAndAddDownsampleToDS.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import org.elasticsearch.cluster.metadata.IndexMetadata;
2020
import org.elasticsearch.cluster.metadata.Metadata;
2121
import org.elasticsearch.cluster.metadata.MetadataDeleteIndexService;
22+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
2223
import org.elasticsearch.common.settings.Settings;
24+
import org.elasticsearch.core.FixForMultiProject;
2325
import org.elasticsearch.core.TimeValue;
2426
import org.elasticsearch.index.IndexSettings;
2527

@@ -148,7 +150,23 @@ private static ClusterState addDownsampleIndexToDataStream(
148150
IndexMetadata sourceIndexMeta,
149151
IndexMetadata downsampleIndexMeta
150152
) {
151-
Metadata.Builder newMetaData = Metadata.builder(state.getMetadata());
153+
@FixForMultiProject
154+
final ProjectMetadata updatedProject = addDownsampleIndexToDataStream(
155+
state.metadata().getProject(),
156+
dataStream,
157+
sourceIndexMeta,
158+
downsampleIndexMeta
159+
);
160+
return ClusterState.builder(state).putProjectMetadata(updatedProject).build();
161+
}
162+
163+
private static ProjectMetadata addDownsampleIndexToDataStream(
164+
ProjectMetadata project,
165+
DataStream dataStream,
166+
IndexMetadata sourceIndexMeta,
167+
IndexMetadata downsampleIndexMeta
168+
) {
169+
ProjectMetadata.Builder newProject = ProjectMetadata.builder(project);
152170
TimeValue generationLifecycleDate = dataStream.getGenerationLifecycleDate(sourceIndexMeta);
153171
// the generation lifecycle date is null only for the write index
154172
// we fail already if attempting to delete/downsample the write index, so the following assertion just re-inforces that
@@ -159,10 +177,10 @@ private static ClusterState addDownsampleIndexToDataStream(
159177
generationLifecycleDate.millis()
160178
);
161179

162-
newMetaData.put(updatedDownsampleMetadata, true);
180+
newProject.put(updatedDownsampleMetadata, true);
163181
// we deleted the source already so let's add the downsample index to the data stream
164-
newMetaData.put(dataStream.addBackingIndex(state.metadata().getProject(), downsampleIndexMeta.getIndex()));
165-
return ClusterState.builder(state).metadata(newMetaData).build();
182+
newProject.put(dataStream.addBackingIndex(project, downsampleIndexMeta.getIndex()));
183+
return newProject.build();
166184
}
167185

168186
/**

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.elasticsearch.common.time.DateFormatters;
3434
import org.elasticsearch.common.util.FeatureFlag;
3535
import org.elasticsearch.common.xcontent.XContentHelper;
36+
import org.elasticsearch.core.FixForMultiProject;
3637
import org.elasticsearch.core.Nullable;
3738
import org.elasticsearch.core.TimeValue;
3839
import org.elasticsearch.core.Tuple;
@@ -889,7 +890,28 @@ public DataStream promoteDataStream() {
889890
* given indices
890891
*/
891892
@Nullable
893+
@Deprecated
892894
public DataStream snapshot(Set<String> indicesInSnapshot, Metadata.Builder snapshotMetadataBuilder) {
895+
@FixForMultiProject
896+
final ProjectId projectId = ProjectId.DEFAULT;
897+
ProjectMetadata.Builder project = snapshotMetadataBuilder.getProject(projectId);
898+
if (project == null) {
899+
project = ProjectMetadata.builder(projectId);
900+
snapshotMetadataBuilder.put(project);
901+
}
902+
return snapshot(indicesInSnapshot, project);
903+
}
904+
905+
/**
906+
* Reconciles this data stream with a list of indices available in a snapshot. Allows snapshots to store accurate data
907+
* stream definitions that do not reference backing indices and failure indices not contained in the snapshot.
908+
*
909+
* @param indicesInSnapshot List of indices in the snapshot
910+
* @param snapshotMetadataBuilder a project metadata builder with the current view of the snapshot metadata
911+
* @return Reconciled {@link DataStream} instance or {@code null} if no reconciled version of this data stream could be built from the
912+
* given indices
913+
*/
914+
public DataStream snapshot(Set<String> indicesInSnapshot, ProjectMetadata.Builder snapshotMetadataBuilder) {
893915
boolean backingIndicesChanged = false;
894916
boolean failureIndicesChanged = false;
895917

@@ -924,7 +946,7 @@ && isAnyIndexMissing(failureIndices.indices, snapshotMetadataBuilder, indicesInS
924946
return builder.setMetadata(metadata == null ? null : new HashMap<>(metadata)).build();
925947
}
926948

927-
private static boolean isAnyIndexMissing(List<Index> indices, Metadata.Builder builder, Set<String> indicesInSnapshot) {
949+
private static boolean isAnyIndexMissing(List<Index> indices, ProjectMetadata.Builder builder, Set<String> indicesInSnapshot) {
928950
for (Index index : indices) {
929951
final String indexName = index.getName();
930952
if (builder.get(indexName) == null || indicesInSnapshot.contains(indexName) == false) {

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,8 @@ public static Metadata readFrom(StreamInput in) throws IOException {
11251125
builder.persistentSettings(readSettingsFromStream(in));
11261126
builder.hashesOfConsistentSettings(DiffableStringMap.readFrom(in));
11271127
if (in.getTransportVersion().before(TransportVersions.MULTI_PROJECT)) {
1128+
final ProjectMetadata.Builder projectBuilder = ProjectMetadata.builder(ProjectId.DEFAULT);
1129+
builder.put(projectBuilder);
11281130
final Function<String, MappingMetadata> mappingLookup;
11291131
final Map<String, MappingMetadata> mappingMetadataMap = in.readMapValues(MappingMetadata::new, MappingMetadata::getSha256);
11301132
if (mappingMetadataMap.isEmpty() == false) {
@@ -1134,11 +1136,11 @@ public static Metadata readFrom(StreamInput in) throws IOException {
11341136
}
11351137
int size = in.readVInt();
11361138
for (int i = 0; i < size; i++) {
1137-
builder.put(IndexMetadata.readFrom(in, mappingLookup), false);
1139+
projectBuilder.put(IndexMetadata.readFrom(in, mappingLookup), false);
11381140
}
11391141
size = in.readVInt();
11401142
for (int i = 0; i < size; i++) {
1141-
builder.put(IndexTemplateMetadata.readFrom(in));
1143+
projectBuilder.put(IndexTemplateMetadata.readFrom(in));
11421144
}
11431145
readBwcCustoms(in, builder);
11441146

@@ -1736,6 +1738,11 @@ public static Metadata fromXContent(XContentParser parser) throws IOException {
17361738
}
17371739
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser);
17381740

1741+
/**
1742+
* Used when reading BWC fields from when indices etc used to be directly on metadata
1743+
*/
1744+
final ProjectMetadata.Builder projectBuilderForBwc = ProjectMetadata.builder(ProjectId.DEFAULT);
1745+
17391746
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
17401747
if (token == XContentParser.Token.FIELD_NAME) {
17411748
currentFieldName = parser.currentName();
@@ -1758,12 +1765,14 @@ public static Metadata fromXContent(XContentParser parser) throws IOException {
17581765
/* BwC Top-level project things */
17591766
case "indices" -> {
17601767
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
1761-
builder.put(IndexMetadata.Builder.fromXContent(parser), false);
1768+
projectBuilderForBwc.put(IndexMetadata.Builder.fromXContent(parser), false);
1769+
builder.put(projectBuilderForBwc);
17621770
}
17631771
}
17641772
case "templates" -> {
17651773
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
1766-
builder.put(IndexTemplateMetadata.Builder.fromXContent(parser, parser.currentName()));
1774+
projectBuilderForBwc.put(IndexTemplateMetadata.Builder.fromXContent(parser, parser.currentName()));
1775+
builder.put(projectBuilderForBwc);
17671776
}
17681777
}
17691778
/* Cluster customs (and project customs in older formats) */
@@ -1780,12 +1789,13 @@ public static Metadata fromXContent(XContentParser parser) throws IOException {
17801789
assert PersistentTasksCustomMetadata.TYPE.equals(name)
17811790
: name + " != " + PersistentTasksCustomMetadata.TYPE;
17821791
final var tuple = persistentTasksCustomMetadata.split();
1783-
builder.putProjectCustom(PersistentTasksCustomMetadata.TYPE, tuple.v2());
1792+
projectBuilderForBwc.putCustom(PersistentTasksCustomMetadata.TYPE, tuple.v2());
17841793
builder.putCustom(ClusterPersistentTasksCustomMetadata.TYPE, tuple.v1());
17851794
} else {
1786-
builder.putProjectCustom(name, projectCustom);
1795+
projectBuilderForBwc.putCustom(name, projectCustom);
17871796
}
17881797
});
1798+
builder.put(projectBuilderForBwc);
17891799
} else {
17901800
logger.warn("Skipping unknown custom object with type {}", currentFieldName);
17911801
parser.skipChildren();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,12 @@ public ClusterState execute(BatchExecutionContext<SystemIndexMetadataUpgradeTask
223223
}
224224

225225
if (updatedMetadata.isEmpty() == false || updatedDataStreams.isEmpty() == false) {
226-
Metadata.Builder builder = Metadata.builder(initialState.metadata());
226+
ProjectMetadata.Builder builder = ProjectMetadata.builder(initialState.metadata().getProject());
227227
updatedMetadata.forEach(idxMeta -> builder.put(idxMeta, true));
228228
updatedDataStreams.forEach(builder::put);
229229
updatedBackingIndices.forEach(idxMeta -> builder.put(idxMeta, true));
230230

231-
return ClusterState.builder(initialState).metadata(builder).build();
231+
return ClusterState.builder(initialState).putProjectMetadata(builder).build();
232232
}
233233
return initialState;
234234
}

server/src/main/java/org/elasticsearch/gateway/LocalAllocateDangledIndices.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import org.elasticsearch.cluster.block.ClusterBlocks;
2121
import org.elasticsearch.cluster.metadata.IndexMetadata;
2222
import org.elasticsearch.cluster.metadata.IndexMetadataVerifier;
23-
import org.elasticsearch.cluster.metadata.Metadata;
23+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
2424
import org.elasticsearch.cluster.node.DiscoveryNode;
2525
import org.elasticsearch.cluster.routing.RoutingTable;
2626
import org.elasticsearch.cluster.routing.allocation.AllocationService;
@@ -118,7 +118,7 @@ public ClusterState execute(ClusterState currentState) {
118118
if (currentState.blocks().disableStatePersistence()) {
119119
return currentState;
120120
}
121-
Metadata.Builder metadata = Metadata.builder(currentState.metadata());
121+
ProjectMetadata.Builder project = ProjectMetadata.builder(currentState.metadata().getProject());
122122
ClusterBlocks.Builder blocks = ClusterBlocks.builder().blocks(currentState.blocks());
123123
RoutingTable.Builder routingTableBuilder = RoutingTable.builder(
124124
allocationService.getShardRoutingRoleStrategy(),
@@ -205,7 +205,7 @@ public ClusterState execute(ClusterState currentState) {
205205
.version(indexMetadata.getVersion() + 1)
206206
.build();
207207
}
208-
metadata.put(newIndexMetadata, false);
208+
project.put(newIndexMetadata, false);
209209
blocks.addBlocks(newIndexMetadata);
210210
if (newIndexMetadata.getState() == IndexMetadata.State.OPEN || isIndexVerifiedBeforeClosed(indexMetadata)) {
211211
routingTableBuilder.addAsFromDangling(newIndexMetadata);
@@ -220,7 +220,7 @@ public ClusterState execute(ClusterState currentState) {
220220

221221
RoutingTable routingTable = routingTableBuilder.build();
222222
ClusterState updatedState = ClusterState.builder(currentState)
223-
.metadata(metadata)
223+
.putProjectMetadata(project)
224224
.blocks(blocks)
225225
.routingTable(routingTable)
226226
.build();

0 commit comments

Comments
 (0)