|
| 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.action.support.replication; |
| 21 | + |
| 22 | +import org.elasticsearch.action.ActionListener; |
| 23 | +import org.elasticsearch.action.ActionRequest; |
| 24 | +import org.elasticsearch.action.ActionResponse; |
| 25 | +import org.elasticsearch.action.ActionType; |
| 26 | +import org.elasticsearch.action.support.ActionFilters; |
| 27 | +import org.elasticsearch.cluster.action.shard.ShardStateAction; |
| 28 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 29 | +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; |
| 30 | +import org.elasticsearch.cluster.service.ClusterService; |
| 31 | +import org.elasticsearch.common.inject.Inject; |
| 32 | +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; |
| 33 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 34 | +import org.elasticsearch.common.settings.Settings; |
| 35 | +import org.elasticsearch.common.util.concurrent.ThreadContext; |
| 36 | +import org.elasticsearch.index.shard.IndexShard; |
| 37 | +import org.elasticsearch.index.shard.ShardId; |
| 38 | +import org.elasticsearch.indices.IndicesService; |
| 39 | +import org.elasticsearch.plugins.ActionPlugin; |
| 40 | +import org.elasticsearch.plugins.NetworkPlugin; |
| 41 | +import org.elasticsearch.plugins.Plugin; |
| 42 | +import org.elasticsearch.plugins.PluginsService; |
| 43 | +import org.elasticsearch.test.ESIntegTestCase; |
| 44 | +import org.elasticsearch.test.InternalTestCluster; |
| 45 | +import org.elasticsearch.test.transport.MockTransportService; |
| 46 | +import org.elasticsearch.threadpool.ThreadPool; |
| 47 | +import org.elasticsearch.transport.Transport; |
| 48 | +import org.elasticsearch.transport.TransportInterceptor; |
| 49 | +import org.elasticsearch.transport.TransportRequest; |
| 50 | +import org.elasticsearch.transport.TransportRequestOptions; |
| 51 | +import org.elasticsearch.transport.TransportResponse; |
| 52 | +import org.elasticsearch.transport.TransportResponseHandler; |
| 53 | +import org.elasticsearch.transport.TransportService; |
| 54 | +import org.hamcrest.Matchers; |
| 55 | + |
| 56 | +import java.io.IOException; |
| 57 | +import java.util.Collection; |
| 58 | +import java.util.List; |
| 59 | +import java.util.concurrent.CountDownLatch; |
| 60 | +import java.util.concurrent.TimeUnit; |
| 61 | +import java.util.concurrent.atomic.AtomicReference; |
| 62 | + |
| 63 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 64 | + |
| 65 | + |
| 66 | +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0) |
| 67 | +public class TransportReplicationActionRetryOnClosedNodeIT extends ESIntegTestCase { |
| 68 | + |
| 69 | + @Override |
| 70 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 71 | + return List.of(TestPlugin.class, MockTransportService.TestPlugin.class); |
| 72 | + } |
| 73 | + |
| 74 | + public static class Request extends ReplicationRequest<Request> { |
| 75 | + public Request(ShardId shardId) { |
| 76 | + super(shardId); |
| 77 | + } |
| 78 | + |
| 79 | + public Request(StreamInput in) throws IOException { |
| 80 | + super(in); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public String toString() { |
| 85 | + return "test-request"; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public static class Response extends ReplicationResponse { |
| 90 | + public Response() { |
| 91 | + } |
| 92 | + |
| 93 | + public Response(StreamInput in) throws IOException { |
| 94 | + super(in); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public static class TestAction extends TransportReplicationAction<Request, Request, Response> { |
| 99 | + private static final String ACTION_NAME = "internal:test-replication-action"; |
| 100 | + private static final ActionType<Response> TYPE = new ActionType<>(ACTION_NAME, Response::new); |
| 101 | + |
| 102 | + @Inject |
| 103 | + public TestAction(Settings settings, TransportService transportService, ClusterService clusterService, |
| 104 | + IndicesService indicesService, ThreadPool threadPool, ShardStateAction shardStateAction, |
| 105 | + ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) { |
| 106 | + super(settings, ACTION_NAME, transportService, clusterService, indicesService, threadPool, shardStateAction, actionFilters, |
| 107 | + indexNameExpressionResolver, Request::new, Request::new, ThreadPool.Names.GENERIC); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + protected Response newResponseInstance(StreamInput in) throws IOException { |
| 112 | + return new Response(in); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + protected void shardOperationOnPrimary(Request shardRequest, IndexShard primary, |
| 117 | + ActionListener<PrimaryResult<Request, Response>> listener) { |
| 118 | + listener.onResponse(new PrimaryResult<>(shardRequest, new Response())); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + protected ReplicaResult shardOperationOnReplica(Request shardRequest, IndexShard replica) { |
| 123 | + return new ReplicaResult(); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public static class TestPlugin extends Plugin implements ActionPlugin, NetworkPlugin { |
| 128 | + private CountDownLatch actionRunningLatch = new CountDownLatch(1); |
| 129 | + private CountDownLatch actionWaitLatch = new CountDownLatch(1); |
| 130 | + private volatile String testActionName; |
| 131 | + |
| 132 | + public TestPlugin() { |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public List<ActionHandler<? extends ActionRequest, ? extends ActionResponse>> getActions() { |
| 137 | + return List.of(new ActionHandler<>(TestAction.TYPE, TestAction.class)); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public List<TransportInterceptor> getTransportInterceptors(NamedWriteableRegistry namedWriteableRegistry, |
| 142 | + ThreadContext threadContext) { |
| 143 | + return List.of(new TransportInterceptor() { |
| 144 | + @Override |
| 145 | + public AsyncSender interceptSender(AsyncSender sender) { |
| 146 | + return new AsyncSender() { |
| 147 | + @Override |
| 148 | + public <T extends TransportResponse> void sendRequest(Transport.Connection connection, String action, |
| 149 | + TransportRequest request, TransportRequestOptions options, |
| 150 | + TransportResponseHandler<T> handler) { |
| 151 | + // only activated on primary |
| 152 | + if (action.equals(testActionName)) { |
| 153 | + actionRunningLatch.countDown(); |
| 154 | + try { |
| 155 | + actionWaitLatch.await(10, TimeUnit.SECONDS); |
| 156 | + } catch (InterruptedException e) { |
| 157 | + throw new AssertionError(e); |
| 158 | + } |
| 159 | + } |
| 160 | + sender.sendRequest(connection, action, request, options, handler); |
| 161 | + } |
| 162 | + }; |
| 163 | + } |
| 164 | + }); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + public void testRetryOnStoppedTransportService() throws Exception { |
| 169 | + internalCluster().startMasterOnlyNodes(2); |
| 170 | + String primary = internalCluster().startDataOnlyNode(); |
| 171 | + assertAcked(prepareCreate("test") |
| 172 | + .setSettings(Settings.builder() |
| 173 | + .put(indexSettings()) |
| 174 | + .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) |
| 175 | + .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1) |
| 176 | + )); |
| 177 | + |
| 178 | + String replica = internalCluster().startDataOnlyNode(); |
| 179 | + String coordinator = internalCluster().startCoordinatingOnlyNode(Settings.EMPTY); |
| 180 | + ensureGreen("test"); |
| 181 | + |
| 182 | + TestPlugin primaryTestPlugin = getTestPlugin(primary); |
| 183 | + // this test only provoked an issue for the primary action, but for completeness, we pick the action randomly |
| 184 | + primaryTestPlugin.testActionName = TestAction.ACTION_NAME + (randomBoolean() ? "[p]" : "[r]"); |
| 185 | + logger.info("--> Test action {}, primary {}, replica {}", primaryTestPlugin.testActionName, primary, replica); |
| 186 | + |
| 187 | + AtomicReference<Object> response = new AtomicReference<>(); |
| 188 | + CountDownLatch doneLatch = new CountDownLatch(1); |
| 189 | + client(coordinator).execute(TestAction.TYPE, new Request(new ShardId(resolveIndex("test"), 0)), |
| 190 | + ActionListener.runAfter(ActionListener.wrap( |
| 191 | + r -> assertTrue(response.compareAndSet(null, r)), |
| 192 | + e -> assertTrue(response.compareAndSet(null, e))), |
| 193 | + doneLatch::countDown)); |
| 194 | + |
| 195 | + assertTrue(primaryTestPlugin.actionRunningLatch.await(10, TimeUnit.SECONDS)); |
| 196 | + |
| 197 | + MockTransportService primaryTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, |
| 198 | + primary); |
| 199 | + // we pause node after TransportService has moved to stopped, but before closing connections, since if connections are closed |
| 200 | + // we would not hit the transport service closed case. |
| 201 | + primaryTransportService.addOnStopListener(() -> { |
| 202 | + primaryTestPlugin.actionWaitLatch.countDown(); |
| 203 | + try { |
| 204 | + assertTrue(doneLatch.await(10, TimeUnit.SECONDS)); |
| 205 | + } catch (InterruptedException e) { |
| 206 | + throw new AssertionError(e); |
| 207 | + } |
| 208 | + }); |
| 209 | + internalCluster().stopRandomNode(InternalTestCluster.nameFilter(primary)); |
| 210 | + |
| 211 | + assertTrue(doneLatch.await(10, TimeUnit.SECONDS)); |
| 212 | + if (response.get() instanceof Exception) { |
| 213 | + throw new AssertionError(response.get()); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + private TestPlugin getTestPlugin(String node) { |
| 218 | + PluginsService pluginsService = internalCluster().getInstance(PluginsService.class, node); |
| 219 | + List<TestPlugin> testPlugins = pluginsService.filterPlugins(TestPlugin.class); |
| 220 | + assertThat(testPlugins, Matchers.hasSize(1)); |
| 221 | + return testPlugins.get(0); |
| 222 | + } |
| 223 | +} |
0 commit comments