Search in sources :

Example 1 with LeaderCheckRequest

use of org.opensearch.cluster.coordination.LeaderChecker.LeaderCheckRequest in project OpenSearch by opensearch-project.

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();
    AtomicReference<StatusInfo> nodeHealthServiceStatus = new AtomicReference<>(new StatusInfo(UNHEALTHY, "unhealthy-info"));
    final TransportService transportService = capturingTransport.createTransportService(settings, deterministicTaskQueue.getThreadPool(), NOOP_TRANSPORT_INTERCEPTOR, boundTransportAddress -> localNode, null, emptySet());
    transportService.start();
    transportService.acceptIncomingRequests();
    final LeaderChecker leaderChecker = new LeaderChecker(settings, transportService, e -> fail("shouldn't be checking anything"), () -> nodeHealthServiceStatus.get());
    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(NodeHealthCheckFailureException.class));
        NodeHealthCheckFailureException cause = (NodeHealthCheckFailureException) handler.transportException.getRootCause();
        assertThat(cause.getMessage(), equalTo("rejecting leader check from [" + otherNode + "] since node is unhealthy [unhealthy-info]"));
    }
    nodeHealthServiceStatus.getAndSet(new StatusInfo(HEALTHY, "healthy-info"));
    {
        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("rejecting leader check since [" + otherNode + "] has been removed from the cluster"));
    }
    {
        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("rejecting leader check from [" + otherNode + "] sent to a node that is no longer the cluster-manager"));
    }
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) CapturingTransport(org.opensearch.test.transport.CapturingTransport) AtomicReference(java.util.concurrent.atomic.AtomicReference) LeaderCheckRequest(org.opensearch.cluster.coordination.LeaderChecker.LeaderCheckRequest) StatusInfo(org.opensearch.monitor.StatusInfo) TransportService(org.opensearch.transport.TransportService) Settings(org.opensearch.common.settings.Settings) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

Example 2 with LeaderCheckRequest

use of org.opensearch.cluster.coordination.LeaderChecker.LeaderCheckRequest in project OpenSearch by opensearch-project.

the class LeaderCheckerTests method testLeaderCheckRequestEqualsHashcodeSerialization.

public void testLeaderCheckRequestEqualsHashcodeSerialization() {
    LeaderCheckRequest request = new LeaderCheckRequest(new DiscoveryNode(randomAlphaOfLength(10), buildNewFakeTransportAddress(), Version.CURRENT));
    // noinspection RedundantCast since it is needed for some IDEs (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 : LeaderCheckRequest(org.opensearch.cluster.coordination.LeaderChecker.LeaderCheckRequest) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) LEADER_CHECK_ACTION_NAME(org.opensearch.cluster.coordination.LeaderChecker.LEADER_CHECK_ACTION_NAME) HANDSHAKE_ACTION_NAME(org.opensearch.transport.TransportService.HANDSHAKE_ACTION_NAME) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Version(org.opensearch.Version) OpenSearchException(org.opensearch.OpenSearchException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.matchesRegex(org.hamcrest.Matchers.matchesRegex) LEADER_CHECK_TIMEOUT_SETTING(org.opensearch.cluster.coordination.LeaderChecker.LEADER_CHECK_TIMEOUT_SETTING) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MockTransport(org.opensearch.test.transport.MockTransport) Matchers.nullValue(org.hamcrest.Matchers.nullValue) LEADER_CHECK_RETRY_COUNT_SETTING(org.opensearch.cluster.coordination.LeaderChecker.LEADER_CHECK_RETRY_COUNT_SETTING) LEADER_CHECK_INTERVAL_SETTING(org.opensearch.cluster.coordination.LeaderChecker.LEADER_CHECK_INTERVAL_SETTING) EqualsHashCodeTestUtils(org.opensearch.test.EqualsHashCodeTestUtils) UNHEALTHY(org.opensearch.monitor.StatusInfo.Status.UNHEALTHY) StreamInput(org.opensearch.common.io.stream.StreamInput) Empty(org.opensearch.transport.TransportResponse.Empty) TransportRequest(org.opensearch.transport.TransportRequest) Collections.emptySet(java.util.Collections.emptySet) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) TransportResponseHandler(org.opensearch.transport.TransportResponseHandler) HEALTHY(org.opensearch.monitor.StatusInfo.Status.HEALTHY) Settings(org.opensearch.common.settings.Settings) TransportResponse(org.opensearch.transport.TransportResponse) TransportService(org.opensearch.transport.TransportService) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) AtomicLong(java.util.concurrent.atomic.AtomicLong) NODE_NAME_SETTING(org.opensearch.node.Node.NODE_NAME_SETTING) Matchers.equalTo(org.hamcrest.Matchers.equalTo) ClusterName(org.opensearch.cluster.ClusterName) StatusInfo(org.opensearch.monitor.StatusInfo) ConnectTransportException(org.opensearch.transport.ConnectTransportException) CopyFunction(org.opensearch.test.EqualsHashCodeTestUtils.CopyFunction) Matchers.anyOf(org.hamcrest.Matchers.anyOf) CapturingTransport(org.opensearch.test.transport.CapturingTransport) Matchers.endsWith(org.hamcrest.Matchers.endsWith) TransportException(org.opensearch.transport.TransportException) Names(org.opensearch.threadpool.ThreadPool.Names) NOOP_TRANSPORT_INTERCEPTOR(org.opensearch.transport.TransportService.NOOP_TRANSPORT_INTERCEPTOR) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) LeaderCheckRequest(org.opensearch.cluster.coordination.LeaderChecker.LeaderCheckRequest)

Aggregations

AtomicReference (java.util.concurrent.atomic.AtomicReference)2 LeaderCheckRequest (org.opensearch.cluster.coordination.LeaderChecker.LeaderCheckRequest)2 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)2 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)2 Settings (org.opensearch.common.settings.Settings)2 StatusInfo (org.opensearch.monitor.StatusInfo)2 CapturingTransport (org.opensearch.test.transport.CapturingTransport)2 TransportService (org.opensearch.transport.TransportService)2 Collections.emptySet (java.util.Collections.emptySet)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Matchers.anyOf (org.hamcrest.Matchers.anyOf)1 Matchers.endsWith (org.hamcrest.Matchers.endsWith)1 Matchers.equalTo (org.hamcrest.Matchers.equalTo)1 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)1 Matchers.lessThanOrEqualTo (org.hamcrest.Matchers.lessThanOrEqualTo)1 Matchers.matchesRegex (org.hamcrest.Matchers.matchesRegex)1 Matchers.nullValue (org.hamcrest.Matchers.nullValue)1 OpenSearchException (org.opensearch.OpenSearchException)1 Version (org.opensearch.Version)1