1111import org .elasticsearch .action .ActionListener ;
1212import org .elasticsearch .action .DocWriteResponse ;
1313import org .elasticsearch .action .index .IndexResponse ;
14- import org .elasticsearch .action .support .ThreadedActionListener ;
1514import org .elasticsearch .action .support .WriteRequest ;
16- import org .elasticsearch .action .update .UpdateAction ;
17- import org .elasticsearch .action .update .UpdateRequest ;
18- import org .elasticsearch .action .update .UpdateResponse ;
1915import org .elasticsearch .client .Client ;
2016import org .elasticsearch .common .Nullable ;
2117import org .elasticsearch .common .unit .ByteSizeUnit ;
2218import org .elasticsearch .common .unit .ByteSizeValue ;
2319import org .elasticsearch .xpack .core .ml .MachineLearningField ;
24- import org .elasticsearch .xpack .core .ml .job .config .Job ;
20+ import org .elasticsearch .xpack .core .ml .action .PutJobAction ;
21+ import org .elasticsearch .xpack .core .ml .action .UpdateJobAction ;
22+ import org .elasticsearch .xpack .core .ml .job .config .JobUpdate ;
2523import org .elasticsearch .xpack .core .ml .job .messages .Messages ;
26- import org .elasticsearch .xpack .core .ml .job .persistence .AnomalyDetectorsIndex ;
27- import org .elasticsearch .xpack .core .ml .job .persistence .ElasticsearchMappings ;
2824import org .elasticsearch .xpack .core .ml .job .process .autodetect .output .FlushAcknowledgement ;
2925import org .elasticsearch .xpack .core .ml .job .process .autodetect .state .ModelSizeStats ;
3026import org .elasticsearch .xpack .core .ml .job .process .autodetect .state .ModelSnapshot ;
3632import org .elasticsearch .xpack .core .ml .job .results .ForecastRequestStats ;
3733import org .elasticsearch .xpack .core .ml .job .results .Influencer ;
3834import org .elasticsearch .xpack .core .ml .job .results .ModelPlot ;
39- import org .elasticsearch .xpack .ml .MachineLearning ;
4035import org .elasticsearch .xpack .ml .job .persistence .JobResultsPersister ;
4136import org .elasticsearch .xpack .ml .job .process .autodetect .AutodetectProcess ;
4237import org .elasticsearch .xpack .ml .job .process .normalizer .Renormalizer ;
4338import org .elasticsearch .xpack .ml .job .results .AutodetectResult ;
4439import org .elasticsearch .xpack .ml .notifications .Auditor ;
4540
4641import java .time .Duration ;
47- import java .util .Collections ;
48- import java .util .Date ;
49- import java .util .HashMap ;
5042import java .util .Iterator ;
5143import java .util .List ;
52- import java .util .Map ;
5344import java .util .Objects ;
5445import java .util .concurrent .CountDownLatch ;
5546import java .util .concurrent .Semaphore ;
@@ -88,7 +79,6 @@ public class AutoDetectResultProcessor {
8879
8980 final CountDownLatch completionLatch = new CountDownLatch (1 );
9081 final Semaphore updateModelSnapshotSemaphore = new Semaphore (1 );
91- volatile CountDownLatch onCloseActionsLatch ;
9282 private final FlushListener flushListener ;
9383 private volatile boolean processKilled ;
9484 private volatile boolean failed ;
@@ -149,18 +139,8 @@ public void process(AutodetectProcess process) {
149139 } catch (Exception e ) {
150140 LOGGER .warn (new ParameterizedMessage ("[{}] Error persisting autodetect results" , jobId ), e );
151141 }
152- if (processKilled == false ) {
153- try {
154- onAutodetectClose ();
155- } catch (Exception e ) {
156- if (onCloseActionsLatch != null ) {
157- onCloseActionsLatch .countDown ();
158- }
159- throw e ;
160- }
161- }
162-
163142 LOGGER .info ("[{}] {} buckets parsed from autodetect output" , jobId , bucketCount );
143+
164144 } catch (Exception e ) {
165145 failed = true ;
166146
@@ -313,6 +293,9 @@ private void notifyModelMemoryStatusChange(Context context, ModelSizeStats model
313293 }
314294
315295 protected void updateModelSnapshotOnJob (ModelSnapshot modelSnapshot ) {
296+ JobUpdate update = new JobUpdate .Builder (jobId ).setModelSnapshotId (modelSnapshot .getSnapshotId ()).build ();
297+ UpdateJobAction .Request updateRequest = UpdateJobAction .Request .internal (jobId , update );
298+
316299 try {
317300 // This blocks the main processing thread in the unlikely event
318301 // there are 2 model snapshots queued up. But it also has the
@@ -324,52 +307,20 @@ protected void updateModelSnapshotOnJob(ModelSnapshot modelSnapshot) {
324307 return ;
325308 }
326309
327- Map <String , Object > update = new HashMap <>();
328- update .put (Job .MODEL_SNAPSHOT_ID .getPreferredName (), modelSnapshot .getSnapshotId ());
329- update .put (Job .MODEL_SNAPSHOT_MIN_VERSION .getPreferredName (), modelSnapshot .getMinVersion ().toString ());
330-
331- updateJob (jobId , update , new ActionListener <UpdateResponse >() {
332- @ Override
333- public void onResponse (UpdateResponse updateResponse ) {
334- updateModelSnapshotSemaphore .release ();
335- LOGGER .debug ("[{}] Updated job with model snapshot id [{}]" , jobId , modelSnapshot .getSnapshotId ());
336- }
337-
338- @ Override
339- public void onFailure (Exception e ) {
340- updateModelSnapshotSemaphore .release ();
341- LOGGER .error ("[" + jobId + "] Failed to update job with new model snapshot id [" +
342- modelSnapshot .getSnapshotId () + "]" , e );
343- }
344- });
345- }
346-
347- private void onAutodetectClose () {
348- onCloseActionsLatch = new CountDownLatch (1 );
349-
350- ActionListener <UpdateResponse > updateListener = ActionListener .wrap (
351- updateResponse -> {
352- onCloseActionsLatch .countDown ();
353- },
354- e -> {
355- LOGGER .error ("[" + jobId + "] Failed to finalize job on autodetect close" , e );
356- onCloseActionsLatch .countDown ();
357- }
358- );
359-
360- updateJob (jobId , Collections .singletonMap (Job .FINISHED_TIME .getPreferredName (), new Date ()),
361- new ThreadedActionListener <>(LOGGER , client .threadPool (),
362- MachineLearning .UTILITY_THREAD_POOL_NAME , updateListener , false )
363- );
364- }
310+ executeAsyncWithOrigin (client , ML_ORIGIN , UpdateJobAction .INSTANCE , updateRequest , new ActionListener <PutJobAction .Response >() {
311+ @ Override
312+ public void onResponse (PutJobAction .Response response ) {
313+ updateModelSnapshotSemaphore .release ();
314+ LOGGER .debug ("[{}] Updated job with model snapshot id [{}]" , jobId , modelSnapshot .getSnapshotId ());
315+ }
365316
366- private void updateJob ( String jobId , Map < String , Object > update , ActionListener < UpdateResponse > listener ) {
367- UpdateRequest updateRequest = new UpdateRequest ( AnomalyDetectorsIndex . configIndexName (),
368- ElasticsearchMappings . DOC_TYPE , Job . documentId ( jobId ) );
369- updateRequest . retryOnConflict ( 3 );
370- updateRequest . doc ( update );
371- updateRequest . setRefreshPolicy ( WriteRequest . RefreshPolicy . IMMEDIATE );
372- executeAsyncWithOrigin ( client , ML_ORIGIN , UpdateAction . INSTANCE , updateRequest , listener );
317+ @ Override
318+ public void onFailure ( Exception e ) {
319+ updateModelSnapshotSemaphore . release ( );
320+ LOGGER . error ( "[" + jobId + "] Failed to update job with new model snapshot id [" +
321+ modelSnapshot . getSnapshotId () + "]" , e );
322+ }
323+ } );
373324 }
374325
375326 public void awaitCompletion () throws TimeoutException {
@@ -381,13 +332,6 @@ public void awaitCompletion() throws TimeoutException {
381332 throw new TimeoutException ("Timed out waiting for results processor to complete for job " + jobId );
382333 }
383334
384- // Once completionLatch has passed then onCloseActionsLatch must either
385- // be set or null, it will not be set later.
386- if (onCloseActionsLatch != null && onCloseActionsLatch .await (
387- MachineLearningField .STATE_PERSIST_RESTORE_TIMEOUT .getMinutes (), TimeUnit .MINUTES ) == false ) {
388- throw new TimeoutException ("Timed out waiting for results processor run post close actions " + jobId );
389- }
390-
391335 // Input stream has been completely processed at this point.
392336 // Wait for any updateModelSnapshotOnJob calls to complete.
393337 updateModelSnapshotSemaphore .acquire ();
0 commit comments