Skip to content

Commit 2176184

Browse files
authored
[Zen2] Introduce gossip-like discovery of master nodes (#32246)
This commit introduces the `PeerFinder` which can be used to collect the identities of the master-eligible nodes in a masterless cluster, based on the `UnicastHostsProvider`, the nodes in the `ClusterState`, and nodes that other nodes have discovered.
1 parent d80b639 commit 2176184

File tree

8 files changed

+1415
-27
lines changed

8 files changed

+1415
-27
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.cluster.coordination;
21+
22+
import org.elasticsearch.cluster.node.DiscoveryNode;
23+
import org.elasticsearch.common.io.stream.StreamInput;
24+
import org.elasticsearch.common.io.stream.StreamOutput;
25+
import org.elasticsearch.transport.TransportResponse;
26+
27+
import java.io.IOException;
28+
import java.util.List;
29+
import java.util.Objects;
30+
import java.util.Optional;
31+
32+
public class PeersResponse extends TransportResponse {
33+
private final Optional<DiscoveryNode> masterNode;
34+
private final List<DiscoveryNode> knownPeers;
35+
private final long term;
36+
37+
public PeersResponse(Optional<DiscoveryNode> masterNode, List<DiscoveryNode> knownPeers, long term) {
38+
assert masterNode.isPresent() == false || knownPeers.isEmpty();
39+
this.masterNode = masterNode;
40+
this.knownPeers = knownPeers;
41+
this.term = term;
42+
}
43+
44+
public PeersResponse(StreamInput in) throws IOException {
45+
masterNode = Optional.ofNullable(in.readOptionalWriteable(DiscoveryNode::new));
46+
knownPeers = in.readList(DiscoveryNode::new);
47+
term = in.readLong();
48+
assert masterNode.isPresent() == false || knownPeers.isEmpty();
49+
}
50+
51+
@Override
52+
public void writeTo(StreamOutput out) throws IOException {
53+
super.writeTo(out);
54+
out.writeOptionalWriteable(masterNode.orElse(null));
55+
out.writeList(knownPeers);
56+
out.writeLong(term);
57+
}
58+
59+
/**
60+
* @return the node that is currently leading, according to the responding node.
61+
*/
62+
public Optional<DiscoveryNode> getMasterNode() {
63+
return masterNode;
64+
}
65+
66+
/**
67+
* @return the collection of known peers of the responding node, or an empty collection if the responding node believes there
68+
* is currently a leader.
69+
*/
70+
public List<DiscoveryNode> getKnownPeers() {
71+
return knownPeers;
72+
}
73+
74+
/**
75+
* @return the current term of the responding node. If the responding node is the leader then this is the term in which it is
76+
* currently leading.
77+
*/
78+
public long getTerm() {
79+
return term;
80+
}
81+
82+
@Override
83+
public String toString() {
84+
return "PeersResponse{" +
85+
"masterNode=" + masterNode +
86+
", knownPeers=" + knownPeers +
87+
", term=" + term +
88+
'}';
89+
}
90+
91+
@Override
92+
public boolean equals(Object o) {
93+
if (this == o) return true;
94+
if (o == null || getClass() != o.getClass()) return false;
95+
PeersResponse that = (PeersResponse) o;
96+
return term == that.term &&
97+
Objects.equals(masterNode, that.masterNode) &&
98+
Objects.equals(knownPeers, that.knownPeers);
99+
}
100+
101+
@Override
102+
public int hashCode() {
103+
return Objects.hash(masterNode, knownPeers, term);
104+
}
105+
}

server/src/main/java/org/elasticsearch/common/settings/ClusterSettings.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.elasticsearch.common.util.concurrent.ThreadContext;
5555
import org.elasticsearch.discovery.DiscoveryModule;
5656
import org.elasticsearch.discovery.DiscoverySettings;
57+
import org.elasticsearch.discovery.PeerFinder;
5758
import org.elasticsearch.discovery.zen.ElectMasterService;
5859
import org.elasticsearch.discovery.zen.FaultDetection;
5960
import org.elasticsearch.discovery.zen.SettingsBasedHostsProvider;
@@ -423,6 +424,7 @@ public void apply(Settings value, Settings current, Settings previous) {
423424
Node.BREAKER_TYPE_KEY,
424425
OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING,
425426
IndexGraveyard.SETTING_MAX_TOMBSTONES,
426-
EnableAssignmentDecider.CLUSTER_TASKS_ALLOCATION_ENABLE_SETTING
427+
EnableAssignmentDecider.CLUSTER_TASKS_ALLOCATION_ENABLE_SETTING,
428+
PeerFinder.DISCOVERY_FIND_PEERS_INTERVAL_SETTING
427429
)));
428430
}

0 commit comments

Comments
 (0)