Search in sources :

Example 1 with DeterministicTaskQueue

use of org.elasticsearch.cluster.coordination.DeterministicTaskQueue in project crate by crate.

the class RetryableActionTests method setUp.

@Before
public void setUp() throws Exception {
    super.setUp();
    Settings settings = Settings.builder().put(NODE_NAME_SETTING.getKey(), "node").build();
    taskQueue = new DeterministicTaskQueue(settings, random());
}
Also used : DeterministicTaskQueue(org.elasticsearch.cluster.coordination.DeterministicTaskQueue) Settings(org.elasticsearch.common.settings.Settings) Before(org.junit.Before)

Example 2 with DeterministicTaskQueue

use of org.elasticsearch.cluster.coordination.DeterministicTaskQueue in project crate by crate.

the class NodeConnectionsServiceTests method testPeriodicReconnection.

public void testPeriodicReconnection() {
    final Settings.Builder settings = Settings.builder();
    final long reconnectIntervalMillis;
    if (randomBoolean()) {
        reconnectIntervalMillis = CLUSTER_NODE_RECONNECT_INTERVAL_SETTING.get(Settings.EMPTY).millis();
    } else {
        reconnectIntervalMillis = randomLongBetween(1, 100000);
        settings.put(CLUSTER_NODE_RECONNECT_INTERVAL_SETTING.getKey(), reconnectIntervalMillis + "ms");
    }
    final DeterministicTaskQueue deterministicTaskQueue = new DeterministicTaskQueue(builder().put(NODE_NAME_SETTING.getKey(), "node").build(), random());
    MockTransport transport = new MockTransport(deterministicTaskQueue.getThreadPool());
    TestTransportService transportService = new TestTransportService(transport, deterministicTaskQueue.getThreadPool());
    transportService.start();
    transportService.acceptIncomingRequests();
    final NodeConnectionsService service = new NodeConnectionsService(settings.build(), deterministicTaskQueue.getThreadPool(), transportService);
    service.start();
    final List<DiscoveryNode> allNodes = generateNodes();
    final DiscoveryNodes targetNodes = discoveryNodesFromList(randomSubsetOf(allNodes));
    transport.randomConnectionExceptions = true;
    final AtomicBoolean connectionCompleted = new AtomicBoolean();
    service.connectToNodes(targetNodes, () -> connectionCompleted.set(true));
    deterministicTaskQueue.runAllRunnableTasks();
    assertTrue(connectionCompleted.get());
    long maxDisconnectionTime = 0;
    for (int iteration = 0; iteration < 3; iteration++) {
        // simulate disconnects
        for (DiscoveryNode node : allNodes) {
            if (randomBoolean()) {
                final long disconnectionTime = randomLongBetween(0, 120000);
                maxDisconnectionTime = Math.max(maxDisconnectionTime, disconnectionTime);
                deterministicTaskQueue.scheduleAt(disconnectionTime, new Runnable() {

                    @Override
                    public void run() {
                        transportService.disconnectFromNode(node);
                    }

                    @Override
                    public String toString() {
                        return "scheduled disconnection of " + node;
                    }
                });
            }
        }
    }
    runTasksUntil(deterministicTaskQueue, maxDisconnectionTime);
    // disable exceptions so things can be restored
    transport.randomConnectionExceptions = false;
    logger.info("renewing connections");
    runTasksUntil(deterministicTaskQueue, maxDisconnectionTime + reconnectIntervalMillis);
    assertConnectedExactlyToNodes(transportService, targetNodes);
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DeterministicTaskQueue(org.elasticsearch.cluster.coordination.DeterministicTaskQueue) CheckedRunnable(org.elasticsearch.common.CheckedRunnable) Settings(org.elasticsearch.common.settings.Settings) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 3 with DeterministicTaskQueue

use of org.elasticsearch.cluster.coordination.DeterministicTaskQueue in project crate by crate.

the class NodeConnectionsServiceTests method testDebugLogging.

@TestLogging(value = "org.elasticsearch.cluster.NodeConnectionsService:DEBUG")
public void testDebugLogging() throws IllegalAccessException {
    final DeterministicTaskQueue deterministicTaskQueue = new DeterministicTaskQueue(builder().put(NODE_NAME_SETTING.getKey(), "node").build(), random());
    MockTransport transport = new MockTransport(deterministicTaskQueue.getThreadPool());
    TestTransportService transportService = new TestTransportService(transport, deterministicTaskQueue.getThreadPool());
    transportService.start();
    transportService.acceptIncomingRequests();
    final NodeConnectionsService service = new NodeConnectionsService(Settings.EMPTY, deterministicTaskQueue.getThreadPool(), transportService);
    service.start();
    final List<DiscoveryNode> allNodes = generateNodes();
    final DiscoveryNodes targetNodes = discoveryNodesFromList(randomSubsetOf(allNodes));
    service.connectToNodes(targetNodes, () -> {
    });
    deterministicTaskQueue.runAllRunnableTasks();
    // periodic reconnections to unexpectedly-disconnected nodes are logged
    final Set<DiscoveryNode> disconnectedNodes = new HashSet<>(randomSubsetOf(allNodes));
    for (DiscoveryNode disconnectedNode : disconnectedNodes) {
        transportService.disconnectFromNode(disconnectedNode);
    }
    MockLogAppender appender = new MockLogAppender();
    try {
        appender.start();
        Loggers.addAppender(LogManager.getLogger("org.elasticsearch.cluster.NodeConnectionsService"), appender);
        for (DiscoveryNode targetNode : targetNodes) {
            if (disconnectedNodes.contains(targetNode)) {
                appender.addExpectation(new MockLogAppender.SeenEventExpectation("connecting to " + targetNode, "org.elasticsearch.cluster.NodeConnectionsService", Level.DEBUG, "connecting to " + targetNode));
                appender.addExpectation(new MockLogAppender.SeenEventExpectation("connected to " + targetNode, "org.elasticsearch.cluster.NodeConnectionsService", Level.DEBUG, "connected to " + targetNode));
            } else {
                appender.addExpectation(new MockLogAppender.UnseenEventExpectation("connecting to " + targetNode, "org.elasticsearch.cluster.NodeConnectionsService", Level.DEBUG, "connecting to " + targetNode));
                appender.addExpectation(new MockLogAppender.UnseenEventExpectation("connected to " + targetNode, "org.elasticsearch.cluster.NodeConnectionsService", Level.DEBUG, "connected to " + targetNode));
            }
        }
        runTasksUntil(deterministicTaskQueue, CLUSTER_NODE_RECONNECT_INTERVAL_SETTING.get(Settings.EMPTY).millis());
        appender.assertAllExpectationsMatched();
    } finally {
        Loggers.removeAppender(LogManager.getLogger("org.elasticsearch.cluster.NodeConnectionsService"), appender);
        appender.stop();
    }
    for (DiscoveryNode disconnectedNode : disconnectedNodes) {
        transportService.disconnectFromNode(disconnectedNode);
    }
    // changes to the expected set of nodes are logged, including reconnections to any unexpectedly-disconnected nodes
    final DiscoveryNodes newTargetNodes = discoveryNodesFromList(randomSubsetOf(allNodes));
    for (DiscoveryNode disconnectedNode : disconnectedNodes) {
        transportService.disconnectFromNode(disconnectedNode);
    }
    appender = new MockLogAppender();
    try {
        appender.start();
        Loggers.addAppender(LogManager.getLogger("org.elasticsearch.cluster.NodeConnectionsService"), appender);
        for (DiscoveryNode targetNode : targetNodes) {
            if (disconnectedNodes.contains(targetNode) && newTargetNodes.get(targetNode.getId()) != null) {
                appender.addExpectation(new MockLogAppender.SeenEventExpectation("connecting to " + targetNode, "org.elasticsearch.cluster.NodeConnectionsService", Level.DEBUG, "connecting to " + targetNode));
                appender.addExpectation(new MockLogAppender.SeenEventExpectation("connected to " + targetNode, "org.elasticsearch.cluster.NodeConnectionsService", Level.DEBUG, "connected to " + targetNode));
            } else {
                appender.addExpectation(new MockLogAppender.UnseenEventExpectation("connecting to " + targetNode, "org.elasticsearch.cluster.NodeConnectionsService", Level.DEBUG, "connecting to " + targetNode));
                appender.addExpectation(new MockLogAppender.UnseenEventExpectation("connected to " + targetNode, "org.elasticsearch.cluster.NodeConnectionsService", Level.DEBUG, "connected to " + targetNode));
            }
            if (newTargetNodes.get(targetNode.getId()) == null) {
                appender.addExpectation(new MockLogAppender.SeenEventExpectation("disconnected from " + targetNode, "org.elasticsearch.cluster.NodeConnectionsService", Level.DEBUG, "disconnected from " + targetNode));
            }
        }
        for (DiscoveryNode targetNode : newTargetNodes) {
            appender.addExpectation(new MockLogAppender.UnseenEventExpectation("disconnected from " + targetNode, "org.elasticsearch.cluster.NodeConnectionsService", Level.DEBUG, "disconnected from " + targetNode));
            if (targetNodes.get(targetNode.getId()) == null) {
                appender.addExpectation(new MockLogAppender.SeenEventExpectation("connecting to " + targetNode, "org.elasticsearch.cluster.NodeConnectionsService", Level.DEBUG, "connecting to " + targetNode));
                appender.addExpectation(new MockLogAppender.SeenEventExpectation("connected to " + targetNode, "org.elasticsearch.cluster.NodeConnectionsService", Level.DEBUG, "connected to " + targetNode));
            }
        }
        service.disconnectFromNodesExcept(newTargetNodes);
        service.connectToNodes(newTargetNodes, () -> {
        });
        deterministicTaskQueue.runAllRunnableTasks();
        appender.assertAllExpectationsMatched();
    } finally {
        Loggers.removeAppender(LogManager.getLogger("org.elasticsearch.cluster.NodeConnectionsService"), appender);
        appender.stop();
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockLogAppender(org.elasticsearch.test.MockLogAppender) DeterministicTaskQueue(org.elasticsearch.cluster.coordination.DeterministicTaskQueue) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) HashSet(java.util.HashSet) TestLogging(org.elasticsearch.test.junit.annotations.TestLogging)

Example 4 with DeterministicTaskQueue

use of org.elasticsearch.cluster.coordination.DeterministicTaskQueue in project crate by crate.

the class PeerFinderTests method setup.

@Before
public void setup() {
    capturingTransport = new CapturingTransport();
    transportAddressConnector = new MockTransportAddressConnector();
    providedAddresses = new ArrayList<>();
    addressResolveDelay = 0L;
    final Settings settings = Settings.builder().put(NODE_NAME_SETTING.getKey(), "node").build();
    deterministicTaskQueue = new DeterministicTaskQueue(settings, random());
    localNode = newDiscoveryNode("local-node");
    ConnectionManager innerConnectionManager = new ConnectionManager(settings, capturingTransport);
    StubbableConnectionManager connectionManager = new StubbableConnectionManager(innerConnectionManager, settings, capturingTransport, deterministicTaskQueue.getThreadPool());
    connectionManager.setDefaultNodeConnectedBehavior(cm -> {
        assertTrue(Sets.haveEmptyIntersection(connectedNodes, disconnectedNodes));
        return connectedNodes;
    });
    connectionManager.setDefaultGetConnectionBehavior((cm, discoveryNode) -> capturingTransport.createConnection(discoveryNode));
    transportService = new TransportService(settings, capturingTransport, deterministicTaskQueue.getThreadPool(), boundTransportAddress -> localNode, null, connectionManager);
    transportService.start();
    transportService.acceptIncomingRequests();
    lastAcceptedNodes = DiscoveryNodes.builder().localNodeId(localNode.getId()).add(localNode).build();
    peerFinder = new TestPeerFinder(settings, transportService, transportAddressConnector);
    foundPeersFromNotification = emptyList();
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) Arrays(java.util.Arrays) Builder(org.elasticsearch.cluster.node.DiscoveryNodes.Builder) Matchers.emptyArray(org.hamcrest.Matchers.emptyArray) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) Collections.singletonList(java.util.Collections.singletonList) Sets(io.crate.common.collections.Sets) Settings(org.elasticsearch.common.settings.Settings) After(org.junit.After) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) DeterministicTaskQueue(org.elasticsearch.cluster.coordination.DeterministicTaskQueue) Collections.emptyList(java.util.Collections.emptyList) Set(java.util.Set) ConnectionManager(org.elasticsearch.transport.ConnectionManager) Collectors(java.util.stream.Collectors) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) Version(org.elasticsearch.Version) Stream(java.util.stream.Stream) TransportAddress(org.elasticsearch.common.transport.TransportAddress) NODE_NAME_SETTING(org.elasticsearch.node.Node.NODE_NAME_SETTING) Matchers.contains(org.hamcrest.Matchers.contains) PeersResponse(org.elasticsearch.cluster.coordination.PeersResponse) Matchers.equalTo(org.hamcrest.Matchers.equalTo) TransportResponseHandler(org.elasticsearch.transport.TransportResponseHandler) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) REQUEST_PEERS_ACTION_NAME(org.elasticsearch.discovery.PeerFinder.REQUEST_PEERS_ACTION_NAME) TransportException(org.elasticsearch.transport.TransportException) CapturedRequest(org.elasticsearch.test.transport.CapturingTransport.CapturedRequest) TransportAddressConnector(org.elasticsearch.discovery.PeerFinder.TransportAddressConnector) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Names(org.elasticsearch.threadpool.ThreadPool.Names) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) OptionalLong(java.util.OptionalLong) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) StreamSupport(java.util.stream.StreamSupport) ESTestCase(org.elasticsearch.test.ESTestCase) TransportService(org.elasticsearch.transport.TransportService) Before(org.junit.Before) StubbableConnectionManager(org.elasticsearch.test.transport.StubbableConnectionManager) Collections.emptyMap(java.util.Collections.emptyMap) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) Matchers.empty(org.hamcrest.Matchers.empty) Collections.emptySet(java.util.Collections.emptySet) IOException(java.io.IOException) Consumer(java.util.function.Consumer) StreamInput(org.elasticsearch.common.io.stream.StreamInput) Collections(java.util.Collections) ActionListener(org.elasticsearch.action.ActionListener) ConnectionManager(org.elasticsearch.transport.ConnectionManager) StubbableConnectionManager(org.elasticsearch.test.transport.StubbableConnectionManager) StubbableConnectionManager(org.elasticsearch.test.transport.StubbableConnectionManager) DeterministicTaskQueue(org.elasticsearch.cluster.coordination.DeterministicTaskQueue) TransportService(org.elasticsearch.transport.TransportService) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) Settings(org.elasticsearch.common.settings.Settings) Before(org.junit.Before)

Aggregations

DeterministicTaskQueue (org.elasticsearch.cluster.coordination.DeterministicTaskQueue)4 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)3 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)3 Settings (org.elasticsearch.common.settings.Settings)3 HashSet (java.util.HashSet)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Sets (io.crate.common.collections.Sets)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Collections.emptySet (java.util.Collections.emptySet)1 Collections.singletonList (java.util.Collections.singletonList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 OptionalLong (java.util.OptionalLong)1