Skip to content

Commit 8521b2d

Browse files
jaymodemartijnvg
authored andcommitted
Remove InternalClient and InternalSecurityClient (elastic#3054)
This change removes the InternalClient and the InternalSecurityClient. These are replaced with usage of the ThreadContext and a transient value, `action.origin`, to indicate which component the request came from. The security code has been updated to look for this value and ensure the request is executed as the proper user. This work comes from elastic#2808 where @s1monw suggested that we do this. While working on this, I came across index template registries and rather than updating them to use the new method, I replaced the ML one with the template upgrade framework so that we could remove this template registry. The watcher template registry is still needed as the template must be updated for rolling upgrades to work (see elastic#2950).
1 parent 4dd6995 commit 8521b2d

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

server/src/main/java/org/elasticsearch/persistent/PersistentTasksService.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.elasticsearch.action.ActionListener;
2222
import org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest;
2323
import org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse;
24+
import org.elasticsearch.client.Client;
2425
import org.elasticsearch.cluster.ClusterState;
2526
import org.elasticsearch.cluster.ClusterStateObserver;
2627
import org.elasticsearch.cluster.node.DiscoveryNode;
@@ -34,20 +35,22 @@
3435
import org.elasticsearch.tasks.TaskId;
3536
import org.elasticsearch.threadpool.ThreadPool;
3637
import org.elasticsearch.persistent.PersistentTasksCustomMetaData.PersistentTask;
37-
import org.elasticsearch.security.InternalClient;
3838

3939
import java.util.function.Predicate;
4040

41+
import static org.elasticsearch.ClientHelper.PERSISTENT_TASK_ORIGIN;
42+
import static org.elasticsearch.ClientHelper.executeAsyncWithOrigin;
43+
4144
/**
4245
* This service is used by persistent actions to propagate changes in the action state and notify about completion
4346
*/
4447
public class PersistentTasksService extends AbstractComponent {
4548

46-
private final InternalClient client;
49+
private final Client client;
4750
private final ClusterService clusterService;
4851
private final ThreadPool threadPool;
4952

50-
public PersistentTasksService(Settings settings, ClusterService clusterService, ThreadPool threadPool, InternalClient client) {
53+
public PersistentTasksService(Settings settings, ClusterService clusterService, ThreadPool threadPool, Client client) {
5154
super(settings);
5255
this.client = client;
5356
this.clusterService = clusterService;
@@ -63,8 +66,8 @@ public <Params extends PersistentTaskParams> void startPersistentTask(String tas
6366
StartPersistentTaskAction.Request createPersistentActionRequest =
6467
new StartPersistentTaskAction.Request(taskId, taskName, params);
6568
try {
66-
client.execute(StartPersistentTaskAction.INSTANCE, createPersistentActionRequest, ActionListener.wrap(
67-
o -> listener.onResponse((PersistentTask<Params>) o.getTask()), listener::onFailure));
69+
executeAsyncWithOrigin(client, PERSISTENT_TASK_ORIGIN, StartPersistentTaskAction.INSTANCE, createPersistentActionRequest,
70+
ActionListener.wrap(o -> listener.onResponse((PersistentTask<Params>) o.getTask()), listener::onFailure));
6871
} catch (Exception e) {
6972
listener.onFailure(e);
7073
}
@@ -77,7 +80,7 @@ public void sendCompletionNotification(String taskId, long allocationId, Excepti
7780
ActionListener<PersistentTask<?>> listener) {
7881
CompletionPersistentTaskAction.Request restartRequest = new CompletionPersistentTaskAction.Request(taskId, allocationId, failure);
7982
try {
80-
client.execute(CompletionPersistentTaskAction.INSTANCE, restartRequest,
83+
executeAsyncWithOrigin(client, PERSISTENT_TASK_ORIGIN, CompletionPersistentTaskAction.INSTANCE, restartRequest,
8184
ActionListener.wrap(o -> listener.onResponse(o.getTask()), listener::onFailure));
8285
} catch (Exception e) {
8386
listener.onFailure(e);
@@ -93,7 +96,8 @@ void sendTaskManagerCancellation(long taskId, ActionListener<CancelTasksResponse
9396
cancelTasksRequest.setTaskId(new TaskId(localNode.getId(), taskId));
9497
cancelTasksRequest.setReason("persistent action was removed");
9598
try {
96-
client.admin().cluster().cancelTasks(cancelTasksRequest, listener);
99+
executeAsyncWithOrigin(client.threadPool().getThreadContext(), PERSISTENT_TASK_ORIGIN, cancelTasksRequest, listener,
100+
client.admin().cluster()::cancelTasks);
97101
} catch (Exception e) {
98102
listener.onFailure(e);
99103
}
@@ -109,8 +113,8 @@ void updateStatus(String taskId, long allocationId, Task.Status status, ActionLi
109113
UpdatePersistentTaskStatusAction.Request updateStatusRequest =
110114
new UpdatePersistentTaskStatusAction.Request(taskId, allocationId, status);
111115
try {
112-
client.execute(UpdatePersistentTaskStatusAction.INSTANCE, updateStatusRequest, ActionListener.wrap(
113-
o -> listener.onResponse(o.getTask()), listener::onFailure));
116+
executeAsyncWithOrigin(client, PERSISTENT_TASK_ORIGIN, UpdatePersistentTaskStatusAction.INSTANCE, updateStatusRequest,
117+
ActionListener.wrap(o -> listener.onResponse(o.getTask()), listener::onFailure));
114118
} catch (Exception e) {
115119
listener.onFailure(e);
116120
}
@@ -122,8 +126,8 @@ void updateStatus(String taskId, long allocationId, Task.Status status, ActionLi
122126
public void cancelPersistentTask(String taskId, ActionListener<PersistentTask<?>> listener) {
123127
RemovePersistentTaskAction.Request removeRequest = new RemovePersistentTaskAction.Request(taskId);
124128
try {
125-
client.execute(RemovePersistentTaskAction.INSTANCE, removeRequest, ActionListener.wrap(o -> listener.onResponse(o.getTask()),
126-
listener::onFailure));
129+
executeAsyncWithOrigin(client, PERSISTENT_TASK_ORIGIN, RemovePersistentTaskAction.INSTANCE, removeRequest,
130+
ActionListener.wrap(o -> listener.onResponse(o.getTask()), listener::onFailure));
127131
} catch (Exception e) {
128132
listener.onFailure(e);
129133
}

server/src/test/java/org/elasticsearch/persistent/TestPersistentTasksPlugin.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
import org.elasticsearch.watcher.ResourceWatcherService;
6565
import org.elasticsearch.persistent.PersistentTasksCustomMetaData.Assignment;
6666
import org.elasticsearch.persistent.PersistentTasksCustomMetaData.PersistentTask;
67-
import org.elasticsearch.security.InternalClient;
6867

6968
import java.io.IOException;
7069
import java.util.ArrayList;
@@ -105,8 +104,7 @@ public Collection<Object> createComponents(Client client, ClusterService cluster
105104
ResourceWatcherService resourceWatcherService, ScriptService scriptService,
106105
NamedXContentRegistry xContentRegistry, Environment environment,
107106
NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry) {
108-
InternalClient internalClient = new InternalClient(Settings.EMPTY, threadPool, client);
109-
PersistentTasksService persistentTasksService = new PersistentTasksService(Settings.EMPTY, clusterService, threadPool, internalClient);
107+
PersistentTasksService persistentTasksService = new PersistentTasksService(Settings.EMPTY, clusterService, threadPool, client);
110108
TestPersistentTasksExecutor testPersistentAction = new TestPersistentTasksExecutor(Settings.EMPTY, clusterService);
111109
PersistentTasksExecutorRegistry persistentTasksExecutorRegistry = new PersistentTasksExecutorRegistry(Settings.EMPTY,
112110
Collections.singletonList(testPersistentAction));

0 commit comments

Comments
 (0)