Skip to content
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 @@ -21,7 +21,7 @@
import org.elasticsearch.cluster.SimpleBatchedExecutor;
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.cluster.service.MasterServiceTaskQueue;
import org.elasticsearch.common.Priority;
Expand Down Expand Up @@ -103,17 +103,17 @@ private record DeleteDatabaseConfigurationTask(ActionListener<AcknowledgedRespon
ClusterStateTaskListener {

ClusterState execute(ClusterState currentState) throws Exception {
final IngestGeoIpMetadata geoIpMeta = currentState.metadata()
.getProject()
.custom(IngestGeoIpMetadata.TYPE, IngestGeoIpMetadata.EMPTY);
final var project = currentState.metadata().getProject();
final IngestGeoIpMetadata geoIpMeta = project.custom(IngestGeoIpMetadata.TYPE, IngestGeoIpMetadata.EMPTY);

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

Metadata currentMeta = currentState.metadata();
return ClusterState.builder(currentState)
.metadata(Metadata.builder(currentMeta).putCustom(IngestGeoIpMetadata.TYPE, new IngestGeoIpMetadata(databases)))
.putProjectMetadata(
ProjectMetadata.builder(project).putCustom(IngestGeoIpMetadata.TYPE, new IngestGeoIpMetadata(databases))
)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.elasticsearch.cluster.SimpleBatchedExecutor;
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.cluster.service.MasterServiceTaskQueue;
import org.elasticsearch.common.Priority;
Expand Down Expand Up @@ -130,9 +130,8 @@ private record UpdateDatabaseConfigurationTask(ActionListener<AcknowledgedRespon
ClusterStateTaskListener {

ClusterState execute(ClusterState currentState) throws Exception {
IngestGeoIpMetadata geoIpMeta = currentState.metadata()
.getProject()
.custom(IngestGeoIpMetadata.TYPE, IngestGeoIpMetadata.EMPTY);
final var project = currentState.metadata().getProject();
IngestGeoIpMetadata geoIpMeta = project.custom(IngestGeoIpMetadata.TYPE, IngestGeoIpMetadata.EMPTY);

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

Metadata currentMeta = currentState.metadata();
return ClusterState.builder(currentState)
.metadata(Metadata.builder(currentMeta).putCustom(IngestGeoIpMetadata.TYPE, geoIpMeta))
.putProjectMetadata(ProjectMetadata.builder(project).putCustom(IngestGeoIpMetadata.TYPE, geoIpMeta))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,7 @@ public Builder putCustom(String type, ClusterCustom custom) {
return this;
}

@Deprecated(forRemoval = true)
public Builder putCustom(String type, ProjectCustom custom) {
return putProjectCustom(type, custom);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2593,7 +2593,8 @@ private void markRepoCorrupted(long corruptedGeneration, Exception originalExcep
new ClusterStateUpdateTask() {
@Override
public ClusterState execute(ClusterState currentState) {
final RepositoriesMetadata state = RepositoriesMetadata.get(currentState);
final var project = currentState.metadata().getDefaultProject();
final RepositoriesMetadata state = RepositoriesMetadata.get(project);
final RepositoryMetadata repoState = state.repository(metadata.name());
if (repoState.generation() != corruptedGeneration) {
throw new IllegalStateException(
Expand All @@ -2605,8 +2606,8 @@ public ClusterState execute(ClusterState currentState) {
);
}
return ClusterState.builder(currentState)
.metadata(
Metadata.builder(currentState.metadata())
.putProjectMetadata(
ProjectMetadata.builder(project)
.putCustom(
RepositoriesMetadata.TYPE,
state.withUpdatedGeneration(
Expand All @@ -2615,7 +2616,6 @@ public ClusterState execute(ClusterState currentState) {
repoState.pendingGeneration()
)
)
.build()
)
.build();
}
Expand Down Expand Up @@ -2787,12 +2787,13 @@ public ClusterState execute(ClusterState currentState) {
+ "] must be larger than latest known generation ["
+ latestKnownRepoGen.get()
+ "]";
final var project = currentState.metadata().getDefaultProject();
return ClusterState.builder(currentState)
.metadata(
Metadata.builder(currentState.getMetadata())
.putProjectMetadata(
ProjectMetadata.builder(project)
.putCustom(
RepositoriesMetadata.TYPE,
RepositoriesMetadata.get(currentState).withUpdatedGeneration(repoName, safeGeneration, newGen)
RepositoriesMetadata.get(project).withUpdatedGeneration(repoName, safeGeneration, newGen)
)
.build()
)
Expand Down
17 changes: 8 additions & 9 deletions server/src/main/java/org/elasticsearch/script/ScriptService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateApplier;
import org.elasticsearch.cluster.ClusterStateUpdateTask;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.logging.DeprecationCategory;
Expand Down Expand Up @@ -733,11 +732,11 @@ public void putStoredScript(
submitUnbatchedTask(clusterService, "put-script-" + request.id(), new AckedClusterStateUpdateTask(request, listener) {
@Override
public ClusterState execute(ClusterState currentState) {
ScriptMetadata smd = currentState.metadata().getProject().custom(ScriptMetadata.TYPE);
smd = ScriptMetadata.putStoredScript(smd, request.id(), source);
Metadata.Builder mdb = Metadata.builder(currentState.getMetadata()).putCustom(ScriptMetadata.TYPE, smd);
final var project = currentState.metadata().getProject();
final ScriptMetadata originalSmd = project.custom(ScriptMetadata.TYPE);
final ScriptMetadata updatedSmd = ScriptMetadata.putStoredScript(originalSmd, request.id(), source);

return ClusterState.builder(currentState).metadata(mdb).build();
return currentState.copyAndUpdateProject(project.id(), builder -> builder.putCustom(ScriptMetadata.TYPE, updatedSmd));
}
});
}
Expand All @@ -750,11 +749,11 @@ public static void deleteStoredScript(
submitUnbatchedTask(clusterService, "delete-script-" + request.id(), new AckedClusterStateUpdateTask(request, listener) {
@Override
public ClusterState execute(ClusterState currentState) {
ScriptMetadata smd = currentState.metadata().getProject().custom(ScriptMetadata.TYPE);
smd = ScriptMetadata.deleteStoredScript(smd, request.id());
Metadata.Builder mdb = Metadata.builder(currentState.getMetadata()).putCustom(ScriptMetadata.TYPE, smd);
final var project = currentState.metadata().getProject();
final ScriptMetadata originalSmd = project.custom(ScriptMetadata.TYPE);
final ScriptMetadata updatedSmd = ScriptMetadata.deleteStoredScript(originalSmd, request.id());

return ClusterState.builder(currentState).metadata(mdb).build();
return currentState.copyAndUpdateProject(project.id(), builder -> builder.putCustom(ScriptMetadata.TYPE, updatedSmd));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,8 @@ private void ensureSnapshotNotDeleted(ClusterState currentState) {
}

private void applyGlobalStateRestore(ClusterState currentState, Metadata.Builder mdBuilder) {
@FixForMultiProject
final var projectBuilder = mdBuilder.getProject(ProjectId.DEFAULT);
if (metadata.persistentSettings() != null) {
Settings settings = metadata.persistentSettings();
if (request.skipOperatorOnlyState()) {
Expand All @@ -1607,13 +1609,13 @@ private void applyGlobalStateRestore(ClusterState currentState, Metadata.Builder
if (metadata.getProject().templates() != null) {
// TODO: Should all existing templates be deleted first?
for (IndexTemplateMetadata cursor : metadata.getProject().templates().values()) {
mdBuilder.put(cursor);
projectBuilder.put(cursor);
}
}

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

// restore customs from the snapshot
if (metadata.customs() != null) {
Expand All @@ -1630,7 +1632,7 @@ private void applyGlobalStateRestore(ClusterState currentState, Metadata.Builder
for (var entry : metadata.getProject().customs().entrySet()) {
if (entry.getValue().isRestorable()) {
// Also, don't restore data streams here, we already added them to the metadata builder above
mdBuilder.putCustom(entry.getKey(), entry.getValue());
projectBuilder.putCustom(entry.getKey(), entry.getValue());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4090,10 +4090,11 @@ public ClusterState execute(BatchExecutionContext<SnapshotTask> batchExecutionCo
// Handle the tasks to apply the shard snapshot updates (ShardSnapshotUpdate tasks).
SnapshotsInProgress snapshotsInProgress = shardsUpdateContext.computeUpdatedState();

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

return ClusterState.builder(state)
.putCustom(SnapshotsInProgress.TYPE, snapshotsInProgress)
.metadata(Metadata.builder(state.metadata()).putCustom(RegisteredPolicySnapshots.TYPE, registeredPolicySnapshots.build()))
.putProjectMetadata(
ProjectMetadata.builder(project).putCustom(RegisteredPolicySnapshots.TYPE, registeredPolicySnapshots.build())
)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.elasticsearch.cluster.ClusterStateUpdateTask;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.ProjectId;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.cluster.metadata.RepositoriesMetadata;
import org.elasticsearch.cluster.metadata.RepositoryMetadata;
import org.elasticsearch.cluster.node.DiscoveryNode;
Expand Down Expand Up @@ -391,18 +393,25 @@ public static ClusterService mockClusterService() {
/**
* Creates a mocked {@link ClusterService} for use in {@link BlobStoreRepository} related tests that mocks out all the necessary
* functionality to make {@link BlobStoreRepository} work. Initializes the cluster state with a {@link RepositoriesMetadata} instance
* that contains the given {@code metadata}.
* that contains the given {@code repositoryMetadata}.
*
* @param metadata RepositoryMetadata to initialize the cluster state with
* @param repositoryMetadata RepositoryMetadata to initialize the cluster state with
* @return Mock ClusterService
*/
public static ClusterService mockClusterService(RepositoryMetadata metadata) {
public static ClusterService mockClusterService(RepositoryMetadata repositoryMetadata) {
return mockClusterService(
ClusterState.builder(ClusterState.EMPTY_STATE)
.metadata(
Metadata.builder()
.clusterUUID(UUIDs.randomBase64UUID(random()))
.putCustom(RepositoriesMetadata.TYPE, new RepositoriesMetadata(Collections.singletonList(metadata)))
.put(
ProjectMetadata.builder(ProjectId.DEFAULT)
.putCustom(
RepositoriesMetadata.TYPE,
new RepositoriesMetadata(Collections.singletonList(repositoryMetadata))
)
.build()
)
.build()
)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.elasticsearch.cluster.metadata.IndexAbstraction;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.cluster.routing.IndexRoutingTable;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.component.AbstractLifecycleComponent;
Expand Down Expand Up @@ -879,7 +880,8 @@ static String getFollowerIndexName(AutoFollowPattern autoFollowPattern, String l

static Function<ClusterState, ClusterState> recordLeaderIndexAsFollowFunction(String name, Index indexToFollow) {
return currentState -> {
AutoFollowMetadata currentAutoFollowMetadata = currentState.metadata().getProject().custom(AutoFollowMetadata.TYPE);
final var project = currentState.metadata().getProject();
AutoFollowMetadata currentAutoFollowMetadata = project.custom(AutoFollowMetadata.TYPE);
Map<String, List<String>> newFollowedIndexUUIDS = new HashMap<>(currentAutoFollowMetadata.getFollowedLeaderIndexUUIDs());
if (newFollowedIndexUUIDS.containsKey(name) == false) {
// A delete auto follow pattern request can have removed the auto follow pattern while we want to update
Expand All @@ -900,9 +902,7 @@ static Function<ClusterState, ClusterState> recordLeaderIndexAsFollowFunction(St
currentAutoFollowMetadata.getHeaders()
);
return ClusterState.builder(currentState)
.metadata(
Metadata.builder(currentState.getMetadata()).putCustom(AutoFollowMetadata.TYPE, newAutoFollowMetadata).build()
)
.putProjectMetadata(ProjectMetadata.builder(project).putCustom(AutoFollowMetadata.TYPE, newAutoFollowMetadata).build())
.build();
};
}
Expand All @@ -920,7 +920,8 @@ static Function<ClusterState, ClusterState> cleanFollowedRemoteIndices(
final List<String> autoFollowPatternNames
) {
return currentState -> {
AutoFollowMetadata currentAutoFollowMetadata = currentState.metadata().getProject().custom(AutoFollowMetadata.TYPE);
final var currentProject = currentState.metadata().getProject();
AutoFollowMetadata currentAutoFollowMetadata = currentProject.custom(AutoFollowMetadata.TYPE);
Map<String, List<String>> autoFollowPatternNameToFollowedIndexUUIDs = new HashMap<>(
currentAutoFollowMetadata.getFollowedLeaderIndexUUIDs()
);
Expand Down Expand Up @@ -958,8 +959,8 @@ static Function<ClusterState, ClusterState> cleanFollowedRemoteIndices(
currentAutoFollowMetadata.getHeaders()
);
return ClusterState.builder(currentState)
.metadata(
Metadata.builder(currentState.getMetadata()).putCustom(AutoFollowMetadata.TYPE, newAutoFollowMetadata).build()
.putProjectMetadata(
ProjectMetadata.builder(currentProject).putCustom(AutoFollowMetadata.TYPE, newAutoFollowMetadata).build()
)
.build();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ private void submitUnbatchedTask(@SuppressWarnings("SameParameterValue") String
}

static ClusterState innerActivate(final Request request, ClusterState currentState) {
final AutoFollowMetadata autoFollowMetadata = currentState.metadata().getProject().custom(AutoFollowMetadata.TYPE);
final var project = currentState.metadata().getProject();
final AutoFollowMetadata autoFollowMetadata = project.custom(AutoFollowMetadata.TYPE);
if (autoFollowMetadata == null) {
throw new ResourceNotFoundException("auto-follow pattern [{}] is missing", request.getName());
}
Expand Down Expand Up @@ -114,8 +115,9 @@ static ClusterState innerActivate(final Request request, ClusterState currentSta
)
);

return currentState.copyAndUpdateMetadata(
metadata -> metadata.putCustom(
return currentState.copyAndUpdateProject(
project.id(),
builder -> builder.putCustom(
AutoFollowMetadata.TYPE,
new AutoFollowMetadata(newPatterns, autoFollowMetadata.getFollowedLeaderIndexUUIDs(), autoFollowMetadata.getHeaders())
)
Expand Down
Loading