Search in sources :

Example 11 with ClusterService

use of org.opensearch.cluster.service.ClusterService in project k-NN by opensearch-project.

the class TrainingJobRouterTransportActionTests method testMultiNode_withCapacity_withPreferredAvailable.

public void testMultiNode_withCapacity_withPreferredAvailable() {
    // Mock datanodes in the cluster through mocking the cluster service
    List<String> nodeIds = ImmutableList.of("node-1", "node-2", "node-3");
    String preferredNode = nodeIds.get(2);
    ImmutableOpenMap<String, DiscoveryNode> discoveryNodesMap = generateDiscoveryNodes(nodeIds);
    ClusterService clusterService = generateMockedClusterService(discoveryNodesMap);
    // Create a response to be returned with job route decision info
    List<TrainingJobRouteDecisionInfoNodeResponse> responseList = new ArrayList<>();
    // First node has capacity
    responseList.add(new TrainingJobRouteDecisionInfoNodeResponse(discoveryNodesMap.get(nodeIds.get(0)), 0));
    // Second node has capacity
    responseList.add(new TrainingJobRouteDecisionInfoNodeResponse(discoveryNodesMap.get(nodeIds.get(1)), 0));
    // Third node with capacity
    responseList.add(new TrainingJobRouteDecisionInfoNodeResponse(discoveryNodesMap.get(nodeIds.get(2)), 0));
    TrainingJobRouteDecisionInfoResponse infoResponse = new TrainingJobRouteDecisionInfoResponse(ClusterName.DEFAULT, responseList, Collections.emptyList());
    TransportService transportService = mock(TransportService.class);
    Client client = mock(Client.class);
    // Setup the action
    TrainingJobRouterTransportAction transportAction = new TrainingJobRouterTransportAction(transportService, new ActionFilters(Collections.emptySet()), clusterService, client);
    // Select the node
    DiscoveryNode selectedNode = transportAction.selectNode(preferredNode, infoResponse);
    assertEquals(preferredNode, selectedNode.getId());
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ArrayList(java.util.ArrayList) ActionFilters(org.opensearch.action.support.ActionFilters) ClusterService(org.opensearch.cluster.service.ClusterService) TransportService(org.opensearch.transport.TransportService) Client(org.opensearch.client.Client)

Example 12 with ClusterService

use of org.opensearch.cluster.service.ClusterService in project k-NN by opensearch-project.

the class TrainingJobRouterTransportActionTests method testMultiNode_withoutCapacity.

public void testMultiNode_withoutCapacity() {
    // Mock datanodes in the cluster through mocking the cluster service
    List<String> nodeIds = ImmutableList.of("node-1", "node-2", "node-3");
    ImmutableOpenMap<String, DiscoveryNode> discoveryNodesMap = generateDiscoveryNodes(nodeIds);
    ClusterService clusterService = generateMockedClusterService(discoveryNodesMap);
    // Create a response to be returned with job route decision info
    List<TrainingJobRouteDecisionInfoNodeResponse> responseList = new ArrayList<>();
    // First node has no capacity
    responseList.add(new TrainingJobRouteDecisionInfoNodeResponse(discoveryNodesMap.get(nodeIds.get(0)), 1));
    // Second node has no capacity
    responseList.add(new TrainingJobRouteDecisionInfoNodeResponse(discoveryNodesMap.get(nodeIds.get(1)), 1));
    // Third node has no capacity
    responseList.add(new TrainingJobRouteDecisionInfoNodeResponse(discoveryNodesMap.get(nodeIds.get(1)), 1));
    TrainingJobRouteDecisionInfoResponse infoResponse = new TrainingJobRouteDecisionInfoResponse(ClusterName.DEFAULT, responseList, Collections.emptyList());
    TransportService transportService = mock(TransportService.class);
    Client client = mock(Client.class);
    // Setup the action
    TrainingJobRouterTransportAction transportAction = new TrainingJobRouterTransportAction(transportService, new ActionFilters(Collections.emptySet()), clusterService, client);
    // Select the node
    DiscoveryNode selectedNode = transportAction.selectNode(null, infoResponse);
    assertNull(selectedNode);
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ArrayList(java.util.ArrayList) ActionFilters(org.opensearch.action.support.ActionFilters) ClusterService(org.opensearch.cluster.service.ClusterService) TransportService(org.opensearch.transport.TransportService) Client(org.opensearch.client.Client)

Example 13 with ClusterService

use of org.opensearch.cluster.service.ClusterService in project k-NN by opensearch-project.

the class TrainingJobRouterTransportActionTests method testTrainingIndexSize.

@SuppressWarnings("unchecked")
public void testTrainingIndexSize() {
    String trainingIndexName = "training-index";
    int dimension = 133;
    int vectorCount = 1000000;
    // 519,531.25 KB ~= 520 MB
    int expectedSize = dimension * vectorCount * Float.BYTES / BYTES_PER_KILOBYTES + 1;
    // Setup the request
    TrainingModelRequest trainingModelRequest = new TrainingModelRequest(null, KNNMethodContext.getDefault(), dimension, trainingIndexName, "training-field", null, "description");
    // Mock client to return the right number of docs
    TotalHits totalHits = new TotalHits(vectorCount, TotalHits.Relation.EQUAL_TO);
    SearchHits searchHits = new SearchHits(new SearchHit[2], totalHits, 1.0f);
    SearchResponse searchResponse = mock(SearchResponse.class);
    when(searchResponse.getHits()).thenReturn(searchHits);
    Client client = mock(Client.class);
    doAnswer(invocationOnMock -> {
        ((ActionListener<SearchResponse>) invocationOnMock.getArguments()[1]).onResponse(searchResponse);
        return null;
    }).when(client).search(any(), any());
    // Setup the action
    ClusterService clusterService = mock(ClusterService.class);
    TransportService transportService = mock(TransportService.class);
    TrainingJobRouterTransportAction transportAction = new TrainingJobRouterTransportAction(transportService, new ActionFilters(Collections.emptySet()), clusterService, client);
    ActionListener<Integer> listener = ActionListener.wrap(size -> assertEquals(expectedSize, size.intValue()), e -> fail(e.getMessage()));
    transportAction.getTrainingIndexSizeInKB(trainingModelRequest, listener);
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) ActionFilters(org.opensearch.action.support.ActionFilters) SearchResponse(org.opensearch.action.search.SearchResponse) ClusterService(org.opensearch.cluster.service.ClusterService) ActionListener(org.opensearch.action.ActionListener) TransportService(org.opensearch.transport.TransportService) SearchHits(org.opensearch.search.SearchHits) Client(org.opensearch.client.Client)

Example 14 with ClusterService

use of org.opensearch.cluster.service.ClusterService in project k-NN by opensearch-project.

the class TrainingJobRouterTransportActionTests method generateMockedClusterService.

private ClusterService generateMockedClusterService(ImmutableOpenMap<String, DiscoveryNode> discoveryNodeMap) {
    DiscoveryNodes discoveryNodes = mock(DiscoveryNodes.class);
    when(discoveryNodes.getDataNodes()).thenReturn(discoveryNodeMap);
    ClusterState clusterState = mock(ClusterState.class);
    when(clusterState.nodes()).thenReturn(discoveryNodes);
    ClusterService clusterService = mock(ClusterService.class);
    when(clusterService.state()).thenReturn(clusterState);
    return clusterService;
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) ClusterService(org.opensearch.cluster.service.ClusterService) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

Example 15 with ClusterService

use of org.opensearch.cluster.service.ClusterService in project k-NN by opensearch-project.

the class TrainingModelRequestTests method testValidation_invalid_trainingIndexDoesNotExist.

public void testValidation_invalid_trainingIndexDoesNotExist() {
    // Check that validation produces exception when the training index doesnt exist
    // Setup the training request
    String modelId = "test-model-id";
    KNNMethodContext knnMethodContext = mock(KNNMethodContext.class);
    when(knnMethodContext.validate()).thenReturn(null);
    when(knnMethodContext.isTrainingRequired()).thenReturn(true);
    int dimension = 10;
    String trainingIndex = "test-training-index";
    String trainingField = "test-training-field";
    TrainingModelRequest trainingModelRequest = new TrainingModelRequest(modelId, knnMethodContext, dimension, trainingIndex, trainingField, null, null);
    // Mock the model dao to return null so that no exception is produced
    ModelDao modelDao = mock(ModelDao.class);
    when(modelDao.getMetadata(modelId)).thenReturn(null);
    Metadata metadata = mock(Metadata.class);
    when(metadata.index(trainingIndex)).thenReturn(null);
    ClusterState clusterState = mock(ClusterState.class);
    when(clusterState.metadata()).thenReturn(metadata);
    ClusterService clusterService = mock(ClusterService.class);
    when(clusterService.state()).thenReturn(clusterState);
    // Initialize static components with the mocks
    TrainingModelRequest.initialize(modelDao, clusterService);
    // Test that validation produces model already exists error message
    ActionRequestValidationException exception = trainingModelRequest.validate();
    assertNotNull(exception);
    List<String> validationErrors = exception.validationErrors();
    assertEquals(1, validationErrors.size());
    assertTrue(validationErrors.get(0).contains("does not exist"));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) KNNMethodContext(org.opensearch.knn.index.KNNMethodContext) ClusterService(org.opensearch.cluster.service.ClusterService) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ModelMetadata(org.opensearch.knn.indices.ModelMetadata) ModelDao(org.opensearch.knn.indices.ModelDao)

Aggregations

ClusterService (org.opensearch.cluster.service.ClusterService)296 ThreadPool (org.opensearch.threadpool.ThreadPool)123 Settings (org.opensearch.common.settings.Settings)115 ClusterState (org.opensearch.cluster.ClusterState)106 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)103 TestThreadPool (org.opensearch.threadpool.TestThreadPool)93 TransportService (org.opensearch.transport.TransportService)86 ClusterSettings (org.opensearch.common.settings.ClusterSettings)76 ActionListener (org.opensearch.action.ActionListener)75 Before (org.junit.Before)66 ActionFilters (org.opensearch.action.support.ActionFilters)66 CountDownLatch (java.util.concurrent.CountDownLatch)65 HashSet (java.util.HashSet)63 TimeValue (org.opensearch.common.unit.TimeValue)61 IOException (java.io.IOException)56 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)53 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)48 Collections (java.util.Collections)47 ClusterName (org.opensearch.cluster.ClusterName)47 Metadata (org.opensearch.cluster.metadata.Metadata)47