|
| 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.resync; |
| 20 | + |
| 21 | +import org.elasticsearch.action.ActionListener; |
| 22 | +import org.elasticsearch.action.support.ActionFilters; |
| 23 | +import org.elasticsearch.action.support.PlainActionFuture; |
| 24 | +import org.elasticsearch.cluster.ClusterState; |
| 25 | +import org.elasticsearch.cluster.action.shard.ShardStateAction; |
| 26 | +import org.elasticsearch.cluster.block.ClusterBlocks; |
| 27 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 28 | +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; |
| 29 | +import org.elasticsearch.cluster.routing.IndexShardRoutingTable; |
| 30 | +import org.elasticsearch.cluster.routing.ShardRouting; |
| 31 | +import org.elasticsearch.cluster.routing.ShardRoutingState; |
| 32 | +import org.elasticsearch.cluster.service.ClusterService; |
| 33 | +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; |
| 34 | +import org.elasticsearch.common.lease.Releasable; |
| 35 | +import org.elasticsearch.common.network.NetworkService; |
| 36 | +import org.elasticsearch.common.settings.Settings; |
| 37 | +import org.elasticsearch.common.util.BigArrays; |
| 38 | +import org.elasticsearch.discovery.DiscoverySettings; |
| 39 | +import org.elasticsearch.index.Index; |
| 40 | +import org.elasticsearch.index.IndexService; |
| 41 | +import org.elasticsearch.index.shard.IndexShard; |
| 42 | +import org.elasticsearch.index.shard.ReplicationGroup; |
| 43 | +import org.elasticsearch.index.shard.ShardId; |
| 44 | +import org.elasticsearch.index.translog.Translog; |
| 45 | +import org.elasticsearch.indices.IndicesService; |
| 46 | +import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; |
| 47 | +import org.elasticsearch.tasks.Task; |
| 48 | +import org.elasticsearch.test.ESTestCase; |
| 49 | +import org.elasticsearch.test.transport.MockTransportService; |
| 50 | +import org.elasticsearch.threadpool.TestThreadPool; |
| 51 | +import org.elasticsearch.threadpool.ThreadPool; |
| 52 | +import org.elasticsearch.transport.MockTcpTransport; |
| 53 | +import org.junit.AfterClass; |
| 54 | +import org.junit.BeforeClass; |
| 55 | + |
| 56 | +import java.nio.charset.Charset; |
| 57 | +import java.util.Collections; |
| 58 | +import java.util.HashSet; |
| 59 | +import java.util.concurrent.TimeUnit; |
| 60 | + |
| 61 | +import static java.util.Collections.emptyList; |
| 62 | +import static org.elasticsearch.action.support.replication.ClusterStateCreationUtils.state; |
| 63 | +import static org.elasticsearch.test.ClusterServiceUtils.createClusterService; |
| 64 | +import static org.elasticsearch.test.ClusterServiceUtils.setState; |
| 65 | +import static org.elasticsearch.transport.TransportService.NOOP_TRANSPORT_INTERCEPTOR; |
| 66 | +import static org.hamcrest.Matchers.equalTo; |
| 67 | +import static org.hamcrest.Matchers.is; |
| 68 | +import static org.hamcrest.Matchers.nullValue; |
| 69 | +import static org.mockito.Matchers.any; |
| 70 | +import static org.mockito.Matchers.anyObject; |
| 71 | +import static org.mockito.Matchers.anyString; |
| 72 | +import static org.mockito.Matchers.eq; |
| 73 | +import static org.mockito.Mockito.doAnswer; |
| 74 | +import static org.mockito.Mockito.mock; |
| 75 | +import static org.mockito.Mockito.when; |
| 76 | + |
| 77 | +public class TransportResyncReplicationActionTests extends ESTestCase { |
| 78 | + |
| 79 | + private static ThreadPool threadPool; |
| 80 | + |
| 81 | + @BeforeClass |
| 82 | + public static void beforeClass() { |
| 83 | + threadPool = new TestThreadPool("ShardReplicationTests"); |
| 84 | + } |
| 85 | + |
| 86 | + @AfterClass |
| 87 | + public static void afterClass() { |
| 88 | + ThreadPool.terminate(threadPool, 30, TimeUnit.SECONDS); |
| 89 | + threadPool = null; |
| 90 | + } |
| 91 | + |
| 92 | + public void testResyncDoesNotBlockOnPrimaryAction() throws Exception { |
| 93 | + try (ClusterService clusterService = createClusterService(threadPool)) { |
| 94 | + final String indexName = randomAlphaOfLength(5); |
| 95 | + setState(clusterService, state(indexName, true, ShardRoutingState.STARTED)); |
| 96 | + |
| 97 | + setState(clusterService, |
| 98 | + ClusterState.builder(clusterService.state()).blocks(ClusterBlocks.builder() |
| 99 | + .addGlobalBlock(DiscoverySettings.NO_MASTER_BLOCK_ALL) |
| 100 | + .addIndexBlock(indexName, IndexMetaData.INDEX_WRITE_BLOCK))); |
| 101 | + |
| 102 | + try (MockTcpTransport transport = new MockTcpTransport(Settings.EMPTY, threadPool, BigArrays.NON_RECYCLING_INSTANCE, |
| 103 | + new NoneCircuitBreakerService(), new NamedWriteableRegistry(emptyList()), new NetworkService(emptyList()))) { |
| 104 | + |
| 105 | + final MockTransportService transportService = new MockTransportService(Settings.EMPTY, transport, threadPool, |
| 106 | + NOOP_TRANSPORT_INTERCEPTOR, x -> clusterService.localNode(), null, Collections.emptySet()); |
| 107 | + transportService.start(); |
| 108 | + transportService.acceptIncomingRequests(); |
| 109 | + final ShardStateAction shardStateAction = new ShardStateAction(clusterService, transportService, null, null, threadPool); |
| 110 | + |
| 111 | + final IndexMetaData indexMetaData = clusterService.state().metaData().index(indexName); |
| 112 | + final Index index = indexMetaData.getIndex(); |
| 113 | + final ShardId shardId = new ShardId(index, 0); |
| 114 | + final IndexShardRoutingTable shardRoutingTable = clusterService.state().routingTable().shardRoutingTable(shardId); |
| 115 | + final ShardRouting primaryShardRouting = clusterService.state().routingTable().shardRoutingTable(shardId).primaryShard(); |
| 116 | + final String allocationId = primaryShardRouting.allocationId().getId(); |
| 117 | + final long primaryTerm = indexMetaData.primaryTerm(shardId.id()); |
| 118 | + |
| 119 | + final IndexShard indexShard = mock(IndexShard.class); |
| 120 | + when(indexShard.shardId()).thenReturn(shardId); |
| 121 | + when(indexShard.routingEntry()).thenReturn(primaryShardRouting); |
| 122 | + when(indexShard.getPendingPrimaryTerm()).thenReturn(primaryTerm); |
| 123 | + doAnswer(invocation -> { |
| 124 | + ActionListener<Releasable> callback = (ActionListener<Releasable>) invocation.getArguments()[0]; |
| 125 | + callback.onResponse(() -> logger.trace("released")); |
| 126 | + return null; |
| 127 | + }).when(indexShard).acquirePrimaryOperationPermit(any(ActionListener.class), anyString(), anyObject()); |
| 128 | + when(indexShard.getReplicationGroup()).thenReturn( |
| 129 | + new ReplicationGroup(shardRoutingTable, |
| 130 | + clusterService.state().metaData().index(index).inSyncAllocationIds(shardId.id()), |
| 131 | + shardRoutingTable.getAllAllocationIds())); |
| 132 | + |
| 133 | + final IndexService indexService = mock(IndexService.class); |
| 134 | + when(indexService.getShard(eq(shardId.id()))).thenReturn(indexShard); |
| 135 | + |
| 136 | + final IndicesService indexServices = mock(IndicesService.class); |
| 137 | + when(indexServices.indexServiceSafe(eq(index))).thenReturn(indexService); |
| 138 | + |
| 139 | + final IndexNameExpressionResolver resolver = new IndexNameExpressionResolver(Settings.EMPTY); |
| 140 | + final TransportResyncReplicationAction action = new TransportResyncReplicationAction(Settings.EMPTY, transportService, |
| 141 | + clusterService, indexServices, threadPool, shardStateAction, new ActionFilters(new HashSet<>()), resolver); |
| 142 | + |
| 143 | + assertThat(action.globalBlockLevel(), nullValue()); |
| 144 | + assertThat(action.indexBlockLevel(), nullValue()); |
| 145 | + |
| 146 | + final Task task = mock(Task.class); |
| 147 | + when(task.getId()).thenReturn(randomNonNegativeLong()); |
| 148 | + |
| 149 | + final byte[] bytes = "{}".getBytes(Charset.forName("UTF-8")); |
| 150 | + final ResyncReplicationRequest request = new ResyncReplicationRequest(shardId, 42L, 100, |
| 151 | + new Translog.Operation[]{new Translog.Index("type", "id", 0, primaryTerm, bytes)}); |
| 152 | + |
| 153 | + final PlainActionFuture<ResyncReplicationResponse> listener = new PlainActionFuture<>(); |
| 154 | + action.sync(request, task, allocationId, primaryTerm, listener); |
| 155 | + |
| 156 | + assertThat(listener.get().getShardInfo().getFailed(), equalTo(0)); |
| 157 | + assertThat(listener.isDone(), is(true)); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments