|
| 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.discovery; |
| 21 | + |
| 22 | +import org.apache.lucene.util.SetOnce; |
| 23 | +import org.elasticsearch.common.component.AbstractLifecycleComponent; |
| 24 | +import org.elasticsearch.common.settings.Settings; |
| 25 | +import org.elasticsearch.common.transport.TransportAddress; |
| 26 | +import org.elasticsearch.common.unit.TimeValue; |
| 27 | +import org.elasticsearch.common.util.concurrent.AbstractRunnable; |
| 28 | +import org.elasticsearch.common.util.concurrent.EsExecutors; |
| 29 | +import org.elasticsearch.discovery.PeerFinder.ConfiguredHostsResolver; |
| 30 | +import org.elasticsearch.discovery.zen.UnicastHostsProvider; |
| 31 | +import org.elasticsearch.discovery.zen.UnicastZenPing; |
| 32 | +import org.elasticsearch.threadpool.ThreadPool; |
| 33 | +import org.elasticsearch.transport.TransportService; |
| 34 | + |
| 35 | +import java.util.List; |
| 36 | +import java.util.concurrent.ExecutorService; |
| 37 | +import java.util.concurrent.ThreadFactory; |
| 38 | +import java.util.concurrent.TimeUnit; |
| 39 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 40 | +import java.util.function.Consumer; |
| 41 | + |
| 42 | +import static org.elasticsearch.discovery.zen.UnicastZenPing.DISCOVERY_ZEN_PING_UNICAST_CONCURRENT_CONNECTS_SETTING; |
| 43 | + |
| 44 | +public class UnicastConfiguredHostsResolver extends AbstractLifecycleComponent implements ConfiguredHostsResolver { |
| 45 | + |
| 46 | + private final AtomicBoolean resolveInProgress = new AtomicBoolean(); |
| 47 | + private final TransportService transportService; |
| 48 | + private final UnicastHostsProvider hostsProvider; |
| 49 | + private final SetOnce<ExecutorService> executorService = new SetOnce<>(); |
| 50 | + private final TimeValue resolveTimeout; |
| 51 | + |
| 52 | + public UnicastConfiguredHostsResolver(Settings settings, TransportService transportService, UnicastHostsProvider hostsProvider) { |
| 53 | + super(settings); |
| 54 | + this.transportService = transportService; |
| 55 | + this.hostsProvider = hostsProvider; |
| 56 | + resolveTimeout = UnicastZenPing.DISCOVERY_ZEN_PING_UNICAST_HOSTS_RESOLVE_TIMEOUT.get(settings); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected void doStart() { |
| 61 | + final int concurrentConnects = DISCOVERY_ZEN_PING_UNICAST_CONCURRENT_CONNECTS_SETTING.get(settings); |
| 62 | + logger.debug("using concurrent_connects [{}], resolve_timeout [{}]", concurrentConnects, resolveTimeout); |
| 63 | + final ThreadFactory threadFactory = EsExecutors.daemonThreadFactory(settings, "[unicast_configured_hosts_resolver]"); |
| 64 | + executorService.set(EsExecutors.newScaling(nodeName() + "/" + "unicast_configured_hosts_resolver", |
| 65 | + 0, concurrentConnects, 60, TimeUnit.SECONDS, threadFactory, transportService.getThreadPool().getThreadContext())); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + protected void doStop() { |
| 70 | + ThreadPool.terminate(executorService.get(), 10, TimeUnit.SECONDS); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + protected void doClose() { |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void resolveConfiguredHosts(Consumer<List<TransportAddress>> consumer) { |
| 79 | + if (lifecycle.started() == false) { |
| 80 | + logger.debug("resolveConfiguredHosts: lifecycle is {}, not proceeding", lifecycle); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + if (resolveInProgress.compareAndSet(false, true)) { |
| 85 | + transportService.getThreadPool().generic().execute(new AbstractRunnable() { |
| 86 | + @Override |
| 87 | + public void onFailure(Exception e) { |
| 88 | + logger.debug("failure when resolving unicast hosts list", e); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + protected void doRun() { |
| 93 | + if (lifecycle.started() == false) { |
| 94 | + logger.debug("resolveConfiguredHosts.doRun: lifecycle is {}, not proceeding", lifecycle); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + List<TransportAddress> providedAddresses |
| 99 | + = hostsProvider.buildDynamicHosts((hosts, limitPortCounts) |
| 100 | + -> UnicastZenPing.resolveHostsLists(executorService.get(), logger, hosts, limitPortCounts, |
| 101 | + transportService, resolveTimeout)); |
| 102 | + |
| 103 | + consumer.accept(providedAddresses); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void onAfter() { |
| 108 | + resolveInProgress.set(false); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public String toString() { |
| 113 | + return "UnicastConfiguredHostsResolver resolving unicast hosts list"; |
| 114 | + } |
| 115 | + }); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments