26
26
import org .elasticsearch .common .unit .TimeValue ;
27
27
import org .elasticsearch .tasks .CancellableTask ;
28
28
import org .elasticsearch .tasks .Task ;
29
- import org .elasticsearch .tasks .TaskCancelledException ;
30
29
import org .elasticsearch .tasks .TaskId ;
31
30
import org .elasticsearch .tasks .TaskManager ;
32
31
38
37
* Represents a executor node operation that corresponds to a persistent task
39
38
*/
40
39
public class AllocatedPersistentTask extends CancellableTask {
41
- private volatile String persistentTaskId ;
42
- private volatile long allocationId ;
43
40
44
41
private final AtomicReference <State > state ;
45
- @ Nullable
46
- private volatile Exception failure ;
47
42
43
+ private volatile String persistentTaskId ;
44
+ private volatile long allocationId ;
45
+ private volatile @ Nullable Exception failure ;
48
46
private volatile PersistentTasksService persistentTasksService ;
49
47
private volatile Logger logger ;
50
48
private volatile TaskManager taskManager ;
51
49
52
-
53
50
public AllocatedPersistentTask (long id , String type , String action , String description , TaskId parentTask ,
54
51
Map <String , String > headers ) {
55
52
super (id , type , action , description , parentTask , headers );
@@ -101,24 +98,10 @@ public Exception getFailure() {
101
98
return failure ;
102
99
}
103
100
104
- boolean markAsCancelled () {
105
- return state .compareAndSet (AllocatedPersistentTask .State .STARTED , AllocatedPersistentTask .State .PENDING_CANCEL );
106
- }
107
-
108
- public State getState () {
109
- return state .get ();
110
- }
111
-
112
101
public long getAllocationId () {
113
102
return allocationId ;
114
103
}
115
104
116
- public enum State {
117
- STARTED , // the task is currently running
118
- PENDING_CANCEL , // the task is cancelled on master, cancelling it locally
119
- COMPLETED // the task is done running and trying to notify caller
120
- }
121
-
122
105
/**
123
106
* Waits for this persistent task to have the desired state.
124
107
*/
@@ -128,6 +111,14 @@ public void waitForPersistentTaskStatus(Predicate<PersistentTasksCustomMetaData.
128
111
persistentTasksService .waitForPersistentTaskStatus (persistentTaskId , predicate , timeout , listener );
129
112
}
130
113
114
+ final boolean isCompleted () {
115
+ return state .get () == State .COMPLETED ;
116
+ }
117
+
118
+ boolean markAsCancelled () {
119
+ return state .compareAndSet (State .STARTED , State .PENDING_CANCEL );
120
+ }
121
+
131
122
public void markAsCompleted () {
132
123
completeAndNotifyIfNeeded (null );
133
124
}
@@ -138,11 +129,10 @@ public void markAsFailed(Exception e) {
138
129
} else {
139
130
completeAndNotifyIfNeeded (e );
140
131
}
141
-
142
132
}
143
133
144
134
private void completeAndNotifyIfNeeded (@ Nullable Exception failure ) {
145
- State prevState = state .getAndSet (AllocatedPersistentTask . State .COMPLETED );
135
+ final State prevState = state .getAndSet (State .COMPLETED );
146
136
if (prevState == State .COMPLETED ) {
147
137
logger .warn ("attempt to complete task [{}] with id [{}] in the [{}] state" , getAction (), getPersistentTaskId (), prevState );
148
138
} else {
@@ -173,4 +163,10 @@ public void onFailure(Exception e) {
173
163
}
174
164
}
175
165
}
166
+
167
+ public enum State {
168
+ STARTED , // the task is currently running
169
+ PENDING_CANCEL , // the task is cancelled on master, cancelling it locally
170
+ COMPLETED // the task is done running and trying to notify caller
171
+ }
176
172
}
0 commit comments