20
20
package org .elasticsearch .persistent ;
21
21
22
22
import org .apache .logging .log4j .Logger ;
23
+ import org .elasticsearch .ResourceAlreadyExistsException ;
23
24
import org .elasticsearch .ResourceNotFoundException ;
24
25
import org .elasticsearch .action .ActionListener ;
25
26
import org .elasticsearch .cluster .ClusterChangedEvent ;
@@ -60,15 +61,19 @@ public PersistentTasksClusterService(Settings settings, PersistentTasksExecutorR
60
61
* @param request request
61
62
* @param listener the listener that will be called when task is started
62
63
*/
63
- public <Request extends PersistentTaskRequest > void createPersistentTask (String action , Request request ,
64
+ public <Request extends PersistentTaskRequest > void createPersistentTask (String taskId , String action , Request request ,
64
65
ActionListener <PersistentTask <?>> listener ) {
65
66
clusterService .submitStateUpdateTask ("create persistent task" , new ClusterStateUpdateTask () {
66
67
@ Override
67
68
public ClusterState execute (ClusterState currentState ) throws Exception {
69
+ PersistentTasksCustomMetaData .Builder builder = builder (currentState );
70
+ if (builder .hasTask (taskId )) {
71
+ throw new ResourceAlreadyExistsException ("task with id {" + taskId + "} already exist" );
72
+ }
68
73
validate (action , clusterService .state (), request );
69
74
final Assignment assignment ;
70
75
assignment = getAssignement (action , currentState , request );
71
- return update (currentState , builder ( currentState ) .addTask (action , request , assignment ));
76
+ return update (currentState , builder .addTask (taskId , action , request , assignment ));
72
77
}
73
78
74
79
@ Override
@@ -81,7 +86,7 @@ public void onFailure(String source, Exception e) {
81
86
public void clusterStateProcessed (String source , ClusterState oldState , ClusterState newState ) {
82
87
PersistentTasksCustomMetaData tasks = newState .getMetaData ().custom (PersistentTasksCustomMetaData .TYPE );
83
88
if (tasks != null ) {
84
- listener .onResponse (tasks .getTask (tasks . getCurrentId () ));
89
+ listener .onResponse (tasks .getTask (taskId ));
85
90
} else {
86
91
listener .onResponse (null );
87
92
}
@@ -97,7 +102,7 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS
97
102
* @param failure the reason for restarting the task or null if the task completed successfully
98
103
* @param listener the listener that will be called when task is removed
99
104
*/
100
- public void completePersistentTask (long id , Exception failure , ActionListener <PersistentTask <?>> listener ) {
105
+ public void completePersistentTask (String id , Exception failure , ActionListener <PersistentTask <?>> listener ) {
101
106
final String source ;
102
107
if (failure != null ) {
103
108
logger .warn ("persistent task " + id + " failed" , failure );
@@ -138,7 +143,7 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS
138
143
* @param id the id of a persistent task
139
144
* @param listener the listener that will be called when task is removed
140
145
*/
141
- public void removePersistentTask (long id , ActionListener <PersistentTask <?>> listener ) {
146
+ public void removePersistentTask (String id , ActionListener <PersistentTask <?>> listener ) {
142
147
clusterService .submitStateUpdateTask ("remove persistent task" , new ClusterStateUpdateTask () {
143
148
@ Override
144
149
public ClusterState execute (ClusterState currentState ) throws Exception {
@@ -166,12 +171,12 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS
166
171
/**
167
172
* Update task status
168
173
*
169
- * @param id the id of a persistent task
170
- * @param allocationId the expected allocation id of the persistent task
171
- * @param status new status
172
- * @param listener the listener that will be called when task is removed
174
+ * @param id the id of a persistent task
175
+ * @param allocationId the expected allocation id of the persistent task
176
+ * @param status new status
177
+ * @param listener the listener that will be called when task is removed
173
178
*/
174
- public void updatePersistentTaskStatus (long id , long allocationId , Task .Status status , ActionListener <PersistentTask <?>> listener ) {
179
+ public void updatePersistentTaskStatus (String id , long allocationId , Task .Status status , ActionListener <PersistentTask <?>> listener ) {
175
180
clusterService .submitStateUpdateTask ("update task status" , new ClusterStateUpdateTask () {
176
181
@ Override
177
182
public ClusterState execute (ClusterState currentState ) throws Exception {
0 commit comments