Java Code Examples for org.elasticsearch.cluster.node.DiscoveryNodes#builder()
The following examples show how to use
org.elasticsearch.cluster.node.DiscoveryNodes#builder() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HashRingTests.java From anomaly-detection with Apache License 2.0 | 6 votes |
private void setNodeState(Map<String, String> attributesForNode1) { DiscoveryNodes.Builder discoBuilder = DiscoveryNodes.builder(); List<DiscoveryNode> discoveryNodes = new ArrayList<>(); for (int i = 0; i < 3; i++) { DiscoveryNode node = null; if (i != 1) { node = createNode(Integer.toString(i), emptyMap()); } else { node = createNode(Integer.toString(i), attributesForNode1); } discoBuilder = discoBuilder.add(node); discoveryNodes.add(node); } discoBuilder.localNodeId("1"); discoBuilder.masterNodeId("0"); ClusterState.Builder stateBuilder = ClusterState.builder(clusterService.getClusterName()); stateBuilder.nodes(discoBuilder); ClusterState clusterState = stateBuilder.build(); setState(clusterService.getClusterApplierService(), clusterState); }
Example 2
Source File: JoinClusterAction.java From Elasticsearch with Apache License 2.0 | 6 votes |
@Override public ClusterState execute(ClusterState currentState) { DiscoveryNodes.Builder nodesBuilder; nodesBuilder = DiscoveryNodes.builder(currentState.nodes()); if (currentState.nodes().nodeExists(node.id())) { logger.debug("received a join request for an existing node [{}]", node); return currentState; } // If this node is not in dead node list, then ignore this request ImmutableOpenMap<String, DiscoveryNode> deadNodes = clusterService.state().nodes().deadNodes(); if (deadNodes.get(node.getIpPortAddress()) == null) { logger.warn("failed to find node [{}] in node list, ignore the join request", node); throw new IllegalStateException("could not find this node " + node + " from active node list and dead node list"); } nodesBuilder.put(node); nodesBuilder.removeDeadNodeByIpPort(node); final ClusterState.Builder newStateBuilder = ClusterState.builder(currentState); newStateBuilder.nodes(nodesBuilder); ClusterState newState = newStateBuilder.build(); return newState; }
Example 3
Source File: NodeRemovalClusterStateTaskExecutor.java From crate with Apache License 2.0 | 6 votes |
@Override public ClusterTasksResult<Task> execute(final ClusterState currentState, final List<Task> tasks) throws Exception { final DiscoveryNodes.Builder remainingNodesBuilder = DiscoveryNodes.builder(currentState.nodes()); boolean removed = false; for (final Task task : tasks) { if (currentState.nodes().nodeExists(task.node())) { remainingNodesBuilder.remove(task.node()); removed = true; } else { logger.debug("node [{}] does not exist in cluster state, ignoring", task); } } if (!removed) { // no nodes to remove, keep the current cluster state return ClusterTasksResult.<Task>builder().successes(tasks).build(currentState); } final ClusterState remainingNodesClusterState = remainingNodesClusterState(currentState, remainingNodesBuilder); return getTaskClusterTasksResult(currentState, tasks, remainingNodesClusterState); }
Example 4
Source File: NodeRemovalClusterStateTaskExecutorTests.java From crate with Apache License 2.0 | 6 votes |
public void testRemovingNonExistentNodes() throws Exception { final NodeRemovalClusterStateTaskExecutor executor = new NodeRemovalClusterStateTaskExecutor(null, logger); final DiscoveryNodes.Builder builder = DiscoveryNodes.builder(); final int nodes = randomIntBetween(2, 16); for (int i = 0; i < nodes; i++) { builder.add(node(i)); } final ClusterState clusterState = ClusterState.builder(new ClusterName("test")).nodes(builder).build(); final DiscoveryNodes.Builder removeBuilder = DiscoveryNodes.builder(); for (int i = nodes; i < nodes + randomIntBetween(1, 16); i++) { removeBuilder.add(node(i)); } final List<NodeRemovalClusterStateTaskExecutor.Task> tasks = StreamSupport .stream(removeBuilder.build().spliterator(), false) .map(node -> new NodeRemovalClusterStateTaskExecutor.Task(node, randomBoolean() ? "left" : "failed")) .collect(Collectors.toList()); final ClusterStateTaskExecutor.ClusterTasksResult<NodeRemovalClusterStateTaskExecutor.Task> result = executor.execute(clusterState, tasks); assertThat(result.resultingState, equalTo(clusterState)); }
Example 5
Source File: NodeJoinController.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public ClusterState execute(ClusterState currentState) { DiscoveryNodes.Builder nodesBuilder; synchronized (pendingJoinRequests) { if (pendingJoinRequests.isEmpty()) { return currentState; } nodesBuilder = DiscoveryNodes.builder(currentState.nodes()); Iterator<Map.Entry<DiscoveryNode, List<MembershipAction.JoinCallback>>> iterator = pendingJoinRequests.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<DiscoveryNode, List<MembershipAction.JoinCallback>> entry = iterator.next(); final DiscoveryNode node = entry.getKey(); joinCallbacksToRespondTo.addAll(entry.getValue()); iterator.remove(); if (currentState.nodes().nodeExists(node.id())) { logger.debug("received a join request for an existing node [{}]", node); } else { nodeAdded = true; nodesBuilder.put(node); for (DiscoveryNode existingNode : currentState.nodes()) { if (node.address().equals(existingNode.address())) { nodesBuilder.remove(existingNode.id()); logger.warn("received join request from node [{}], but found existing node {} with same address, removing existing node", node, existingNode); } } } } } // we must return a new cluster state instance to force publishing. This is important // for the joining node to finalize it's join and set us as a master final ClusterState.Builder newState = ClusterState.builder(currentState); if (nodeAdded) { newState.nodes(nodesBuilder); } return newState.build(); }
Example 6
Source File: PublicationTests.java From crate with Apache License 2.0 | 5 votes |
public void testPublishingToMastersFirst() { VotingConfiguration singleNodeConfig = new VotingConfiguration(Sets.newHashSet(n1.getId())); initializeCluster(singleNodeConfig); DiscoveryNodes.Builder discoNodesBuilder = DiscoveryNodes.builder(); randomNodes(10).forEach(dn -> discoNodesBuilder.add(dn)); DiscoveryNodes discoveryNodes = discoNodesBuilder.add(n1).localNodeId(n1.getId()).build(); MockPublication publication = node1.publish(CoordinationStateTests.clusterState(1L, 2L, discoveryNodes, singleNodeConfig, singleNodeConfig, 42L), null, Collections.emptySet()); List<DiscoveryNode> publicationTargets = new ArrayList<>(publication.pendingPublications.keySet()); List<DiscoveryNode> sortedPublicationTargets = new ArrayList<>(publicationTargets); Collections.sort(sortedPublicationTargets, Comparator.comparing(n -> n.isMasterNode() == false)); assertEquals(sortedPublicationTargets, publicationTargets); }
Example 7
Source File: NodeRemovalClusterStateTaskExecutorTests.java From crate with Apache License 2.0 | 5 votes |
public void testRerouteAfterRemovingNodes() throws Exception { final AllocationService allocationService = mock(AllocationService.class); when(allocationService.disassociateDeadNodes(any(ClusterState.class), eq(true), any(String.class))) .thenAnswer(im -> im.getArguments()[0]); final AtomicReference<ClusterState> remainingNodesClusterState = new AtomicReference<>(); final NodeRemovalClusterStateTaskExecutor executor = new NodeRemovalClusterStateTaskExecutor(allocationService, logger) { @Override protected ClusterState remainingNodesClusterState(ClusterState currentState, DiscoveryNodes.Builder remainingNodesBuilder) { remainingNodesClusterState.set(super.remainingNodesClusterState(currentState, remainingNodesBuilder)); return remainingNodesClusterState.get(); } }; final DiscoveryNodes.Builder builder = DiscoveryNodes.builder(); final int nodes = randomIntBetween(2, 16); final List<NodeRemovalClusterStateTaskExecutor.Task> tasks = new ArrayList<>(); // to ensure that there is at least one removal boolean first = true; for (int i = 0; i < nodes; i++) { final DiscoveryNode node = node(i); builder.add(node); if (first || randomBoolean()) { tasks.add(new NodeRemovalClusterStateTaskExecutor.Task(node, randomBoolean() ? "left" : "failed")); } first = false; } final ClusterState clusterState = ClusterState.builder(new ClusterName("test")).nodes(builder).build(); final ClusterStateTaskExecutor.ClusterTasksResult<NodeRemovalClusterStateTaskExecutor.Task> result = executor.execute(clusterState, tasks); verify(allocationService).disassociateDeadNodes(eq(remainingNodesClusterState.get()), eq(true), any(String.class)); for (final NodeRemovalClusterStateTaskExecutor.Task task : tasks) { assertNull(result.resultingState.nodes().get(task.node().getId())); } }
Example 8
Source File: PrimaryTermsTests.java From crate with Apache License 2.0 | 5 votes |
/** * puts primary shard indexRoutings into initializing state */ private void initPrimaries() { logger.info("adding {} nodes and performing rerouting", this.numberOfReplicas + 1); Builder discoBuilder = DiscoveryNodes.builder(); for (int i = 0; i < this.numberOfReplicas + 1; i++) { discoBuilder = discoBuilder.add(newNode("node" + i)); } this.clusterState = ClusterState.builder(clusterState).nodes(discoBuilder).build(); ClusterState rerouteResult = allocationService.reroute(clusterState, "reroute"); assertThat(rerouteResult, not(equalTo(this.clusterState))); applyRerouteResult(rerouteResult); primaryTermsPerIndex.keySet().forEach(this::incrementPrimaryTerm); }
Example 9
Source File: PrimaryTermsTests.java From crate with Apache License 2.0 | 5 votes |
private void addNodes() { DiscoveryNodes.Builder nodesBuilder = DiscoveryNodes.builder(clusterState.nodes()); final int newNodes = randomInt(10); logger.info("adding [{}] nodes", newNodes); for (int i = 0; i < newNodes; i++) { nodesBuilder.add(newNode("extra_" + i)); } this.clusterState = ClusterState.builder(clusterState).nodes(nodesBuilder).build(); applyRerouteResult(allocationService.reroute(this.clusterState, "nodes added")); }
Example 10
Source File: SQLExecutor.java From crate with Apache License 2.0 | 5 votes |
private void addNodesToClusterState(int numNodes) { ClusterState prevState = clusterService.state(); DiscoveryNodes.Builder builder = DiscoveryNodes.builder(prevState.nodes()); for (int i = 1; i <= numNodes; i++) { if (builder.get("n" + i) == null) { builder.add(new DiscoveryNode("n" + i, newFakeAddress(), Version.CURRENT)); } } builder.localNodeId("n1"); builder.masterNodeId("n1"); ClusterServiceUtils.setState( clusterService, ClusterState.builder(prevState).nodes(builder).build()); }