Skip to content

Commit 63e371b

Browse files
committed
Make ProjectClient an interface
1 parent bcf22f9 commit 63e371b

File tree

6 files changed

+52
-54
lines changed

6 files changed

+52
-54
lines changed

modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpDownloaderTests.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -703,12 +703,14 @@ private GeoIpTaskState.Metadata newGeoIpTaskStateMetadata(boolean expired) {
703703
return new GeoIpTaskState.Metadata(0, 0, 0, randomAlphaOfLength(20), lastChecked.toEpochMilli());
704704
}
705705

706-
private static class MockClient extends ProjectClient {
706+
private static class MockClient extends NoOpClient implements ProjectClient {
707707

708708
private final Map<ActionType<?>, BiConsumer<? extends ActionRequest, ? extends ActionListener<?>>> handlers = new HashMap<>();
709+
private final ProjectId projectId;
709710

710711
private MockClient(ThreadPool threadPool, ProjectId projectId) {
711-
super(new NoOpClient(threadPool, TestProjectResolvers.singleProject(projectId)), projectId);
712+
super(threadPool, TestProjectResolvers.singleProject(projectId));
713+
this.projectId = projectId;
712714
}
713715

714716
public <Response extends ActionResponse, Request extends ActionRequest> void addHandler(
@@ -718,6 +720,11 @@ public <Response extends ActionResponse, Request extends ActionRequest> void add
718720
handlers.put(action, listener);
719721
}
720722

723+
@Override
724+
public ProjectId projectId() {
725+
return projectId;
726+
}
727+
721728
@SuppressWarnings("unchecked")
722729
@Override
723730
protected <Request extends ActionRequest, Response extends ActionResponse> void doExecute(

server/src/main/java/org/elasticsearch/client/internal/ProjectClient.java

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,13 @@
99

1010
package org.elasticsearch.client.internal;
1111

12-
import org.elasticsearch.action.ActionListener;
13-
import org.elasticsearch.action.ActionRequest;
14-
import org.elasticsearch.action.ActionResponse;
15-
import org.elasticsearch.action.ActionType;
1612
import org.elasticsearch.cluster.metadata.ProjectId;
1713

1814
/**
19-
* A dedicated {@link Client} that is scoped to a specific project. It will set the project ID in the thread context before executing any
20-
* requests.
15+
* A {@link Client} that is scoped to a specific project. It should execute any request in the scope of that project. This scope is usually
16+
* defined by the thread context.
2117
*/
22-
public class ProjectClient extends FilterClient {
18+
public interface ProjectClient extends Client {
2319

24-
private final ProjectId projectId;
25-
26-
public ProjectClient(Client in, ProjectId projectId) {
27-
super(in);
28-
this.projectId = projectId;
29-
}
30-
31-
@Override
32-
protected <Request extends ActionRequest, Response extends ActionResponse> void doExecute(
33-
ActionType<Response> action,
34-
Request request,
35-
ActionListener<Response> listener
36-
) {
37-
projectResolver().executeOnProject(projectId, () -> super.doExecute(action, request, listener));
38-
}
39-
40-
@Override
41-
public ProjectClient projectClient(ProjectId projectId) {
42-
throw new IllegalStateException(
43-
"Unable to create a project client for project [" + projectId + "], nested project client creation is not supported"
44-
);
45-
}
20+
ProjectId projectId();
4621
}

server/src/main/java/org/elasticsearch/client/internal/support/AbstractClient.java

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public abstract class AbstractClient implements Client {
9797
private final ThreadPool threadPool;
9898
private final ProjectResolver projectResolver;
9999
private final AdminClient admin;
100-
private ProjectClient defaultProjectClient;
100+
private final ProjectClient defaultProjectClient;
101101

102102
@SuppressWarnings("this-escape")
103103
public AbstractClient(Settings settings, ThreadPool threadPool, ProjectResolver projectResolver) {
@@ -106,6 +106,10 @@ public AbstractClient(Settings settings, ThreadPool threadPool, ProjectResolver
106106
this.projectResolver = projectResolver;
107107
this.admin = new AdminClient(this);
108108
this.logger = LogManager.getLogger(this.getClass());
109+
// We construct a dedicated project client for the default project if we're in single project mode, to avoid reconstructing it.
110+
this.defaultProjectClient = projectResolver.supportsMultipleProjects() == false
111+
? new ProjectClientImpl(this, ProjectId.DEFAULT)
112+
: null;
109113
}
110114

111115
@Override
@@ -124,7 +128,7 @@ public ProjectResolver projectResolver() {
124128
}
125129

126130
@Override
127-
public AdminClient admin() {
131+
public final AdminClient admin() {
128132
return admin;
129133
}
130134

@@ -423,23 +427,9 @@ public ProjectClient projectClient(ProjectId projectId) {
423427
// We only take the shortcut when the given project ID matches the "current" project ID. If it doesn't, we'll let #executeOnProject
424428
// take care of error handling.
425429
if (projectResolver.supportsMultipleProjects() == false && projectId.equals(projectResolver.getProjectId())) {
426-
// We construct a dedicated project client for the default project if we're in single project mode. This dedicated project
427-
// client is an optimization in that it does not use the project resolver and instead executes the request directly.
428-
if (defaultProjectClient == null) {
429-
defaultProjectClient = new ProjectClient(this, projectId) {
430-
@Override
431-
protected <Request extends ActionRequest, Response extends ActionResponse> void doExecute(
432-
ActionType<Response> action,
433-
Request request,
434-
ActionListener<Response> listener
435-
) {
436-
in().execute(action, request, listener);
437-
}
438-
};
439-
}
440430
return defaultProjectClient;
441431
}
442-
return new ProjectClient(this, projectId);
432+
return new ProjectClientImpl(this, projectId);
443433
}
444434

445435
/**
@@ -477,4 +467,28 @@ public R get() throws InterruptedException, ExecutionException {
477467
return super.get();
478468
}
479469
}
470+
471+
private static class ProjectClientImpl extends FilterClient implements ProjectClient {
472+
473+
private final ProjectId projectId;
474+
475+
ProjectClientImpl(Client in, ProjectId projectId) {
476+
super(in);
477+
this.projectId = projectId;
478+
}
479+
480+
@Override
481+
public ProjectId projectId() {
482+
return projectId;
483+
}
484+
485+
@Override
486+
protected <Request extends ActionRequest, Response extends ActionResponse> void doExecute(
487+
ActionType<Response> action,
488+
Request request,
489+
ActionListener<Response> listener
490+
) {
491+
projectResolver().executeOnProject(projectId, () -> super.doExecute(action, request, listener));
492+
}
493+
}
480494
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CleanupSnapshotStepTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.elasticsearch.cluster.metadata.IndexMetadata;
1717
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
1818
import org.elasticsearch.cluster.metadata.ProjectMetadata;
19+
import org.elasticsearch.cluster.project.TestProjectResolvers;
1920
import org.elasticsearch.index.IndexVersion;
2021
import org.elasticsearch.test.client.NoOpClient;
2122
import org.elasticsearch.threadpool.ThreadPool;
@@ -105,7 +106,7 @@ public void testPerformAction() {
105106
}
106107

107108
private NoOpClient getDeleteSnapshotRequestAssertingClient(ThreadPool threadPool, String expectedSnapshotName) {
108-
return new NoOpClient(threadPool) {
109+
return new NoOpClient(threadPool, TestProjectResolvers.usingRequestHeader(threadPool.getThreadContext())) {
109110
@Override
110111
protected <Request extends ActionRequest, Response extends ActionResponse> void doExecute(
111112
ActionType<Response> action,

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CreateSnapshotStepTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public void testNextStepKey() {
154154
ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true));
155155
{
156156
try (var threadPool = createThreadPool()) {
157-
final var client = new NoOpClient(threadPool);
157+
final var client = new NoOpClient(threadPool, TestProjectResolvers.usingRequestHeader(threadPool.getThreadContext()));
158158
StepKey nextKeyOnComplete = randomStepKey();
159159
StepKey nextKeyOnIncomplete = randomStepKey();
160160
CreateSnapshotStep completeStep = new CreateSnapshotStep(randomStepKey(), nextKeyOnComplete, nextKeyOnIncomplete, client) {
@@ -170,7 +170,7 @@ void createSnapshot(ProjectId projectId, IndexMetadata indexMetadata, ActionList
170170

171171
{
172172
try (var threadPool = createThreadPool()) {
173-
final var client = new NoOpClient(threadPool);
173+
final var client = new NoOpClient(threadPool, TestProjectResolvers.usingRequestHeader(threadPool.getThreadContext()));
174174
StepKey nextKeyOnComplete = randomStepKey();
175175
StepKey nextKeyOnIncomplete = randomStepKey();
176176
CreateSnapshotStep incompleteStep = new CreateSnapshotStep(
@@ -191,7 +191,7 @@ void createSnapshot(ProjectId projectId, IndexMetadata indexMetadata, ActionList
191191

192192
{
193193
try (var threadPool = createThreadPool()) {
194-
final var client = new NoOpClient(threadPool);
194+
final var client = new NoOpClient(threadPool, TestProjectResolvers.usingRequestHeader(threadPool.getThreadContext()));
195195
StepKey nextKeyOnComplete = randomStepKey();
196196
StepKey nextKeyOnIncomplete = randomStepKey();
197197
CreateSnapshotStep doubleInvocationStep = new CreateSnapshotStep(

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DownsampleStepTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
1616
import org.elasticsearch.cluster.metadata.ProjectId;
1717
import org.elasticsearch.cluster.metadata.ProjectMetadata;
18+
import org.elasticsearch.cluster.project.TestProjectResolvers;
1819
import org.elasticsearch.core.TimeValue;
1920
import org.elasticsearch.index.IndexVersion;
2021
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
@@ -252,7 +253,7 @@ public void testNextStepKey() {
252253
ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(sourceIndexMetadata, true));
253254
{
254255
try (var threadPool = createThreadPool()) {
255-
final var client = new NoOpClient(threadPool);
256+
final var client = new NoOpClient(threadPool, TestProjectResolvers.usingRequestHeader(threadPool.getThreadContext()));
256257
StepKey nextKey = randomStepKey();
257258
DateHistogramInterval fixedInterval = ConfigTestHelpers.randomInterval();
258259
TimeValue timeout = DownsampleAction.DEFAULT_WAIT_TIMEOUT;
@@ -273,7 +274,7 @@ void performDownsampleIndex(
273274
}
274275
{
275276
try (var threadPool = createThreadPool()) {
276-
final var client = new NoOpClient(threadPool);
277+
final var client = new NoOpClient(threadPool, TestProjectResolvers.usingRequestHeader(threadPool.getThreadContext()));
277278
StepKey nextKey = randomStepKey();
278279
DateHistogramInterval fixedInterval = ConfigTestHelpers.randomInterval();
279280
TimeValue timeout = DownsampleAction.DEFAULT_WAIT_TIMEOUT;

0 commit comments

Comments
 (0)