1919
2020package org .elasticsearch .common .network ;
2121
22- import org .elasticsearch .common .Strings ;
23- import org .elasticsearch .common .component .AbstractComponent ;
2422import org .elasticsearch .common .settings .Setting ;
2523import org .elasticsearch .common .settings .Setting .Property ;
26- import org .elasticsearch .common .settings .Settings ;
2724import org .elasticsearch .common .unit .ByteSizeValue ;
2825import org .elasticsearch .common .unit .TimeValue ;
2926
3027import java .io .IOException ;
3128import java .net .InetAddress ;
3229import java .util .ArrayList ;
3330import java .util .Arrays ;
31+ import java .util .Collections ;
3432import java .util .HashSet ;
3533import java .util .List ;
34+ import java .util .Objects ;
3635import java .util .concurrent .TimeUnit ;
3736import java .util .function .Function ;
3837
39- public class NetworkService extends AbstractComponent {
38+ public final class NetworkService {
4039
4140 /** By default, we bind to loopback interfaces */
4241 public static final String DEFAULT_NETWORK_HOST = "_local_" ;
43-
4442 public static final Setting <List <String >> GLOBAL_NETWORK_HOST_SETTING =
45- Setting .listSetting ("network.host" , Arrays . asList ( DEFAULT_NETWORK_HOST ), Function .identity (), Property .NodeScope );
43+ Setting .listSetting ("network.host" , Collections . emptyList ( ), Function .identity (), Property .NodeScope );
4644 public static final Setting <List <String >> GLOBAL_NETWORK_BINDHOST_SETTING =
4745 Setting .listSetting ("network.bind_host" , GLOBAL_NETWORK_HOST_SETTING , Function .identity (), Property .NodeScope );
4846 public static final Setting <List <String >> GLOBAL_NETWORK_PUBLISHHOST_SETTING =
4947 Setting .listSetting ("network.publish_host" , GLOBAL_NETWORK_HOST_SETTING , Function .identity (), Property .NodeScope );
5048 public static final Setting <Boolean > NETWORK_SERVER = Setting .boolSetting ("network.server" , true , Property .NodeScope );
5149
52- public static final class TcpSettings {
53- public static final Setting <Boolean > TCP_NO_DELAY =
54- Setting .boolSetting ("network.tcp.no_delay" , true , Property .NodeScope );
55- public static final Setting <Boolean > TCP_KEEP_ALIVE =
56- Setting .boolSetting ("network.tcp.keep_alive" , true , Property .NodeScope );
57- public static final Setting <Boolean > TCP_REUSE_ADDRESS =
58- Setting .boolSetting ("network.tcp.reuse_address" , NetworkUtils .defaultReuseAddress (), Property .NodeScope );
59- public static final Setting <ByteSizeValue > TCP_SEND_BUFFER_SIZE =
60- Setting .byteSizeSetting ("network.tcp.send_buffer_size" , new ByteSizeValue (-1 ), Property .NodeScope );
61- public static final Setting <ByteSizeValue > TCP_RECEIVE_BUFFER_SIZE =
62- Setting .byteSizeSetting ("network.tcp.receive_buffer_size" , new ByteSizeValue (-1 ), Property .NodeScope );
63- public static final Setting <TimeValue > TCP_CONNECT_TIMEOUT =
64- Setting .timeSetting ("network.tcp.connect_timeout" , new TimeValue (30 , TimeUnit .SECONDS ), Property .NodeScope );
65- }
50+ public static final Setting <Boolean > TCP_NO_DELAY =
51+ Setting .boolSetting ("network.tcp.no_delay" , true , Property .NodeScope );
52+ public static final Setting <Boolean > TCP_KEEP_ALIVE =
53+ Setting .boolSetting ("network.tcp.keep_alive" , true , Property .NodeScope );
54+ public static final Setting <Boolean > TCP_REUSE_ADDRESS =
55+ Setting .boolSetting ("network.tcp.reuse_address" , NetworkUtils .defaultReuseAddress (), Property .NodeScope );
56+ public static final Setting <ByteSizeValue > TCP_SEND_BUFFER_SIZE =
57+ Setting .byteSizeSetting ("network.tcp.send_buffer_size" , new ByteSizeValue (-1 ), Property .NodeScope );
58+ public static final Setting <ByteSizeValue > TCP_RECEIVE_BUFFER_SIZE =
59+ Setting .byteSizeSetting ("network.tcp.receive_buffer_size" , new ByteSizeValue (-1 ), Property .NodeScope );
60+ public static final Setting <TimeValue > TCP_CONNECT_TIMEOUT =
61+ Setting .timeSetting ("network.tcp.connect_timeout" , new TimeValue (30 , TimeUnit .SECONDS ), Property .NodeScope );
6662
6763 /**
6864 * A custom name resolver can support custom lookup keys (my_net_key:ipv4) and also change
@@ -82,39 +78,29 @@ public interface CustomNameResolver {
8278
8379 private final List <CustomNameResolver > customNameResolvers ;
8480
85- public NetworkService (Settings settings , List <CustomNameResolver > customNameResolvers ) {
86- super (settings );
87- this .customNameResolvers = customNameResolvers ;
81+ public NetworkService (List <CustomNameResolver > customNameResolvers ) {
82+ this .customNameResolvers = Objects .requireNonNull (customNameResolvers , "customNameResolvers must be non null" );
8883 }
8984
9085 /**
9186 * Resolves {@code bindHosts} to a list of internet addresses. The list will
9287 * not contain duplicate addresses.
9388 *
9489 * @param bindHosts list of hosts to bind to. this may contain special pseudo-hostnames
95- * such as _local_ (see the documentation). if it is null, it will be populated
96- * based on global default settings.
90+ * such as _local_ (see the documentation). if it is null, it will fall back to _local_
91+ *
9792 * @return unique set of internet addresses
9893 */
9994 public InetAddress [] resolveBindHostAddresses (String bindHosts []) throws IOException {
100- // first check settings
10195 if (bindHosts == null || bindHosts .length == 0 ) {
102- if (GLOBAL_NETWORK_BINDHOST_SETTING .exists (settings ) || GLOBAL_NETWORK_HOST_SETTING .exists (settings )) {
103- // if we have settings use them (we have a fallback to GLOBAL_NETWORK_HOST_SETTING inline
104- bindHosts = GLOBAL_NETWORK_BINDHOST_SETTING .get (settings ).toArray (Strings .EMPTY_ARRAY );
105- } else {
106- // next check any registered custom resolvers if any
107- if (customNameResolvers != null ) {
108- for (CustomNameResolver customNameResolver : customNameResolvers ) {
109- InetAddress addresses [] = customNameResolver .resolveDefault ();
110- if (addresses != null ) {
111- return addresses ;
112- }
113- }
96+ for (CustomNameResolver customNameResolver : customNameResolvers ) {
97+ InetAddress addresses [] = customNameResolver .resolveDefault ();
98+ if (addresses != null ) {
99+ return addresses ;
114100 }
115- // we know it's not here. get the defaults
116- bindHosts = GLOBAL_NETWORK_BINDHOST_SETTING .get (settings ).toArray (Strings .EMPTY_ARRAY );
117101 }
102+ // we know it's not here. get the defaults
103+ bindHosts = new String [] {"_local_" };
118104 }
119105
120106 InetAddress addresses [] = resolveInetAddresses (bindHosts );
@@ -140,29 +126,20 @@ public InetAddress[] resolveBindHostAddresses(String bindHosts[]) throws IOExcep
140126 * If {@code publishHosts} resolves to more than one address, <b>then one is selected with magic</b>
141127 *
142128 * @param publishHosts list of hosts to publish as. this may contain special pseudo-hostnames
143- * such as _local_ (see the documentation). if it is null, it will be populated
144- * based on global default settings.
129+ * such as _local_ (see the documentation). if it is null, it will fall back to _local_
145130 * @return single internet address
146131 */
147132 // TODO: needs to be InetAddress[]
148133 public InetAddress resolvePublishHostAddresses (String publishHosts []) throws IOException {
149134 if (publishHosts == null || publishHosts .length == 0 ) {
150- if (GLOBAL_NETWORK_PUBLISHHOST_SETTING .exists (settings ) || GLOBAL_NETWORK_HOST_SETTING .exists (settings )) {
151- // if we have settings use them (we have a fallback to GLOBAL_NETWORK_HOST_SETTING inline
152- publishHosts = GLOBAL_NETWORK_PUBLISHHOST_SETTING .get (settings ).toArray (Strings .EMPTY_ARRAY );
153- } else {
154- // next check any registered custom resolvers if any
155- if (customNameResolvers != null ) {
156- for (CustomNameResolver customNameResolver : customNameResolvers ) {
157- InetAddress addresses [] = customNameResolver .resolveDefault ();
158- if (addresses != null ) {
159- return addresses [0 ];
160- }
161- }
135+ for (CustomNameResolver customNameResolver : customNameResolvers ) {
136+ InetAddress addresses [] = customNameResolver .resolveDefault ();
137+ if (addresses != null ) {
138+ return addresses [0 ];
162139 }
163- // we know it's not here. get the defaults
164- publishHosts = GLOBAL_NETWORK_PUBLISHHOST_SETTING .get (settings ).toArray (Strings .EMPTY_ARRAY );
165140 }
141+ // we know it's not here. get the defaults
142+ publishHosts = new String [] {DEFAULT_NETWORK_HOST };
166143 }
167144
168145 InetAddress addresses [] = resolveInetAddresses (publishHosts );
0 commit comments