30
30
import org .elasticsearch .common .io .stream .StreamOutput ;
31
31
import org .elasticsearch .common .settings .Settings ;
32
32
import org .elasticsearch .common .xcontent .XContentBuilder ;
33
+ import org .elasticsearch .gateway .GatewayService ;
33
34
import org .elasticsearch .tasks .Task ;
34
35
import org .elasticsearch .tasks .TaskAwareRequest ;
35
36
import org .elasticsearch .tasks .TaskId ;
@@ -70,6 +71,11 @@ public PersistentTasksNodeService(Settings settings,
70
71
71
72
@ Override
72
73
public void clusterChanged (ClusterChangedEvent event ) {
74
+ if (event .state ().blocks ().hasGlobalBlock (GatewayService .STATE_NOT_RECOVERED_BLOCK )) {
75
+ // wait until the gateway has recovered from disk, otherwise if the only master restarts
76
+ // we start cancelling all local tasks before cluster has a chance to recover.
77
+ return ;
78
+ }
73
79
PersistentTasksCustomMetaData tasks = event .state ().getMetaData ().custom (PersistentTasksCustomMetaData .TYPE );
74
80
PersistentTasksCustomMetaData previousTasks = event .previousState ().getMetaData ().custom (PersistentTasksCustomMetaData .TYPE );
75
81
@@ -120,11 +126,14 @@ public void clusterChanged(ClusterChangedEvent event) {
120
126
AllocatedPersistentTask task = runningTasks .get (id );
121
127
if (task .getState () == AllocatedPersistentTask .State .COMPLETED ) {
122
128
// Result was sent to the caller and the caller acknowledged acceptance of the result
129
+ logger .trace ("Found completed persistent task [{}] with id [{}] and allocation id [{}] - removing" ,
130
+ task .getAction (), task .getPersistentTaskId (), task .getAllocationId ());
123
131
runningTasks .remove (id );
124
132
} else {
125
133
// task is running locally, but master doesn't know about it - that means that the persistent task was removed
126
134
// cancel the task without notifying master
127
- logger .trace ("Found unregistered persistent task with id {} - cancelling " , id );
135
+ logger .trace ("Found unregistered persistent task [{}] with id [{}] and allocation id [{}] - cancelling" ,
136
+ task .getAction (), task .getPersistentTaskId (), task .getAllocationId ());
128
137
cancelTask (id );
129
138
}
130
139
}
@@ -160,6 +169,8 @@ public Task createTask(long id, String type, String action, TaskId parentTaskId)
160
169
boolean processed = false ;
161
170
try {
162
171
task .init (persistentTasksService , taskManager , logger , taskInProgress .getId (), taskInProgress .getAllocationId ());
172
+ logger .trace ("Persistent task [{}] with id [{}] and allocation id [{}] was created" , task .getAction (),
173
+ task .getPersistentTaskId (), task .getAllocationId ());
163
174
try {
164
175
runningTasks .put (taskInProgress .getAllocationId (), task );
165
176
nodePersistentTasksExecutor .executeTask (taskInProgress .getParams (), task , executor );
@@ -171,6 +182,8 @@ public Task createTask(long id, String type, String action, TaskId parentTaskId)
171
182
} finally {
172
183
if (processed == false ) {
173
184
// something went wrong - unregistering task
185
+ logger .warn ("Persistent task [{}] with id [{}] and allocation id [{}] failed to create" , task .getAction (),
186
+ task .getPersistentTaskId (), task .getAllocationId ());
174
187
taskManager .unregister (task );
175
188
}
176
189
}
@@ -187,14 +200,16 @@ private void cancelTask(Long allocationId) {
187
200
persistentTasksService .sendTaskManagerCancellation (task .getId (), new ActionListener <CancelTasksResponse >() {
188
201
@ Override
189
202
public void onResponse (CancelTasksResponse cancelTasksResponse ) {
190
- logger .trace ("Persistent task with id {} was cancelled" , task .getId ());
191
-
203
+ logger .trace ("Persistent task [{}] with id [{}] and allocation id [{}] was cancelled" , task .getAction (),
204
+ task . getPersistentTaskId (), task . getAllocationId ());
192
205
}
193
206
194
207
@ Override
195
208
public void onFailure (Exception e ) {
196
209
// There is really nothing we can do in case of failure here
197
- logger .warn ((Supplier <?>) () -> new ParameterizedMessage ("failed to cancel task {}" , task .getPersistentTaskId ()), e );
210
+ logger .warn ((Supplier <?>) () ->
211
+ new ParameterizedMessage ("failed to cancel task [{}] with id [{}] and allocation id [{}]" , task .getAction (),
212
+ task .getPersistentTaskId (), task .getAllocationId ()), e );
198
213
}
199
214
});
200
215
}
0 commit comments