use of org.opensearch.cluster.node.DiscoveryNode in project k-NN by opensearch-project.
the class TrainingJobRouteDecisionInfoResponseTests method testToXContent.
public void testToXContent() throws IOException {
// Set up the data
String id1 = "id_1";
DiscoveryNode discoveryNode1 = mock(DiscoveryNode.class);
when(discoveryNode1.getId()).thenReturn(id1);
Integer trainingJobCount1 = 1;
TrainingJobRouteDecisionInfoNodeResponse nodeResponse1 = new TrainingJobRouteDecisionInfoNodeResponse(discoveryNode1, trainingJobCount1);
String id2 = "id_2";
DiscoveryNode discoveryNode2 = mock(DiscoveryNode.class);
when(discoveryNode2.getId()).thenReturn(id2);
Integer trainingJobCount2 = 2;
TrainingJobRouteDecisionInfoNodeResponse nodeResponse2 = new TrainingJobRouteDecisionInfoNodeResponse(discoveryNode2, trainingJobCount2);
String id3 = "id_3";
DiscoveryNode discoveryNode3 = mock(DiscoveryNode.class);
when(discoveryNode3.getId()).thenReturn(id3);
Integer trainingJobCount3 = 3;
TrainingJobRouteDecisionInfoNodeResponse nodeResponse3 = new TrainingJobRouteDecisionInfoNodeResponse(discoveryNode3, trainingJobCount3);
// We expect this:
// {
// "nodes": {
// "id_1": {
// "training_job_count": 1
// },
// "id_2": {
// "training_job_count": 2
// },
// "id_3": {
// "training_job_count": 3
// },
// }
// }
XContentBuilder expectedXContentBuilder = XContentFactory.jsonBuilder().startObject().startObject(NODES_KEY).startObject(id1).field(TRAINING_JOB_COUNT_FIELD_NAME, trainingJobCount1).endObject().startObject(id2).field(TRAINING_JOB_COUNT_FIELD_NAME, trainingJobCount2).endObject().startObject(id3).field(TRAINING_JOB_COUNT_FIELD_NAME, trainingJobCount3).endObject().endObject().endObject();
Map<String, Object> expected = xContentBuilderToMap(expectedXContentBuilder);
// Configure response
List<TrainingJobRouteDecisionInfoNodeResponse> nodeResponses = ImmutableList.of(nodeResponse1, nodeResponse2, nodeResponse3);
List<FailedNodeException> failedNodeExceptions = Collections.emptyList();
TrainingJobRouteDecisionInfoResponse response = new TrainingJobRouteDecisionInfoResponse(ClusterName.DEFAULT, nodeResponses, failedNodeExceptions);
XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
builder = response.toXContent(builder, ToXContent.EMPTY_PARAMS).endObject();
Map<String, Object> actual = xContentBuilderToMap(builder);
// Check responses are equal
assertTrue(Maps.difference(expected, actual).areEqual());
}
use of org.opensearch.cluster.node.DiscoveryNode in project anomaly-detection by opensearch-project.
the class NodeStateManagerTests method setUp.
@Override
@Before
public void setUp() throws Exception {
super.setUp();
client = mock(Client.class);
settings = Settings.builder().put("plugins.anomaly_detection.max_retry_for_unresponsive_node", 3).put("plugins.anomaly_detection.ad_mute_minutes", TimeValue.timeValueMinutes(10)).build();
clock = mock(Clock.class);
duration = Duration.ofHours(1);
context = TestHelpers.createThreadPool();
throttler = new Throttler(clock);
clientUtil = new ClientUtil(Settings.EMPTY, client, throttler, mock(ThreadPool.class));
Set<Setting<?>> nodestateSetting = new HashSet<>(ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
nodestateSetting.add(MAX_RETRY_FOR_UNRESPONSIVE_NODE);
nodestateSetting.add(BACKOFF_MINUTES);
clusterSettings = new ClusterSettings(Settings.EMPTY, nodestateSetting);
DiscoveryNode discoveryNode = new DiscoveryNode("node1", OpenSearchTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), DiscoveryNodeRole.BUILT_IN_ROLES, Version.CURRENT);
clusterService = ClusterServiceUtils.createClusterService(threadPool, discoveryNode, clusterSettings);
stateManager = new NodeStateManager(client, xContentRegistry(), settings, clientUtil, clock, duration, clusterService);
checkpointResponse = mock(GetResponse.class);
}
use of org.opensearch.cluster.node.DiscoveryNode in project anomaly-detection by opensearch-project.
the class ADClusterEventListenerTests method testIsWarmNode.
public void testIsWarmNode() {
HashMap<String, String> attributesForNode1 = new HashMap<>();
attributesForNode1.put(CommonName.BOX_TYPE_KEY, CommonName.WARM_BOX_TYPE);
dataNode1 = new DiscoveryNode(dataNode1Id, buildNewFakeTransportAddress(), attributesForNode1, BUILT_IN_ROLES, Version.CURRENT);
ClusterState warmNodeClusterState = ClusterState.builder(new ClusterName(clusterName)).nodes(new DiscoveryNodes.Builder().masterNodeId(masterNodeId).localNodeId(dataNode1Id).add(masterNode).add(dataNode1)).blocks(ClusterBlocks.builder().addGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK)).build();
listener.clusterChanged(new ClusterChangedEvent("foo", warmNodeClusterState, oldClusterState));
assertTrue(testAppender.containsMessage(ADClusterEventListener.NOT_RECOVERED_MSG));
}
use of org.opensearch.cluster.node.DiscoveryNode in project anomaly-detection by opensearch-project.
the class ADDataMigratorTests method testMigrateDataWithNormalJobResponse_ExistingDetector_ExistingInternalError.
public void testMigrateDataWithNormalJobResponse_ExistingDetector_ExistingInternalError() {
when(detectionIndices.doesAnomalyDetectorJobIndexExist()).thenReturn(true);
when(detectionIndices.doesDetectorStateIndexExist()).thenReturn(true);
String detectorId = randomAlphaOfLength(10);
doAnswer(invocation -> {
// Return correct AD job when search job index
ActionListener<SearchResponse> listener = invocation.getArgument(1);
SearchHit job1 = SearchHit.fromXContent(TestHelpers.parser(jobContent));
SearchHits searchHits = new SearchHits(new SearchHit[] { job1 }, new TotalHits(2, TotalHits.Relation.EQUAL_TO), Float.NaN);
InternalSearchResponse response = new InternalSearchResponse(searchHits, InternalAggregations.EMPTY, null, null, false, null, 1);
SearchResponse searchResponse = new SearchResponse(response, null, 1, 1, 0, 100, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY);
listener.onResponse(searchResponse);
return null;
}).doAnswer(invocation -> {
// Return null when search realtime tasks
ActionListener<SearchResponse> listener = invocation.getArgument(1);
listener.onResponse(null);
return null;
}).when(client).search(any(), any());
doAnswer(invocation -> {
// Return null when get detector internal error from index.
ActionListener<GetResponse> listener = invocation.getArgument(1);
XContentParser parser = TestHelpers.parser(internalError, false);
GetResponse getResponse = GetResponse.fromXContent(parser);
listener.onResponse(getResponse);
return null;
}).doAnswer(invocation -> {
// Return correct detector when get detector index.
ActionListener<GetResponse> listener = invocation.getArgument(1);
XContentParser parser = TestHelpers.parser(detectorContent, false);
GetResponse getResponse = GetResponse.fromXContent(parser);
listener.onResponse(getResponse);
return null;
}).when(client).get(any(), any());
doAnswer(invocation -> {
ActionListener<IndexResponse> listener = invocation.getArgument(1);
String taskId = randomAlphaOfLength(5);
IndexResponse indexResponse = IndexResponse.fromXContent(TestHelpers.parser(indexResponseContent, false));
listener.onResponse(indexResponse);
return null;
}).when(client).index(any(), any());
DiscoveryNode localNode = createNode("localNodeId");
doReturn(localNode).when(clusterService).localNode();
adDataMigrator.migrateData();
verify(adDataMigrator, times(2)).backfillRealtimeTask(any(), anyBoolean());
verify(client, times(1)).index(any(), any());
}
use of org.opensearch.cluster.node.DiscoveryNode in project anomaly-detection by opensearch-project.
the class HashRingTests method testGetOwningNode.
public void testGetOwningNode() throws UnknownHostException {
List<DiscoveryNode> addedNodes = setupNodeDelta();
// Add first node,
hashRing.buildCircles(delta, ActionListener.wrap(r -> {
Optional<DiscoveryNode> node = hashRing.getOwningNodeWithSameLocalAdVersionForRealtimeAD("http-latency-rcf-1");
assertTrue(node.isPresent());
assertTrue(asList(newNodeId, localNodeId).contains(node.get().getId()));
DiscoveryNode[] nodesWithSameLocalAdVersion = hashRing.getNodesWithSameLocalAdVersion();
Set<String> nodesWithSameLocalAdVersionIds = new HashSet<>();
for (DiscoveryNode n : nodesWithSameLocalAdVersion) {
nodesWithSameLocalAdVersionIds.add(n.getId());
}
assertFalse("Should not build warm node into hash ring", nodesWithSameLocalAdVersionIds.contains(warmNodeId));
assertEquals("Wrong hash ring size", 2, nodesWithSameLocalAdVersion.length);
assertEquals("Wrong hash ring size for historical analysis", 2, hashRing.getNodesWithSameAdVersion(Version.V_1_1_0, false).size());
// Circles for realtime AD will change as it's eligible to build for when its empty
assertEquals("Wrong hash ring size for realtime AD", 2, hashRing.getNodesWithSameAdVersion(Version.V_1_1_0, true).size());
}, e -> {
e.printStackTrace();
assertFalse("Build hash ring failed", true);
}));
// Second new node joins cluster, test realtime circles will not update.
String newNodeId2 = "newNode2";
DiscoveryNode newNode2 = createNode(newNodeId2, "127.0.0.4", 9200, emptyMap());
addedNodes.add(newNode2);
when(delta.addedNodes()).thenReturn(addedNodes);
setupClusterAdminClient(localNode, newNode, newNode2);
hashRing.buildCircles(delta, ActionListener.wrap(r -> {
assertEquals("Wrong hash ring size for historical analysis", 3, hashRing.getNodesWithSameAdVersion(Version.V_1_1_0, false).size());
// Circles for realtime AD will not change as it's eligible to rebuild
assertEquals("Wrong hash ring size for realtime AD", 2, hashRing.getNodesWithSameAdVersion(Version.V_1_1_0, true).size());
}, e -> {
e.printStackTrace();
assertFalse("Build hash ring failed", true);
}));
// Mock it's eligible to rebuild circles for realtime AD, then add new node. Realtime circles should change.
when(hashRing.eligibleToRebuildCirclesForRealtimeAD()).thenReturn(true);
String newNodeId3 = "newNode3";
DiscoveryNode newNode3 = createNode(newNodeId3, "127.0.0.5", 9200, emptyMap());
addedNodes.add(newNode3);
when(delta.addedNodes()).thenReturn(addedNodes);
setupClusterAdminClient(localNode, newNode, newNode2, newNode3);
hashRing.buildCircles(delta, ActionListener.wrap(r -> {
assertEquals("Wrong hash ring size for historical analysis", 4, hashRing.getNodesWithSameAdVersion(Version.V_1_1_0, false).size());
assertEquals("Wrong hash ring size for realtime AD", 4, hashRing.getNodesWithSameAdVersion(Version.V_1_1_0, true).size());
}, e -> {
e.printStackTrace();
assertFalse("Failed to build hash ring", true);
}));
}
Aggregations