use of org.elasticsearch.test.transport.MockTransportService in project crate by crate.
the class GlobalCheckpointSyncIT method testBackgroundGlobalCheckpointSync.
/*
* This test swallows the post-operation global checkpoint syncs, and then restores the ability to send these requests at the end of the
* test so that a background sync can fire and sync the global checkpoint.
*/
public void testBackgroundGlobalCheckpointSync() throws Exception {
runGlobalCheckpointSyncTest(TimeValue.timeValueSeconds(randomIntBetween(1, 3)), (indexName, client) -> {
// prevent global checkpoint syncs between all nodes
final DiscoveryNodes nodes = client.admin().cluster().prepareState().get().getState().getNodes();
for (final DiscoveryNode node : nodes) {
for (final DiscoveryNode other : nodes) {
if (node == other) {
continue;
}
final MockTransportService senderTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, node.getName());
final MockTransportService receiverTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, other.getName());
senderTransportService.addSendBehavior(receiverTransportService, (connection, requestId, action, request, options) -> {
if ("indices:admin/seq_no/global_checkpoint_sync[r]".equals(action)) {
throw new IllegalStateException("blocking indices:admin/seq_no/global_checkpoint_sync[r]");
} else {
connection.sendRequest(requestId, action, request, options);
}
});
}
}
}, (indexName, client) -> {
// restore global checkpoint syncs between all nodes
final DiscoveryNodes nodes = client.admin().cluster().prepareState().get().getState().getNodes();
for (final DiscoveryNode node : nodes) {
for (final DiscoveryNode other : nodes) {
if (node == other) {
continue;
}
final MockTransportService senderTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, node.getName());
final MockTransportService receiverTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, other.getName());
senderTransportService.clearOutboundRules(receiverTransportService);
}
}
});
}
use of org.elasticsearch.test.transport.MockTransportService in project crate by crate.
the class InternalTestCluster method reset.
private synchronized void reset(boolean wipeData) throws IOException {
// clear all rules for mock transport services
for (NodeAndClient nodeAndClient : nodes.values()) {
TransportService transportService = nodeAndClient.node.injector().getInstance(TransportService.class);
if (transportService instanceof MockTransportService) {
final MockTransportService mockTransportService = (MockTransportService) transportService;
mockTransportService.clearAllRules();
}
}
randomlyResetClients();
final int newSize = sharedNodesSeeds.length;
if (nextNodeId.get() == newSize && nodes.size() == newSize) {
if (wipeData) {
wipePendingDataDirectories();
}
logger.debug("Cluster hasn't changed - moving out - nodes: [{}] nextNodeId: [{}] numSharedNodes: [{}]", nodes.keySet(), nextNodeId.get(), newSize);
return;
}
logger.debug("Cluster is NOT consistent - restarting shared nodes - nodes: [{}] nextNodeId: [{}] numSharedNodes: [{}]", nodes.keySet(), nextNodeId.get(), newSize);
// trash all nodes with id >= sharedNodesSeeds.length - they are non shared
final List<NodeAndClient> toClose = new ArrayList<>();
for (NodeAndClient nodeAndClient : nodes.values()) {
if (nodeAndClient.nodeAndClientId() >= sharedNodesSeeds.length) {
logger.debug("Close Node [{}] not shared", nodeAndClient.name);
toClose.add(nodeAndClient);
}
}
stopNodesAndClients(toClose);
// clean up what the nodes left that is unused
if (wipeData) {
wipePendingDataDirectories();
}
assertTrue("expected at least one master-eligible node left in " + nodes, nodes.isEmpty() || nodes.values().stream().anyMatch(NodeAndClient::isMasterEligible));
final int prevNodeCount = nodes.size();
// start any missing node
assert newSize == numSharedDedicatedMasterNodes + numSharedDataNodes + numSharedCoordOnlyNodes;
// we want to start nodes in one go
final List<NodeAndClient> toStartAndPublish = new ArrayList<>();
final Runnable onTransportServiceStarted = () -> rebuildUnicastHostFiles(toStartAndPublish);
final List<Settings> settings = new ArrayList<>();
for (int i = 0; i < numSharedDedicatedMasterNodes; i++) {
final Settings.Builder extraSettings = Settings.builder();
extraSettings.put(Node.NODE_MASTER_SETTING.getKey(), true);
extraSettings.put(Node.NODE_DATA_SETTING.getKey(), false);
settings.add(getNodeSettings(i, sharedNodesSeeds[i], extraSettings.build()));
}
for (int i = numSharedDedicatedMasterNodes; i < numSharedDedicatedMasterNodes + numSharedDataNodes; i++) {
final Settings.Builder extraSettings = Settings.builder();
if (numSharedDedicatedMasterNodes > 0) {
// if we don't have dedicated master nodes, keep things default
extraSettings.put(Node.NODE_MASTER_SETTING.getKey(), false).build();
extraSettings.put(Node.NODE_DATA_SETTING.getKey(), true).build();
}
settings.add(getNodeSettings(i, sharedNodesSeeds[i], extraSettings.build()));
}
for (int i = numSharedDedicatedMasterNodes + numSharedDataNodes; i < numSharedDedicatedMasterNodes + numSharedDataNodes + numSharedCoordOnlyNodes; i++) {
final Builder extraSettings = Settings.builder().put(Node.NODE_MASTER_SETTING.getKey(), false).put(Node.NODE_DATA_SETTING.getKey(), false);
settings.add(getNodeSettings(i, sharedNodesSeeds[i], extraSettings.build()));
}
int autoBootstrapMasterNodeIndex = -1;
final List<String> masterNodeNames = settings.stream().filter(Node.NODE_MASTER_SETTING::get).map(Node.NODE_NAME_SETTING::get).collect(Collectors.toList());
if (prevNodeCount == 0 && autoManageMasterNodes) {
if (numSharedDedicatedMasterNodes > 0) {
autoBootstrapMasterNodeIndex = RandomNumbers.randomIntBetween(random, 0, numSharedDedicatedMasterNodes - 1);
} else if (numSharedDataNodes > 0) {
autoBootstrapMasterNodeIndex = RandomNumbers.randomIntBetween(random, 0, numSharedDataNodes - 1);
}
}
final List<Settings> updatedSettings = bootstrapMasterNodeWithSpecifiedIndex(settings);
for (int i = 0; i < numSharedDedicatedMasterNodes + numSharedDataNodes + numSharedCoordOnlyNodes; i++) {
Settings nodeSettings = updatedSettings.get(i);
if (i == autoBootstrapMasterNodeIndex) {
nodeSettings = Settings.builder().putList(INITIAL_MASTER_NODES_SETTING.getKey(), masterNodeNames).put(nodeSettings).build();
}
final NodeAndClient nodeAndClient = buildNode(i, nodeSettings, true, onTransportServiceStarted);
toStartAndPublish.add(nodeAndClient);
}
startAndPublishNodesAndClients(toStartAndPublish);
nextNodeId.set(newSize);
assert size() == newSize;
if (autoManageMasterNodes && newSize > 0) {
validateClusterFormed();
}
logger.debug("Cluster is consistent again - nodes: [{}] nextNodeId: [{}] numSharedNodes: [{}]", nodes.keySet(), nextNodeId.get(), newSize);
}
use of org.elasticsearch.test.transport.MockTransportService in project crate by crate.
the class VotingConfigurationIT method testElectsNodeNotInVotingConfiguration.
public void testElectsNodeNotInVotingConfiguration() throws Exception {
internalCluster().setBootstrapMasterNodeIndex(0);
final List<String> nodeNames = internalCluster().startNodes(4);
// a 4-node cluster settles on a 3-node configuration; we then prevent the nodes in the configuration from winning an election
// by failing at the pre-voting stage, so that the extra node must be elected instead when the master shuts down. This extra node
// should then add itself into the voting configuration.
assertFalse(internalCluster().client().admin().cluster().prepareHealth().setWaitForNodes("4").setWaitForEvents(Priority.LANGUID).get().isTimedOut());
String excludedNodeName = null;
final ClusterState clusterState = internalCluster().client().admin().cluster().prepareState().clear().setNodes(true).setMetadata(true).get().getState();
final Set<String> votingConfiguration = clusterState.getLastCommittedConfiguration().getNodeIds();
assertThat(votingConfiguration, hasSize(3));
assertThat(clusterState.nodes().getSize(), equalTo(4));
assertThat(votingConfiguration, hasItem(clusterState.nodes().getMasterNodeId()));
for (DiscoveryNode discoveryNode : clusterState.nodes()) {
if (votingConfiguration.contains(discoveryNode.getId()) == false) {
assertThat(excludedNodeName, nullValue());
excludedNodeName = discoveryNode.getName();
}
}
for (final String sender : nodeNames) {
if (sender.equals(excludedNodeName)) {
continue;
}
final MockTransportService senderTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, sender);
for (final String receiver : nodeNames) {
senderTransportService.addSendBehavior(internalCluster().getInstance(TransportService.class, receiver), (connection, requestId, action, request, options) -> {
if (action.equals(PreVoteCollector.REQUEST_PRE_VOTE_ACTION_NAME)) {
throw new ElasticsearchException("rejected");
}
connection.sendRequest(requestId, action, request, options);
});
}
}
internalCluster().stopCurrentMasterNode();
assertFalse(internalCluster().client().admin().cluster().prepareHealth().setWaitForNodes("3").setWaitForEvents(Priority.LANGUID).get().isTimedOut());
final ClusterState newClusterState = internalCluster().client().admin().cluster().prepareState().clear().setNodes(true).setMetadata(true).get().getState();
assertThat(newClusterState.nodes().getMasterNode().getName(), equalTo(excludedNodeName));
assertThat(newClusterState.getLastCommittedConfiguration().getNodeIds(), hasItem(newClusterState.nodes().getMasterNodeId()));
}
use of org.elasticsearch.test.transport.MockTransportService in project crate by crate.
the class TransportServiceHandshakeTests method startServices.
private NetworkHandle startServices(String nodeNameAndId, Settings settings, Version version) {
var allSettings = Settings.builder().put(TransportSettings.PORT.getKey(), ESTestCase.getPortRange()).put(settings).build();
var transport = new Netty4Transport(allSettings, // handle the real world scenario instead of a faked one.
Version.CURRENT, threadPool, new NetworkService(Collections.emptyList()), PageCacheRecycler.NON_RECYCLING_INSTANCE, new NamedWriteableRegistry(Collections.emptyList()), new NoneCircuitBreakerService(), new NettyBootstrap(), new AlwaysOKAuthentication(userName -> User.CRATE_USER), new SslContextProvider(settings));
TransportService transportService = new MockTransportService(allSettings, transport, threadPool, (boundAddress) -> new DiscoveryNode(nodeNameAndId, nodeNameAndId, boundAddress.publishAddress(), emptyMap(), emptySet(), version), null);
transportService.start();
transportService.acceptIncomingRequests();
transportServices.add(transportService);
return new NetworkHandle(transportService, transportService.getLocalNode());
}
Aggregations