2727import org .elasticsearch .test .MockHttpTransport ;
2828import org .elasticsearch .test .discovery .TestZenDiscovery ;
2929import org .elasticsearch .xpack .core .XPackSettings ;
30+ import org .elasticsearch .xpack .core .ml .action .GetDatafeedsAction ;
31+ import org .elasticsearch .xpack .core .ml .action .GetJobsAction ;
32+ import org .elasticsearch .xpack .core .ml .action .util .QueryPage ;
33+ import org .elasticsearch .xpack .core .ml .client .MachineLearningClient ;
3034import org .elasticsearch .xpack .ml .LocalStateMachineLearning ;
3135import org .elasticsearch .xpack .ml .MachineLearning ;
3236import org .elasticsearch .xpack .core .ml .MachineLearningField ;
@@ -271,7 +275,9 @@ public static GetDatafeedsStatsAction.Response.DatafeedStats getDatafeedStats(St
271275 }
272276
273277 public static void deleteAllDatafeeds (Logger logger , Client client ) throws Exception {
274- MlMetadata mlMetadata = MlMetadata .getMlMetadata (client .admin ().cluster ().prepareState ().get ().getState ());
278+ final MachineLearningClient mlClient = new MachineLearningClient (client );
279+ final QueryPage <DatafeedConfig > datafeeds =
280+ mlClient .getDatafeeds (new GetDatafeedsAction .Request (GetDatafeedsAction .ALL )).actionGet ().getResponse ();
275281 try {
276282 logger .info ("Closing all datafeeds (using _all)" );
277283 StopDatafeedAction .Response stopResponse = client
@@ -292,25 +298,25 @@ public static void deleteAllDatafeeds(Logger logger, Client client) throws Excep
292298 "Had to resort to force-stopping datafeed, something went wrong?" , e1 );
293299 }
294300
295- for (DatafeedConfig datafeed : mlMetadata .getDatafeeds ().values ()) {
296- String datafeedId = datafeed .getId ();
301+ for (final DatafeedConfig datafeed : datafeeds .results ()) {
297302 assertBusy (() -> {
298303 try {
299- GetDatafeedsStatsAction .Request request = new GetDatafeedsStatsAction .Request (datafeedId );
304+ GetDatafeedsStatsAction .Request request = new GetDatafeedsStatsAction .Request (datafeed . getId () );
300305 GetDatafeedsStatsAction .Response r = client .execute (GetDatafeedsStatsAction .INSTANCE , request ).get ();
301306 assertThat (r .getResponse ().results ().get (0 ).getDatafeedState (), equalTo (DatafeedState .STOPPED ));
302307 } catch (InterruptedException | ExecutionException e ) {
303308 throw new RuntimeException (e );
304309 }
305310 });
306311 DeleteDatafeedAction .Response deleteResponse =
307- client .execute (DeleteDatafeedAction .INSTANCE , new DeleteDatafeedAction .Request (datafeedId )).get ();
312+ client .execute (DeleteDatafeedAction .INSTANCE , new DeleteDatafeedAction .Request (datafeed . getId () )).get ();
308313 assertTrue (deleteResponse .isAcknowledged ());
309314 }
310315 }
311316
312317 public static void deleteAllJobs (Logger logger , Client client ) throws Exception {
313- MlMetadata mlMetadata = MlMetadata .getMlMetadata (client .admin ().cluster ().prepareState ().get ().getState ());
318+ final MachineLearningClient mlClient = new MachineLearningClient (client );
319+ final QueryPage <Job > jobs = mlClient .getJobs (new GetJobsAction .Request (MetaData .ALL )).actionGet ().getResponse ();
314320
315321 try {
316322 CloseJobAction .Request closeRequest = new CloseJobAction .Request (MetaData .ALL );
@@ -334,15 +340,14 @@ public static void deleteAllJobs(Logger logger, Client client) throws Exception
334340 e1 );
335341 }
336342
337- for (Map .Entry <String , Job > entry : mlMetadata .getJobs ().entrySet ()) {
338- String jobId = entry .getKey ();
343+ for (final Job job : jobs .results ()) {
339344 assertBusy (() -> {
340345 GetJobsStatsAction .Response statsResponse =
341- client ().execute (GetJobsStatsAction .INSTANCE , new GetJobsStatsAction .Request (jobId )).actionGet ();
346+ client ().execute (GetJobsStatsAction .INSTANCE , new GetJobsStatsAction .Request (job . getId () )).actionGet ();
342347 assertEquals (JobState .CLOSED , statsResponse .getResponse ().results ().get (0 ).getState ());
343348 });
344349 DeleteJobAction .Response response =
345- client .execute (DeleteJobAction .INSTANCE , new DeleteJobAction .Request (jobId )).get ();
350+ client .execute (DeleteJobAction .INSTANCE , new DeleteJobAction .Request (job . getId () )).get ();
346351 assertTrue (response .isAcknowledged ());
347352 }
348353 }
0 commit comments