Skip to content

Commit e4bd482

Browse files
committed
Revert ClusterStateChanges
1 parent b9d407f commit e4bd482

File tree

1 file changed

+37
-62
lines changed

1 file changed

+37
-62
lines changed

server/src/test/java/org/elasticsearch/indices/cluster/ClusterStateChanges.java

Lines changed: 37 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.indices.cluster;
2121

22+
import org.elasticsearch.ExceptionsHelper;
2223
import org.elasticsearch.Version;
2324
import org.elasticsearch.action.ActionResponse;
2425
import org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteRequest;
@@ -40,13 +41,13 @@
4041
import org.elasticsearch.action.support.master.TransportMasterNodeAction;
4142
import org.elasticsearch.action.support.master.TransportMasterNodeActionUtils;
4243
import org.elasticsearch.cluster.ClusterState;
43-
import org.elasticsearch.cluster.ClusterStateTaskConfig;
4444
import org.elasticsearch.cluster.ClusterStateTaskExecutor;
45+
import org.elasticsearch.cluster.ClusterStateTaskExecutor.ClusterTasksResult;
46+
import org.elasticsearch.cluster.ClusterStateUpdateTask;
4547
import org.elasticsearch.cluster.EmptyClusterInfoService;
46-
import org.elasticsearch.cluster.NodeConnectionsService;
4748
import org.elasticsearch.cluster.action.shard.ShardStateAction;
48-
import org.elasticsearch.cluster.action.shard.ShardStateAction.FailedShardEntry;
4949
import org.elasticsearch.cluster.action.shard.ShardStateAction.StartedShardEntry;
50+
import org.elasticsearch.cluster.action.shard.ShardStateAction.FailedShardEntry;
5051
import org.elasticsearch.cluster.metadata.AliasValidator;
5152
import org.elasticsearch.cluster.metadata.IndexMetaData;
5253
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
@@ -56,7 +57,6 @@
5657
import org.elasticsearch.cluster.metadata.MetaDataIndexUpgradeService;
5758
import org.elasticsearch.cluster.metadata.MetaDataUpdateSettingsService;
5859
import org.elasticsearch.cluster.node.DiscoveryNode;
59-
import org.elasticsearch.cluster.node.DiscoveryNodes;
6060
import org.elasticsearch.cluster.routing.ShardRouting;
6161
import org.elasticsearch.cluster.routing.allocation.AllocationService;
6262
import org.elasticsearch.cluster.routing.allocation.FailedShard;
@@ -65,9 +65,7 @@
6565
import org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders;
6666
import org.elasticsearch.cluster.routing.allocation.decider.ReplicaAfterPrimaryActiveAllocationDecider;
6767
import org.elasticsearch.cluster.routing.allocation.decider.SameShardAllocationDecider;
68-
import org.elasticsearch.cluster.service.ClusterApplierService;
6968
import org.elasticsearch.cluster.service.ClusterService;
70-
import org.elasticsearch.common.Priority;
7169
import org.elasticsearch.common.UUIDs;
7270
import org.elasticsearch.common.component.AbstractComponent;
7371
import org.elasticsearch.common.settings.ClusterSettings;
@@ -94,25 +92,23 @@
9492
import java.util.Collections;
9593
import java.util.HashSet;
9694
import java.util.List;
97-
import java.util.concurrent.atomic.AtomicReference;
9895
import java.util.stream.Collectors;
9996

10097
import static com.carrotsearch.randomizedtesting.RandomizedTest.getRandom;
10198
import static org.elasticsearch.env.Environment.PATH_HOME_SETTING;
102-
import static org.junit.Assert.assertNotNull;
103-
import static org.junit.Assert.assertNull;
104-
import static org.junit.Assert.assertSame;
99+
import static org.hamcrest.Matchers.notNullValue;
100+
import static org.junit.Assert.assertThat;
105101
import static org.mockito.Matchers.any;
106102
import static org.mockito.Matchers.anyList;
103+
import static org.mockito.Matchers.anyString;
104+
import static org.mockito.Mockito.doAnswer;
107105
import static org.mockito.Mockito.mock;
108106
import static org.mockito.Mockito.when;
109107

110108
public class ClusterStateChanges extends AbstractComponent {
111109

112-
private final ClusterService clusterService;
113110
private final AllocationService allocationService;
114-
private final AtomicReference<Runnable> nextMasterTaskToRun;
115-
private final AtomicReference<ClusterState> lastClusterStateRef;
111+
private final ClusterService clusterService;
116112
private final ShardStateAction.ShardFailedClusterStateTaskExecutor shardFailedClusterStateTaskExecutor;
117113
private final ShardStateAction.ShardStartedClusterStateTaskExecutor shardStartedClusterStateTaskExecutor;
118114

@@ -144,29 +140,9 @@ public ClusterStateChanges(NamedXContentRegistry xContentRegistry, ThreadPool th
144140
DestructiveOperations destructiveOperations = new DestructiveOperations(settings, clusterSettings);
145141
Environment environment = TestEnvironment.newEnvironment(settings);
146142
Transport transport = mock(Transport.class); // it's not used
147-
nextMasterTaskToRun = new AtomicReference<>();
148-
FakeThreadPoolMasterService masterService = new FakeThreadPoolMasterService("fake-master", nextMasterTaskToRun::set);
149-
lastClusterStateRef = new AtomicReference<>();
150-
masterService.setClusterStateSupplier(lastClusterStateRef::get);
151-
masterService.setClusterStatePublisher((event, publishListener, ackListener) -> {
152-
lastClusterStateRef.set(event.state());
153-
publishListener.onResponse(null);
154-
});
155-
clusterService = new ClusterService(settings, clusterSettings, masterService, mock(ClusterApplierService.class));
156-
clusterService.setNodeConnectionsService(new NodeConnectionsService(Settings.EMPTY, null, null) {
157-
@Override
158-
public void connectToNodes(DiscoveryNodes discoveryNodes) {
159-
// skip
160-
}
161-
162-
@Override
163-
public void disconnectFromNodesExcept(DiscoveryNodes nodesToKeep) {
164-
// skip
165-
}
166-
});
167-
clusterService.start();
168143

169144
// mocks
145+
clusterService = mock(ClusterService.class);
170146
IndicesService indicesService = mock(IndicesService.class);
171147
// MetaDataCreateIndexService creates indices using its IndicesService instance to check mappings -> fake it here
172148
try {
@@ -286,40 +262,39 @@ public ClusterState applyStartedShards(ClusterState clusterState, List<ShardRout
286262
}
287263

288264
private <T> ClusterState runTasks(ClusterStateTaskExecutor<T> executor, ClusterState clusterState, List<T> entries) {
289-
lastClusterStateRef.set(clusterState);
290-
assertNull(nextMasterTaskToRun.get());
291-
for (T task : entries) {
292-
clusterService.submitStateUpdateTask(task.toString(), task, ClusterStateTaskConfig.build(Priority.NORMAL), executor,
293-
(source, e) -> {});
294-
}
295-
if (entries.isEmpty()) {
296-
assertNull(nextMasterTaskToRun.get());
297-
} else {
298-
assertNotNull(nextMasterTaskToRun.get());
299-
nextMasterTaskToRun.getAndSet(null).run();
300-
ClusterState firstClusterState = lastClusterStateRef.get();
301-
for (int i = 1; i < entries.size(); i++) {
302-
nextMasterTaskToRun.getAndSet(null).run();
303-
assertSame(firstClusterState, lastClusterStateRef.get()); // due to batching, only the first actually does something
265+
try {
266+
ClusterTasksResult<T> result = executor.execute(clusterState, entries);
267+
for (ClusterStateTaskExecutor.TaskResult taskResult : result.executionResults.values()) {
268+
if (taskResult.isSuccess() == false) {
269+
throw taskResult.getFailure();
270+
}
304271
}
272+
return result.resultingState;
273+
} catch (Exception e) {
274+
throw ExceptionsHelper.convertToRuntime(e);
305275
}
306-
assertNull(nextMasterTaskToRun.get());
307-
return lastClusterStateRef.get();
308276
}
309277

310278
private <Request extends MasterNodeRequest<Request>, Response extends ActionResponse> ClusterState execute(
311279
TransportMasterNodeAction<Request, Response> masterNodeAction, Request request, ClusterState clusterState) {
312-
lastClusterStateRef.set(clusterState);
313-
assertNull(nextMasterTaskToRun.get());
314-
try {
315-
TransportMasterNodeActionUtils.runMasterOperation(masterNodeAction, request, clusterState, new PlainActionFuture<>());
316-
} catch (Exception e) {
317-
throw new RuntimeException(e);
318-
}
319-
assertNotNull(nextMasterTaskToRun.get());
320-
nextMasterTaskToRun.getAndSet(null).run();
321-
assertNull(nextMasterTaskToRun.get());
322-
return lastClusterStateRef.get();
280+
return executeClusterStateUpdateTask(clusterState, () -> {
281+
try {
282+
TransportMasterNodeActionUtils.runMasterOperation(masterNodeAction, request, clusterState, new PlainActionFuture<>());
283+
} catch (Exception e) {
284+
throw new RuntimeException(e);
285+
}
286+
});
323287
}
324288

289+
private ClusterState executeClusterStateUpdateTask(ClusterState state, Runnable runnable) {
290+
ClusterState[] result = new ClusterState[1];
291+
doAnswer(invocationOnMock -> {
292+
ClusterStateUpdateTask task = (ClusterStateUpdateTask)invocationOnMock.getArguments()[1];
293+
result[0] = task.execute(state);
294+
return null;
295+
}).when(clusterService).submitStateUpdateTask(anyString(), any(ClusterStateUpdateTask.class));
296+
runnable.run();
297+
assertThat(result[0], notNullValue());
298+
return result[0];
299+
}
325300
}

0 commit comments

Comments
 (0)