20
20
package org .elasticsearch .action .admin .cluster .node .tasks .cancel ;
21
21
22
22
import com .carrotsearch .hppc .cursors .ObjectObjectCursor ;
23
+ import org .elasticsearch .ElasticsearchSecurityException ;
24
+ import org .elasticsearch .ExceptionsHelper ;
23
25
import org .elasticsearch .ResourceNotFoundException ;
24
26
import org .elasticsearch .action .ActionListener ;
25
27
import org .elasticsearch .action .FailedNodeException ;
@@ -117,7 +119,11 @@ protected synchronized void taskOperation(CancelTasksRequest request, Cancellabl
117
119
final boolean canceled ;
118
120
if (cancellableTask .shouldCancelChildrenOnCancellation ()) {
119
121
DiscoveryNodes childNodes = clusterService .state ().nodes ();
120
- final BanLock banLock = new BanLock (childNodes .getSize (), () -> removeBanOnNodes (cancellableTask , childNodes ));
122
+ // If the task runs with a user, and it's cancelled after we have sent ban requests, then the unban request
123
+ // will be denied as it must not execute with a user. We need to wrap it with the current thread context.
124
+ final Runnable removeBans = transportService .getThreadPool ().getThreadContext ()
125
+ .preserveContext (() -> removeBanOnNodes (cancellableTask , childNodes ));
126
+ final BanLock banLock = new BanLock (childNodes .getSize (), removeBans );
121
127
canceled = taskManager .cancel (cancellableTask , request .getReason (), banLock ::onTaskFinished );
122
128
if (canceled ) {
123
129
// /In case the task has some child tasks, we need to wait for until ban is set on all nodes
@@ -190,6 +196,7 @@ public void handleResponse(TransportResponse.Empty response) {
190
196
191
197
@ Override
192
198
public void handleException (TransportException exp ) {
199
+ assert ExceptionsHelper .unwrapCause (exp ) instanceof ElasticsearchSecurityException == false ;
193
200
logger .warn ("Cannot send ban for tasks with the parent [{}] to the node [{}]" , request .parentTaskId , node .key );
194
201
listener .onFailure (exp );
195
202
}
@@ -200,8 +207,14 @@ public void handleException(TransportException exp) {
200
207
private void sendRemoveBanRequest (DiscoveryNodes nodes , BanParentTaskRequest request ) {
201
208
for (ObjectObjectCursor <String , DiscoveryNode > node : nodes .getNodes ()) {
202
209
logger .debug ("Sending remove ban for tasks with the parent [{}] to the node [{}]" , request .parentTaskId , node .key );
203
- transportService .sendRequest (node .value , BAN_PARENT_ACTION_NAME , request , EmptyTransportResponseHandler
204
- .INSTANCE_SAME );
210
+ transportService .sendRequest (node .value , BAN_PARENT_ACTION_NAME , request ,
211
+ new EmptyTransportResponseHandler (ThreadPool .Names .SAME ) {
212
+ @ Override
213
+ public void handleException (TransportException exp ) {
214
+ assert ExceptionsHelper .unwrapCause (exp ) instanceof ElasticsearchSecurityException == false ;
215
+ logger .info ("failed to remove the parent ban for task {} on node {}" , request .parentTaskId , node );
216
+ }
217
+ });
205
218
}
206
219
}
207
220
0 commit comments