Skip to content

Commit 3f03775

Browse files
authored
Remove non-test usages of Metadata.Builder#putCustom (elastic#128801)
This removes all non-test usages of ``` Metadata.Builder.putCustom(String type, ProjectCustom custom) ``` 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 330d127 commit 3f03775

File tree

25 files changed

+132
-153
lines changed

25 files changed

+132
-153
lines changed

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/direct/TransportDeleteDatabaseConfigurationAction.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.elasticsearch.cluster.SimpleBatchedExecutor;
2222
import org.elasticsearch.cluster.block.ClusterBlockException;
2323
import org.elasticsearch.cluster.block.ClusterBlockLevel;
24-
import org.elasticsearch.cluster.metadata.Metadata;
24+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
2525
import org.elasticsearch.cluster.service.ClusterService;
2626
import org.elasticsearch.cluster.service.MasterServiceTaskQueue;
2727
import org.elasticsearch.common.Priority;
@@ -103,17 +103,17 @@ private record DeleteDatabaseConfigurationTask(ActionListener<AcknowledgedRespon
103103
ClusterStateTaskListener {
104104

105105
ClusterState execute(ClusterState currentState) throws Exception {
106-
final IngestGeoIpMetadata geoIpMeta = currentState.metadata()
107-
.getProject()
108-
.custom(IngestGeoIpMetadata.TYPE, IngestGeoIpMetadata.EMPTY);
106+
final var project = currentState.metadata().getProject();
107+
final IngestGeoIpMetadata geoIpMeta = project.custom(IngestGeoIpMetadata.TYPE, IngestGeoIpMetadata.EMPTY);
109108

110109
logger.debug("deleting database configuration [{}]", databaseId);
111110
Map<String, DatabaseConfigurationMetadata> databases = new HashMap<>(geoIpMeta.getDatabases());
112111
databases.remove(databaseId);
113112

114-
Metadata currentMeta = currentState.metadata();
115113
return ClusterState.builder(currentState)
116-
.metadata(Metadata.builder(currentMeta).putCustom(IngestGeoIpMetadata.TYPE, new IngestGeoIpMetadata(databases)))
114+
.putProjectMetadata(
115+
ProjectMetadata.builder(project).putCustom(IngestGeoIpMetadata.TYPE, new IngestGeoIpMetadata(databases))
116+
)
117117
.build();
118118
}
119119

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/direct/TransportPutDatabaseConfigurationAction.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import org.elasticsearch.cluster.SimpleBatchedExecutor;
2121
import org.elasticsearch.cluster.block.ClusterBlockException;
2222
import org.elasticsearch.cluster.block.ClusterBlockLevel;
23-
import org.elasticsearch.cluster.metadata.Metadata;
23+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
2424
import org.elasticsearch.cluster.service.ClusterService;
2525
import org.elasticsearch.cluster.service.MasterServiceTaskQueue;
2626
import org.elasticsearch.common.Priority;
@@ -130,9 +130,8 @@ private record UpdateDatabaseConfigurationTask(ActionListener<AcknowledgedRespon
130130
ClusterStateTaskListener {
131131

132132
ClusterState execute(ClusterState currentState) throws Exception {
133-
IngestGeoIpMetadata geoIpMeta = currentState.metadata()
134-
.getProject()
135-
.custom(IngestGeoIpMetadata.TYPE, IngestGeoIpMetadata.EMPTY);
133+
final var project = currentState.metadata().getProject();
134+
IngestGeoIpMetadata geoIpMeta = project.custom(IngestGeoIpMetadata.TYPE, IngestGeoIpMetadata.EMPTY);
136135

137136
String id = database.id();
138137
final DatabaseConfigurationMetadata existingDatabase = geoIpMeta.getDatabases().get(id);
@@ -160,9 +159,8 @@ ClusterState execute(ClusterState currentState) throws Exception {
160159
logger.debug("updating existing database configuration [{}]", id);
161160
}
162161

163-
Metadata currentMeta = currentState.metadata();
164162
return ClusterState.builder(currentState)
165-
.metadata(Metadata.builder(currentMeta).putCustom(IngestGeoIpMetadata.TYPE, geoIpMeta))
163+
.putProjectMetadata(ProjectMetadata.builder(project).putCustom(IngestGeoIpMetadata.TYPE, geoIpMeta))
166164
.build();
167165
}
168166

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,6 +1712,7 @@ public Builder putCustom(String type, ClusterCustom custom) {
17121712
return this;
17131713
}
17141714

1715+
@Deprecated(forRemoval = true)
17151716
public Builder putCustom(String type, ProjectCustom custom) {
17161717
return putProjectCustom(type, custom);
17171718
}

server/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2593,7 +2593,8 @@ private void markRepoCorrupted(long corruptedGeneration, Exception originalExcep
25932593
new ClusterStateUpdateTask() {
25942594
@Override
25952595
public ClusterState execute(ClusterState currentState) {
2596-
final RepositoriesMetadata state = RepositoriesMetadata.get(currentState);
2596+
final var project = currentState.metadata().getDefaultProject();
2597+
final RepositoriesMetadata state = RepositoriesMetadata.get(project);
25972598
final RepositoryMetadata repoState = state.repository(metadata.name());
25982599
if (repoState.generation() != corruptedGeneration) {
25992600
throw new IllegalStateException(
@@ -2605,8 +2606,8 @@ public ClusterState execute(ClusterState currentState) {
26052606
);
26062607
}
26072608
return ClusterState.builder(currentState)
2608-
.metadata(
2609-
Metadata.builder(currentState.metadata())
2609+
.putProjectMetadata(
2610+
ProjectMetadata.builder(project)
26102611
.putCustom(
26112612
RepositoriesMetadata.TYPE,
26122613
state.withUpdatedGeneration(
@@ -2615,7 +2616,6 @@ public ClusterState execute(ClusterState currentState) {
26152616
repoState.pendingGeneration()
26162617
)
26172618
)
2618-
.build()
26192619
)
26202620
.build();
26212621
}
@@ -2787,12 +2787,13 @@ public ClusterState execute(ClusterState currentState) {
27872787
+ "] must be larger than latest known generation ["
27882788
+ latestKnownRepoGen.get()
27892789
+ "]";
2790+
final var project = currentState.metadata().getDefaultProject();
27902791
return ClusterState.builder(currentState)
2791-
.metadata(
2792-
Metadata.builder(currentState.getMetadata())
2792+
.putProjectMetadata(
2793+
ProjectMetadata.builder(project)
27932794
.putCustom(
27942795
RepositoriesMetadata.TYPE,
2795-
RepositoriesMetadata.get(currentState).withUpdatedGeneration(repoName, safeGeneration, newGen)
2796+
RepositoriesMetadata.get(project).withUpdatedGeneration(repoName, safeGeneration, newGen)
27962797
)
27972798
.build()
27982799
)

server/src/main/java/org/elasticsearch/script/ScriptService.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.elasticsearch.cluster.ClusterState;
2323
import org.elasticsearch.cluster.ClusterStateApplier;
2424
import org.elasticsearch.cluster.ClusterStateUpdateTask;
25-
import org.elasticsearch.cluster.metadata.Metadata;
2625
import org.elasticsearch.cluster.service.ClusterService;
2726
import org.elasticsearch.common.Strings;
2827
import org.elasticsearch.common.logging.DeprecationCategory;
@@ -733,11 +732,11 @@ public void putStoredScript(
733732
submitUnbatchedTask(clusterService, "put-script-" + request.id(), new AckedClusterStateUpdateTask(request, listener) {
734733
@Override
735734
public ClusterState execute(ClusterState currentState) {
736-
ScriptMetadata smd = currentState.metadata().getProject().custom(ScriptMetadata.TYPE);
737-
smd = ScriptMetadata.putStoredScript(smd, request.id(), source);
738-
Metadata.Builder mdb = Metadata.builder(currentState.getMetadata()).putCustom(ScriptMetadata.TYPE, smd);
735+
final var project = currentState.metadata().getProject();
736+
final ScriptMetadata originalSmd = project.custom(ScriptMetadata.TYPE);
737+
final ScriptMetadata updatedSmd = ScriptMetadata.putStoredScript(originalSmd, request.id(), source);
739738

740-
return ClusterState.builder(currentState).metadata(mdb).build();
739+
return currentState.copyAndUpdateProject(project.id(), builder -> builder.putCustom(ScriptMetadata.TYPE, updatedSmd));
741740
}
742741
});
743742
}
@@ -750,11 +749,11 @@ public static void deleteStoredScript(
750749
submitUnbatchedTask(clusterService, "delete-script-" + request.id(), new AckedClusterStateUpdateTask(request, listener) {
751750
@Override
752751
public ClusterState execute(ClusterState currentState) {
753-
ScriptMetadata smd = currentState.metadata().getProject().custom(ScriptMetadata.TYPE);
754-
smd = ScriptMetadata.deleteStoredScript(smd, request.id());
755-
Metadata.Builder mdb = Metadata.builder(currentState.getMetadata()).putCustom(ScriptMetadata.TYPE, smd);
752+
final var project = currentState.metadata().getProject();
753+
final ScriptMetadata originalSmd = project.custom(ScriptMetadata.TYPE);
754+
final ScriptMetadata updatedSmd = ScriptMetadata.deleteStoredScript(originalSmd, request.id());
756755

757-
return ClusterState.builder(currentState).metadata(mdb).build();
756+
return currentState.copyAndUpdateProject(project.id(), builder -> builder.putCustom(ScriptMetadata.TYPE, updatedSmd));
758757
}
759758
});
760759
}

server/src/main/java/org/elasticsearch/snapshots/RestoreService.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,8 @@ private void ensureSnapshotNotDeleted(ClusterState currentState) {
15831583
}
15841584

15851585
private void applyGlobalStateRestore(ClusterState currentState, Metadata.Builder mdBuilder) {
1586+
@FixForMultiProject
1587+
final var projectBuilder = mdBuilder.getProject(ProjectId.DEFAULT);
15861588
if (metadata.persistentSettings() != null) {
15871589
Settings settings = metadata.persistentSettings();
15881590
if (request.skipOperatorOnlyState()) {
@@ -1607,13 +1609,13 @@ private void applyGlobalStateRestore(ClusterState currentState, Metadata.Builder
16071609
if (metadata.getProject().templates() != null) {
16081610
// TODO: Should all existing templates be deleted first?
16091611
for (IndexTemplateMetadata cursor : metadata.getProject().templates().values()) {
1610-
mdBuilder.put(cursor);
1612+
projectBuilder.put(cursor);
16111613
}
16121614
}
16131615

16141616
// override existing restorable customs (as there might be nothing in snapshot to override them)
16151617
mdBuilder.removeCustomIf((key, value) -> value.isRestorable());
1616-
mdBuilder.removeProjectCustomIf((key, value) -> value.isRestorable());
1618+
projectBuilder.removeCustomIf((key, value) -> value.isRestorable());
16171619

16181620
// restore customs from the snapshot
16191621
if (metadata.customs() != null) {
@@ -1630,7 +1632,7 @@ private void applyGlobalStateRestore(ClusterState currentState, Metadata.Builder
16301632
for (var entry : metadata.getProject().customs().entrySet()) {
16311633
if (entry.getValue().isRestorable()) {
16321634
// Also, don't restore data streams here, we already added them to the metadata builder above
1633-
mdBuilder.putCustom(entry.getKey(), entry.getValue());
1635+
projectBuilder.putCustom(entry.getKey(), entry.getValue());
16341636
}
16351637
}
16361638
}

server/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4090,10 +4090,11 @@ public ClusterState execute(BatchExecutionContext<SnapshotTask> batchExecutionCo
40904090
// Handle the tasks to apply the shard snapshot updates (ShardSnapshotUpdate tasks).
40914091
SnapshotsInProgress snapshotsInProgress = shardsUpdateContext.computeUpdatedState();
40924092

4093-
final RegisteredPolicySnapshots.Builder registeredPolicySnapshots = state.metadata()
4094-
.getProject()
4095-
.custom(RegisteredPolicySnapshots.TYPE, RegisteredPolicySnapshots.EMPTY)
4096-
.builder();
4093+
final var project = state.metadata().getProject();
4094+
final RegisteredPolicySnapshots.Builder registeredPolicySnapshots = project.custom(
4095+
RegisteredPolicySnapshots.TYPE,
4096+
RegisteredPolicySnapshots.EMPTY
4097+
).builder();
40974098
// Handle the tasks to create new snapshots (CreateSnapshotTask tasks).
40984099
for (final var taskContext : batchExecutionContext.taskContexts()) {
40994100
if (taskContext.getTask() instanceof CreateSnapshotTask task) {
@@ -4135,7 +4136,9 @@ public ClusterState execute(BatchExecutionContext<SnapshotTask> batchExecutionCo
41354136

41364137
return ClusterState.builder(state)
41374138
.putCustom(SnapshotsInProgress.TYPE, snapshotsInProgress)
4138-
.metadata(Metadata.builder(state.metadata()).putCustom(RegisteredPolicySnapshots.TYPE, registeredPolicySnapshots.build()))
4139+
.putProjectMetadata(
4140+
ProjectMetadata.builder(project).putCustom(RegisteredPolicySnapshots.TYPE, registeredPolicySnapshots.build())
4141+
)
41394142
.build();
41404143
}
41414144

test/framework/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreTestUtil.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import org.elasticsearch.cluster.ClusterStateUpdateTask;
1919
import org.elasticsearch.cluster.metadata.IndexMetadata;
2020
import org.elasticsearch.cluster.metadata.Metadata;
21+
import org.elasticsearch.cluster.metadata.ProjectId;
22+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
2123
import org.elasticsearch.cluster.metadata.RepositoriesMetadata;
2224
import org.elasticsearch.cluster.metadata.RepositoryMetadata;
2325
import org.elasticsearch.cluster.node.DiscoveryNode;
@@ -391,18 +393,25 @@ public static ClusterService mockClusterService() {
391393
/**
392394
* Creates a mocked {@link ClusterService} for use in {@link BlobStoreRepository} related tests that mocks out all the necessary
393395
* functionality to make {@link BlobStoreRepository} work. Initializes the cluster state with a {@link RepositoriesMetadata} instance
394-
* that contains the given {@code metadata}.
396+
* that contains the given {@code repositoryMetadata}.
395397
*
396-
* @param metadata RepositoryMetadata to initialize the cluster state with
398+
* @param repositoryMetadata RepositoryMetadata to initialize the cluster state with
397399
* @return Mock ClusterService
398400
*/
399-
public static ClusterService mockClusterService(RepositoryMetadata metadata) {
401+
public static ClusterService mockClusterService(RepositoryMetadata repositoryMetadata) {
400402
return mockClusterService(
401403
ClusterState.builder(ClusterState.EMPTY_STATE)
402404
.metadata(
403405
Metadata.builder()
404406
.clusterUUID(UUIDs.randomBase64UUID(random()))
405-
.putCustom(RepositoriesMetadata.TYPE, new RepositoriesMetadata(Collections.singletonList(metadata)))
407+
.put(
408+
ProjectMetadata.builder(ProjectId.DEFAULT)
409+
.putCustom(
410+
RepositoriesMetadata.TYPE,
411+
new RepositoriesMetadata(Collections.singletonList(repositoryMetadata))
412+
)
413+
.build()
414+
)
406415
.build()
407416
)
408417
.build()

x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.elasticsearch.cluster.metadata.IndexAbstraction;
2424
import org.elasticsearch.cluster.metadata.IndexMetadata;
2525
import org.elasticsearch.cluster.metadata.Metadata;
26+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
2627
import org.elasticsearch.cluster.routing.IndexRoutingTable;
2728
import org.elasticsearch.cluster.service.ClusterService;
2829
import org.elasticsearch.common.component.AbstractLifecycleComponent;
@@ -879,7 +880,8 @@ static String getFollowerIndexName(AutoFollowPattern autoFollowPattern, String l
879880

880881
static Function<ClusterState, ClusterState> recordLeaderIndexAsFollowFunction(String name, Index indexToFollow) {
881882
return currentState -> {
882-
AutoFollowMetadata currentAutoFollowMetadata = currentState.metadata().getProject().custom(AutoFollowMetadata.TYPE);
883+
final var project = currentState.metadata().getProject();
884+
AutoFollowMetadata currentAutoFollowMetadata = project.custom(AutoFollowMetadata.TYPE);
883885
Map<String, List<String>> newFollowedIndexUUIDS = new HashMap<>(currentAutoFollowMetadata.getFollowedLeaderIndexUUIDs());
884886
if (newFollowedIndexUUIDS.containsKey(name) == false) {
885887
// A delete auto follow pattern request can have removed the auto follow pattern while we want to update
@@ -900,9 +902,7 @@ static Function<ClusterState, ClusterState> recordLeaderIndexAsFollowFunction(St
900902
currentAutoFollowMetadata.getHeaders()
901903
);
902904
return ClusterState.builder(currentState)
903-
.metadata(
904-
Metadata.builder(currentState.getMetadata()).putCustom(AutoFollowMetadata.TYPE, newAutoFollowMetadata).build()
905-
)
905+
.putProjectMetadata(ProjectMetadata.builder(project).putCustom(AutoFollowMetadata.TYPE, newAutoFollowMetadata).build())
906906
.build();
907907
};
908908
}
@@ -920,7 +920,8 @@ static Function<ClusterState, ClusterState> cleanFollowedRemoteIndices(
920920
final List<String> autoFollowPatternNames
921921
) {
922922
return currentState -> {
923-
AutoFollowMetadata currentAutoFollowMetadata = currentState.metadata().getProject().custom(AutoFollowMetadata.TYPE);
923+
final var currentProject = currentState.metadata().getProject();
924+
AutoFollowMetadata currentAutoFollowMetadata = currentProject.custom(AutoFollowMetadata.TYPE);
924925
Map<String, List<String>> autoFollowPatternNameToFollowedIndexUUIDs = new HashMap<>(
925926
currentAutoFollowMetadata.getFollowedLeaderIndexUUIDs()
926927
);
@@ -958,8 +959,8 @@ static Function<ClusterState, ClusterState> cleanFollowedRemoteIndices(
958959
currentAutoFollowMetadata.getHeaders()
959960
);
960961
return ClusterState.builder(currentState)
961-
.metadata(
962-
Metadata.builder(currentState.getMetadata()).putCustom(AutoFollowMetadata.TYPE, newAutoFollowMetadata).build()
962+
.putProjectMetadata(
963+
ProjectMetadata.builder(currentProject).putCustom(AutoFollowMetadata.TYPE, newAutoFollowMetadata).build()
963964
)
964965
.build();
965966
} else {

x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportActivateAutoFollowPatternAction.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ private void submitUnbatchedTask(@SuppressWarnings("SameParameterValue") String
7676
}
7777

7878
static ClusterState innerActivate(final Request request, ClusterState currentState) {
79-
final AutoFollowMetadata autoFollowMetadata = currentState.metadata().getProject().custom(AutoFollowMetadata.TYPE);
79+
final var project = currentState.metadata().getProject();
80+
final AutoFollowMetadata autoFollowMetadata = project.custom(AutoFollowMetadata.TYPE);
8081
if (autoFollowMetadata == null) {
8182
throw new ResourceNotFoundException("auto-follow pattern [{}] is missing", request.getName());
8283
}
@@ -114,8 +115,9 @@ static ClusterState innerActivate(final Request request, ClusterState currentSta
114115
)
115116
);
116117

117-
return currentState.copyAndUpdateMetadata(
118-
metadata -> metadata.putCustom(
118+
return currentState.copyAndUpdateProject(
119+
project.id(),
120+
builder -> builder.putCustom(
119121
AutoFollowMetadata.TYPE,
120122
new AutoFollowMetadata(newPatterns, autoFollowMetadata.getFollowedLeaderIndexUUIDs(), autoFollowMetadata.getHeaders())
121123
)

0 commit comments

Comments
 (0)