|
| 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.action.admin.cluster.state; |
| 20 | + |
| 21 | +import org.elasticsearch.cluster.ClusterState; |
| 22 | +import org.elasticsearch.cluster.coordination.ClusterBootstrapService; |
| 23 | +import org.elasticsearch.cluster.metadata.MetaData; |
| 24 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 25 | +import org.elasticsearch.cluster.node.DiscoveryNodes; |
| 26 | +import org.elasticsearch.cluster.service.ClusterService; |
| 27 | +import org.elasticsearch.common.settings.Settings; |
| 28 | +import org.elasticsearch.common.unit.TimeValue; |
| 29 | +import org.elasticsearch.discovery.MasterNotDiscoveredException; |
| 30 | +import org.elasticsearch.plugins.Plugin; |
| 31 | +import org.elasticsearch.test.ESIntegTestCase; |
| 32 | +import org.elasticsearch.test.transport.MockTransportService; |
| 33 | +import org.elasticsearch.transport.TransportService; |
| 34 | + |
| 35 | +import java.util.Collection; |
| 36 | +import java.util.Collections; |
| 37 | +import java.util.List; |
| 38 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 39 | +import java.util.stream.Collectors; |
| 40 | +import java.util.stream.StreamSupport; |
| 41 | + |
| 42 | +import static org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider.CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING; |
| 43 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 44 | +import static org.hamcrest.Matchers.equalTo; |
| 45 | +import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 46 | +import static org.hamcrest.Matchers.hasSize; |
| 47 | +import static org.hamcrest.Matchers.not; |
| 48 | + |
| 49 | +@ESIntegTestCase.ClusterScope(numDataNodes = 0, scope = ESIntegTestCase.Scope.TEST, transportClientRatio = 0) |
| 50 | +public class TransportClusterStateActionDisruptionIT extends ESIntegTestCase { |
| 51 | + |
| 52 | + @Override |
| 53 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 54 | + return Collections.singletonList(MockTransportService.TestPlugin.class); |
| 55 | + } |
| 56 | + |
| 57 | + public void testNonLocalRequestAlwaysFindsMaster() throws Exception { |
| 58 | + runRepeatedlyWhileChangingMaster(() -> { |
| 59 | + final ClusterStateRequestBuilder clusterStateRequestBuilder = client().admin().cluster().prepareState() |
| 60 | + .clear().setNodes(true).setMasterNodeTimeout("100ms"); |
| 61 | + final ClusterStateResponse clusterStateResponse; |
| 62 | + try { |
| 63 | + clusterStateResponse = clusterStateRequestBuilder.get(); |
| 64 | + } catch (MasterNotDiscoveredException e) { |
| 65 | + return; // ok, we hit the disconnected node |
| 66 | + } |
| 67 | + assertNotNull("should always contain a master node", clusterStateResponse.getState().nodes().getMasterNodeId()); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + public void testLocalRequestAlwaysSucceeds() throws Exception { |
| 72 | + runRepeatedlyWhileChangingMaster(() -> { |
| 73 | + final String node = randomFrom(internalCluster().getNodeNames()); |
| 74 | + final DiscoveryNodes discoveryNodes = client(node).admin().cluster().prepareState() |
| 75 | + .clear().setLocal(true).setNodes(true).setMasterNodeTimeout("100ms").get().getState().nodes(); |
| 76 | + for (DiscoveryNode discoveryNode : discoveryNodes) { |
| 77 | + if (discoveryNode.getName().equals(node)) { |
| 78 | + return; |
| 79 | + } |
| 80 | + } |
| 81 | + fail("nodes did not contain [" + node + "]: " + discoveryNodes); |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + public void testNonLocalRequestAlwaysFindsMasterAndWaitsForMetadata() throws Exception { |
| 86 | + runRepeatedlyWhileChangingMaster(() -> { |
| 87 | + final String node = randomFrom(internalCluster().getNodeNames()); |
| 88 | + final long metadataVersion |
| 89 | + = internalCluster().getInstance(ClusterService.class, node).getClusterApplierService().state().metaData().version(); |
| 90 | + final long waitForMetaDataVersion = randomLongBetween(Math.max(1, metadataVersion - 3), metadataVersion + 5); |
| 91 | + final ClusterStateRequestBuilder clusterStateRequestBuilder = client(node).admin().cluster().prepareState() |
| 92 | + .clear().setNodes(true).setMetaData(true) |
| 93 | + .setMasterNodeTimeout(TimeValue.timeValueMillis(100)).setWaitForTimeOut(TimeValue.timeValueMillis(100)) |
| 94 | + .setWaitForMetaDataVersion(waitForMetaDataVersion); |
| 95 | + final ClusterStateResponse clusterStateResponse; |
| 96 | + try { |
| 97 | + clusterStateResponse = clusterStateRequestBuilder.get(); |
| 98 | + } catch (MasterNotDiscoveredException e) { |
| 99 | + return; // ok, we hit the disconnected node |
| 100 | + } |
| 101 | + if (clusterStateResponse.isWaitForTimedOut() == false) { |
| 102 | + final ClusterState state = clusterStateResponse.getState(); |
| 103 | + assertNotNull("should always contain a master node", state.nodes().getMasterNodeId()); |
| 104 | + assertThat("waited for metadata version", state.metaData().version(), greaterThanOrEqualTo(waitForMetaDataVersion)); |
| 105 | + } |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + public void testLocalRequestWaitsForMetadata() throws Exception { |
| 110 | + runRepeatedlyWhileChangingMaster(() -> { |
| 111 | + final String node = randomFrom(internalCluster().getNodeNames()); |
| 112 | + final long metadataVersion |
| 113 | + = internalCluster().getInstance(ClusterService.class, node).getClusterApplierService().state().metaData().version(); |
| 114 | + final long waitForMetaDataVersion = randomLongBetween(Math.max(1, metadataVersion - 3), metadataVersion + 5); |
| 115 | + final ClusterStateResponse clusterStateResponse = client(node).admin().cluster() |
| 116 | + .prepareState().clear().setLocal(true).setMetaData(true).setWaitForMetaDataVersion(waitForMetaDataVersion) |
| 117 | + .setMasterNodeTimeout(TimeValue.timeValueMillis(100)).setWaitForTimeOut(TimeValue.timeValueMillis(100)) |
| 118 | + .get(); |
| 119 | + if (clusterStateResponse.isWaitForTimedOut() == false) { |
| 120 | + final MetaData metaData = clusterStateResponse.getState().metaData(); |
| 121 | + assertThat("waited for metadata version " + waitForMetaDataVersion + " with node " + node, |
| 122 | + metaData.version(), greaterThanOrEqualTo(waitForMetaDataVersion)); |
| 123 | + } |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + public void runRepeatedlyWhileChangingMaster(Runnable runnable) throws Exception { |
| 128 | + internalCluster().startNodes(3); |
| 129 | + |
| 130 | + assertBusy(() -> assertThat(client().admin().cluster().prepareState().clear().setMetaData(true) |
| 131 | + .get().getState().getLastCommittedConfiguration().getNodeIds().stream() |
| 132 | + .filter(n -> ClusterBootstrapService.isBootstrapPlaceholder(n) == false).collect(Collectors.toSet()), hasSize(3))); |
| 133 | + |
| 134 | + final String masterName = internalCluster().getMasterName(); |
| 135 | + |
| 136 | + final AtomicBoolean shutdown = new AtomicBoolean(); |
| 137 | + final Thread assertingThread = new Thread(() -> { |
| 138 | + while (shutdown.get() == false) { |
| 139 | + runnable.run(); |
| 140 | + } |
| 141 | + }, "asserting thread"); |
| 142 | + |
| 143 | + final Thread updatingThread = new Thread(() -> { |
| 144 | + String value = "none"; |
| 145 | + while (shutdown.get() == false) { |
| 146 | + value = "none".equals(value) ? "all" : "none"; |
| 147 | + final String nonMasterNode = randomValueOtherThan(masterName, () -> randomFrom(internalCluster().getNodeNames())); |
| 148 | + assertAcked(client(nonMasterNode).admin().cluster().prepareUpdateSettings().setPersistentSettings( |
| 149 | + Settings.builder().put(CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), value))); |
| 150 | + } |
| 151 | + }, "updating thread"); |
| 152 | + |
| 153 | + final List<MockTransportService> mockTransportServices |
| 154 | + = StreamSupport.stream(internalCluster().getInstances(TransportService.class).spliterator(), false) |
| 155 | + .map(ts -> (MockTransportService) ts).collect(Collectors.toList()); |
| 156 | + |
| 157 | + assertingThread.start(); |
| 158 | + updatingThread.start(); |
| 159 | + |
| 160 | + final MockTransportService masterTransportService |
| 161 | + = (MockTransportService) internalCluster().getInstance(TransportService.class, masterName); |
| 162 | + |
| 163 | + for (MockTransportService mockTransportService : mockTransportServices) { |
| 164 | + if (masterTransportService != mockTransportService) { |
| 165 | + masterTransportService.addFailToSendNoConnectRule(mockTransportService); |
| 166 | + mockTransportService.addFailToSendNoConnectRule(masterTransportService); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + assertBusy(() -> { |
| 171 | + final String nonMasterNode = randomValueOtherThan(masterName, () -> randomFrom(internalCluster().getNodeNames())); |
| 172 | + final String claimedMasterName = internalCluster().getMasterName(nonMasterNode); |
| 173 | + assertThat(claimedMasterName, not(equalTo(masterName))); |
| 174 | + }); |
| 175 | + |
| 176 | + shutdown.set(true); |
| 177 | + assertingThread.join(); |
| 178 | + updatingThread.join(); |
| 179 | + internalCluster().close(); |
| 180 | + } |
| 181 | + |
| 182 | +} |
0 commit comments