Skip to content

Commit 1bf6a77

Browse files
authored
Use singleton instance for default project-id (#123677)
No need to create new instances for the default project-id. We can use the singleton field which should speeds up key comparsion for Map.get operations. Relates: #123662
1 parent 10e23b3 commit 1bf6a77

File tree

59 files changed

+254
-227
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+254
-227
lines changed

server/src/main/java/org/elasticsearch/cluster/block/ClusterBlocks.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ private static void writeBlockSet(Set<ClusterBlock> blocks, StreamOutput out) th
438438
public static ClusterBlocks readFrom(StreamInput in) throws IOException {
439439
if (in.getTransportVersion().onOrAfter(TransportVersions.MULTI_PROJECT)) {
440440
final Set<ClusterBlock> global = readBlockSet(in);
441-
final Map<ProjectId, ProjectBlocks> projectBlocksMap = in.readImmutableMap(ProjectId::new, ProjectBlocks::readFrom);
441+
final Map<ProjectId, ProjectBlocks> projectBlocksMap = in.readImmutableMap(ProjectId::readFrom, ProjectBlocks::readFrom);
442442
if (global.isEmpty()
443443
&& noProjectOrDefaultProjectOnly(projectBlocksMap)
444444
&& projectBlocksMap.getOrDefault(Metadata.DEFAULT_PROJECT_ID, ProjectBlocks.EMPTY).indices().isEmpty()) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public class Metadata implements Diffable<Metadata>, ChunkedToXContent {
8888
public static final String UNKNOWN_CLUSTER_UUID = "_na_";
8989
// TODO multi-project: verify that usages are really expected to work on the default project only,
9090
// and that they are not a stop-gap solution to make the tests pass
91-
public static final ProjectId DEFAULT_PROJECT_ID = new ProjectId("default");
91+
public static final ProjectId DEFAULT_PROJECT_ID = ProjectId.DEFAULT;
9292

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

1157-
builder.projectMetadata(in.readMap(ProjectId::new, ProjectMetadata::readFrom));
1157+
builder.projectMetadata(in.readMap(ProjectId::readFrom, ProjectMetadata::readFrom));
11581158
}
11591159
return builder.build();
11601160
}

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

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,39 @@
1919
import org.elasticsearch.xcontent.XContentParser;
2020

2121
import java.io.IOException;
22+
import java.util.Objects;
2223

23-
public record ProjectId(String id) implements Writeable, ToXContent {
24+
public class ProjectId implements Writeable, ToXContent {
2425

25-
public static final Reader<ProjectId> READER = ProjectId::new;
26+
private static final String DEFAULT_STRING = "default";
27+
public static final ProjectId DEFAULT = new ProjectId(DEFAULT_STRING);
28+
public static final Reader<ProjectId> READER = ProjectId::readFrom;
2629
private static final int MAX_LENGTH = 128;
2730

28-
public ProjectId {
31+
private final String id;
32+
33+
private ProjectId(String id) {
2934
if (Strings.isNullOrBlank(id)) {
3035
throw new IllegalArgumentException("project-id cannot be empty");
3136
}
32-
assert isValidFormatId(id) : "project-id [" + id + "] must be alphanumeric ASCII with up to " + MAX_LENGTH + " chars";
37+
if (isValidFormatId(id) == false) {
38+
final var message = "project-id [" + id + "] must be alphanumeric ASCII with up to " + MAX_LENGTH + " chars";
39+
assert false : message;
40+
throw new IllegalArgumentException(message);
41+
}
42+
this.id = id;
43+
}
44+
45+
public String id() {
46+
return id;
47+
}
48+
49+
public static ProjectId fromId(String id) {
50+
if (DEFAULT_STRING.equals(id)) {
51+
return DEFAULT;
52+
} else {
53+
return new ProjectId(id);
54+
}
3355
}
3456

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

56-
public ProjectId(StreamInput in) throws IOException {
57-
this(in.readString());
78+
public static ProjectId readFrom(StreamInput in) throws IOException {
79+
return fromId(in.readString());
5880
}
5981

6082
@Override
@@ -79,4 +101,16 @@ public static ProjectId ofNullable(@Nullable String id, @Nullable ProjectId fall
79101
public String toString() {
80102
return this.id;
81103
}
104+
105+
@Override
106+
public boolean equals(Object o) {
107+
if (o == null || getClass() != o.getClass()) return false;
108+
ProjectId projectId = (ProjectId) o;
109+
return Objects.equals(id, projectId.id);
110+
}
111+
112+
@Override
113+
public int hashCode() {
114+
return Objects.hashCode(id);
115+
}
82116
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2136,7 +2136,7 @@ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params p) {
21362136
}
21372137

21382138
public static ProjectMetadata readFrom(StreamInput in) throws IOException {
2139-
ProjectId id = new ProjectId(in);
2139+
ProjectId id = ProjectId.readFrom(in);
21402140
Builder builder = builder(id);
21412141
Function<String, MappingMetadata> mappingLookup;
21422142
Map<String, MappingMetadata> mappingMetadataMap = in.readMapValues(MappingMetadata::new, MappingMetadata::getSha256);

server/src/main/java/org/elasticsearch/cluster/project/AbstractProjectResolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public ProjectId getProjectId() {
5050
if (headerValue == null) {
5151
return getFallbackProjectId();
5252
}
53-
return new ProjectId(headerValue);
53+
return ProjectId.fromId(headerValue);
5454
}
5555

5656
@Override
@@ -89,7 +89,7 @@ public boolean supportsMultipleProjects() {
8989
}
9090

9191
protected static ProjectMetadata findProject(Metadata metadata, String headerValue) {
92-
var project = metadata.projects().get(new ProjectId(headerValue));
92+
var project = metadata.projects().get(ProjectId.fromId(headerValue));
9393
if (project == null) {
9494
throw new IllegalArgumentException("Could not find project with id [" + headerValue + "]");
9595
}

server/src/main/java/org/elasticsearch/cluster/routing/GlobalRoutingTable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public static Diff<GlobalRoutingTable> readDiffFrom(StreamInput in) throws IOExc
179179
}
180180

181181
public static GlobalRoutingTable readFrom(StreamInput in) throws IOException {
182-
final var table = in.readImmutableOpenMap(ProjectId::new, RoutingTable::readFrom);
182+
final var table = in.readImmutableOpenMap(ProjectId::readFrom, RoutingTable::readFrom);
183183
return new GlobalRoutingTable(table);
184184
}
185185

server/src/main/java/org/elasticsearch/cluster/routing/allocation/command/AbstractAllocateAllocationCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ protected AbstractAllocateAllocationCommand(StreamInput in) throws IOException {
109109
shardId = in.readVInt();
110110
node = in.readString();
111111
if (in.getTransportVersion().onOrAfter(TransportVersions.MULTI_PROJECT)) {
112-
projectId = new ProjectId(in);
112+
projectId = ProjectId.readFrom(in);
113113
} else {
114114
projectId = Metadata.DEFAULT_PROJECT_ID;
115115
}

server/src/main/java/org/elasticsearch/cluster/routing/allocation/command/CancelAllocationCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public CancelAllocationCommand(StreamInput in) throws IOException {
8484
node = in.readString();
8585
allowPrimary = in.readBoolean();
8686
if (in.getTransportVersion().onOrAfter(TransportVersions.MULTI_PROJECT)) {
87-
projectId = new ProjectId(in);
87+
projectId = ProjectId.readFrom(in);
8888
} else {
8989
projectId = Metadata.DEFAULT_PROJECT_ID;
9090
}

server/src/main/java/org/elasticsearch/cluster/routing/allocation/command/MoveAllocationCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public MoveAllocationCommand(StreamInput in) throws IOException {
6969
fromNode = in.readString();
7070
toNode = in.readString();
7171
if (in.getTransportVersion().onOrAfter(TransportVersions.MULTI_PROJECT)) {
72-
projectId = new ProjectId(in);
72+
projectId = ProjectId.readFrom(in);
7373
} else {
7474
projectId = Metadata.DEFAULT_PROJECT_ID;
7575
}

server/src/test/java/org/elasticsearch/action/admin/cluster/health/TransportClusterHealthActionTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,21 @@ public void testWaitForInitializingShards() throws Exception {
4141
final String[] indices = { "test" };
4242
final ClusterHealthRequest request = new ClusterHealthRequest(TEST_REQUEST_TIMEOUT);
4343
request.waitForNoInitializingShards(true);
44-
var projectId = new ProjectId(randomUUID());
44+
var projectId = randomUniqueProjectId();
4545
ClusterState clusterState = randomClusterStateWithInitializingShards("test", 0, projectId);
4646
var project = clusterState.metadata().getProject(projectId);
4747
ClusterHealthResponse response = createResponse(indices, clusterState, project);
4848
assertThat(TransportClusterHealthAction.prepareResponse(request, response, project, null), equalTo(1));
4949

5050
request.waitForNoInitializingShards(true);
51-
projectId = new ProjectId(randomUUID());
51+
projectId = randomUniqueProjectId();
5252
clusterState = randomClusterStateWithInitializingShards("test", between(1, 10), projectId);
5353
project = clusterState.metadata().getProject(projectId);
5454
response = createResponse(indices, clusterState, project);
5555
assertThat(TransportClusterHealthAction.prepareResponse(request, response, project, null), equalTo(0));
5656

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

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

127127
var projects = randomMap(0, 5, () -> {
128-
var id = new ProjectId(randomUUID());
128+
var id = randomUniqueProjectId();
129129
return Tuple.tuple(id, ProjectMetadata.builder(id).build());
130130
});
131131
return ClusterState.builder(ClusterName.DEFAULT)

0 commit comments

Comments
 (0)