Search in sources :

Example 1 with LeaderCheckRequest

use of org.elasticsearch.cluster.coordination.LeaderChecker.LeaderCheckRequest in project crate by crate.

the class LeaderCheckerTests method testLeaderCheckRequestEqualsHashcodeSerialization.

public void testLeaderCheckRequestEqualsHashcodeSerialization() {
    LeaderCheckRequest request = new LeaderCheckRequest(new DiscoveryNode(randomAlphaOfLength(10), buildNewFakeTransportAddress(), Version.CURRENT));
    // Note: the explicit cast of the CopyFunction is needed for some IDE (specifically Eclipse 4.8.0) to infer the right type
    EqualsHashCodeTestUtils.checkEqualsAndHashCode(request, (CopyFunction<LeaderCheckRequest>) rq -> copyWriteable(rq, writableRegistry(), LeaderCheckRequest::new), rq -> new LeaderCheckRequest(new DiscoveryNode(randomAlphaOfLength(10), buildNewFakeTransportAddress(), Version.CURRENT)));
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) LEADER_CHECK_TIMEOUT_SETTING(org.elasticsearch.cluster.coordination.LeaderChecker.LEADER_CHECK_TIMEOUT_SETTING) Empty(org.elasticsearch.transport.TransportResponse.Empty) HANDSHAKE_ACTION_NAME(org.elasticsearch.transport.TransportService.HANDSHAKE_ACTION_NAME) TransportRequest(org.elasticsearch.transport.TransportRequest) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Names(org.elasticsearch.threadpool.ThreadPool.Names) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) CopyFunction(org.elasticsearch.test.EqualsHashCodeTestUtils.CopyFunction) Matchers.matchesRegex(org.hamcrest.Matchers.matchesRegex) LEADER_CHECK_ACTION_NAME(org.elasticsearch.cluster.coordination.LeaderChecker.LEADER_CHECK_ACTION_NAME) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) Settings(org.elasticsearch.common.settings.Settings) AbstractSimpleTransportTestCase(org.elasticsearch.transport.AbstractSimpleTransportTestCase) Matchers.nullValue(org.hamcrest.Matchers.nullValue) LeaderCheckRequest(org.elasticsearch.cluster.coordination.LeaderChecker.LeaderCheckRequest) TransportResponse(org.elasticsearch.transport.TransportResponse) ClusterName(org.elasticsearch.cluster.ClusterName) ESTestCase(org.elasticsearch.test.ESTestCase) TransportService(org.elasticsearch.transport.TransportService) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) EqualsHashCodeTestUtils(org.elasticsearch.test.EqualsHashCodeTestUtils) LEADER_CHECK_INTERVAL_SETTING(org.elasticsearch.cluster.coordination.LeaderChecker.LEADER_CHECK_INTERVAL_SETTING) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) MockTransport(org.elasticsearch.test.transport.MockTransport) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) AtomicLong(java.util.concurrent.atomic.AtomicLong) Version(org.elasticsearch.Version) NODE_NAME_SETTING(org.elasticsearch.node.Node.NODE_NAME_SETTING) LEADER_CHECK_RETRY_COUNT_SETTING(org.elasticsearch.cluster.coordination.LeaderChecker.LEADER_CHECK_RETRY_COUNT_SETTING) StreamInput(org.elasticsearch.common.io.stream.StreamInput) Matchers.equalTo(org.hamcrest.Matchers.equalTo) TransportResponseHandler(org.elasticsearch.transport.TransportResponseHandler) Matchers.anyOf(org.hamcrest.Matchers.anyOf) TransportException(org.elasticsearch.transport.TransportException) Matchers.endsWith(org.hamcrest.Matchers.endsWith) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) LeaderCheckRequest(org.elasticsearch.cluster.coordination.LeaderChecker.LeaderCheckRequest)

Example 2 with LeaderCheckRequest

use of org.elasticsearch.cluster.coordination.LeaderChecker.LeaderCheckRequest in project crate by crate.

the class LeaderCheckerTests method testLeaderBehaviour.

public void testLeaderBehaviour() {
    final DiscoveryNode localNode = new DiscoveryNode("local-node", buildNewFakeTransportAddress(), Version.CURRENT);
    final DiscoveryNode otherNode = new DiscoveryNode("other-node", buildNewFakeTransportAddress(), Version.CURRENT);
    final Settings settings = Settings.builder().put(NODE_NAME_SETTING.getKey(), localNode.getId()).build();
    final DeterministicTaskQueue deterministicTaskQueue = new DeterministicTaskQueue(settings, random());
    final CapturingTransport capturingTransport = new CapturingTransport();
    final TransportService transportService = capturingTransport.createTransportService(settings, deterministicTaskQueue.getThreadPool(), boundTransportAddress -> localNode, null);
    transportService.start();
    transportService.acceptIncomingRequests();
    final LeaderChecker leaderChecker = new LeaderChecker(settings, transportService, e -> fail("shouldn't be checking anything"));
    final DiscoveryNodes discoveryNodes = DiscoveryNodes.builder().add(localNode).localNodeId(localNode.getId()).masterNodeId(localNode.getId()).build();
    {
        leaderChecker.setCurrentNodes(discoveryNodes);
        final CapturingTransportResponseHandler handler = new CapturingTransportResponseHandler();
        transportService.sendRequest(localNode, LEADER_CHECK_ACTION_NAME, new LeaderCheckRequest(otherNode), handler);
        deterministicTaskQueue.runAllTasks();
        assertFalse(handler.successfulResponseReceived);
        assertThat(handler.transportException.getRootCause(), instanceOf(CoordinationStateRejectedException.class));
        CoordinationStateRejectedException cause = (CoordinationStateRejectedException) handler.transportException.getRootCause();
        assertThat(cause.getMessage(), equalTo("leader check from unknown node"));
    }
    {
        leaderChecker.setCurrentNodes(DiscoveryNodes.builder(discoveryNodes).add(otherNode).build());
        final CapturingTransportResponseHandler handler = new CapturingTransportResponseHandler();
        transportService.sendRequest(localNode, LEADER_CHECK_ACTION_NAME, new LeaderCheckRequest(otherNode), handler);
        deterministicTaskQueue.runAllTasks();
        assertTrue(handler.successfulResponseReceived);
        assertThat(handler.transportException, nullValue());
    }
    {
        leaderChecker.setCurrentNodes(DiscoveryNodes.builder(discoveryNodes).add(otherNode).masterNodeId(null).build());
        final CapturingTransportResponseHandler handler = new CapturingTransportResponseHandler();
        transportService.sendRequest(localNode, LEADER_CHECK_ACTION_NAME, new LeaderCheckRequest(otherNode), handler);
        deterministicTaskQueue.runAllTasks();
        assertFalse(handler.successfulResponseReceived);
        assertThat(handler.transportException.getRootCause(), instanceOf(CoordinationStateRejectedException.class));
        CoordinationStateRejectedException cause = (CoordinationStateRejectedException) handler.transportException.getRootCause();
        assertThat(cause.getMessage(), equalTo("non-leader rejecting leader check"));
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) LeaderCheckRequest(org.elasticsearch.cluster.coordination.LeaderChecker.LeaderCheckRequest) TransportService(org.elasticsearch.transport.TransportService) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) Settings(org.elasticsearch.common.settings.Settings) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Aggregations

LeaderCheckRequest (org.elasticsearch.cluster.coordination.LeaderChecker.LeaderCheckRequest)2 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)2 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)2 Settings (org.elasticsearch.common.settings.Settings)2 CapturingTransport (org.elasticsearch.test.transport.CapturingTransport)2 TransportService (org.elasticsearch.transport.TransportService)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 Version (org.elasticsearch.Version)1 ClusterName (org.elasticsearch.cluster.ClusterName)1 LEADER_CHECK_ACTION_NAME (org.elasticsearch.cluster.coordination.LeaderChecker.LEADER_CHECK_ACTION_NAME)1 LEADER_CHECK_INTERVAL_SETTING (org.elasticsearch.cluster.coordination.LeaderChecker.LEADER_CHECK_INTERVAL_SETTING)1 LEADER_CHECK_RETRY_COUNT_SETTING (org.elasticsearch.cluster.coordination.LeaderChecker.LEADER_CHECK_RETRY_COUNT_SETTING)1 LEADER_CHECK_TIMEOUT_SETTING (org.elasticsearch.cluster.coordination.LeaderChecker.LEADER_CHECK_TIMEOUT_SETTING)1 StreamInput (org.elasticsearch.common.io.stream.StreamInput)1 NODE_NAME_SETTING (org.elasticsearch.node.Node.NODE_NAME_SETTING)1 ESTestCase (org.elasticsearch.test.ESTestCase)1 EqualsHashCodeTestUtils (org.elasticsearch.test.EqualsHashCodeTestUtils)1 CopyFunction (org.elasticsearch.test.EqualsHashCodeTestUtils.CopyFunction)1