|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.ml.integration; |
| 7 | + |
| 8 | +import org.elasticsearch.Version; |
| 9 | +import org.elasticsearch.action.ActionListener; |
| 10 | +import org.elasticsearch.cluster.ClusterName; |
| 11 | +import org.elasticsearch.cluster.service.ClusterService; |
| 12 | +import org.elasticsearch.common.unit.TimeValue; |
| 13 | +import org.elasticsearch.common.util.concurrent.EsExecutors; |
| 14 | +import org.elasticsearch.threadpool.ThreadPool; |
| 15 | +import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig; |
| 16 | +import org.elasticsearch.xpack.core.ml.job.config.DataDescription; |
| 17 | +import org.elasticsearch.xpack.core.ml.job.config.Detector; |
| 18 | +import org.elasticsearch.xpack.core.ml.job.config.Job; |
| 19 | +import org.elasticsearch.xpack.ml.MlAssignmentNotifier; |
| 20 | +import org.elasticsearch.xpack.ml.MlDailyMaintenanceService; |
| 21 | +import org.elasticsearch.xpack.ml.job.persistence.JobConfigProvider; |
| 22 | +import org.junit.Before; |
| 23 | + |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.concurrent.CountDownLatch; |
| 28 | +import java.util.concurrent.ExecutorService; |
| 29 | +import java.util.concurrent.atomic.AtomicReference; |
| 30 | +import java.util.function.Consumer; |
| 31 | + |
| 32 | +import static java.util.stream.Collectors.toSet; |
| 33 | +import static org.hamcrest.Matchers.containsInAnyOrder; |
| 34 | +import static org.hamcrest.Matchers.is; |
| 35 | +import static org.mockito.Mockito.mock; |
| 36 | +import static org.mockito.Mockito.when; |
| 37 | + |
| 38 | +public class MlDailyMaintenanceServiceIT extends MlNativeAutodetectIntegTestCase { |
| 39 | + |
| 40 | + private JobConfigProvider jobConfigProvider; |
| 41 | + private ThreadPool threadPool; |
| 42 | + |
| 43 | + @Before |
| 44 | + public void setUpMocks() { |
| 45 | + jobConfigProvider = new JobConfigProvider(client(), xContentRegistry()); |
| 46 | + threadPool = mock(ThreadPool.class); |
| 47 | + ExecutorService directExecutorService = EsExecutors.newDirectExecutorService(); |
| 48 | + when(threadPool.executor(ThreadPool.Names.SAME)).thenReturn(directExecutorService); |
| 49 | + } |
| 50 | + |
| 51 | + public void testTriggerDeleteJobsInStateDeletingWithoutDeletionTask() throws InterruptedException { |
| 52 | + MlDailyMaintenanceService maintenanceService = |
| 53 | + new MlDailyMaintenanceService( |
| 54 | + settings(Version.CURRENT).build(), |
| 55 | + ClusterName.DEFAULT, |
| 56 | + threadPool, |
| 57 | + client(), |
| 58 | + mock(ClusterService.class), |
| 59 | + mock(MlAssignmentNotifier.class)); |
| 60 | + |
| 61 | + putJob("maintenance-test-1"); |
| 62 | + putJob("maintenance-test-2"); |
| 63 | + putJob("maintenance-test-3"); |
| 64 | + assertThat(getJobIds(), containsInAnyOrder("maintenance-test-1", "maintenance-test-2", "maintenance-test-3")); |
| 65 | + |
| 66 | + blockingCall(maintenanceService::triggerDeleteJobsInStateDeletingWithoutDeletionTask); |
| 67 | + assertThat(getJobIds(), containsInAnyOrder("maintenance-test-1", "maintenance-test-2", "maintenance-test-3")); |
| 68 | + |
| 69 | + this.<Boolean>blockingCall(listener -> jobConfigProvider.markJobAsDeleting("maintenance-test-2", listener)); |
| 70 | + this.<Boolean>blockingCall(listener -> jobConfigProvider.markJobAsDeleting("maintenance-test-3", listener)); |
| 71 | + assertThat(getJobIds(), containsInAnyOrder("maintenance-test-1", "maintenance-test-2", "maintenance-test-3")); |
| 72 | + assertThat(getJob("maintenance-test-1").get(0).isDeleting(), is(false)); |
| 73 | + assertThat(getJob("maintenance-test-2").get(0).isDeleting(), is(true)); |
| 74 | + assertThat(getJob("maintenance-test-3").get(0).isDeleting(), is(true)); |
| 75 | + |
| 76 | + blockingCall(maintenanceService::triggerDeleteJobsInStateDeletingWithoutDeletionTask); |
| 77 | + assertThat(getJobIds(), containsInAnyOrder("maintenance-test-1")); |
| 78 | + } |
| 79 | + |
| 80 | + private <T> void blockingCall(Consumer<ActionListener<T>> function) throws InterruptedException { |
| 81 | + AtomicReference<Exception> exceptionHolder = new AtomicReference<>(); |
| 82 | + CountDownLatch latch = new CountDownLatch(1); |
| 83 | + ActionListener<T> listener = ActionListener.wrap( |
| 84 | + r -> { |
| 85 | + latch.countDown(); |
| 86 | + }, |
| 87 | + e -> { |
| 88 | + exceptionHolder.set(e); |
| 89 | + latch.countDown(); |
| 90 | + } |
| 91 | + ); |
| 92 | + function.accept(listener); |
| 93 | + latch.await(); |
| 94 | + if (exceptionHolder.get() != null) { |
| 95 | + fail(exceptionHolder.get().getMessage()); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private void putJob(String jobId) { |
| 100 | + Job.Builder job = |
| 101 | + new Job.Builder(jobId) |
| 102 | + .setAnalysisConfig( |
| 103 | + new AnalysisConfig.Builder((List<Detector>) null) |
| 104 | + .setBucketSpan(TimeValue.timeValueHours(1)) |
| 105 | + .setDetectors( |
| 106 | + Collections.singletonList( |
| 107 | + new Detector.Builder("count", null) |
| 108 | + .setPartitionFieldName("user") |
| 109 | + .build()))) |
| 110 | + .setDataDescription( |
| 111 | + new DataDescription.Builder() |
| 112 | + .setTimeFormat("epoch")); |
| 113 | + |
| 114 | + registerJob(job); |
| 115 | + putJob(job); |
| 116 | + } |
| 117 | + |
| 118 | + private Set<String> getJobIds() { |
| 119 | + return getJob("*").stream().map(Job::getId).collect(toSet()); |
| 120 | + } |
| 121 | +} |
0 commit comments