|
| 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.rest.discovery; |
| 21 | + |
| 22 | +import org.apache.http.HttpHost; |
| 23 | +import org.elasticsearch.ESNetty4IntegTestCase; |
| 24 | +import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; |
| 25 | +import org.elasticsearch.client.Client; |
| 26 | +import org.elasticsearch.client.Node; |
| 27 | +import org.elasticsearch.client.Request; |
| 28 | +import org.elasticsearch.client.Response; |
| 29 | +import org.elasticsearch.client.ResponseException; |
| 30 | +import org.elasticsearch.client.RestClient; |
| 31 | +import org.elasticsearch.cluster.coordination.ClusterBootstrapService; |
| 32 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 33 | +import org.elasticsearch.cluster.routing.UnassignedInfo; |
| 34 | +import org.elasticsearch.common.Priority; |
| 35 | +import org.elasticsearch.common.settings.Settings; |
| 36 | +import org.elasticsearch.common.settings.Settings.Builder; |
| 37 | +import org.elasticsearch.common.unit.TimeValue; |
| 38 | +import org.elasticsearch.discovery.zen.ElectMasterService; |
| 39 | +import org.elasticsearch.gateway.GatewayService; |
| 40 | +import org.elasticsearch.http.HttpServerTransport; |
| 41 | +import org.elasticsearch.test.ESIntegTestCase; |
| 42 | +import org.elasticsearch.test.InternalTestCluster; |
| 43 | +import org.elasticsearch.test.discovery.TestZenDiscovery; |
| 44 | +import org.hamcrest.Matchers; |
| 45 | + |
| 46 | +import java.io.IOException; |
| 47 | +import java.util.Collections; |
| 48 | +import java.util.List; |
| 49 | + |
| 50 | +import static org.hamcrest.core.Is.is; |
| 51 | + |
| 52 | +// These tests are here today so they have access to a proper REST client. They cannot be in :server:integTest since the REST client needs a |
| 53 | +// proper transport implementation, and they cannot be REST tests today since they need to restart nodes. When #35599 and friends land we |
| 54 | +// should be able to move these tests to run against a proper cluster instead. TODO do this. |
| 55 | +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, transportClientRatio = 0, autoMinMasterNodes = false) |
| 56 | +public class Zen2RestApiIT extends ESNetty4IntegTestCase { |
| 57 | + |
| 58 | + @Override |
| 59 | + protected Settings nodeSettings(int nodeOrdinal) { |
| 60 | + final Builder builder = Settings.builder().put(super.nodeSettings(nodeOrdinal)) |
| 61 | + .put(TestZenDiscovery.USE_ZEN2.getKey(), true) |
| 62 | + .put(GatewayService.RECOVER_AFTER_MASTER_NODES_SETTING.getKey(), 1) |
| 63 | + .put(ElectMasterService.DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.getKey(), Integer.MAX_VALUE); |
| 64 | + |
| 65 | + if (nodeOrdinal == 0) { |
| 66 | + builder.put(ClusterBootstrapService.INITIAL_MASTER_NODE_COUNT_SETTING.getKey(), 2); |
| 67 | + } |
| 68 | + |
| 69 | + return builder.build(); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + protected boolean addMockHttpTransport() { |
| 74 | + return false; // enable http |
| 75 | + } |
| 76 | + |
| 77 | + public void testRollingRestartOfTwoNodeCluster() throws Exception { |
| 78 | + final List<String> nodes = internalCluster().startNodes(2); |
| 79 | + createIndex("test", |
| 80 | + Settings.builder() |
| 81 | + .put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), TimeValue.ZERO) // assign shards |
| 82 | + .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 2) // causes rebalancing |
| 83 | + .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1) |
| 84 | + .build()); |
| 85 | + ensureGreen("test"); |
| 86 | + |
| 87 | + RestClient restClient = getRestClient(); |
| 88 | + |
| 89 | + internalCluster().rollingRestart(new InternalTestCluster.RestartCallback() { |
| 90 | + @Override |
| 91 | + public void doAfterNodes(int n, Client client) throws IOException { |
| 92 | + ensureGreen("test"); |
| 93 | + Response response = |
| 94 | + restClient.performRequest(new Request("POST", "/_cluster/voting_config_exclusions/" + |
| 95 | + internalCluster().getNodeNames()[n])); |
| 96 | + assertThat(response.getStatusLine().getStatusCode(), is(200)); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public Settings onNodeStopped(String nodeName) throws IOException { |
| 101 | + String viaNode = randomValueOtherThan(nodeName, () -> randomFrom(nodes)); |
| 102 | + |
| 103 | + List<Node> allNodes = restClient.getNodes(); |
| 104 | + try { |
| 105 | + restClient.setNodes( |
| 106 | + Collections.singletonList( |
| 107 | + new Node( |
| 108 | + HttpHost.create( |
| 109 | + internalCluster().getInstance(HttpServerTransport.class, viaNode) |
| 110 | + .boundAddress().publishAddress().toString() |
| 111 | + ) |
| 112 | + ) |
| 113 | + ) |
| 114 | + ); |
| 115 | + Response deleteResponse = restClient.performRequest(new Request("DELETE", "/_cluster/voting_config_exclusions")); |
| 116 | + assertThat(deleteResponse.getStatusLine().getStatusCode(), is(200)); |
| 117 | + |
| 118 | + ClusterHealthResponse clusterHealthResponse = client(viaNode).admin().cluster().prepareHealth() |
| 119 | + .setWaitForEvents(Priority.LANGUID) |
| 120 | + .setWaitForNodes(Integer.toString(1)) |
| 121 | + .setTimeout(TimeValue.timeValueSeconds(30L)) |
| 122 | + .setWaitForYellowStatus() |
| 123 | + .get(); |
| 124 | + assertFalse(nodeName, clusterHealthResponse.isTimedOut()); |
| 125 | + return Settings.EMPTY; |
| 126 | + } finally { |
| 127 | + restClient.setNodes(allNodes); |
| 128 | + } |
| 129 | + } |
| 130 | + }); |
| 131 | + ensureStableCluster(2); |
| 132 | + ensureGreen("test"); |
| 133 | + assertThat(internalCluster().size(), is(2)); |
| 134 | + } |
| 135 | + |
| 136 | + public void testClearVotingTombstonesNotWaitingForRemoval() throws Exception { |
| 137 | + List<String> nodes = internalCluster().startNodes(3); |
| 138 | + RestClient restClient = getRestClient(); |
| 139 | + Response response = restClient.performRequest(new Request("POST", "/_cluster/voting_config_exclusions/" + nodes.get(2))); |
| 140 | + assertThat(response.getStatusLine().getStatusCode(), is(200)); |
| 141 | + assertThat(response.getEntity().getContentLength(), is(0L)); |
| 142 | + Response deleteResponse = restClient.performRequest( |
| 143 | + new Request("DELETE", "/_cluster/voting_config_exclusions/?wait_for_removal=false")); |
| 144 | + assertThat(deleteResponse.getStatusLine().getStatusCode(), is(200)); |
| 145 | + assertThat(deleteResponse.getEntity().getContentLength(), is(0L)); |
| 146 | + } |
| 147 | + |
| 148 | + public void testClearVotingTombstonesWaitingForRemoval() throws Exception { |
| 149 | + List<String> nodes = internalCluster().startNodes(3); |
| 150 | + RestClient restClient = getRestClient(); |
| 151 | + String nodeToWithdraw = nodes.get(randomIntBetween(0, 2)); |
| 152 | + Response response = restClient.performRequest(new Request("POST", "/_cluster/voting_config_exclusions/" + nodeToWithdraw)); |
| 153 | + assertThat(response.getStatusLine().getStatusCode(), is(200)); |
| 154 | + assertThat(response.getEntity().getContentLength(), is(0L)); |
| 155 | + internalCluster().stopRandomNode(InternalTestCluster.nameFilter(nodeToWithdraw)); |
| 156 | + Response deleteResponse = restClient.performRequest(new Request("DELETE", "/_cluster/voting_config_exclusions")); |
| 157 | + assertThat(deleteResponse.getStatusLine().getStatusCode(), is(200)); |
| 158 | + assertThat(deleteResponse.getEntity().getContentLength(), is(0L)); |
| 159 | + } |
| 160 | + |
| 161 | + public void testFailsOnUnknownNode() throws Exception { |
| 162 | + internalCluster().startNodes(3); |
| 163 | + RestClient restClient = getRestClient(); |
| 164 | + try { |
| 165 | + restClient.performRequest(new Request("POST", "/_cluster/voting_config_exclusions/invalid")); |
| 166 | + fail("Invalid node name should throw."); |
| 167 | + } catch (ResponseException e) { |
| 168 | + assertThat(e.getResponse().getStatusLine().getStatusCode(), is(400)); |
| 169 | + assertThat( |
| 170 | + e.getCause().getMessage(), |
| 171 | + Matchers.containsString("add voting config exclusions request for [invalid] matched no master-eligible nodes") |
| 172 | + ); |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments