PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/main/java/org/elasticsearch/cluster/node/DiscoveryNode.java

https://gitlab.com/0072016/0072016--
Java | 372 lines | 228 code | 39 blank | 105 comment | 30 complexity | 1c56b366abb280fe20f4609f18909c40 MD5 | raw file
  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. package org.elasticsearch.cluster.node;
  20. import org.elasticsearch.Version;
  21. import org.elasticsearch.common.Strings;
  22. import org.elasticsearch.common.io.stream.StreamInput;
  23. import org.elasticsearch.common.io.stream.StreamOutput;
  24. import org.elasticsearch.common.io.stream.Writeable;
  25. import org.elasticsearch.common.settings.Settings;
  26. import org.elasticsearch.common.transport.TransportAddress;
  27. import org.elasticsearch.common.transport.TransportAddressSerializers;
  28. import org.elasticsearch.common.xcontent.ToXContent;
  29. import org.elasticsearch.common.xcontent.XContentBuilder;
  30. import org.elasticsearch.node.Node;
  31. import java.io.IOException;
  32. import java.util.Collections;
  33. import java.util.EnumSet;
  34. import java.util.HashMap;
  35. import java.util.Map;
  36. import java.util.Set;
  37. import java.util.function.Predicate;
  38. import static org.elasticsearch.common.transport.TransportAddressSerializers.addressToStream;
  39. /**
  40. * A discovery node represents a node that is part of the cluster.
  41. */
  42. public class DiscoveryNode implements Writeable, ToXContent {
  43. public static boolean isLocalNode(Settings settings) {
  44. if (Node.NODE_LOCAL_SETTING.exists(settings)) {
  45. return Node.NODE_LOCAL_SETTING.get(settings);
  46. }
  47. if (Node.NODE_MODE_SETTING.exists(settings)) {
  48. String nodeMode = Node.NODE_MODE_SETTING.get(settings);
  49. if ("local".equals(nodeMode)) {
  50. return true;
  51. } else if ("network".equals(nodeMode)) {
  52. return false;
  53. } else {
  54. throw new IllegalArgumentException("unsupported node.mode [" + nodeMode + "]. Should be one of [local, network].");
  55. }
  56. }
  57. return false;
  58. }
  59. public static boolean nodeRequiresLocalStorage(Settings settings) {
  60. return Node.NODE_DATA_SETTING.get(settings) || Node.NODE_MASTER_SETTING.get(settings);
  61. }
  62. public static boolean isMasterNode(Settings settings) {
  63. return Node.NODE_MASTER_SETTING.get(settings);
  64. }
  65. public static boolean isDataNode(Settings settings) {
  66. return Node.NODE_DATA_SETTING.get(settings);
  67. }
  68. public static boolean isIngestNode(Settings settings) {
  69. return Node.NODE_INGEST_SETTING.get(settings);
  70. }
  71. private final String nodeName;
  72. private final String nodeId;
  73. private final String hostName;
  74. private final String hostAddress;
  75. private final TransportAddress address;
  76. private final Map<String, String> attributes;
  77. private final Version version;
  78. private final Set<Role> roles;
  79. /**
  80. * Creates a new {@link DiscoveryNode}
  81. * <p>
  82. * <b>Note:</b> if the version of the node is unknown {@link Version#minimumCompatibilityVersion()} should be used for the current
  83. * version. it corresponds to the minimum version this elasticsearch version can communicate with. If a higher version is used
  84. * the node might not be able to communicate with the remove node. After initial handshakes node versions will be discovered
  85. * and updated.
  86. * </p>
  87. *
  88. * @param nodeId the nodes unique id.
  89. * @param address the nodes transport address
  90. * @param attributes node attributes
  91. * @param roles node roles
  92. * @param version the version of the node.
  93. */
  94. public DiscoveryNode(String nodeId, TransportAddress address, Map<String, String> attributes, Set<Role> roles, Version version) {
  95. this("", nodeId, address.getHost(), address.getAddress(), address, attributes, roles, version);
  96. }
  97. /**
  98. * Creates a new {@link DiscoveryNode}
  99. * <p>
  100. * <b>Note:</b> if the version of the node is unknown {@link Version#minimumCompatibilityVersion()} should be used for the current
  101. * version. it corresponds to the minimum version this elasticsearch version can communicate with. If a higher version is used
  102. * the node might not be able to communicate with the remove node. After initial handshakes node versions will be discovered
  103. * and updated.
  104. * </p>
  105. *
  106. * @param nodeName the nodes name
  107. * @param nodeId the nodes unique id.
  108. * @param address the nodes transport address
  109. * @param attributes node attributes
  110. * @param roles node roles
  111. * @param version the version of the node.
  112. */
  113. public DiscoveryNode(String nodeName, String nodeId, TransportAddress address, Map<String, String> attributes,
  114. Set<Role> roles, Version version) {
  115. this(nodeName, nodeId, address.getHost(), address.getAddress(), address, attributes, roles, version);
  116. }
  117. /**
  118. * Creates a new {@link DiscoveryNode}.
  119. * <p>
  120. * <b>Note:</b> if the version of the node is unknown {@link Version#minimumCompatibilityVersion()} should be used for the current
  121. * version. it corresponds to the minimum version this elasticsearch version can communicate with. If a higher version is used
  122. * the node might not be able to communicate with the remove node. After initial handshakes node versions will be discovered
  123. * and updated.
  124. * </p>
  125. *
  126. * @param nodeName the nodes name
  127. * @param nodeId the nodes unique id.
  128. * @param hostName the nodes hostname
  129. * @param hostAddress the nodes host address
  130. * @param address the nodes transport address
  131. * @param attributes node attributes
  132. * @param roles node roles
  133. * @param version the version of the node.
  134. */
  135. public DiscoveryNode(String nodeName, String nodeId, String hostName, String hostAddress, TransportAddress address,
  136. Map<String, String> attributes, Set<Role> roles, Version version) {
  137. if (nodeName != null) {
  138. this.nodeName = nodeName.intern();
  139. } else {
  140. this.nodeName = "";
  141. }
  142. this.nodeId = nodeId.intern();
  143. this.hostName = hostName.intern();
  144. this.hostAddress = hostAddress.intern();
  145. this.address = address;
  146. if (version == null) {
  147. this.version = Version.CURRENT;
  148. } else {
  149. this.version = version;
  150. }
  151. this.attributes = Collections.unmodifiableMap(attributes);
  152. //verify that no node roles are being provided as attributes
  153. Predicate<Map<String, String>> predicate = (attrs) -> {
  154. for (Role role : Role.values()) {
  155. assert attrs.containsKey(role.getRoleName()) == false;
  156. }
  157. return true;
  158. };
  159. assert predicate.test(attributes);
  160. Set<Role> rolesSet = EnumSet.noneOf(Role.class);
  161. rolesSet.addAll(roles);
  162. this.roles = Collections.unmodifiableSet(rolesSet);
  163. }
  164. /**
  165. * Creates a new {@link DiscoveryNode} by reading from the stream provided as argument
  166. * @param in the stream
  167. * @throws IOException if there is an error while reading from the stream
  168. */
  169. public DiscoveryNode(StreamInput in) throws IOException {
  170. this.nodeName = in.readString().intern();
  171. this.nodeId = in.readString().intern();
  172. this.hostName = in.readString().intern();
  173. this.hostAddress = in.readString().intern();
  174. this.address = TransportAddressSerializers.addressFromStream(in);
  175. int size = in.readVInt();
  176. this.attributes = new HashMap<>(size);
  177. for (int i = 0; i < size; i++) {
  178. this.attributes.put(in.readString(), in.readString());
  179. }
  180. int rolesSize = in.readVInt();
  181. this.roles = EnumSet.noneOf(Role.class);
  182. for (int i = 0; i < rolesSize; i++) {
  183. int ordinal = in.readVInt();
  184. if (ordinal < 0 || ordinal >= Role.values().length) {
  185. throw new IOException("Unknown Role ordinal [" + ordinal + "]");
  186. }
  187. this.roles.add(Role.values()[ordinal]);
  188. }
  189. this.version = Version.readVersion(in);
  190. }
  191. @Override
  192. public void writeTo(StreamOutput out) throws IOException {
  193. out.writeString(nodeName);
  194. out.writeString(nodeId);
  195. out.writeString(hostName);
  196. out.writeString(hostAddress);
  197. addressToStream(out, address);
  198. out.writeVInt(attributes.size());
  199. for (Map.Entry<String, String> entry : attributes.entrySet()) {
  200. out.writeString(entry.getKey());
  201. out.writeString(entry.getValue());
  202. }
  203. out.writeVInt(roles.size());
  204. for (Role role : roles) {
  205. out.writeVInt(role.ordinal());
  206. }
  207. Version.writeVersion(version, out);
  208. }
  209. /**
  210. * The address that the node can be communicated with.
  211. */
  212. public TransportAddress getAddress() {
  213. return address;
  214. }
  215. /**
  216. * The unique id of the node.
  217. */
  218. public String getId() {
  219. return nodeId;
  220. }
  221. /**
  222. * The name of the node.
  223. */
  224. public String getName() {
  225. return this.nodeName;
  226. }
  227. /**
  228. * The node attributes.
  229. */
  230. public Map<String, String> getAttributes() {
  231. return this.attributes;
  232. }
  233. /**
  234. * Should this node hold data (shards) or not.
  235. */
  236. public boolean isDataNode() {
  237. return roles.contains(Role.DATA);
  238. }
  239. /**
  240. * Can this node become master or not.
  241. */
  242. public boolean isMasterNode() {
  243. return roles.contains(Role.MASTER);
  244. }
  245. /**
  246. * Returns a boolean that tells whether this an ingest node or not
  247. */
  248. public boolean isIngestNode() {
  249. return roles.contains(Role.INGEST);
  250. }
  251. /**
  252. * Returns a set of all the roles that the node fulfills.
  253. * If the node doesn't have any specific role, the set is returned empty, which means that the node is a coordinating only node.
  254. */
  255. public Set<Role> getRoles() {
  256. return roles;
  257. }
  258. public Version getVersion() {
  259. return this.version;
  260. }
  261. public String getHostName() {
  262. return this.hostName;
  263. }
  264. public String getHostAddress() {
  265. return this.hostAddress;
  266. }
  267. @Override
  268. public boolean equals(Object obj) {
  269. if (!(obj instanceof DiscoveryNode)) {
  270. return false;
  271. }
  272. DiscoveryNode other = (DiscoveryNode) obj;
  273. return this.nodeId.equals(other.nodeId);
  274. }
  275. @Override
  276. public int hashCode() {
  277. return nodeId.hashCode();
  278. }
  279. @Override
  280. public String toString() {
  281. StringBuilder sb = new StringBuilder();
  282. if (nodeName.length() > 0) {
  283. sb.append('{').append(nodeName).append('}');
  284. }
  285. if (nodeId != null) {
  286. sb.append('{').append(nodeId).append('}');
  287. }
  288. if (Strings.hasLength(hostName)) {
  289. sb.append('{').append(hostName).append('}');
  290. }
  291. if (address != null) {
  292. sb.append('{').append(address).append('}');
  293. }
  294. if (!attributes.isEmpty()) {
  295. sb.append(attributes);
  296. }
  297. return sb.toString();
  298. }
  299. @Override
  300. public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
  301. builder.startObject(getId());
  302. builder.field("name", getName());
  303. builder.field("transport_address", getAddress().toString());
  304. builder.startObject("attributes");
  305. for (Map.Entry<String, String> entry : attributes.entrySet()) {
  306. builder.field(entry.getKey(), entry.getValue());
  307. }
  308. builder.endObject();
  309. builder.endObject();
  310. return builder;
  311. }
  312. /**
  313. * Enum that holds all the possible roles that that a node can fulfill in a cluster.
  314. * Each role has its name and a corresponding abbreviation used by cat apis.
  315. */
  316. public enum Role {
  317. MASTER("master", "m"),
  318. DATA("data", "d"),
  319. INGEST("ingest", "i");
  320. private final String roleName;
  321. private final String abbreviation;
  322. Role(String roleName, String abbreviation) {
  323. this.roleName = roleName;
  324. this.abbreviation = abbreviation;
  325. }
  326. public String getRoleName() {
  327. return roleName;
  328. }
  329. public String getAbbreviation() {
  330. return abbreviation;
  331. }
  332. }
  333. }