Search in sources :

Example 81 with ClusterState

use of org.elasticsearch.cluster.ClusterState in project elasticsearch by elastic.

the class FullRollingRestartIT method testNoRebalanceOnRollingRestart.

public void testNoRebalanceOnRollingRestart() throws Exception {
    // see https://github.com/elastic/elasticsearch/issues/14387
    internalCluster().startMasterOnlyNode(Settings.EMPTY);
    internalCluster().startDataOnlyNodes(3);
    /**
         * We start 3 nodes and a dedicated master. Restart on of the data-nodes and ensure that we got no relocations.
         * Yet we have 6 shards 0 replica so that means if the restarting node comes back both other nodes are subject
         * to relocating to the restarting node since all had 2 shards and now one node has nothing allocated.
         * We have a fix for this to wait until we have allocated unallocated shards now so this shouldn't happen.
         */
    prepareCreate("test").setSettings(Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, "6").put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, "0").put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), TimeValue.timeValueMinutes(1))).get();
    for (int i = 0; i < 100; i++) {
        client().prepareIndex("test", "type1", Long.toString(i)).setSource(MapBuilder.<String, Object>newMapBuilder().put("test", "value" + i).map()).execute().actionGet();
    }
    ensureGreen();
    ClusterState state = client().admin().cluster().prepareState().get().getState();
    RecoveryResponse recoveryResponse = client().admin().indices().prepareRecoveries("test").get();
    for (RecoveryState recoveryState : recoveryResponse.shardRecoveryStates().get("test")) {
        assertTrue("relocated from: " + recoveryState.getSourceNode() + " to: " + recoveryState.getTargetNode() + "\n" + state, recoveryState.getRecoverySource().getType() != RecoverySource.Type.PEER || recoveryState.getPrimary() == false);
    }
    internalCluster().restartRandomDataNode();
    ensureGreen();
    ClusterState afterState = client().admin().cluster().prepareState().get().getState();
    recoveryResponse = client().admin().indices().prepareRecoveries("test").get();
    for (RecoveryState recoveryState : recoveryResponse.shardRecoveryStates().get("test")) {
        assertTrue("relocated from: " + recoveryState.getSourceNode() + " to: " + recoveryState.getTargetNode() + "-- \nbefore: \n" + state, recoveryState.getRecoverySource().getType() != RecoverySource.Type.PEER || recoveryState.getPrimary() == false);
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState) RecoveryResponse(org.elasticsearch.action.admin.indices.recovery.RecoveryResponse)

Example 82 with ClusterState

use of org.elasticsearch.cluster.ClusterState in project elasticsearch by elastic.

the class TransportClusterStateAction method masterOperation.

@Override
protected void masterOperation(final ClusterStateRequest request, final ClusterState state, final ActionListener<ClusterStateResponse> listener) throws IOException {
    ClusterState currentState = clusterService.state();
    logger.trace("Serving cluster state request using version {}", currentState.version());
    ClusterState.Builder builder = ClusterState.builder(currentState.getClusterName());
    builder.version(currentState.version());
    builder.stateUUID(currentState.stateUUID());
    if (request.nodes()) {
        builder.nodes(currentState.nodes());
    }
    if (request.routingTable()) {
        if (request.indices().length > 0) {
            RoutingTable.Builder routingTableBuilder = RoutingTable.builder();
            String[] indices = indexNameExpressionResolver.concreteIndexNames(currentState, request);
            for (String filteredIndex : indices) {
                if (currentState.routingTable().getIndicesRouting().containsKey(filteredIndex)) {
                    routingTableBuilder.add(currentState.routingTable().getIndicesRouting().get(filteredIndex));
                }
            }
            builder.routingTable(routingTableBuilder.build());
        } else {
            builder.routingTable(currentState.routingTable());
        }
    }
    if (request.blocks()) {
        builder.blocks(currentState.blocks());
    }
    if (request.metaData()) {
        MetaData.Builder mdBuilder;
        if (request.indices().length == 0) {
            mdBuilder = MetaData.builder(currentState.metaData());
        } else {
            mdBuilder = MetaData.builder();
        }
        if (request.indices().length > 0) {
            String[] indices = indexNameExpressionResolver.concreteIndexNames(currentState, request);
            for (String filteredIndex : indices) {
                IndexMetaData indexMetaData = currentState.metaData().index(filteredIndex);
                if (indexMetaData != null) {
                    mdBuilder.put(indexMetaData, false);
                }
            }
        }
        // Filter our metadata that shouldn't be returned by API
        for (ObjectObjectCursor<String, Custom> custom : currentState.metaData().customs()) {
            if (!custom.value.context().contains(MetaData.XContentContext.API)) {
                mdBuilder.removeCustom(custom.key);
            }
        }
        builder.metaData(mdBuilder);
    }
    if (request.customs()) {
        builder.customs(currentState.customs());
    }
    listener.onResponse(new ClusterStateResponse(currentState.getClusterName(), builder.build(), serializeFullClusterState(currentState, Version.CURRENT).length()));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) PublishClusterStateAction.serializeFullClusterState(org.elasticsearch.discovery.zen.PublishClusterStateAction.serializeFullClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Custom(org.elasticsearch.cluster.metadata.MetaData.Custom) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 83 with ClusterState

use of org.elasticsearch.cluster.ClusterState 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 84 with ClusterState

use of org.elasticsearch.cluster.ClusterState in project elasticsearch by elastic.

the class SettingsUpdater method updateSettings.

synchronized ClusterState updateSettings(final ClusterState currentState, Settings transientToApply, Settings persistentToApply) {
    boolean changed = false;
    Settings.Builder transientSettings = Settings.builder();
    transientSettings.put(currentState.metaData().transientSettings());
    changed |= clusterSettings.updateDynamicSettings(transientToApply, transientSettings, transientUpdates, "transient");
    Settings.Builder persistentSettings = Settings.builder();
    persistentSettings.put(currentState.metaData().persistentSettings());
    changed |= clusterSettings.updateDynamicSettings(persistentToApply, persistentSettings, persistentUpdates, "persistent");
    if (!changed) {
        return currentState;
    }
    MetaData.Builder metaData = MetaData.builder(currentState.metaData()).persistentSettings(persistentSettings.build()).transientSettings(transientSettings.build());
    ClusterBlocks.Builder blocks = ClusterBlocks.builder().blocks(currentState.blocks());
    boolean updatedReadOnly = MetaData.SETTING_READ_ONLY_SETTING.get(metaData.persistentSettings()) || MetaData.SETTING_READ_ONLY_SETTING.get(metaData.transientSettings());
    if (updatedReadOnly) {
        blocks.addGlobalBlock(MetaData.CLUSTER_READ_ONLY_BLOCK);
    } else {
        blocks.removeGlobalBlock(MetaData.CLUSTER_READ_ONLY_BLOCK);
    }
    ClusterState build = builder(currentState).metaData(metaData).blocks(blocks).build();
    Settings settings = build.metaData().settings();
    // now we try to apply things and if they are invalid we fail
    // this dryRun will validate & parse settings but won't actually apply them.
    clusterSettings.validateUpdate(settings);
    return build;
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) MetaData(org.elasticsearch.cluster.metadata.MetaData) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) Settings(org.elasticsearch.common.settings.Settings)

Example 85 with ClusterState

use of org.elasticsearch.cluster.ClusterState in project elasticsearch by elastic.

the class ClusterService method patchVersionsAndNoMasterBlocks.

private ClusterState patchVersionsAndNoMasterBlocks(ClusterState previousClusterState, ClusterTasksResult<Object> executionResult) {
    ClusterState newClusterState = executionResult.resultingState;
    if (executionResult.noMaster) {
        assert newClusterState == previousClusterState : "state can only be changed by ClusterService when noMaster = true";
        if (previousClusterState.nodes().getMasterNodeId() != null) {
            // remove block if it already exists before adding new one
            assert previousClusterState.blocks().hasGlobalBlock(discoverySettings.getNoMasterBlock().id()) == false : "NO_MASTER_BLOCK should only be added by ClusterService";
            ClusterBlocks clusterBlocks = ClusterBlocks.builder().blocks(previousClusterState.blocks()).addGlobalBlock(discoverySettings.getNoMasterBlock()).build();
            DiscoveryNodes discoveryNodes = new DiscoveryNodes.Builder(previousClusterState.nodes()).masterNodeId(null).build();
            newClusterState = ClusterState.builder(previousClusterState).blocks(clusterBlocks).nodes(discoveryNodes).build();
        }
    } else if (newClusterState.nodes().isLocalNodeElectedMaster() && previousClusterState != newClusterState) {
        // only the master controls the version numbers
        Builder builder = ClusterState.builder(newClusterState).incrementVersion();
        if (previousClusterState.routingTable() != newClusterState.routingTable()) {
            builder.routingTable(RoutingTable.builder(newClusterState.routingTable()).version(newClusterState.routingTable().version() + 1).build());
        }
        if (previousClusterState.metaData() != newClusterState.metaData()) {
            builder.metaData(MetaData.builder(newClusterState.metaData()).version(newClusterState.metaData().version() + 1));
        }
        // remove the no master block, if it exists
        if (newClusterState.blocks().hasGlobalBlock(discoverySettings.getNoMasterBlock().id())) {
            builder.blocks(ClusterBlocks.builder().blocks(newClusterState.blocks()).removeGlobalBlock(discoverySettings.getNoMasterBlock().id()));
        }
        newClusterState = builder.build();
    }
    assert newClusterState.nodes().getMasterNodeId() == null || newClusterState.blocks().hasGlobalBlock(discoverySettings.getNoMasterBlock().id()) == false : "cluster state with master node must not have NO_MASTER_BLOCK";
    return newClusterState;
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) Builder(org.elasticsearch.cluster.ClusterState.Builder) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Aggregations

ClusterState (org.elasticsearch.cluster.ClusterState)564 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)211 MetaData (org.elasticsearch.cluster.metadata.MetaData)179 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)150 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)123 Settings (org.elasticsearch.common.settings.Settings)100 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)85 ClusterName (org.elasticsearch.cluster.ClusterName)82 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)75 Matchers.containsString (org.hamcrest.Matchers.containsString)72 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)62 ShardId (org.elasticsearch.index.shard.ShardId)61 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)59 ArrayList (java.util.ArrayList)57 IOException (java.io.IOException)55 Index (org.elasticsearch.index.Index)53 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)49 CountDownLatch (java.util.concurrent.CountDownLatch)47 HashSet (java.util.HashSet)45 List (java.util.List)45