Search in sources :

Example 16 with ClusterSettings

use of org.opensearch.common.settings.ClusterSettings in project anomaly-detection by opensearch-project.

the class ADUnitTestCase method clusterSetting.

/**
 * Create cluster setting.
 *
 * @param settings cluster settings
 * @param setting add setting if the code to be tested contains setting update consumer
 * @return instance of ClusterSettings
 */
public ClusterSettings clusterSetting(Settings settings, Setting<?>... setting) {
    final Set<Setting<?>> settingsSet = Stream.concat(ClusterSettings.BUILT_IN_CLUSTER_SETTINGS.stream(), Sets.newHashSet(setting).stream()).collect(Collectors.toSet());
    ClusterSettings clusterSettings = new ClusterSettings(settings, settingsSet);
    return clusterSettings;
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) Setting(org.opensearch.common.settings.Setting)

Example 17 with ClusterSettings

use of org.opensearch.common.settings.ClusterSettings in project k-NN by opensearch-project.

the class ModelCacheTests method testGet_modelDoesNotFitInCache.

public void testGet_modelDoesNotFitInCache() throws ExecutionException, InterruptedException {
    String modelId = "test-model-id";
    int dimension = 2;
    String cacheSize = "1kb";
    Model mockModel = new Model(new ModelMetadata(KNNEngine.DEFAULT, SpaceType.DEFAULT, dimension, ModelState.CREATED, ZonedDateTime.now(ZoneOffset.UTC).toString(), "", ""), new byte[BYTES_PER_KILOBYTES + 1], modelId);
    ModelDao modelDao = mock(ModelDao.class);
    when(modelDao.get(modelId)).thenReturn(mockModel);
    Settings settings = Settings.builder().put(MODEL_CACHE_SIZE_LIMIT_SETTING.getKey(), cacheSize).build();
    ClusterSettings clusterSettings = new ClusterSettings(settings, ImmutableSet.of(MODEL_CACHE_SIZE_LIMIT_SETTING));
    ClusterService clusterService = mock(ClusterService.class);
    when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
    when(clusterService.getSettings()).thenReturn(settings);
    ModelCache.initialize(modelDao, clusterService);
    ModelCache modelCache = new ModelCache();
    assertEquals(mockModel, modelCache.get(modelId));
    assertFalse(modelCache.contains(modelId));
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) ClusterService(org.opensearch.cluster.service.ClusterService) Settings(org.opensearch.common.settings.Settings) ClusterSettings(org.opensearch.common.settings.ClusterSettings)

Example 18 with ClusterSettings

use of org.opensearch.common.settings.ClusterSettings in project k-NN by opensearch-project.

the class ModelCacheTests method testRebuild_afterSettingUpdate.

public void testRebuild_afterSettingUpdate() throws ExecutionException, InterruptedException {
    String modelId = "test-model-id";
    int dimension = 2;
    int modelSize = 2 * BYTES_PER_KILOBYTES;
    Model mockModel = new Model(new ModelMetadata(KNNEngine.DEFAULT, SpaceType.DEFAULT, dimension, ModelState.CREATED, ZonedDateTime.now(ZoneOffset.UTC).toString(), "", ""), new byte[modelSize], modelId);
    String cacheSize1 = "1kb";
    String cacheSize2 = "4kb";
    ModelDao modelDao = mock(ModelDao.class);
    when(modelDao.get(modelId)).thenReturn(mockModel);
    Settings settings = Settings.builder().put(MODEL_CACHE_SIZE_LIMIT_SETTING.getKey(), cacheSize1).build();
    ClusterSettings clusterSettings = new ClusterSettings(settings, ImmutableSet.of(MODEL_CACHE_SIZE_LIMIT_SETTING));
    ClusterService clusterService = mock(ClusterService.class);
    when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
    when(clusterService.getSettings()).thenReturn(settings);
    ModelCache.initialize(modelDao, clusterService);
    ModelCache modelCache = new ModelCache();
    // Add element to cache - element should not remain in cache
    modelCache.get(modelId);
    assertEquals(0, modelCache.getTotalWeightInKB());
    // Rebuild and make sure cache is empty
    Settings newSettings = Settings.builder().put(MODEL_CACHE_SIZE_LIMIT_SETTING.getKey(), cacheSize2).build();
    clusterService.getClusterSettings().applySettings(newSettings);
    assertEquals(0, modelCache.getTotalWeightInKB());
    // Add element again - element should remain in cache
    modelCache.get(modelId);
    assertEquals((modelSize / BYTES_PER_KILOBYTES) + 1, modelCache.getTotalWeightInKB());
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) ClusterService(org.opensearch.cluster.service.ClusterService) Settings(org.opensearch.common.settings.Settings) ClusterSettings(org.opensearch.common.settings.ClusterSettings)

Example 19 with ClusterSettings

use of org.opensearch.common.settings.ClusterSettings in project k-NN by opensearch-project.

the class ModelCacheTests method testContains.

public void testContains() throws ExecutionException, InterruptedException {
    String modelId1 = "test-model-id-1";
    int dimension = 2;
    int modelSize1 = 100;
    Model mockModel1 = new Model(new ModelMetadata(KNNEngine.DEFAULT, SpaceType.DEFAULT, dimension, ModelState.CREATED, ZonedDateTime.now(ZoneOffset.UTC).toString(), "", ""), new byte[modelSize1], modelId1);
    String modelId2 = "test-model-id-2";
    String cacheSize = "10%";
    ModelDao modelDao = mock(ModelDao.class);
    when(modelDao.get(modelId1)).thenReturn(mockModel1);
    Settings settings = Settings.builder().put(MODEL_CACHE_SIZE_LIMIT_SETTING.getKey(), cacheSize).build();
    ClusterSettings clusterSettings = new ClusterSettings(settings, ImmutableSet.of(MODEL_CACHE_SIZE_LIMIT_SETTING));
    ClusterService clusterService = mock(ClusterService.class);
    when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
    when(clusterService.getSettings()).thenReturn(settings);
    ModelCache.initialize(modelDao, clusterService);
    ModelCache modelCache = new ModelCache();
    assertFalse(modelCache.contains(modelId1));
    modelCache.get(modelId1);
    assertTrue(modelCache.contains(modelId1));
    assertFalse(modelCache.contains(modelId2));
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) ClusterService(org.opensearch.cluster.service.ClusterService) Settings(org.opensearch.common.settings.Settings) ClusterSettings(org.opensearch.common.settings.ClusterSettings)

Example 20 with ClusterSettings

use of org.opensearch.common.settings.ClusterSettings in project k-NN by opensearch-project.

the class ModelCacheTests method testGetTotalWeight.

public void testGetTotalWeight() throws ExecutionException, InterruptedException {
    String modelId1 = "test-model-id-1";
    String modelId2 = "test-model-id-2";
    int dimension = 2;
    String cacheSize = "10%";
    int size1 = BYTES_PER_KILOBYTES;
    Model mockModel1 = new Model(new ModelMetadata(KNNEngine.DEFAULT, SpaceType.DEFAULT, dimension, ModelState.CREATED, ZonedDateTime.now(ZoneOffset.UTC).toString(), "", ""), new byte[size1], modelId1);
    int size2 = BYTES_PER_KILOBYTES * 3;
    Model mockModel2 = new Model(new ModelMetadata(KNNEngine.DEFAULT, SpaceType.DEFAULT, dimension, ModelState.CREATED, ZonedDateTime.now(ZoneOffset.UTC).toString(), "", ""), new byte[size2], modelId2);
    ModelDao modelDao = mock(ModelDao.class);
    when(modelDao.get(modelId1)).thenReturn(mockModel1);
    when(modelDao.get(modelId2)).thenReturn(mockModel2);
    Settings settings = Settings.builder().put(MODEL_CACHE_SIZE_LIMIT_SETTING.getKey(), cacheSize).build();
    ClusterSettings clusterSettings = new ClusterSettings(settings, ImmutableSet.of(MODEL_CACHE_SIZE_LIMIT_SETTING));
    ClusterService clusterService = mock(ClusterService.class);
    when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
    when(clusterService.getSettings()).thenReturn(settings);
    ModelCache.initialize(modelDao, clusterService);
    ModelCache modelCache = new ModelCache();
    modelCache.get(modelId1);
    modelCache.get(modelId2);
    modelCache.get(modelId1);
    modelCache.get(modelId2);
    assertEquals((size1 + size2) / BYTES_PER_KILOBYTES + 2, modelCache.getTotalWeightInKB());
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) ClusterService(org.opensearch.cluster.service.ClusterService) Settings(org.opensearch.common.settings.Settings) ClusterSettings(org.opensearch.common.settings.ClusterSettings)

Aggregations

ClusterSettings (org.opensearch.common.settings.ClusterSettings)218 Settings (org.opensearch.common.settings.Settings)136 ClusterState (org.opensearch.cluster.ClusterState)70 ClusterService (org.opensearch.cluster.service.ClusterService)67 HashSet (java.util.HashSet)46 Metadata (org.opensearch.cluster.metadata.Metadata)37 Matchers.containsString (org.hamcrest.Matchers.containsString)36 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)36 Before (org.junit.Before)33 ArrayList (java.util.ArrayList)31 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)31 ClusterName (org.opensearch.cluster.ClusterName)30 ThreadPool (org.opensearch.threadpool.ThreadPool)29 RoutingTable (org.opensearch.cluster.routing.RoutingTable)26 TestThreadPool (org.opensearch.threadpool.TestThreadPool)25 AtomicReference (java.util.concurrent.atomic.AtomicReference)23 Setting (org.opensearch.common.settings.Setting)23 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)23 IOException (java.io.IOException)19 BalancedShardsAllocator (org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator)19