|
| 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.zen; |
| 21 | + |
| 22 | +import org.elasticsearch.action.admin.cluster.node.info.NodesInfoRequest; |
| 23 | +import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse; |
| 24 | +import org.elasticsearch.common.settings.Settings; |
| 25 | +import org.elasticsearch.common.settings.Settings.Builder; |
| 26 | +import org.elasticsearch.common.transport.TransportAddress; |
| 27 | +import org.elasticsearch.test.ESIntegTestCase; |
| 28 | +import org.junit.Before; |
| 29 | + |
| 30 | +import java.util.function.Consumer; |
| 31 | + |
| 32 | +import static org.elasticsearch.discovery.DiscoveryModule.DISCOVERY_HOSTS_PROVIDER_SETTING; |
| 33 | +import static org.elasticsearch.discovery.zen.SettingsBasedHostsProvider.DISCOVERY_ZEN_PING_UNICAST_HOSTS_SETTING; |
| 34 | +import static org.elasticsearch.discovery.zen.SettingsBasedHostsProvider.LIMIT_LOCAL_PORTS_COUNT; |
| 35 | +import static org.elasticsearch.transport.TcpTransport.PORT; |
| 36 | + |
| 37 | +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, numClientNodes = 0) |
| 38 | +public class SettingsBasedHostProviderIT extends ESIntegTestCase { |
| 39 | + |
| 40 | + private Consumer<Builder> configureDiscovery; |
| 41 | + |
| 42 | + @Before |
| 43 | + public void setDefaultDiscoveryConfiguration() { |
| 44 | + configureDiscovery = b -> { |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + protected Settings nodeSettings(int nodeOrdinal) { |
| 50 | + Settings.Builder builder = Settings.builder().put(super.nodeSettings(nodeOrdinal)); |
| 51 | + |
| 52 | + // super.nodeSettings enables file-based discovery, but here we disable it again so we can test the static list: |
| 53 | + if (randomBoolean()) { |
| 54 | + builder.putList(DISCOVERY_HOSTS_PROVIDER_SETTING.getKey()); |
| 55 | + } else { |
| 56 | + builder.remove(DISCOVERY_HOSTS_PROVIDER_SETTING.getKey()); |
| 57 | + } |
| 58 | + |
| 59 | + builder.remove(DISCOVERY_ZEN_PING_UNICAST_HOSTS_SETTING.getKey()); |
| 60 | + |
| 61 | + configureDiscovery.accept(builder); |
| 62 | + return builder.build(); |
| 63 | + } |
| 64 | + |
| 65 | + public void testClusterFormsWithSingleSeedHostInSettings() { |
| 66 | + final String seedNodeName = internalCluster().startNode(); |
| 67 | + final NodesInfoResponse nodesInfoResponse |
| 68 | + = client(seedNodeName).admin().cluster().nodesInfo(new NodesInfoRequest("_local")).actionGet(); |
| 69 | + final String seedNodeAddress = nodesInfoResponse.getNodes().get(0).getTransport().getAddress().publishAddress().toString(); |
| 70 | + configureDiscovery = b -> b.putList(DISCOVERY_ZEN_PING_UNICAST_HOSTS_SETTING.getKey(), seedNodeAddress); |
| 71 | + logger.info("--> using seed node address {}", seedNodeAddress); |
| 72 | + |
| 73 | + int extraNodes = randomIntBetween(1, 5); |
| 74 | + internalCluster().startNodes(extraNodes); |
| 75 | + |
| 76 | + ensureStableCluster(extraNodes + 1); |
| 77 | + } |
| 78 | + |
| 79 | + public void testClusterFormsByScanningPorts() { |
| 80 | + final String seedNodeName = internalCluster().startNode(); |
| 81 | + final NodesInfoResponse nodesInfoResponse |
| 82 | + = client(seedNodeName).admin().cluster().nodesInfo(new NodesInfoRequest("_local")).actionGet(); |
| 83 | + final int seedNodePort = nodesInfoResponse.getNodes().get(0).getTransport().getAddress().publishAddress().getPort(); |
| 84 | + final int minPort = randomIntBetween(seedNodePort - LIMIT_LOCAL_PORTS_COUNT + 1, seedNodePort - 1); |
| 85 | + final String portSpec = minPort + "-" + seedNodePort; |
| 86 | + |
| 87 | + logger.info("--> using port specification [{}]", portSpec); |
| 88 | + |
| 89 | + configureDiscovery = b -> b.put(PORT.getKey(), portSpec); |
| 90 | + |
| 91 | + internalCluster().startNode(); |
| 92 | + ensureStableCluster(2); |
| 93 | + } |
| 94 | +} |
0 commit comments