Skip to content

Commit

Permalink
Add an id to ProjectMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
tvernum committed Jun 20, 2024
1 parent 4dd8ac7 commit c91ad2d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
11 changes: 11 additions & 0 deletions server/src/main/java/org/elasticsearch/cluster/ProjectId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.cluster;

public record ProjectId(String id) {}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.elasticsearch.cluster.DiffableUtils.MapDiff;
import org.elasticsearch.cluster.NamedDiffable;
import org.elasticsearch.cluster.NamedDiffableValueSerializer;
import org.elasticsearch.cluster.ProjectId;
import org.elasticsearch.cluster.SimpleDiffable;
import org.elasticsearch.cluster.block.ClusterBlock;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
Expand Down Expand Up @@ -267,6 +268,7 @@ private Metadata(
clusterCustoms,
reservedStateMetadata,
new ProjectMetadata(
new ProjectId(clusterUUID),
totalNumberOfShards,
totalOpenIndexShards,
indices,
Expand Down Expand Up @@ -1165,6 +1167,7 @@ private static Metadata readWithEmbeddedProject(StreamInput in) throws IOExcepti
Builder builder = new Builder();
builder.version = in.readLong();
builder.clusterUUID = in.readString();
builder.project.id(new ProjectId(builder.clusterUUID));
builder.clusterUUIDCommitted = in.readBoolean();
builder.coordinationMetadata(new CoordinationMetadata(in));
builder.transientSettings(readSettingsFromStream(in));
Expand Down Expand Up @@ -1337,6 +1340,10 @@ private Builder(Map<String, MappingMetadata> mappingsByHash, int indexCountHint)
project = new ProjectMetadata.Builder(mappingsByHash, indexCountHint);
}

public ProjectMetadata.Builder project() {
return project;
}

public Builder put(IndexMetadata.Builder indexMetadataBuilder) {
project.put(indexMetadataBuilder);
return this;
Expand Down Expand Up @@ -1605,6 +1612,9 @@ public Builder version(long version) {

public Builder clusterUUID(String clusterUUID) {
this.clusterUUID = clusterUUID;
if (project.id() == null) {
project.id(new ProjectId(clusterUUID));
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.elasticsearch.cluster.Diffable;
import org.elasticsearch.cluster.DiffableUtils;
import org.elasticsearch.cluster.NamedDiffableValueSerializer;
import org.elasticsearch.cluster.ProjectId;
import org.elasticsearch.cluster.SimpleDiffable;
import org.elasticsearch.cluster.routing.RoutingTable;
import org.elasticsearch.common.Strings;
Expand Down Expand Up @@ -94,6 +95,8 @@ public interface ProjectCustom extends Metadata.MetadataCustom<ProjectCustom> {}
/*TODO private*/ static final NamedDiffableValueSerializer<ProjectCustom> PROJECT_CUSTOM_VALUE_SERIALIZER =
new NamedDiffableValueSerializer<>(ProjectCustom.class);

private final ProjectId projectId;

private final ImmutableOpenMap<String, IndexMetadata> indices;
private final ImmutableOpenMap<String, Set<Index>> aliasedIndices;
private final ImmutableOpenMap<String, IndexTemplateMetadata> templates;
Expand Down Expand Up @@ -125,6 +128,7 @@ private interface AliasInfoSetter {
}

ProjectMetadata(
ProjectId id,
int totalNumberOfShards,
int totalOpenIndexShards,
ImmutableOpenMap<String, IndexMetadata> indices,
Expand All @@ -141,6 +145,7 @@ private interface AliasInfoSetter {
Map<String, MappingMetadata> mappingsByHash,
IndexVersion oldestIndexVersion
) {
this.projectId = id;
this.indices = indices;
this.aliasedIndices = aliasedIndices;
this.templates = templates;
Expand Down Expand Up @@ -219,6 +224,7 @@ public ProjectMetadata withLifecycleState(final Index index, final LifecycleExec
// have changed, and hence it is expensive -- since we are changing so little about the metadata
// (and at a leaf in the object tree), we can bypass that validation for efficiency's sake
return new ProjectMetadata(
projectId,
totalNumberOfShards,
totalOpenIndexShards,
builder.build(),
Expand Down Expand Up @@ -250,6 +256,7 @@ public ProjectMetadata withIndexSettingsUpdates(final Map<Index, Settings> updat
);
});
return new ProjectMetadata(
projectId,
totalNumberOfShards,
totalOpenIndexShards,
builder.build(),
Expand Down Expand Up @@ -283,6 +290,7 @@ public ProjectMetadata withAllocationAndTermUpdatesOnly(Map<String, IndexMetadat
final var updatedIndicesBuilder = ImmutableOpenMap.builder(indices);
updatedIndicesBuilder.putAllFromMap(updates);
return new ProjectMetadata(
projectId,
totalNumberOfShards,
totalOpenIndexShards,
updatedIndicesBuilder.build(),
Expand Down Expand Up @@ -370,6 +378,7 @@ public ProjectMetadata withAddedIndex(IndexMetadata index) {
Builder.validateAlias(entry.getKey(), aliasIndices);
}
return new ProjectMetadata(
projectId,
totalNumberOfShards + index.getTotalNumberOfShards(),
totalOpenIndexShards + (index.getState() == IndexMetadata.State.OPEN ? index.getTotalNumberOfShards() : 0),
indicesMap,
Expand Down Expand Up @@ -1150,6 +1159,7 @@ public static boolean isGlobalStateEquals(ProjectMetadata metadata1, ProjectMeta
}

public void writeTo(StreamOutput out) throws IOException {
out.writeString(projectId.id());
out.writeMapValues(mappingsByHash);
out.writeVInt(indices.size());
for (IndexMetadata indexMetadata : this) {
Expand All @@ -1161,6 +1171,7 @@ public void writeTo(StreamOutput out) throws IOException {

public static ProjectMetadata readFrom(StreamInput in) throws IOException {
var builder = new Builder();
builder.id(new ProjectId(in.readString()));

final Map<String, MappingMetadata> mappingMetadataMap = in.readMapValues(MappingMetadata::new, MappingMetadata::getSha256);
final Function<String, MappingMetadata> mappingLookup;
Expand Down Expand Up @@ -1283,6 +1294,7 @@ public ProjectMetadata apply(ProjectMetadata part) {
// mappings in the builder
final var updatedIndices = indices.apply(part.indices);
var builder = new Builder(part.mappingsByHash, updatedIndices.size());
builder.id(part.projectId);
builder.indices(updatedIndices);
builder.templates(templates.apply(part.templates));
builder.customs(customs.apply(part.customs));
Expand Down Expand Up @@ -1320,6 +1332,7 @@ static DiffableUtils.MapDiff<String, ProjectCustom, ImmutableOpenMap<String, Pro
}

public static class Builder {
private ProjectId id;
private final ImmutableOpenMap.Builder<String, IndexMetadata> indices;
private final ImmutableOpenMap.Builder<String, Set<Index>> aliasedIndices;
private final ImmutableOpenMap.Builder<String, IndexTemplateMetadata> templates;
Expand All @@ -1340,6 +1353,7 @@ public Builder() {
}

Builder(ProjectMetadata metadata) {
this.id = metadata.projectId;
this.indices = ImmutableOpenMap.builder(metadata.indices);
this.aliasedIndices = ImmutableOpenMap.builder(metadata.aliasedIndices);
this.templates = ImmutableOpenMap.builder(metadata.templates);
Expand Down Expand Up @@ -1373,6 +1387,15 @@ public Builder put(IndexMetadata.Builder indexMetadataBuilder) {
return this;
}

public ProjectId id() {
return id;
}

public Builder id(ProjectId id) {
this.id = id;
return this;
}

public Builder put(IndexMetadata indexMetadata, boolean incrementVersion) {
final String name = indexMetadata.getIndex().getName();
indexMetadata = dedupeMapping(indexMetadata);
Expand Down Expand Up @@ -1848,6 +1871,7 @@ public ProjectMetadata build(boolean skipNameCollisionChecks) {
String[] visibleClosedIndicesArray = visibleClosedIndices.toArray(Strings.EMPTY_ARRAY);

return new ProjectMetadata(
id == null ? new ProjectId("_na_") : id, // TODO, we shouldn't default this, but need to for now.
totalNumberOfShards,
totalOpenIndexShards,
indicesMap,
Expand Down Expand Up @@ -2173,6 +2197,10 @@ public static ProjectMetadata.Builder fromXContent(XContentParser parser) throws
} else {
parseCustoms(parser, currentFieldName, builder);
}
} else if (token == XContentParser.Token.VALUE_STRING) {
if ("id".equals(currentFieldName)) {
builder.id(new ProjectId(parser.text()));
}
} else {
throw new IllegalArgumentException("Unexpected token " + token);
}
Expand Down Expand Up @@ -2258,6 +2286,9 @@ private Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params p, Me
)
);

return Iterators.concat(templates, indices, customs);
final Iterator<ToXContent> id = context == Metadata.XContentContext.API
? Collections.emptyIterator()
: ChunkedToXContentHelper.field("id", projectId.id());
return Iterators.concat(id, templates, indices, customs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.elasticsearch.TransportVersion;
import org.elasticsearch.action.admin.indices.rollover.RolloverInfo;
import org.elasticsearch.cluster.ProjectId;
import org.elasticsearch.cluster.coordination.CoordinationMetadata;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.compress.CompressedXContent;
Expand Down Expand Up @@ -258,6 +259,7 @@ public void testToXContentGateway_FlatSettingTrue_ReduceMappingFalse() throws IO
"index.version.created" : "%s"
},
"project" : {
"id" : "pr0j3ct",
"templates" : {
"template" : {
"order" : 0,
Expand Down Expand Up @@ -440,6 +442,7 @@ public void testToXContentGateway_FlatSettingFalse_ReduceMappingTrue() throws IO
"index.version.created" : "%s"
},
"project" : {
"id" : "pr0j3ct",
"templates" : {
"template" : {
"order" : 0,
Expand Down Expand Up @@ -863,7 +866,7 @@ public void testToXContentAPIReservedMetadata() throws IOException {
}

private Metadata buildMetadata() throws IOException {
return Metadata.builder()
final Metadata.Builder builder = Metadata.builder()
.clusterUUID("clusterUUID")
.coordinationMetadata(
CoordinationMetadata.builder()
Expand Down Expand Up @@ -894,8 +897,9 @@ private Metadata buildMetadata() throws IOException {
.settings(Settings.builder().put(SETTING_VERSION_CREATED, IndexVersion.current()))
.putMapping("type", "{ \"key1\": {} }")
.build()
)
.build();
);
builder.project().id(new ProjectId("pr0j3ct"));
return builder.build();
}

public static class CustomMetadata extends TestClusterCustomMetadata {
Expand Down

0 comments on commit c91ad2d

Please sign in to comment.