|
22 | 22 | import org.elasticsearch.persistent.PersistentTaskState;
|
23 | 23 | import org.elasticsearch.persistent.PersistentTasksCustomMetaData;
|
24 | 24 | import org.elasticsearch.persistent.PersistentTasksExecutor;
|
| 25 | +import org.elasticsearch.persistent.PersistentTasksService; |
25 | 26 | import org.elasticsearch.tasks.TaskId;
|
| 27 | +import org.elasticsearch.tasks.TaskManager; |
26 | 28 | import org.elasticsearch.threadpool.ThreadPool;
|
27 | 29 | import org.elasticsearch.xpack.core.ClientHelper;
|
28 | 30 | import org.elasticsearch.xpack.core.indexing.IndexerState;
|
@@ -150,44 +152,59 @@ protected void onAbort() {
|
150 | 152 | private final RollupJob job;
|
151 | 153 | private final SchedulerEngine schedulerEngine;
|
152 | 154 | private final ThreadPool threadPool;
|
153 |
| - private final RollupIndexer indexer; |
| 155 | + private final Client client; |
| 156 | + private final IndexerState initialIndexerState; |
| 157 | + private final Map<String, Object> initialPosition; |
| 158 | + private RollupIndexer indexer; |
154 | 159 |
|
155 | 160 | RollupJobTask(long id, String type, String action, TaskId parentTask, RollupJob job, RollupJobStatus state,
|
156 | 161 | Client client, SchedulerEngine schedulerEngine, ThreadPool threadPool, Map<String, String> headers) {
|
157 | 162 | super(id, type, action, RollupField.NAME + "_" + job.getConfig().getId(), parentTask, headers);
|
158 | 163 | this.job = job;
|
159 | 164 | this.schedulerEngine = schedulerEngine;
|
160 | 165 | this.threadPool = threadPool;
|
| 166 | + this.client = client; |
| 167 | + if (state == null) { |
| 168 | + this.initialIndexerState = null; |
| 169 | + this.initialPosition = null; |
| 170 | + } else { |
| 171 | + this.initialIndexerState = state.getIndexerState(); |
| 172 | + this.initialPosition = state.getPosition(); |
| 173 | + } |
| 174 | + |
| 175 | + } |
161 | 176 |
|
162 |
| - // If status is not null, we are resuming rather than starting fresh. |
163 |
| - Map<String, Object> initialPosition = null; |
164 |
| - IndexerState initialState = IndexerState.STOPPED; |
165 |
| - if (state != null) { |
166 |
| - final IndexerState existingState = state.getIndexerState(); |
167 |
| - logger.debug("We have existing state, setting state to [" + existingState + "] " + |
168 |
| - "and current position to [" + state.getPosition() + "] for job [" + job.getConfig().getId() + "]"); |
169 |
| - if (existingState.equals(IndexerState.INDEXING)) { |
| 177 | + @Override |
| 178 | + protected void init(PersistentTasksService persistentTasksService, TaskManager taskManager, |
| 179 | + String persistentTaskId, long allocationId) { |
| 180 | + super.init(persistentTasksService, taskManager, persistentTaskId, allocationId); |
| 181 | + |
| 182 | + // If initial position is not null, we are resuming rather than starting fresh. |
| 183 | + IndexerState indexerState = IndexerState.STOPPED; |
| 184 | + if (initialPosition != null) { |
| 185 | + logger.debug("We have existing state, setting state to [" + initialIndexerState + "] " + |
| 186 | + "and current position to [" + initialPosition + "] for job [" + job.getConfig().getId() + "]"); |
| 187 | + if (initialIndexerState.equals(IndexerState.INDEXING)) { |
170 | 188 | /*
|
171 | 189 | * If we were indexing, we have to reset back to STARTED otherwise the indexer will be "stuck" thinking
|
172 | 190 | * it is indexing but without the actual indexing thread running.
|
173 | 191 | */
|
174 |
| - initialState = IndexerState.STARTED; |
| 192 | + indexerState = IndexerState.STARTED; |
175 | 193 |
|
176 |
| - } else if (existingState.equals(IndexerState.ABORTING) || existingState.equals(IndexerState.STOPPING)) { |
| 194 | + } else if (initialIndexerState.equals(IndexerState.ABORTING) || initialIndexerState.equals(IndexerState.STOPPING)) { |
177 | 195 | // It shouldn't be possible to persist ABORTING, but if for some reason it does,
|
178 | 196 | // play it safe and restore the job as STOPPED. An admin will have to clean it up,
|
179 | 197 | // but it won't be running, and won't delete itself either. Safest option.
|
180 | 198 | // If we were STOPPING, that means it persisted but was killed before finally stopped... so ok
|
181 | 199 | // to restore as STOPPED
|
182 |
| - initialState = IndexerState.STOPPED; |
| 200 | + indexerState = IndexerState.STOPPED; |
183 | 201 | } else {
|
184 |
| - initialState = existingState; |
| 202 | + indexerState = initialIndexerState; |
185 | 203 | }
|
186 |
| - initialPosition = state.getPosition(); |
187 | 204 |
|
188 | 205 | }
|
189 |
| - this.indexer = new ClientRollupPageManager(job, initialState, initialPosition, |
190 |
| - new ParentTaskAssigningClient(client, new TaskId(getPersistentTaskId()))); |
| 206 | + this.indexer = new ClientRollupPageManager(job, indexerState, initialPosition, |
| 207 | + new ParentTaskAssigningClient(client, getParentTaskId())); |
191 | 208 | }
|
192 | 209 |
|
193 | 210 | @Override
|
|
0 commit comments