|
| 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.gateway; |
| 21 | + |
| 22 | +import org.elasticsearch.action.admin.indices.flush.SyncedFlushResponse; |
| 23 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 24 | +import org.elasticsearch.cluster.routing.UnassignedInfo; |
| 25 | +import org.elasticsearch.common.Priority; |
| 26 | +import org.elasticsearch.common.settings.Settings; |
| 27 | +import org.elasticsearch.index.IndexService; |
| 28 | +import org.elasticsearch.index.IndexSettings; |
| 29 | +import org.elasticsearch.indices.recovery.PeerRecoveryTargetService; |
| 30 | +import org.elasticsearch.plugins.Plugin; |
| 31 | +import org.elasticsearch.test.ESIntegTestCase; |
| 32 | +import org.elasticsearch.test.InternalSettingsPlugin; |
| 33 | +import org.elasticsearch.test.InternalTestCluster; |
| 34 | +import org.elasticsearch.test.transport.MockTransportService; |
| 35 | +import org.elasticsearch.transport.TransportService; |
| 36 | + |
| 37 | +import java.util.Arrays; |
| 38 | +import java.util.Collection; |
| 39 | +import java.util.concurrent.CountDownLatch; |
| 40 | +import java.util.stream.Collectors; |
| 41 | +import java.util.stream.IntStream; |
| 42 | + |
| 43 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 44 | +import static org.hamcrest.Matchers.equalTo; |
| 45 | +import static org.hamcrest.Matchers.hasItem; |
| 46 | + |
| 47 | +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0) |
| 48 | +public class ReplicaShardAllocatorIT extends ESIntegTestCase { |
| 49 | + |
| 50 | + @Override |
| 51 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 52 | + return Arrays.asList(MockTransportService.TestPlugin.class, InternalSettingsPlugin.class); |
| 53 | + } |
| 54 | + |
| 55 | + public void testRecentPrimaryInformation() throws Exception { |
| 56 | + String indexName = "test"; |
| 57 | + String nodeWithPrimary = internalCluster().startNode(); |
| 58 | + assertAcked( |
| 59 | + client().admin().indices().prepareCreate(indexName) |
| 60 | + .setSettings(Settings.builder() |
| 61 | + .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) |
| 62 | + .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1) |
| 63 | + .put(IndexSettings.FILE_BASED_RECOVERY_THRESHOLD_SETTING.getKey(), 1.0f) |
| 64 | + .put(IndexService.GLOBAL_CHECKPOINT_SYNC_INTERVAL_SETTING.getKey(), "100ms") |
| 65 | + .put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), "1ms"))); |
| 66 | + String nodeWithReplica = internalCluster().startDataOnlyNode(); |
| 67 | + Settings nodeWithReplicaSettings = internalCluster().dataPathSettings(nodeWithReplica); |
| 68 | + ensureGreen(indexName); |
| 69 | + indexRandom(randomBoolean(), randomBoolean(), randomBoolean(), IntStream.range(0, between(10, 100)) |
| 70 | + .mapToObj(n -> client().prepareIndex(indexName, "_doc").setSource("f", "v")).collect(Collectors.toList())); |
| 71 | + assertBusy(() -> { |
| 72 | + SyncedFlushResponse syncedFlushResponse = client().admin().indices().prepareSyncedFlush(indexName).get(); |
| 73 | + assertThat(syncedFlushResponse.successfulShards(), equalTo(2)); |
| 74 | + }); |
| 75 | + internalCluster().stopRandomNode(InternalTestCluster.nameFilter(nodeWithReplica)); |
| 76 | + if (randomBoolean()) { |
| 77 | + indexRandom(randomBoolean(), randomBoolean(), randomBoolean(), IntStream.range(0, between(10, 100)) |
| 78 | + .mapToObj(n -> client().prepareIndex(indexName, "_doc").setSource("f", "v")).collect(Collectors.toList())); |
| 79 | + } |
| 80 | + CountDownLatch blockRecovery = new CountDownLatch(1); |
| 81 | + CountDownLatch recoveryStarted = new CountDownLatch(1); |
| 82 | + MockTransportService transportServiceOnPrimary |
| 83 | + = (MockTransportService) internalCluster().getInstance(TransportService.class, nodeWithPrimary); |
| 84 | + transportServiceOnPrimary.addSendBehavior((connection, requestId, action, request, options) -> { |
| 85 | + if (PeerRecoveryTargetService.Actions.FILES_INFO.equals(action)) { |
| 86 | + recoveryStarted.countDown(); |
| 87 | + try { |
| 88 | + blockRecovery.await(); |
| 89 | + } catch (InterruptedException e) { |
| 90 | + Thread.currentThread().interrupt(); |
| 91 | + } |
| 92 | + } |
| 93 | + connection.sendRequest(requestId, action, request, options); |
| 94 | + }); |
| 95 | + String newNode = internalCluster().startDataOnlyNode(); |
| 96 | + recoveryStarted.await(); |
| 97 | + // destroy sync_id after the recovery on the new node has started |
| 98 | + client().admin().indices().prepareFlush(indexName).setForce(true).get(); |
| 99 | + // AllocationService only calls GatewayAllocator if there are unassigned shards |
| 100 | + assertAcked(client().admin().indices().prepareCreate("dummy-index").setWaitForActiveShards(0) |
| 101 | + .setSettings(Settings.builder().put("index.routing.allocation.require.attr", "not-found"))); |
| 102 | + internalCluster().startDataOnlyNode(nodeWithReplicaSettings); |
| 103 | + // need to wait for events to ensure the reroute has happened since we perform it async when a new node joins. |
| 104 | + client().admin().cluster().prepareHealth(indexName).setWaitForYellowStatus().setWaitForEvents(Priority.LANGUID).get(); |
| 105 | + blockRecovery.countDown(); |
| 106 | + ensureGreen(indexName); |
| 107 | + assertThat(internalCluster().nodesInclude(indexName), hasItem(newNode)); |
| 108 | + transportServiceOnPrimary.clearAllRules(); |
| 109 | + } |
| 110 | +} |
0 commit comments