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 ;
@@ -109,7 +111,11 @@ protected synchronized void taskOperation(CancelTasksRequest request, Cancellabl
109
111
final boolean canceled ;
110
112
if (cancellableTask .shouldCancelChildrenOnCancellation ()) {
111
113
DiscoveryNodes childNodes = clusterService .state ().nodes ();
112
- final BanLock banLock = new BanLock (childNodes .getSize (), () -> removeBanOnNodes (cancellableTask , childNodes ));
114
+ // If the task runs with a user, and it's cancelled after we have sent ban requests, then the unban request
115
+ // will be denied as it must not execute with a user. We need to wrap it with the current thread context.
116
+ final Runnable removeBans = transportService .getThreadPool ().getThreadContext ()
117
+ .preserveContext (() -> removeBanOnNodes (cancellableTask , childNodes ));
118
+ final BanLock banLock = new BanLock (childNodes .getSize (), removeBans );
113
119
canceled = taskManager .cancel (cancellableTask , request .getReason (), banLock ::onTaskFinished );
114
120
if (canceled ) {
115
121
// /In case the task has some child tasks, we need to wait for until ban is set on all nodes
@@ -182,6 +188,7 @@ public void handleResponse(TransportResponse.Empty response) {
182
188
183
189
@ Override
184
190
public void handleException (TransportException exp ) {
191
+ assert ExceptionsHelper .unwrapCause (exp ) instanceof ElasticsearchSecurityException == false ;
185
192
logger .warn ("Cannot send ban for tasks with the parent [{}] to the node [{}]" , request .parentTaskId , node .key );
186
193
listener .onFailure (exp );
187
194
}
@@ -192,8 +199,14 @@ public void handleException(TransportException exp) {
192
199
private void sendRemoveBanRequest (DiscoveryNodes nodes , BanParentTaskRequest request ) {
193
200
for (ObjectObjectCursor <String , DiscoveryNode > node : nodes .getNodes ()) {
194
201
logger .debug ("Sending remove ban for tasks with the parent [{}] to the node [{}]" , request .parentTaskId , node .key );
195
- transportService .sendRequest (node .value , BAN_PARENT_ACTION_NAME , request , EmptyTransportResponseHandler
196
- .INSTANCE_SAME );
202
+ transportService .sendRequest (node .value , BAN_PARENT_ACTION_NAME , request ,
203
+ new EmptyTransportResponseHandler (ThreadPool .Names .SAME ) {
204
+ @ Override
205
+ public void handleException (TransportException exp ) {
206
+ assert ExceptionsHelper .unwrapCause (exp ) instanceof ElasticsearchSecurityException == false ;
207
+ logger .info ("failed to remove the parent ban for task {} on node {}" , request .parentTaskId , node );
208
+ }
209
+ });
197
210
}
198
211
}
199
212
0 commit comments