Search in sources :

Example 1 with LocalSampleCalculator

use of org.opensearch.ml.engine.algorithms.sample.LocalSampleCalculator in project ml-commons by opensearch-project.

the class MLEngineClassLoaderTests method initInstance_LocalSampleCalculator.

@Test
public void initInstance_LocalSampleCalculator() {
    List<Double> inputData = new ArrayList<>();
    double d1 = 10.0;
    double d2 = 20.0;
    inputData.add(d1);
    inputData.add(d2);
    LocalSampleCalculatorInput input = LocalSampleCalculatorInput.builder().operation("sum").inputData(inputData).build();
    Map<String, Object> properties = new HashMap<>();
    properties.put("wrongField", "test");
    Client client = mock(Client.class);
    properties.put("client", client);
    Settings settings = Settings.EMPTY;
    properties.put("settings", settings);
    // set properties
    MLEngineClassLoader.deregister(FunctionName.LOCAL_SAMPLE_CALCULATOR);
    LocalSampleCalculator instance = MLEngineClassLoader.initInstance(FunctionName.LOCAL_SAMPLE_CALCULATOR, input, Input.class, properties);
    SampleAlgoOutput output = (SampleAlgoOutput) instance.execute(input);
    assertEquals(d1 + d2, output.getSampleResult(), 1e-6);
    assertEquals(client, instance.getClient());
    assertEquals(settings, instance.getSettings());
    // don't set properties
    instance = MLEngineClassLoader.initInstance(FunctionName.LOCAL_SAMPLE_CALCULATOR, input, Input.class);
    output = (SampleAlgoOutput) instance.execute(input);
    assertEquals(d1 + d2, output.getSampleResult(), 1e-6);
    assertNull(instance.getClient());
    assertNull(instance.getSettings());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LocalSampleCalculator(org.opensearch.ml.engine.algorithms.sample.LocalSampleCalculator) Input(org.opensearch.ml.common.parameter.Input) LocalSampleCalculatorInput(org.opensearch.ml.common.parameter.LocalSampleCalculatorInput) LocalSampleCalculatorInput(org.opensearch.ml.common.parameter.LocalSampleCalculatorInput) Client(org.opensearch.client.Client) SampleAlgoOutput(org.opensearch.ml.common.parameter.SampleAlgoOutput) Settings(org.opensearch.common.settings.Settings) Test(org.junit.Test)

Example 2 with LocalSampleCalculator

use of org.opensearch.ml.engine.algorithms.sample.LocalSampleCalculator in project ml-commons by opensearch-project.

the class MLEngineClassLoaderTests method initInstance_LocalSampleCalculator_RegisterFirst.

@Test
public void initInstance_LocalSampleCalculator_RegisterFirst() {
    Client client = mock(Client.class);
    Settings settings = Settings.EMPTY;
    LocalSampleCalculator calculator = new LocalSampleCalculator(client, settings);
    MLEngineClassLoader.register(FunctionName.LOCAL_SAMPLE_CALCULATOR, calculator);
    LocalSampleCalculator instance = MLEngineClassLoader.initInstance(FunctionName.LOCAL_SAMPLE_CALCULATOR, null, Input.class);
    assertEquals(calculator, instance);
}
Also used : Client(org.opensearch.client.Client) LocalSampleCalculator(org.opensearch.ml.engine.algorithms.sample.LocalSampleCalculator) Settings(org.opensearch.common.settings.Settings) Test(org.junit.Test)

Example 3 with LocalSampleCalculator

use of org.opensearch.ml.engine.algorithms.sample.LocalSampleCalculator in project ml-commons by opensearch-project.

the class MachineLearningPlugin method createComponents.

@Override
public Collection<Object> createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, ResourceWatcherService resourceWatcherService, ScriptService scriptService, NamedXContentRegistry xContentRegistry, Environment environment, NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry, IndexNameExpressionResolver indexNameExpressionResolver, Supplier<RepositoriesService> repositoriesServiceSupplier) {
    this.client = client;
    this.threadPool = threadPool;
    this.clusterService = clusterService;
    Settings settings = environment.settings();
    JvmService jvmService = new JvmService(environment.settings());
    MLCircuitBreakerService mlCircuitBreakerService = new MLCircuitBreakerService(jvmService).init();
    Map<String, MLStat<?>> stats = new ConcurrentHashMap<>();
    stats.put(StatNames.ML_EXECUTING_TASK_COUNT, new MLStat<>(false, new CounterSupplier()));
    stats.put(StatNames.ML_TOTAL_REQUEST_COUNT, new MLStat<>(false, new CounterSupplier()));
    stats.put(StatNames.ML_TOTAL_FAILURE_COUNT, new MLStat<>(false, new CounterSupplier()));
    stats.put(StatNames.ML_TOTAL_MODEL_COUNT, new MLStat<>(false, new CounterSupplier()));
    this.mlStats = new MLStats(stats);
    mlIndicesHandler = new MLIndicesHandler(clusterService, client);
    mlTaskManager = new MLTaskManager(client, mlIndicesHandler);
    mlInputDatasetHandler = new MLInputDatasetHandler(client);
    MLTaskDispatcher mlTaskDispatcher = new MLTaskDispatcher(clusterService, client);
    mlTrainingTaskRunner = new MLTrainingTaskRunner(threadPool, clusterService, client, mlTaskManager, mlStats, mlIndicesHandler, mlInputDatasetHandler, mlTaskDispatcher, mlCircuitBreakerService);
    mlPredictTaskRunner = new MLPredictTaskRunner(threadPool, clusterService, client, mlTaskManager, mlStats, mlInputDatasetHandler, mlTaskDispatcher, mlCircuitBreakerService);
    mlTrainAndPredictTaskRunner = new MLTrainAndPredictTaskRunner(threadPool, clusterService, client, mlTaskManager, mlStats, mlInputDatasetHandler, mlTaskDispatcher, mlCircuitBreakerService);
    mlExecuteTaskRunner = new MLExecuteTaskRunner(threadPool, clusterService, client, mlTaskManager, mlStats, mlInputDatasetHandler, mlTaskDispatcher, mlCircuitBreakerService);
    // Register thread-safe ML objects here.
    LocalSampleCalculator localSampleCalculator = new LocalSampleCalculator(client, settings);
    MLEngineClassLoader.register(FunctionName.LOCAL_SAMPLE_CALCULATOR, localSampleCalculator);
    AnomalyLocalizerImpl anomalyLocalizer = new AnomalyLocalizerImpl(client, settings);
    MLEngineClassLoader.register(FunctionName.ANOMALY_LOCALIZATION, anomalyLocalizer);
    MLSearchHandler mlSearchHandler = new MLSearchHandler(client, xContentRegistry);
    return ImmutableList.of(mlStats, mlTaskManager, mlIndicesHandler, mlInputDatasetHandler, mlTrainingTaskRunner, mlPredictTaskRunner, mlTrainAndPredictTaskRunner, mlExecuteTaskRunner, mlSearchHandler);
}
Also used : MLTrainAndPredictTaskRunner(org.opensearch.ml.task.MLTrainAndPredictTaskRunner) MLInputDatasetHandler(org.opensearch.ml.indices.MLInputDatasetHandler) MLStat(org.opensearch.ml.stats.MLStat) MLTaskManager(org.opensearch.ml.task.MLTaskManager) MLExecuteTaskRunner(org.opensearch.ml.task.MLExecuteTaskRunner) MLTrainingTaskRunner(org.opensearch.ml.task.MLTrainingTaskRunner) LocalSampleCalculator(org.opensearch.ml.engine.algorithms.sample.LocalSampleCalculator) CounterSupplier(org.opensearch.ml.stats.suppliers.CounterSupplier) MLIndicesHandler(org.opensearch.ml.indices.MLIndicesHandler) MLTaskDispatcher(org.opensearch.ml.task.MLTaskDispatcher) AnomalyLocalizerImpl(org.opensearch.ml.engine.algorithms.anomalylocalization.AnomalyLocalizerImpl) JvmService(org.opensearch.monitor.jvm.JvmService) MLStats(org.opensearch.ml.stats.MLStats) MLPredictTaskRunner(org.opensearch.ml.task.MLPredictTaskRunner) MLSearchHandler(org.opensearch.ml.action.handler.MLSearchHandler) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) Settings(org.opensearch.common.settings.Settings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) MLCircuitBreakerService(org.opensearch.ml.common.breaker.MLCircuitBreakerService)

Aggregations

Settings (org.opensearch.common.settings.Settings)3 LocalSampleCalculator (org.opensearch.ml.engine.algorithms.sample.LocalSampleCalculator)3 Test (org.junit.Test)2 Client (org.opensearch.client.Client)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ClusterSettings (org.opensearch.common.settings.ClusterSettings)1 IndexScopedSettings (org.opensearch.common.settings.IndexScopedSettings)1 MLSearchHandler (org.opensearch.ml.action.handler.MLSearchHandler)1 MLCircuitBreakerService (org.opensearch.ml.common.breaker.MLCircuitBreakerService)1 Input (org.opensearch.ml.common.parameter.Input)1 LocalSampleCalculatorInput (org.opensearch.ml.common.parameter.LocalSampleCalculatorInput)1 SampleAlgoOutput (org.opensearch.ml.common.parameter.SampleAlgoOutput)1 AnomalyLocalizerImpl (org.opensearch.ml.engine.algorithms.anomalylocalization.AnomalyLocalizerImpl)1 MLIndicesHandler (org.opensearch.ml.indices.MLIndicesHandler)1 MLInputDatasetHandler (org.opensearch.ml.indices.MLInputDatasetHandler)1 MLStat (org.opensearch.ml.stats.MLStat)1 MLStats (org.opensearch.ml.stats.MLStats)1 CounterSupplier (org.opensearch.ml.stats.suppliers.CounterSupplier)1