use of org.opensearch.ad.task.ADTaskManager in project anomaly-detection by opensearch-project.
the class GetAnomalyDetectorTests method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
ClusterService clusterService = mock(ClusterService.class);
ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AnomalyDetectorSettings.FILTER_BY_BACKEND_ROLES))));
when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet());
nodeFilter = mock(DiscoveryNodeFilterer.class);
actionFilters = mock(ActionFilters.class);
client = mock(Client.class);
when(client.threadPool()).thenReturn(threadPool);
adTaskManager = mock(ADTaskManager.class);
action = new GetAnomalyDetectorTransportAction(transportService, nodeFilter, actionFilters, clusterService, client, Settings.EMPTY, xContentRegistry(), adTaskManager);
entity = Entity.createSingleAttributeEntity(categoryField, entityValue);
}
use of org.opensearch.ad.task.ADTaskManager in project anomaly-detection by opensearch-project.
the class GetAnomalyDetectorTransportActionTests method setUp.
@Override
@Before
public void setUp() throws Exception {
super.setUp();
ClusterService clusterService = mock(ClusterService.class);
ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AnomalyDetectorSettings.FILTER_BY_BACKEND_ROLES))));
when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
adTaskManager = mock(ADTaskManager.class);
action = new GetAnomalyDetectorTransportAction(Mockito.mock(TransportService.class), Mockito.mock(DiscoveryNodeFilterer.class), Mockito.mock(ActionFilters.class), clusterService, client(), Settings.EMPTY, xContentRegistry(), adTaskManager);
task = Mockito.mock(Task.class);
response = new ActionListener<GetAnomalyDetectorResponse>() {
@Override
public void onResponse(GetAnomalyDetectorResponse getResponse) {
// When no detectors exist, get response is not generated
assertTrue(true);
}
@Override
public void onFailure(Exception e) {
}
};
categoryField = "catField";
categoryValue = "app-0";
entity = Entity.createSingleAttributeEntity(categoryField, categoryValue);
}
use of org.opensearch.ad.task.ADTaskManager in project anomaly-detection by opensearch-project.
the class GetAnomalyDetectorTransportAction method getExecute.
protected void getExecute(GetAnomalyDetectorRequest request, ActionListener<GetAnomalyDetectorResponse> listener) {
String detectorID = request.getDetectorID();
String typesStr = request.getTypeStr();
String rawPath = request.getRawPath();
Entity entity = request.getEntity();
boolean all = request.isAll();
boolean returnJob = request.isReturnJob();
boolean returnTask = request.isReturnTask();
try {
if (!Strings.isEmpty(typesStr) || rawPath.endsWith(PROFILE) || rawPath.endsWith(PROFILE + "/")) {
if (entity != null) {
Set<EntityProfileName> entityProfilesToCollect = getEntityProfilesToCollect(typesStr, all);
EntityProfileRunner profileRunner = new EntityProfileRunner(client, xContentRegistry, AnomalyDetectorSettings.NUM_MIN_SAMPLES);
profileRunner.profile(detectorID, entity, entityProfilesToCollect, ActionListener.wrap(profile -> {
listener.onResponse(new GetAnomalyDetectorResponse(0, null, 0, 0, null, null, false, null, null, false, null, null, profile, true));
}, e -> listener.onFailure(e)));
} else {
Set<DetectorProfileName> profilesToCollect = getProfilesToCollect(typesStr, all);
AnomalyDetectorProfileRunner profileRunner = new AnomalyDetectorProfileRunner(client, xContentRegistry, nodeFilter, AnomalyDetectorSettings.NUM_MIN_SAMPLES, transportService, adTaskManager);
profileRunner.profile(detectorID, getProfileActionListener(listener), profilesToCollect);
}
} else {
if (returnTask) {
adTaskManager.getAndExecuteOnLatestADTasks(detectorID, null, null, ALL_DETECTOR_TASK_TYPES, (taskList) -> {
Optional<ADTask> realtimeAdTask = Optional.empty();
Optional<ADTask> historicalAdTask = Optional.empty();
if (taskList != null && taskList.size() > 0) {
Map<String, ADTask> adTasks = new HashMap<>();
List<ADTask> duplicateAdTasks = new ArrayList<>();
for (ADTask task : taskList) {
if (adTasks.containsKey(task.getTaskType())) {
LOG.info("Found duplicate latest task of detector {}, task id: {}, task type: {}", detectorID, task.getTaskType(), task.getTaskId());
duplicateAdTasks.add(task);
continue;
}
adTasks.put(task.getTaskType(), task);
}
if (duplicateAdTasks.size() > 0) {
adTaskManager.resetLatestFlagAsFalse(duplicateAdTasks);
}
if (adTasks.containsKey(ADTaskType.REALTIME_HC_DETECTOR.name())) {
realtimeAdTask = Optional.ofNullable(adTasks.get(ADTaskType.REALTIME_HC_DETECTOR.name()));
} else if (adTasks.containsKey(ADTaskType.REALTIME_SINGLE_ENTITY.name())) {
realtimeAdTask = Optional.ofNullable(adTasks.get(ADTaskType.REALTIME_SINGLE_ENTITY.name()));
}
if (adTasks.containsKey(ADTaskType.HISTORICAL_HC_DETECTOR.name())) {
historicalAdTask = Optional.ofNullable(adTasks.get(ADTaskType.HISTORICAL_HC_DETECTOR.name()));
} else if (adTasks.containsKey(ADTaskType.HISTORICAL_SINGLE_ENTITY.name())) {
historicalAdTask = Optional.ofNullable(adTasks.get(ADTaskType.HISTORICAL_SINGLE_ENTITY.name()));
} else if (adTasks.containsKey(ADTaskType.HISTORICAL.name())) {
historicalAdTask = Optional.ofNullable(adTasks.get(ADTaskType.HISTORICAL.name()));
}
}
getDetectorAndJob(detectorID, returnJob, returnTask, realtimeAdTask, historicalAdTask, listener);
}, transportService, true, 2, listener);
} else {
getDetectorAndJob(detectorID, returnJob, returnTask, Optional.empty(), Optional.empty(), listener);
}
}
} catch (Exception e) {
LOG.error(e);
listener.onFailure(e);
}
}
use of org.opensearch.ad.task.ADTaskManager in project anomaly-detection by opensearch-project.
the class AbstractProfileRunnerTests method setUp.
@SuppressWarnings("unchecked")
@Override
@Before
public void setUp() throws Exception {
super.setUp();
client = mock(Client.class);
nodeFilter = mock(DiscoveryNodeFilterer.class);
clusterService = mock(ClusterService.class);
adTaskManager = mock(ADTaskManager.class);
when(clusterService.state()).thenReturn(ClusterState.builder(new ClusterName("test cluster")).build());
requiredSamples = 128;
neededSamples = 5;
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
Consumer<Optional<ADTask>> function = (Consumer<Optional<ADTask>>) args[2];
function.accept(Optional.of(TestHelpers.randomAdTask()));
return null;
}).when(adTaskManager).getAndExecuteOnLatestDetectorLevelTask(any(), any(), any(), any(), anyBoolean(), any());
runner = new AnomalyDetectorProfileRunner(client, xContentRegistry(), nodeFilter, requiredSamples, transportService, adTaskManager);
detectorIntervalMin = 3;
detectorGetReponse = mock(GetResponse.class);
}
use of org.opensearch.ad.task.ADTaskManager in project anomaly-detection by opensearch-project.
the class ValidateAnomalyDetectorActionHandlerTests method setUp.
@SuppressWarnings("unchecked")
@Override
@Before
public void setUp() throws Exception {
super.setUp();
MockitoAnnotations.initMocks(this);
settings = Settings.EMPTY;
clusterService = mock(ClusterService.class);
channel = mock(ActionListener.class);
transportService = mock(TransportService.class);
anomalyDetectionIndices = mock(AnomalyDetectionIndices.class);
when(anomalyDetectionIndices.doesAnomalyDetectorIndexExist()).thenReturn(true);
detectorId = "123";
seqNo = 0L;
primaryTerm = 0L;
clock = mock(Clock.class);
refreshPolicy = WriteRequest.RefreshPolicy.IMMEDIATE;
String field = "a";
detector = TestHelpers.randomAnomalyDetectorUsingCategoryFields(detectorId, "timestamp", ImmutableList.of("test-index"), Arrays.asList(field));
requestTimeout = new TimeValue(1000L);
maxSingleEntityAnomalyDetectors = 1000;
maxMultiEntityAnomalyDetectors = 10;
maxAnomalyFeatures = 5;
method = RestRequest.Method.POST;
adTaskManager = mock(ADTaskManager.class);
searchFeatureDao = mock(SearchFeatureDao.class);
threadContext = new ThreadContext(settings);
Mockito.doReturn(threadPool).when(clientMock).threadPool();
Mockito.doReturn(threadContext).when(threadPool).getThreadContext();
}
Aggregations