Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -438,7 +438,7 @@ private static void writeBlockSet(Set<ClusterBlock> blocks, StreamOutput out) th
public static ClusterBlocks readFrom(StreamInput in) throws IOException {
if (in.getTransportVersion().onOrAfter(TransportVersions.MULTI_PROJECT)) {
final Set<ClusterBlock> global = readBlockSet(in);
final Map<ProjectId, ProjectBlocks> projectBlocksMap = in.readImmutableMap(ProjectId::new, ProjectBlocks::readFrom);
final Map<ProjectId, ProjectBlocks> projectBlocksMap = in.readImmutableMap(ProjectId::readFrom, ProjectBlocks::readFrom);
if (global.isEmpty()
&& noProjectOrDefaultProjectOnly(projectBlocksMap)
&& projectBlocksMap.getOrDefault(Metadata.DEFAULT_PROJECT_ID, ProjectBlocks.EMPTY).indices().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public class Metadata implements Diffable<Metadata>, ChunkedToXContent {
public static final String UNKNOWN_CLUSTER_UUID = "_na_";
// TODO multi-project: verify that usages are really expected to work on the default project only,
// and that they are not a stop-gap solution to make the tests pass
public static final ProjectId DEFAULT_PROJECT_ID = new ProjectId("default");
public static final ProjectId DEFAULT_PROJECT_ID = ProjectId.DEFAULT;

public enum XContentContext {
/* Custom metadata should be returned as part of API call */
Expand Down Expand Up @@ -1154,7 +1154,7 @@ public static Metadata readFrom(StreamInput in) throws IOException {
builder.put(ReservedStateMetadata.readFrom(in));
}

builder.projectMetadata(in.readMap(ProjectId::new, ProjectMetadata::readFrom));
builder.projectMetadata(in.readMap(ProjectId::readFrom, ProjectMetadata::readFrom));
}
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,39 @@
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
import java.util.Objects;

public record ProjectId(String id) implements Writeable, ToXContent {
public class ProjectId implements Writeable, ToXContent {
Comment on lines -23 to +24
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only this file contains real changes. Everything else is cascading.


public static final Reader<ProjectId> READER = ProjectId::new;
private static final String DEFAULT_STRING = "default";
public static final ProjectId DEFAULT = new ProjectId(DEFAULT_STRING);
public static final Reader<ProjectId> READER = ProjectId::readFrom;
private static final int MAX_LENGTH = 128;

public ProjectId {
private final String id;

private ProjectId(String id) {
if (Strings.isNullOrBlank(id)) {
throw new IllegalArgumentException("project-id cannot be empty");
}
assert isValidFormatId(id) : "project-id [" + id + "] must be alphanumeric ASCII with up to " + MAX_LENGTH + " chars";
if (isValidFormatId(id) == false) {
final var message = "project-id [" + id + "] must be alphanumeric ASCII with up to " + MAX_LENGTH + " chars";
assert false : message;
throw new IllegalArgumentException(message);
}
this.id = id;
}

public String id() {
return id;
}

public static ProjectId fromId(String id) {
if (DEFAULT_STRING.equals(id)) {
return DEFAULT;
} else {
return new ProjectId(id);
}
}

static boolean isValidFormatId(String id) {
Expand All @@ -53,8 +75,8 @@ private static boolean isValidIdChar(char c) {
return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' || c == '-';
}

public ProjectId(StreamInput in) throws IOException {
this(in.readString());
public static ProjectId readFrom(StreamInput in) throws IOException {
return fromId(in.readString());
}

@Override
Expand All @@ -79,4 +101,16 @@ public static ProjectId ofNullable(@Nullable String id, @Nullable ProjectId fall
public String toString() {
return this.id;
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
ProjectId projectId = (ProjectId) o;
return Objects.equals(id, projectId.id);
}

@Override
public int hashCode() {
return Objects.hashCode(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2136,7 +2136,7 @@ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params p) {
}

public static ProjectMetadata readFrom(StreamInput in) throws IOException {
ProjectId id = new ProjectId(in);
ProjectId id = ProjectId.readFrom(in);
Builder builder = builder(id);
Function<String, MappingMetadata> mappingLookup;
Map<String, MappingMetadata> mappingMetadataMap = in.readMapValues(MappingMetadata::new, MappingMetadata::getSha256);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ProjectId getProjectId() {
if (headerValue == null) {
return getFallbackProjectId();
}
return new ProjectId(headerValue);
return ProjectId.fromId(headerValue);
}

@Override
Expand Down Expand Up @@ -89,7 +89,7 @@ public boolean supportsMultipleProjects() {
}

protected static ProjectMetadata findProject(Metadata metadata, String headerValue) {
var project = metadata.projects().get(new ProjectId(headerValue));
var project = metadata.projects().get(ProjectId.fromId(headerValue));
if (project == null) {
throw new IllegalArgumentException("Could not find project with id [" + headerValue + "]");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public static Diff<GlobalRoutingTable> readDiffFrom(StreamInput in) throws IOExc
}

public static GlobalRoutingTable readFrom(StreamInput in) throws IOException {
final var table = in.readImmutableOpenMap(ProjectId::new, RoutingTable::readFrom);
final var table = in.readImmutableOpenMap(ProjectId::readFrom, RoutingTable::readFrom);
return new GlobalRoutingTable(table);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected AbstractAllocateAllocationCommand(StreamInput in) throws IOException {
shardId = in.readVInt();
node = in.readString();
if (in.getTransportVersion().onOrAfter(TransportVersions.MULTI_PROJECT)) {
projectId = new ProjectId(in);
projectId = ProjectId.readFrom(in);
} else {
projectId = Metadata.DEFAULT_PROJECT_ID;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public CancelAllocationCommand(StreamInput in) throws IOException {
node = in.readString();
allowPrimary = in.readBoolean();
if (in.getTransportVersion().onOrAfter(TransportVersions.MULTI_PROJECT)) {
projectId = new ProjectId(in);
projectId = ProjectId.readFrom(in);
} else {
projectId = Metadata.DEFAULT_PROJECT_ID;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public MoveAllocationCommand(StreamInput in) throws IOException {
fromNode = in.readString();
toNode = in.readString();
if (in.getTransportVersion().onOrAfter(TransportVersions.MULTI_PROJECT)) {
projectId = new ProjectId(in);
projectId = ProjectId.readFrom(in);
} else {
projectId = Metadata.DEFAULT_PROJECT_ID;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ public void testWaitForInitializingShards() throws Exception {
final String[] indices = { "test" };
final ClusterHealthRequest request = new ClusterHealthRequest(TEST_REQUEST_TIMEOUT);
request.waitForNoInitializingShards(true);
var projectId = new ProjectId(randomUUID());
var projectId = randomUniqueProjectId();
ClusterState clusterState = randomClusterStateWithInitializingShards("test", 0, projectId);
var project = clusterState.metadata().getProject(projectId);
ClusterHealthResponse response = createResponse(indices, clusterState, project);
assertThat(TransportClusterHealthAction.prepareResponse(request, response, project, null), equalTo(1));

request.waitForNoInitializingShards(true);
projectId = new ProjectId(randomUUID());
projectId = randomUniqueProjectId();
clusterState = randomClusterStateWithInitializingShards("test", between(1, 10), projectId);
project = clusterState.metadata().getProject(projectId);
response = createResponse(indices, clusterState, project);
assertThat(TransportClusterHealthAction.prepareResponse(request, response, project, null), equalTo(0));

request.waitForNoInitializingShards(false);
projectId = new ProjectId(randomUUID());
projectId = randomUniqueProjectId();
clusterState = randomClusterStateWithInitializingShards("test", randomInt(20), projectId);
project = clusterState.metadata().getProject(projectId);
response = createResponse(indices, clusterState, project);
Expand All @@ -67,7 +67,7 @@ public void testWaitForAllShards() {
final ClusterHealthRequest request = new ClusterHealthRequest(TEST_REQUEST_TIMEOUT);
request.waitForActiveShards(ActiveShardCount.ALL);

var projectId = new ProjectId(randomUUID());
var projectId = randomUniqueProjectId();
ClusterState clusterState = randomClusterStateWithInitializingShards("test", 1, projectId);
var project = clusterState.metadata().getProject(projectId);
ClusterHealthResponse response = createResponse(indices, clusterState, project);
Expand Down Expand Up @@ -125,7 +125,7 @@ ClusterState randomClusterStateWithInitializingShards(String index, final int in
}

var projects = randomMap(0, 5, () -> {
var id = new ProjectId(randomUUID());
var id = randomUniqueProjectId();
return Tuple.tuple(id, ProjectMetadata.builder(id).build());
});
return ClusterState.builder(ClusterName.DEFAULT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void testGetClusterStateWithDefaultProjectOnly() throws Exception {

public void testGetClusterStateForOneProjectOfMany() throws Exception {
final Set<String> indexNames = randomSet(1, 8, () -> randomAlphaOfLengthBetween(4, 12));
final ProjectId projectId = new ProjectId(randomUUID());
final ProjectId projectId = randomUniqueProjectId();

final ProjectResolver projectResolver = TestProjectResolvers.singleProject(projectId);
final ClusterStateRequest request = buildRandomRequest(indexNames);
Expand All @@ -117,7 +117,7 @@ public void testGetClusterStateForOneProjectOfMany() throws Exception {
final ProjectMetadata.Builder[] projects = new ProjectMetadata.Builder[numberOfProjects];
projects[0] = projectBuilder(projectId, indexNames);
for (int i = 1; i < numberOfProjects; i++) {
projects[i] = projectBuilder(new ProjectId(randomUUID()), randomSet(0, 12, () -> randomAlphaOfLengthBetween(4, 12)));
projects[i] = projectBuilder(randomUniqueProjectId(), randomSet(0, 12, () -> randomAlphaOfLengthBetween(4, 12)));
}
final ClusterState state = buildClusterState(projects);

Expand All @@ -133,7 +133,7 @@ public void testGetClusterStateForManyProjects() throws Exception {
final ProjectId[] projectIds = new ProjectId[numberOfProjects];
final Set<String> indexNames = randomSet(5, 20, () -> randomAlphaOfLengthBetween(4, 12));
for (int i = 0; i < numberOfProjects; i++) {
projectIds[i] = new ProjectId(randomUUID());
projectIds[i] = randomUniqueProjectId();
projects[i] = projectBuilder(projectIds[i], randomSubsetOf(indexNames));
}
final ClusterState state = buildClusterState(projects);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class TransportCreateIndexActionTests extends ESTestCase {
private static final String UNMANAGED_SYSTEM_INDEX_NAME = ".my-system";
private static final String MANAGED_SYSTEM_INDEX_NAME = ".my-managed";
private static final String SYSTEM_ALIAS_NAME = ".my-alias";
private static final ProjectId PROJECT_ID = new ProjectId("test_project_id");
private static final ProjectId PROJECT_ID = ProjectId.fromId("test_project_id");
private static final ClusterState CLUSTER_STATE = ClusterState.builder(new ClusterName("test"))
.metadata(Metadata.builder().build())
.putProjectMetadata(ProjectMetadata.builder(PROJECT_ID).build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ public void testFailuresDuringPrerequisiteActions() throws InterruptedException
// Construct a cluster state that contains the required data streams.
// using a single, non-default project
final ClusterState oldState = clusterService.state();
final ProjectId projectId = new ProjectId(randomUUID());
final ProjectId projectId = randomUniqueProjectId();
final Metadata metadata = Metadata.builder(oldState.metadata())
.removeProject(Metadata.DEFAULT_PROJECT_ID)
.put(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.ResolvedExpression;
import org.elasticsearch.cluster.metadata.ProjectId;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings;
Expand Down Expand Up @@ -52,7 +51,7 @@ public void testDataStreamsResolveConcreteIndexNames() {
var dataStreamFailureIndex1 = new Index(".fs-foo1", IndexMetadata.INDEX_UUID_NA_VALUE);
var dataStreamFailureIndex2 = new Index(".fs-bar2", IndexMetadata.INDEX_UUID_NA_VALUE);

var projectId = new ProjectId(randomUUID());
var projectId = randomUniqueProjectId();
ClusterState clusterState = ClusterState.builder(new ClusterName("test-cluster"))
.putProjectMetadata(
ProjectMetadata.builder(projectId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.elasticsearch.cluster.TestShardRoutingRoleStrategies;
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.routing.IndexRoutingTable;
import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
Expand Down Expand Up @@ -63,7 +62,7 @@ public void testEnoughShardsWhenProjectIsGone() {
.numberOfShards(randomIntBetween(1, 3))
.numberOfReplicas(randomIntBetween(1, 3))
.build();
ProjectMetadata projectMetadata = ProjectMetadata.builder(new ProjectId(randomUUID())).put(indexMetadata, randomBoolean()).build();
ProjectMetadata projectMetadata = ProjectMetadata.builder(randomUniqueProjectId()).put(indexMetadata, randomBoolean()).build();
Index index = new Index(indexName, "_uuid");
ShardId shardId = new ShardId(index, 0);
ShardRouting shardRouting = ShardRouting.newUnassigned(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void setUp() throws Exception {
);
transportService.start();
transportService.acceptIncomingRequests();
projectId = new ProjectId(randomUUID());
projectId = randomUniqueProjectId();
broadcastReplicationAction = new TestBroadcastReplicationAction(
clusterService,
transportService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static void beforeClass() throws Exception {
emptySet()
);

ProjectId projectId = new ProjectId(randomBase64UUID());
ProjectId projectId = randomUniqueProjectId();
projectResolver = TestProjectResolvers.singleProject(projectId);
final Index index1 = new Index("index1", randomBase64UUID());
final Index index2 = new Index("index2", randomBase64UUID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.elasticsearch.cluster.metadata.IndexGraveyard;
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.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodeRole;
Expand Down Expand Up @@ -485,8 +484,8 @@ public void testChangedCustomMetadataSet() {
public void testChangedCustomMetadataSetMultiProject() {
final CustomProjectMetadata project1Custom = new CustomProjectMetadata("project1");
final CustomProjectMetadata project2Custom = new CustomProjectMetadata("project2");
final ProjectMetadata project1 = ProjectMetadata.builder(new ProjectId(randomUUID())).build();
final ProjectMetadata project2 = ProjectMetadata.builder(new ProjectId(randomUUID())).build();
final ProjectMetadata project1 = ProjectMetadata.builder(randomUniqueProjectId()).build();
final ProjectMetadata project2 = ProjectMetadata.builder(randomUniqueProjectId()).build();
final ClusterState originalState = ClusterState.builder(TEST_CLUSTER_NAME)
.metadata(Metadata.builder().put(project1).put(project2).build())
.build();
Expand Down Expand Up @@ -514,7 +513,7 @@ public void testChangedCustomMetadataSetMultiProject() {
// Add custom in completely new project
newState = ClusterState.builder(originalState)
.putProjectMetadata(
ProjectMetadata.builder(new ProjectId(randomUUID())).putCustom(project2Custom.getWriteableName(), project2Custom).build()
ProjectMetadata.builder(randomUniqueProjectId()).putCustom(project2Custom.getWriteableName(), project2Custom).build()
)
.build();
event = new ClusterChangedEvent("_na_", originalState, newState);
Expand Down Expand Up @@ -727,7 +726,7 @@ private static DiscoveryNode newNode(final String nodeId, Set<DiscoveryNodeRole>

// Create the metadata for a cluster state.
private static ProjectMetadata createProject(final List<Index> indices) {
final ProjectMetadata.Builder builder = ProjectMetadata.builder(new ProjectId(randomUUID()));
final ProjectMetadata.Builder builder = ProjectMetadata.builder(randomUniqueProjectId());
for (Index index : indices) {
builder.put(createIndexMetadata(index), true);
}
Expand Down
Loading