use of org.opensearch.ad.model.Entity in project anomaly-detection by opensearch-project.
the class PriorityCacheTests method testClearMemory.
/*
* Test the scenario:
* 1. A detector's buffer uses dedicated and shared memory
* 2. a new detector's buffer is created and triggers clearMemory (every new
* CacheBuffer creation will trigger it)
* 3. clearMemory found we can reclaim shared memory
*/
public void testClearMemory() {
for (int i = 0; i < 2; i++) {
// bypass doorkeeper
cacheProvider.get(entity2.getModelId(detectorId).get(), detector);
}
for (int i = 0; i < 10; i++) {
// bypass doorkeeper and make entity1 have higher frequency
cacheProvider.get(entity1.getModelId(detectorId).get(), detector);
}
// put modelState5 in dedicated and modelState6 in shared cache
when(memoryTracker.canAllocate(anyLong())).thenReturn(true);
cacheProvider.hostIfPossible(detector, modelState1);
cacheProvider.hostIfPossible(detector, modelState2);
// two entities get inserted to cache
assertTrue(null != cacheProvider.get(entity1.getModelId(detectorId).get(), detector));
assertTrue(null != cacheProvider.get(entity2.getModelId(detectorId).get(), detector));
Entity entity5 = Entity.createSingleAttributeEntity("attributeName1", "attributeVal5");
when(memoryTracker.memoryToShed()).thenReturn(memoryPerEntity);
for (int i = 0; i < 2; i++) {
// bypass doorkeeper, CacheBuffer created, and trigger clearMemory
cacheProvider.get(entity5.getModelId(detectorId2).get(), detector2);
}
assertTrue(null != cacheProvider.get(entity1.getModelId(detectorId).get(), detector));
// entity 2 removed
assertTrue(null == cacheProvider.get(entity2.getModelId(detectorId).get(), detector));
assertTrue(null == cacheProvider.get(entity5.getModelId(detectorId2).get(), detector));
}
use of org.opensearch.ad.model.Entity in project anomaly-detection by opensearch-project.
the class EntityProfileRunnerTests method testModel.
public void testModel() throws InterruptedException {
setUpExecuteEntityProfileAction(InittedEverResultStatus.INITTED);
EntityProfile.Builder expectedProfile = new EntityProfile.Builder();
ModelProfileOnNode modelProfile = new ModelProfileOnNode(nodeId, new ModelProfile(modelId, entity, modelSize));
expectedProfile.modelProfile(modelProfile);
final CountDownLatch inProgressLatch = new CountDownLatch(1);
runner.profile(detectorId, entity, model, ActionListener.wrap(response -> {
assertEquals(expectedProfile.build(), response);
inProgressLatch.countDown();
}, exception -> {
assertTrue("Should not reach here", false);
inProgressLatch.countDown();
}));
assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
}
use of org.opensearch.ad.model.Entity in project anomaly-detection by opensearch-project.
the class EntityProfileRunnerTests method stateTestTemplate.
public void stateTestTemplate(InittedEverResultStatus returnedState, EntityState expectedState) throws InterruptedException {
setUpExecuteEntityProfileAction(returnedState);
final CountDownLatch inProgressLatch = new CountDownLatch(1);
runner.profile(detectorId, entity, state, ActionListener.wrap(response -> {
assertEquals(expectedState, response.getState());
inProgressLatch.countDown();
}, exception -> {
assertTrue("Should not reach here", false);
inProgressLatch.countDown();
}));
assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
}
use of org.opensearch.ad.model.Entity in project anomaly-detection by opensearch-project.
the class EntityProfileRunnerTests method testNotMultiEntityDetector.
@SuppressWarnings("unchecked")
public void testNotMultiEntityDetector() throws IOException, InterruptedException {
detector = TestHelpers.randomAnomalyDetectorWithInterval(new IntervalTimeConfiguration(detectorIntervalMin, ChronoUnit.MINUTES));
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
GetRequest request = (GetRequest) args[0];
ActionListener<GetResponse> listener = (ActionListener<GetResponse>) args[1];
String indexName = request.index();
if (indexName.equals(ANOMALY_DETECTORS_INDEX)) {
listener.onResponse(TestHelpers.createGetResponse(detector, detector.getDetectorId(), AnomalyDetector.ANOMALY_DETECTORS_INDEX));
}
return null;
}).when(client).get(any(), any());
final CountDownLatch inProgressLatch = new CountDownLatch(1);
runner.profile(detectorId, entity, state, ActionListener.wrap(response -> {
assertTrue("Should not reach here", false);
inProgressLatch.countDown();
}, exception -> {
assertTrue(exception.getMessage().contains(EntityProfileRunner.NOT_HC_DETECTOR_ERR_MSG));
inProgressLatch.countDown();
}));
assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
}
use of org.opensearch.ad.model.Entity in project anomaly-detection by opensearch-project.
the class EntityProfileRunnerTests method testJobIndexNotFound.
@SuppressWarnings("unchecked")
public void testJobIndexNotFound() throws InterruptedException {
setUpExecuteEntityProfileAction(InittedEverResultStatus.INITTED);
final CountDownLatch inProgressLatch = new CountDownLatch(1);
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
GetRequest request = (GetRequest) args[0];
ActionListener<GetResponse> listener = (ActionListener<GetResponse>) args[1];
String indexName = request.index();
if (indexName.equals(ANOMALY_DETECTORS_INDEX)) {
listener.onResponse(TestHelpers.createGetResponse(detector, detector.getDetectorId(), AnomalyDetector.ANOMALY_DETECTORS_INDEX));
} else if (indexName.equals(ANOMALY_DETECTOR_JOB_INDEX)) {
listener.onFailure(new IndexNotFoundException(ANOMALY_DETECTOR_JOB_INDEX));
}
return null;
}).when(client).get(any(), any());
EntityProfile expectedProfile = new EntityProfile.Builder().build();
runner.profile(detectorId, entity, initNInfo, ActionListener.wrap(response -> {
assertEquals(expectedProfile, response);
inProgressLatch.countDown();
}, exception -> {
LOG.error("Unexpected error", exception);
assertTrue("Should not reach here", false);
inProgressLatch.countDown();
}));
assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
}
Aggregations