Search in sources :

Example 21 with ClusterService

use of org.elasticsearch.cluster.service.ClusterService in project elasticsearch by elastic.

the class DelayedAllocationServiceTests method createDelayedAllocationService.

@Before
public void createDelayedAllocationService() {
    threadPool = new TestThreadPool(getTestName());
    clusterService = mock(ClusterService.class);
    allocationService = createAllocationService(Settings.EMPTY, new DelayedShardsMockGatewayAllocator());
    delayedAllocationService = new TestDelayAllocationService(Settings.EMPTY, threadPool, clusterService, allocationService);
    verify(clusterService).addListener(delayedAllocationService);
}
Also used : ClusterService(org.elasticsearch.cluster.service.ClusterService) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) Before(org.junit.Before)

Example 22 with ClusterService

use of org.elasticsearch.cluster.service.ClusterService in project elasticsearch by elastic.

the class TransportClusterHealthAction method executeHealth.

private void executeHealth(final ClusterHealthRequest request, final ActionListener<ClusterHealthResponse> listener) {
    int waitFor = 5;
    if (request.waitForStatus() == null) {
        waitFor--;
    }
    if (request.waitForNoRelocatingShards() == false) {
        waitFor--;
    }
    if (request.waitForActiveShards().equals(ActiveShardCount.NONE)) {
        waitFor--;
    }
    if (request.waitForNodes().isEmpty()) {
        waitFor--;
    }
    if (request.indices() == null || request.indices().length == 0) {
        // check that they actually exists in the meta data
        waitFor--;
    }
    assert waitFor >= 0;
    final ClusterState state = clusterService.state();
    final ClusterStateObserver observer = new ClusterStateObserver(state, clusterService, null, logger, threadPool.getThreadContext());
    if (request.timeout().millis() == 0) {
        listener.onResponse(getResponse(request, state, waitFor, request.timeout().millis() == 0));
        return;
    }
    final int concreteWaitFor = waitFor;
    final Predicate<ClusterState> validationPredicate = newState -> validateRequest(request, newState, concreteWaitFor);
    final ClusterStateObserver.Listener stateListener = new ClusterStateObserver.Listener() {

        @Override
        public void onNewClusterState(ClusterState clusterState) {
            listener.onResponse(getResponse(request, clusterState, concreteWaitFor, false));
        }

        @Override
        public void onClusterServiceClose() {
            listener.onFailure(new IllegalStateException("ClusterService was close during health call"));
        }

        @Override
        public void onTimeout(TimeValue timeout) {
            final ClusterHealthResponse response = getResponse(request, observer.setAndGetObservedState(), concreteWaitFor, true);
            listener.onResponse(response);
        }
    };
    if (validationPredicate.test(state)) {
        stateListener.onNewClusterState(state);
    } else {
        observer.waitForNextChange(stateListener, validationPredicate, request.timeout());
    }
}
Also used : ClusterService(org.elasticsearch.cluster.service.ClusterService) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Strings(org.elasticsearch.common.Strings) Inject(org.elasticsearch.common.inject.Inject) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) Settings(org.elasticsearch.common.settings.Settings) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) TransportMasterNodeReadAction(org.elasticsearch.action.support.master.TransportMasterNodeReadAction) TimeValue(org.elasticsearch.common.unit.TimeValue) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TransportService(org.elasticsearch.transport.TransportService) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) ActionFilters(org.elasticsearch.action.support.ActionFilters) Predicate(java.util.function.Predicate) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) ActiveShardCount(org.elasticsearch.action.support.ActiveShardCount) Supplier(org.apache.logging.log4j.util.Supplier) LocalClusterUpdateTask(org.elasticsearch.cluster.LocalClusterUpdateTask) ClusterHealthStatus(org.elasticsearch.cluster.health.ClusterHealthStatus) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Task(org.elasticsearch.tasks.Task) ActionListener(org.elasticsearch.action.ActionListener) GatewayAllocator(org.elasticsearch.gateway.GatewayAllocator) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) ActionListener(org.elasticsearch.action.ActionListener) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 23 with ClusterService

use of org.elasticsearch.cluster.service.ClusterService in project elasticsearch by elastic.

the class MainActionTests method testMainActionClusterAvailable.

public void testMainActionClusterAvailable() {
    final ClusterService clusterService = mock(ClusterService.class);
    final ClusterName clusterName = new ClusterName("elasticsearch");
    final Settings settings = Settings.builder().put("node.name", "my-node").build();
    final boolean available = randomBoolean();
    ClusterBlocks blocks;
    if (available) {
        if (randomBoolean()) {
            blocks = ClusterBlocks.EMPTY_CLUSTER_BLOCK;
        } else {
            blocks = ClusterBlocks.builder().addGlobalBlock(new ClusterBlock(randomIntBetween(1, 16), "test global block 400", randomBoolean(), randomBoolean(), RestStatus.BAD_REQUEST, ClusterBlockLevel.ALL)).build();
        }
    } else {
        blocks = ClusterBlocks.builder().addGlobalBlock(new ClusterBlock(randomIntBetween(1, 16), "test global block 503", randomBoolean(), randomBoolean(), RestStatus.SERVICE_UNAVAILABLE, ClusterBlockLevel.ALL)).build();
    }
    ClusterState state = ClusterState.builder(clusterName).blocks(blocks).build();
    when(clusterService.state()).thenReturn(state);
    TransportService transportService = new TransportService(Settings.EMPTY, null, null, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null);
    TransportMainAction action = new TransportMainAction(settings, mock(ThreadPool.class), transportService, mock(ActionFilters.class), mock(IndexNameExpressionResolver.class), clusterService);
    AtomicReference<MainResponse> responseRef = new AtomicReference<>();
    action.doExecute(new MainRequest(), new ActionListener<MainResponse>() {

        @Override
        public void onResponse(MainResponse mainResponse) {
            responseRef.set(mainResponse);
        }

        @Override
        public void onFailure(Exception e) {
            logger.error("unexpected error", e);
        }
    });
    assertNotNull(responseRef.get());
    assertEquals(available, responseRef.get().isAvailable());
    verify(clusterService, times(1)).state();
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) ThreadPool(org.elasticsearch.threadpool.ThreadPool) AtomicReference(java.util.concurrent.atomic.AtomicReference) ActionFilters(org.elasticsearch.action.support.ActionFilters) IOException(java.io.IOException) ClusterBlock(org.elasticsearch.cluster.block.ClusterBlock) ClusterService(org.elasticsearch.cluster.service.ClusterService) TransportService(org.elasticsearch.transport.TransportService) ClusterName(org.elasticsearch.cluster.ClusterName) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Settings(org.elasticsearch.common.settings.Settings)

Example 24 with ClusterService

use of org.elasticsearch.cluster.service.ClusterService in project elasticsearch by elastic.

the class ShardStateActionTests method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    this.transport = new CapturingTransport();
    clusterService = createClusterService(THREAD_POOL);
    transportService = new TransportService(clusterService.getSettings(), transport, THREAD_POOL, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> clusterService.localNode(), null);
    transportService.start();
    transportService.acceptIncomingRequests();
    shardStateAction = new TestShardStateAction(Settings.EMPTY, clusterService, transportService, null, null);
    shardStateAction.setOnBeforeWaitForNewMasterAndRetry(() -> {
    });
    shardStateAction.setOnAfterWaitForNewMasterAndRetry(() -> {
    });
}
Also used : ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) BeforeClass(org.junit.BeforeClass) ClusterServiceUtils.createClusterService(org.elasticsearch.test.ClusterServiceUtils.createClusterService) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) NotMasterException(org.elasticsearch.cluster.NotMasterException) ClusterService(org.elasticsearch.cluster.service.ClusterService) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TransportResponse(org.elasticsearch.transport.TransportResponse) ESTestCase(org.elasticsearch.test.ESTestCase) TransportService(org.elasticsearch.transport.TransportService) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) ClusterStateCreationUtils(org.elasticsearch.action.support.replication.ClusterStateCreationUtils) ShardsIterator(org.elasticsearch.cluster.routing.ShardsIterator) NodeDisconnectedException(org.elasticsearch.transport.NodeDisconnectedException) Before(org.junit.Before) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) AfterClass(org.junit.AfterClass) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) Discovery(org.elasticsearch.discovery.Discovery) Predicate(java.util.function.Predicate) LongConsumer(java.util.function.LongConsumer) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) NodeNotConnectedException(org.elasticsearch.transport.NodeNotConnectedException) RoutingService(org.elasticsearch.cluster.routing.RoutingService) Matchers.is(org.hamcrest.Matchers.is) TransportException(org.elasticsearch.transport.TransportException) ClusterServiceUtils.setState(org.elasticsearch.test.ClusterServiceUtils.setState) TransportService(org.elasticsearch.transport.TransportService) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) Before(org.junit.Before)

Example 25 with ClusterService

use of org.elasticsearch.cluster.service.ClusterService in project elasticsearch by elastic.

the class MetaDataMappingServiceTests method testClusterStateIsNotChangedWithIdenticalMappings.

public void testClusterStateIsNotChangedWithIdenticalMappings() throws Exception {
    createIndex("test", client().admin().indices().prepareCreate("test").addMapping("type"));
    final MetaDataMappingService mappingService = getInstanceFromNode(MetaDataMappingService.class);
    final ClusterService clusterService = getInstanceFromNode(ClusterService.class);
    final PutMappingClusterStateUpdateRequest request = new PutMappingClusterStateUpdateRequest().type("type");
    request.source("{ \"properties\" { \"field\": { \"type\": \"string\" }}}");
    ClusterState result = mappingService.putMappingExecutor.execute(clusterService.state(), Collections.singletonList(request)).resultingState;
    assertFalse(result != clusterService.state());
    ClusterState result2 = mappingService.putMappingExecutor.execute(result, Collections.singletonList(request)).resultingState;
    assertSame(result, result2);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterService(org.elasticsearch.cluster.service.ClusterService) PutMappingClusterStateUpdateRequest(org.elasticsearch.action.admin.indices.mapping.put.PutMappingClusterStateUpdateRequest)

Aggregations

ClusterService (org.elasticsearch.cluster.service.ClusterService)52 ClusterState (org.elasticsearch.cluster.ClusterState)31 Settings (org.elasticsearch.common.settings.Settings)25 ThreadPool (org.elasticsearch.threadpool.ThreadPool)20 TransportService (org.elasticsearch.transport.TransportService)20 TestThreadPool (org.elasticsearch.threadpool.TestThreadPool)17 CountDownLatch (java.util.concurrent.CountDownLatch)15 IOException (java.io.IOException)13 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)13 ActionFilters (org.elasticsearch.action.support.ActionFilters)12 IndexNameExpressionResolver (org.elasticsearch.cluster.metadata.IndexNameExpressionResolver)12 ClusterServiceUtils.createClusterService (org.elasticsearch.test.ClusterServiceUtils.createClusterService)12 Before (org.junit.Before)12 ShardId (org.elasticsearch.index.shard.ShardId)11 ArrayList (java.util.ArrayList)10 HashSet (java.util.HashSet)10 TimeUnit (java.util.concurrent.TimeUnit)10 ExecutionException (java.util.concurrent.ExecutionException)9 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)9 ESTestCase (org.elasticsearch.test.ESTestCase)9