Search in sources :

Example 11 with EntityCache

use of org.opensearch.ad.caching.EntityCache in project anomaly-detection by opensearch-project.

the class EntityProfileTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    state = new HashSet<EntityProfileName>();
    state.add(EntityProfileName.STATE);
    all = new HashSet<EntityProfileName>();
    all.add(EntityProfileName.INIT_PROGRESS);
    all.add(EntityProfileName.ENTITY_INFO);
    all.add(EntityProfileName.MODELS);
    model = new HashSet<EntityProfileName>();
    model.add(EntityProfileName.MODELS);
    hashRing = mock(HashRing.class);
    actionFilters = mock(ActionFilters.class);
    transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet());
    settings = Settings.EMPTY;
    modelId = "yecrdnUBqurvo9uKU_d8_entity_app_0";
    clusterService = mock(ClusterService.class);
    cacheProvider = mock(CacheProvider.class);
    EntityCache cache = mock(EntityCache.class);
    updates = 1L;
    when(cache.getTotalUpdates(anyString(), anyString())).thenReturn(updates);
    when(cache.isActive(anyString(), anyString())).thenReturn(isActive);
    when(cache.getLastActiveMs(anyString(), anyString())).thenReturn(lastActiveTimestamp);
    Map<String, Long> modelSizeMap = new HashMap<>();
    modelSizeMap.put(modelId, modelSize);
    when(cache.getModelSize(anyString())).thenReturn(modelSizeMap);
    when(cacheProvider.get()).thenReturn(cache);
    action = new EntityProfileTransportAction(actionFilters, transportService, settings, hashRing, clusterService, cacheProvider);
    future = new PlainActionFuture<>();
    transportAddress1 = new TransportAddress(new InetSocketAddress(InetAddress.getByName("1.2.3.4"), 9300));
    entity = Entity.createSingleAttributeEntity(categoryName, entityValue);
    request = new EntityProfileRequest(detectorId, entity, state);
    normalTransportInterceptor = new TransportInterceptor() {

        @Override
        public AsyncSender interceptSender(AsyncSender sender) {
            return new AsyncSender() {

                @Override
                public <T extends TransportResponse> void sendRequest(Transport.Connection connection, String action, TransportRequest request, TransportRequestOptions options, TransportResponseHandler<T> handler) {
                    if (EntityProfileAction.NAME.equals(action)) {
                        sender.sendRequest(connection, action, request, options, entityProfileHandler(handler));
                    } else {
                        sender.sendRequest(connection, action, request, options, handler);
                    }
                }
            };
        }
    };
    failureTransportInterceptor = new TransportInterceptor() {

        @Override
        public AsyncSender interceptSender(AsyncSender sender) {
            return new AsyncSender() {

                @Override
                public <T extends TransportResponse> void sendRequest(Transport.Connection connection, String action, TransportRequest request, TransportRequestOptions options, TransportResponseHandler<T> handler) {
                    if (EntityProfileAction.NAME.equals(action)) {
                        sender.sendRequest(connection, action, request, options, entityFailureProfileandler(handler));
                    } else {
                        sender.sendRequest(connection, action, request, options, handler);
                    }
                }
            };
        }
    };
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CacheProvider(org.opensearch.ad.caching.CacheProvider) JsonDeserializer(test.org.opensearch.ad.util.JsonDeserializer) BeforeClass(org.junit.BeforeClass) ModelProfile(org.opensearch.ad.model.ModelProfile) HashRing(org.opensearch.ad.cluster.HashRing) ToXContent(org.opensearch.common.xcontent.ToXContent) TransportInterceptor(org.opensearch.transport.TransportInterceptor) AbstractADTest(org.opensearch.ad.AbstractADTest) HashMap(java.util.HashMap) Version(org.opensearch.Version) JsonPathNotFoundException(org.opensearch.ad.common.exception.JsonPathNotFoundException) EntityProfileName(org.opensearch.ad.model.EntityProfileName) ModelProfileOnNode(org.opensearch.ad.model.ModelProfileOnNode) AnomalyDetectionException(org.opensearch.ad.common.exception.AnomalyDetectionException) InetAddress(java.net.InetAddress) HashSet(java.util.HashSet) Transport(org.opensearch.transport.Transport) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) Map(java.util.Map) EntityCache(org.opensearch.ad.caching.EntityCache) Mockito.anyString(org.mockito.Mockito.anyString) StreamInput(org.opensearch.common.io.stream.StreamInput) AfterClass(org.junit.AfterClass) CommonName(org.opensearch.ad.constant.CommonName) TransportRequest(org.opensearch.transport.TransportRequest) TransportRequestOptions(org.opensearch.transport.TransportRequestOptions) Set(java.util.Set) TransportResponseHandler(org.opensearch.transport.TransportResponseHandler) IOException(java.io.IOException) Settings(org.opensearch.common.settings.Settings) Mockito.when(org.mockito.Mockito.when) Task(org.opensearch.tasks.Task) TransportResponse(org.opensearch.transport.TransportResponse) InetSocketAddress(java.net.InetSocketAddress) TransportService(org.opensearch.transport.TransportService) TransportAddress(org.opensearch.common.transport.TransportAddress) ActionFilters(org.opensearch.action.support.ActionFilters) Entity(org.opensearch.ad.model.Entity) TestHelpers(org.opensearch.ad.TestHelpers) FakeNode(test.org.opensearch.ad.util.FakeNode) ClusterService(org.opensearch.cluster.service.ClusterService) Optional(java.util.Optional) ConnectTransportException(org.opensearch.transport.ConnectTransportException) Collections(java.util.Collections) TransportException(org.opensearch.transport.TransportException) Mockito.mock(org.mockito.Mockito.mock) TransportInterceptor(org.opensearch.transport.TransportInterceptor) EntityCache(org.opensearch.ad.caching.EntityCache) HashMap(java.util.HashMap) TransportAddress(org.opensearch.common.transport.TransportAddress) InetSocketAddress(java.net.InetSocketAddress) Mockito.anyString(org.mockito.Mockito.anyString) HashRing(org.opensearch.ad.cluster.HashRing) TransportRequestOptions(org.opensearch.transport.TransportRequestOptions) TransportRequest(org.opensearch.transport.TransportRequest) ActionFilters(org.opensearch.action.support.ActionFilters) CacheProvider(org.opensearch.ad.caching.CacheProvider) ClusterService(org.opensearch.cluster.service.ClusterService) TransportService(org.opensearch.transport.TransportService) Transport(org.opensearch.transport.Transport) EntityProfileName(org.opensearch.ad.model.EntityProfileName)

Example 12 with EntityCache

use of org.opensearch.ad.caching.EntityCache in project anomaly-detection by opensearch-project.

the class ProfileTransportActionTests method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    modelManager = mock(ModelManager.class);
    featureManager = mock(FeatureManager.class);
    when(featureManager.getShingleSize(any(String.class))).thenReturn(shingleSize);
    EntityCache cache = mock(EntityCache.class);
    cacheProvider = mock(CacheProvider.class);
    when(cacheProvider.get()).thenReturn(cache);
    when(cache.getActiveEntities(anyString())).thenReturn(activeEntities);
    when(cache.getTotalUpdates(anyString())).thenReturn(totalUpdates);
    Map<String, Long> multiEntityModelSizeMap = new HashMap<>();
    String modelId1 = "T4c3dXUBj-2IZN7itix__entity_app_3";
    String modelId2 = "T4c3dXUBj-2IZN7itix__entity_app_2";
    multiEntityModelSizeMap.put(modelId1, multiEntityModelSize);
    multiEntityModelSizeMap.put(modelId2, multiEntityModelSize);
    when(cache.getModelSize(anyString())).thenReturn(multiEntityModelSizeMap);
    List<ModelProfile> modelProfiles = new ArrayList<>();
    String field = "field";
    String fieldVal1 = "value1";
    String fieldVal2 = "value2";
    Entity entity1 = Entity.createSingleAttributeEntity(field, fieldVal1);
    Entity entity2 = Entity.createSingleAttributeEntity(field, fieldVal2);
    modelProfiles.add(new ModelProfile(modelId1, entity1, multiEntityModelSize));
    modelProfiles.add(new ModelProfile(modelId1, entity2, multiEntityModelSize));
    when(cache.getAllModelProfile(anyString())).thenReturn(modelProfiles);
    Map<String, Long> modelSizes = new HashMap<>();
    modelSizes.put(modelId, modelSize);
    when(modelManager.getModelSize(any(String.class))).thenReturn(modelSizes);
    Settings settings = Settings.builder().put("plugins.anomaly_detection.max_model_size_per_node", 100).build();
    action = new ProfileTransportAction(client().threadPool(), clusterService(), mock(TransportService.class), mock(ActionFilters.class), modelManager, featureManager, cacheProvider, settings);
    profilesToRetrieve = new HashSet<DetectorProfileName>();
    profilesToRetrieve.add(DetectorProfileName.COORDINATING_NODE);
}
Also used : Entity(org.opensearch.ad.model.Entity) EntityCache(org.opensearch.ad.caching.EntityCache) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ModelManager(org.opensearch.ad.ml.ModelManager) CacheProvider(org.opensearch.ad.caching.CacheProvider) ModelProfile(org.opensearch.ad.model.ModelProfile) DetectorProfileName(org.opensearch.ad.model.DetectorProfileName) FeatureManager(org.opensearch.ad.feature.FeatureManager) AnomalyDetectorSettings(org.opensearch.ad.settings.AnomalyDetectorSettings) Settings(org.opensearch.common.settings.Settings) Before(org.junit.Before)

Example 13 with EntityCache

use of org.opensearch.ad.caching.EntityCache in project anomaly-detection by opensearch-project.

the class CronTransportActionTests method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    ThreadPool threadPool = mock(ThreadPool.class);
    ClusterService clusterService = mock(ClusterService.class);
    localNodeID = "foo";
    when(clusterService.localNode()).thenReturn(new DiscoveryNode(localNodeID, buildNewFakeTransportAddress(), Version.CURRENT));
    when(clusterService.getClusterName()).thenReturn(new ClusterName("test"));
    TransportService transportService = mock(TransportService.class);
    ActionFilters actionFilters = mock(ActionFilters.class);
    NodeStateManager tarnsportStatemanager = mock(NodeStateManager.class);
    ModelManager modelManager = mock(ModelManager.class);
    FeatureManager featureManager = mock(FeatureManager.class);
    CacheProvider cacheProvider = mock(CacheProvider.class);
    EntityCache entityCache = mock(EntityCache.class);
    EntityColdStarter entityColdStarter = mock(EntityColdStarter.class);
    when(cacheProvider.get()).thenReturn(entityCache);
    ADTaskManager adTaskManager = mock(ADTaskManager.class);
    action = new CronTransportAction(threadPool, clusterService, transportService, actionFilters, tarnsportStatemanager, modelManager, featureManager, cacheProvider, entityColdStarter, adTaskManager);
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) EntityCache(org.opensearch.ad.caching.EntityCache) ThreadPool(org.opensearch.threadpool.ThreadPool) ActionFilters(org.opensearch.action.support.ActionFilters) ModelManager(org.opensearch.ad.ml.ModelManager) CacheProvider(org.opensearch.ad.caching.CacheProvider) NodeStateManager(org.opensearch.ad.NodeStateManager) ClusterService(org.opensearch.cluster.service.ClusterService) EntityColdStarter(org.opensearch.ad.ml.EntityColdStarter) TransportService(org.opensearch.transport.TransportService) ADTaskManager(org.opensearch.ad.task.ADTaskManager) ClusterName(org.opensearch.cluster.ClusterName) FeatureManager(org.opensearch.ad.feature.FeatureManager) Before(org.junit.Before)

Aggregations

EntityCache (org.opensearch.ad.caching.EntityCache)13 CacheProvider (org.opensearch.ad.caching.CacheProvider)10 Before (org.junit.Before)8 ModelManager (org.opensearch.ad.ml.ModelManager)8 ActionFilters (org.opensearch.action.support.ActionFilters)6 ClusterService (org.opensearch.cluster.service.ClusterService)6 ArrayList (java.util.ArrayList)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 FeatureManager (org.opensearch.ad.feature.FeatureManager)5 Entity (org.opensearch.ad.model.Entity)5 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)5 ClusterSettings (org.opensearch.common.settings.ClusterSettings)5 Settings (org.opensearch.common.settings.Settings)5 TransportService (org.opensearch.transport.TransportService)5 HashSet (java.util.HashSet)4 NodeStateManager (org.opensearch.ad.NodeStateManager)4 AnomalyDetectionIndices (org.opensearch.ad.indices.AnomalyDetectionIndices)4 EntityModel (org.opensearch.ad.ml.EntityModel)4 CounterSupplier (org.opensearch.ad.stats.suppliers.CounterSupplier)4 ThreadPool (org.opensearch.threadpool.ThreadPool)4