|
| 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.client; |
| 21 | + |
| 22 | +import static java.util.Collections.unmodifiableSet; |
| 23 | + |
| 24 | +import java.util.HashSet; |
| 25 | +import java.util.Objects; |
| 26 | +import java.util.Set; |
| 27 | + |
| 28 | +import org.apache.http.HttpHost; |
| 29 | + |
| 30 | +/** |
| 31 | + * Metadata about an {@link HttpHost} running Elasticsearch. |
| 32 | + */ |
| 33 | +public class Node { |
| 34 | + /** |
| 35 | + * Address that this host claims is its primary contact point. |
| 36 | + */ |
| 37 | + private final HttpHost host; |
| 38 | + /** |
| 39 | + * Addresses on which the host is listening. These are useful to have |
| 40 | + * around because they allow you to find a host based on any address it |
| 41 | + * is listening on. |
| 42 | + */ |
| 43 | + private final Set<HttpHost> boundHosts; |
| 44 | + /** |
| 45 | + * Name of the node as configured by the {@code node.name} attribute. |
| 46 | + */ |
| 47 | + private final String name; |
| 48 | + /** |
| 49 | + * Version of Elasticsearch that the node is running or {@code null} |
| 50 | + * if we don't know the version. |
| 51 | + */ |
| 52 | + private final String version; |
| 53 | + /** |
| 54 | + * Roles that the Elasticsearch process on the host has or {@code null} |
| 55 | + * if we don't know what roles the node has. |
| 56 | + */ |
| 57 | + private final Roles roles; |
| 58 | + |
| 59 | + /** |
| 60 | + * Create a {@linkplain Node} with metadata. All parameters except |
| 61 | + * {@code host} are nullable and implementations of {@link NodeSelector} |
| 62 | + * need to decide what to do in their absence. |
| 63 | + */ |
| 64 | + public Node(HttpHost host, Set<HttpHost> boundHosts, String name, String version, Roles roles) { |
| 65 | + if (host == null) { |
| 66 | + throw new IllegalArgumentException("host cannot be null"); |
| 67 | + } |
| 68 | + this.host = host; |
| 69 | + this.boundHosts = boundHosts; |
| 70 | + this.name = name; |
| 71 | + this.version = version; |
| 72 | + this.roles = roles; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Create a {@linkplain Node} without any metadata. |
| 77 | + */ |
| 78 | + public Node(HttpHost host) { |
| 79 | + this(host, null, null, null, null); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Make a copy of this {@link Node} but replacing its |
| 84 | + * {@link #getHost() host}. Use this when the sniffing implementation |
| 85 | + * returns a {@link #getHost() host} that is not useful to the client. |
| 86 | + */ |
| 87 | + public Node withHost(HttpHost host) { |
| 88 | + /* |
| 89 | + * If the new host isn't in the bound hosts list we add it so the |
| 90 | + * result looks sane. |
| 91 | + */ |
| 92 | + Set<HttpHost> boundHosts = this.boundHosts; |
| 93 | + if (false == boundHosts.contains(host)) { |
| 94 | + boundHosts = new HashSet<>(boundHosts); |
| 95 | + boundHosts.add(host); |
| 96 | + boundHosts = unmodifiableSet(boundHosts); |
| 97 | + } |
| 98 | + return new Node(host, boundHosts, name, version, roles); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Contact information for the host. |
| 103 | + */ |
| 104 | + public HttpHost getHost() { |
| 105 | + return host; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Addresses on which the host is listening. These are useful to have |
| 110 | + * around because they allow you to find a host based on any address it |
| 111 | + * is listening on. |
| 112 | + */ |
| 113 | + public Set<HttpHost> getBoundHosts() { |
| 114 | + return boundHosts; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @return the name |
| 119 | + */ |
| 120 | + public String getName() { |
| 121 | + return name; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Version of Elasticsearch that the node is running or {@code null} |
| 126 | + * if we don't know the version. |
| 127 | + */ |
| 128 | + public String getVersion() { |
| 129 | + return version; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Roles that the Elasticsearch process on the host has or {@code null} |
| 134 | + * if we don't know what roles the node has. |
| 135 | + */ |
| 136 | + public Roles getRoles() { |
| 137 | + return roles; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public String toString() { |
| 142 | + StringBuilder b = new StringBuilder(); |
| 143 | + b.append("[host=").append(host); |
| 144 | + if (boundHosts != null) { |
| 145 | + b.append(", bound=").append(boundHosts); |
| 146 | + } |
| 147 | + if (name != null) { |
| 148 | + b.append(", name=").append(name); |
| 149 | + } |
| 150 | + if (version != null) { |
| 151 | + b.append(", version=").append(version); |
| 152 | + } |
| 153 | + if (roles != null) { |
| 154 | + b.append(", roles=").append(roles); |
| 155 | + } |
| 156 | + return b.append(']').toString(); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public boolean equals(Object obj) { |
| 161 | + if (obj == null || obj.getClass() != getClass()) { |
| 162 | + return false; |
| 163 | + } |
| 164 | + Node other = (Node) obj; |
| 165 | + return host.equals(other.host) |
| 166 | + && Objects.equals(boundHosts, other.boundHosts) |
| 167 | + && Objects.equals(version, other.version) |
| 168 | + && Objects.equals(name, other.name) |
| 169 | + && Objects.equals(roles, other.roles); |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public int hashCode() { |
| 174 | + return Objects.hash(host, boundHosts, name, version, roles); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Role information about an Elasticsearch process. |
| 179 | + */ |
| 180 | + public static final class Roles { |
| 181 | + private final boolean masterEligible; |
| 182 | + private final boolean data; |
| 183 | + private final boolean ingest; |
| 184 | + |
| 185 | + public Roles(boolean masterEligible, boolean data, boolean ingest) { |
| 186 | + this.masterEligible = masterEligible; |
| 187 | + this.data = data; |
| 188 | + this.ingest = ingest; |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * The node <strong>could</strong> be elected master. |
| 193 | + */ |
| 194 | + public boolean isMasterEligible() { |
| 195 | + return masterEligible; |
| 196 | + } |
| 197 | + /** |
| 198 | + * The node stores data. |
| 199 | + */ |
| 200 | + public boolean isData() { |
| 201 | + return data; |
| 202 | + } |
| 203 | + /** |
| 204 | + * The node runs ingest pipelines. |
| 205 | + */ |
| 206 | + public boolean isIngest() { |
| 207 | + return ingest; |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + public String toString() { |
| 212 | + StringBuilder result = new StringBuilder(3); |
| 213 | + if (masterEligible) result.append('m'); |
| 214 | + if (data) result.append('d'); |
| 215 | + if (ingest) result.append('i'); |
| 216 | + return result.toString(); |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + public boolean equals(Object obj) { |
| 221 | + if (obj == null || obj.getClass() != getClass()) { |
| 222 | + return false; |
| 223 | + } |
| 224 | + Roles other = (Roles) obj; |
| 225 | + return masterEligible == other.masterEligible |
| 226 | + && data == other.data |
| 227 | + && ingest == other.ingest; |
| 228 | + } |
| 229 | + |
| 230 | + @Override |
| 231 | + public int hashCode() { |
| 232 | + return Objects.hash(masterEligible, data, ingest); |
| 233 | + } |
| 234 | + } |
| 235 | +} |
0 commit comments