|
11 | 11 | import org.elasticsearch.action.support.WriteRequest;
|
12 | 12 | import org.elasticsearch.client.Client;
|
13 | 13 | import org.elasticsearch.cluster.AckedClusterStateUpdateTask;
|
| 14 | +import org.elasticsearch.cluster.ClusterChangedEvent; |
14 | 15 | import org.elasticsearch.cluster.ClusterState;
|
| 16 | +import org.elasticsearch.cluster.ClusterStateUpdateTask; |
15 | 17 | import org.elasticsearch.cluster.metadata.MetaData;
|
16 | 18 | import org.elasticsearch.cluster.service.ClusterService;
|
17 | 19 | import org.elasticsearch.common.CheckedConsumer;
|
@@ -303,57 +305,88 @@ private void validateAnalysisLimitsUpdate(Job job, AnalysisLimits newLimits, Cha
|
303 | 305 | }
|
304 | 306 |
|
305 | 307 | private void internalJobUpdate(UpdateJobAction.Request request, ActionListener<PutJobAction.Response> actionListener) {
|
306 |
| - clusterService.submitStateUpdateTask("update-job-" + request.getJobId(), |
307 |
| - new AckedClusterStateUpdateTask<PutJobAction.Response>(request, actionListener) { |
308 |
| - private volatile Job updatedJob; |
309 |
| - private volatile boolean processUpdateRequired; |
310 | 308 |
|
311 |
| - @Override |
312 |
| - protected PutJobAction.Response newResponse(boolean acknowledged) { |
313 |
| - return new PutJobAction.Response(updatedJob); |
314 |
| - } |
| 309 | + Job job = getJobOrThrowIfUnknown(request.getJobId()); |
| 310 | + final Job updatedJob = request.getJobUpdate().mergeWithJob(job, maxModelMemoryLimit); |
| 311 | + if (updatedJob.equals(job)) { |
| 312 | + // No change will results in a clusterstate update no-op so don't |
| 313 | + // submit the request. |
| 314 | + actionListener.onResponse(new PutJobAction.Response(updatedJob)); |
| 315 | + return; |
| 316 | + } |
315 | 317 |
|
316 |
| - @Override |
317 |
| - public ClusterState execute(ClusterState currentState) { |
318 |
| - Job job = getJobOrThrowIfUnknown(request.getJobId(), currentState); |
319 |
| - updatedJob = request.getJobUpdate().mergeWithJob(job, maxModelMemoryLimit); |
320 |
| - if (updatedJob.equals(job)) { |
321 |
| - // nothing to do |
322 |
| - return currentState; |
| 318 | + if (request.isWaitForAck()) { |
| 319 | + // Use the ack cluster state update |
| 320 | + clusterService.submitStateUpdateTask("update-job-" + request.getJobId(), |
| 321 | + new AckedClusterStateUpdateTask<PutJobAction.Response>(request, actionListener) { |
| 322 | + |
| 323 | + @Override |
| 324 | + protected PutJobAction.Response newResponse(boolean acknowledged) { |
| 325 | + return new PutJobAction.Response(updatedJob); |
323 | 326 | }
|
324 |
| - // No change is required if the fields that the C++ uses aren't being updated |
325 |
| - processUpdateRequired = request.getJobUpdate().isAutodetectProcessUpdate(); |
326 |
| - return updateClusterState(updatedJob, true, currentState); |
327 |
| - } |
328 | 327 |
|
329 |
| - @Override |
330 |
| - public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) { |
331 |
| - JobUpdate jobUpdate = request.getJobUpdate(); |
332 |
| - if (processUpdateRequired && isJobOpen(newState, request.getJobId())) { |
333 |
| - updateJobProcessNotifier.submitJobUpdate(UpdateParams.fromJobUpdate(jobUpdate), ActionListener.wrap( |
334 |
| - isUpdated -> { |
335 |
| - if (isUpdated) { |
336 |
| - auditJobUpdatedIfNotInternal(request); |
337 |
| - } |
338 |
| - }, e -> { |
339 |
| - // No need to do anything |
340 |
| - } |
341 |
| - )); |
342 |
| - } else { |
343 |
| - logger.debug("[{}] No process update required for job update: {}", () -> request.getJobId(), () -> { |
344 |
| - try { |
345 |
| - XContentBuilder jsonBuilder = XContentFactory.jsonBuilder(); |
346 |
| - jobUpdate.toXContent(jsonBuilder, ToXContent.EMPTY_PARAMS); |
347 |
| - return Strings.toString(jsonBuilder); |
348 |
| - } catch (IOException e) { |
349 |
| - return "(unprintable due to " + e.getMessage() + ")"; |
350 |
| - } |
351 |
| - }); |
| 328 | + @Override |
| 329 | + public ClusterState execute(ClusterState currentState) { |
| 330 | + return updateClusterState(updatedJob, true, currentState); |
| 331 | + } |
| 332 | + |
| 333 | + @Override |
| 334 | + public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) { |
| 335 | + afterClusterStateUpdate(newState, request); |
| 336 | + } |
| 337 | + }); |
| 338 | + } else { |
| 339 | + clusterService.submitStateUpdateTask("update-job-" + request.getJobId(), new ClusterStateUpdateTask() { |
352 | 340 |
|
| 341 | + @Override |
| 342 | + public ClusterState execute(ClusterState currentState) throws Exception { |
| 343 | + return updateClusterState(updatedJob, true, currentState); |
| 344 | + } |
| 345 | + |
| 346 | + @Override |
| 347 | + public void onFailure(String source, Exception e) { |
| 348 | + actionListener.onFailure(e); |
| 349 | + } |
| 350 | + |
| 351 | + @Override |
| 352 | + public void clusterStatePublished(ClusterChangedEvent clusterChangedEvent) { |
| 353 | + afterClusterStateUpdate(clusterChangedEvent.state(), request); |
| 354 | + actionListener.onResponse(new PutJobAction.Response(updatedJob)); |
| 355 | + |
| 356 | + } |
| 357 | + }); |
| 358 | + } |
| 359 | + } |
| 360 | + |
| 361 | + private void afterClusterStateUpdate(ClusterState newState, UpdateJobAction.Request request) { |
| 362 | + JobUpdate jobUpdate = request.getJobUpdate(); |
| 363 | + |
| 364 | + // Change is required if the fields that the C++ uses are being updated |
| 365 | + boolean processUpdateRequired = jobUpdate.isAutodetectProcessUpdate(); |
| 366 | + |
| 367 | + if (processUpdateRequired && isJobOpen(newState, request.getJobId())) { |
| 368 | + updateJobProcessNotifier.submitJobUpdate(UpdateParams.fromJobUpdate(jobUpdate), ActionListener.wrap( |
| 369 | + isUpdated -> { |
| 370 | + if (isUpdated) { |
353 | 371 | auditJobUpdatedIfNotInternal(request);
|
354 | 372 | }
|
| 373 | + }, e -> { |
| 374 | + // No need to do anything |
355 | 375 | }
|
356 |
| - }); |
| 376 | + )); |
| 377 | + } else { |
| 378 | + logger.debug("[{}] No process update required for job update: {}", () -> request.getJobId(), () -> { |
| 379 | + try { |
| 380 | + XContentBuilder jsonBuilder = XContentFactory.jsonBuilder(); |
| 381 | + jobUpdate.toXContent(jsonBuilder, ToXContent.EMPTY_PARAMS); |
| 382 | + return Strings.toString(jsonBuilder); |
| 383 | + } catch (IOException e) { |
| 384 | + return "(unprintable due to " + e.getMessage() + ")"; |
| 385 | + } |
| 386 | + }); |
| 387 | + |
| 388 | + auditJobUpdatedIfNotInternal(request); |
| 389 | + } |
357 | 390 | }
|
358 | 391 |
|
359 | 392 | private void auditJobUpdatedIfNotInternal(UpdateJobAction.Request request) {
|
|
0 commit comments