Search in sources :

Example 11 with ClusterSettings

use of org.elasticsearch.common.settings.ClusterSettings in project elasticsearch by elastic.

the class Gateway method performStateRecovery.

public void performStateRecovery(final GatewayStateRecoveredListener listener) throws GatewayException {
    String[] nodesIds = clusterService.state().nodes().getMasterNodes().keys().toArray(String.class);
    logger.trace("performing state recovery from {}", Arrays.toString(nodesIds));
    TransportNodesListGatewayMetaState.NodesGatewayMetaState nodesState = listGatewayMetaState.list(nodesIds, null).actionGet();
    int requiredAllocation = Math.max(1, minimumMasterNodesProvider.get());
    if (nodesState.hasFailures()) {
        for (FailedNodeException failedNodeException : nodesState.failures()) {
            logger.warn("failed to fetch state from node", failedNodeException);
        }
    }
    ObjectFloatHashMap<Index> indices = new ObjectFloatHashMap<>();
    MetaData electedGlobalState = null;
    int found = 0;
    for (TransportNodesListGatewayMetaState.NodeGatewayMetaState nodeState : nodesState.getNodes()) {
        if (nodeState.metaData() == null) {
            continue;
        }
        found++;
        if (electedGlobalState == null) {
            electedGlobalState = nodeState.metaData();
        } else if (nodeState.metaData().version() > electedGlobalState.version()) {
            electedGlobalState = nodeState.metaData();
        }
        for (ObjectCursor<IndexMetaData> cursor : nodeState.metaData().indices().values()) {
            indices.addTo(cursor.value.getIndex(), 1);
        }
    }
    if (found < requiredAllocation) {
        listener.onFailure("found [" + found + "] metadata states, required [" + requiredAllocation + "]");
        return;
    }
    // update the global state, and clean the indices, we elect them in the next phase
    MetaData.Builder metaDataBuilder = MetaData.builder(electedGlobalState).removeAllIndices();
    assert !indices.containsKey(null);
    final Object[] keys = indices.keys;
    for (int i = 0; i < keys.length; i++) {
        if (keys[i] != null) {
            Index index = (Index) keys[i];
            IndexMetaData electedIndexMetaData = null;
            int indexMetaDataCount = 0;
            for (TransportNodesListGatewayMetaState.NodeGatewayMetaState nodeState : nodesState.getNodes()) {
                if (nodeState.metaData() == null) {
                    continue;
                }
                IndexMetaData indexMetaData = nodeState.metaData().index(index);
                if (indexMetaData == null) {
                    continue;
                }
                if (electedIndexMetaData == null) {
                    electedIndexMetaData = indexMetaData;
                } else if (indexMetaData.getVersion() > electedIndexMetaData.getVersion()) {
                    electedIndexMetaData = indexMetaData;
                }
                indexMetaDataCount++;
            }
            if (electedIndexMetaData != null) {
                if (indexMetaDataCount < requiredAllocation) {
                    logger.debug("[{}] found [{}], required [{}], not adding", index, indexMetaDataCount, requiredAllocation);
                }
                // TODO if this logging statement is correct then we are missing an else here
                try {
                    if (electedIndexMetaData.getState() == IndexMetaData.State.OPEN) {
                        // verify that we can actually create this index - if not we recover it as closed with lots of warn logs
                        indicesService.verifyIndexMetadata(electedIndexMetaData, electedIndexMetaData);
                    }
                } catch (Exception e) {
                    final Index electedIndex = electedIndexMetaData.getIndex();
                    logger.warn((org.apache.logging.log4j.util.Supplier<?>) () -> new ParameterizedMessage("recovering index {} failed - recovering as closed", electedIndex), e);
                    electedIndexMetaData = IndexMetaData.builder(electedIndexMetaData).state(IndexMetaData.State.CLOSE).build();
                }
                metaDataBuilder.put(electedIndexMetaData, false);
            }
        }
    }
    final ClusterSettings clusterSettings = clusterService.getClusterSettings();
    metaDataBuilder.persistentSettings(clusterSettings.archiveUnknownOrInvalidSettings(metaDataBuilder.persistentSettings(), e -> logUnknownSetting("persistent", e), (e, ex) -> logInvalidSetting("persistent", e, ex)));
    metaDataBuilder.transientSettings(clusterSettings.archiveUnknownOrInvalidSettings(metaDataBuilder.transientSettings(), e -> logUnknownSetting("transient", e), (e, ex) -> logInvalidSetting("transient", e, ex)));
    ClusterState.Builder builder = ClusterState.builder(clusterService.getClusterName());
    builder.metaData(metaDataBuilder);
    listener.onSuccess(builder.build());
}
Also used : MetaData(org.elasticsearch.cluster.metadata.MetaData) Arrays(java.util.Arrays) FailedNodeException(org.elasticsearch.action.FailedNodeException) AbstractComponent(org.elasticsearch.common.component.AbstractComponent) Discovery(org.elasticsearch.discovery.Discovery) ClusterService(org.elasticsearch.cluster.service.ClusterService) Index(org.elasticsearch.index.Index) ObjectCursor(com.carrotsearch.hppc.cursors.ObjectCursor) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) Supplier(java.util.function.Supplier) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) Settings(org.elasticsearch.common.settings.Settings) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Map(java.util.Map) IndicesService(org.elasticsearch.indices.IndicesService) ObjectFloatHashMap(com.carrotsearch.hppc.ObjectFloatHashMap) ClusterStateApplier(org.elasticsearch.cluster.ClusterStateApplier) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) Index(org.elasticsearch.index.Index) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) FailedNodeException(org.elasticsearch.action.FailedNodeException) Supplier(java.util.function.Supplier) ClusterState(org.elasticsearch.cluster.ClusterState) FailedNodeException(org.elasticsearch.action.FailedNodeException) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ObjectFloatHashMap(com.carrotsearch.hppc.ObjectFloatHashMap)

Example 12 with ClusterSettings

use of org.elasticsearch.common.settings.ClusterSettings in project elasticsearch by elastic.

the class ActionModuleTests method testPluginCantOverwriteBuiltinRestHandler.

public void testPluginCantOverwriteBuiltinRestHandler() throws IOException {
    ActionPlugin dupsMainAction = new ActionPlugin() {

        @Override
        public List<RestHandler> getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver, Supplier<DiscoveryNodes> nodesInCluster) {
            return singletonList(new RestMainAction(settings, restController));
        }
    };
    SettingsModule settings = new SettingsModule(Settings.EMPTY);
    ThreadPool threadPool = new TestThreadPool(getTestName());
    try {
        ActionModule actionModule = new ActionModule(false, settings.getSettings(), new IndexNameExpressionResolver(Settings.EMPTY), settings.getIndexScopedSettings(), settings.getClusterSettings(), settings.getSettingsFilter(), threadPool, singletonList(dupsMainAction), null, null);
        Exception e = expectThrows(IllegalArgumentException.class, () -> actionModule.initRestHandlers(null));
        assertThat(e.getMessage(), startsWith("Path [/] already has a value [" + RestMainAction.class.getName()));
    } finally {
        threadPool.shutdown();
    }
}
Also used : ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) ActionPlugin(org.elasticsearch.plugins.ActionPlugin) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) RestController(org.elasticsearch.rest.RestController) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) IOException(java.io.IOException) SettingsFilter(org.elasticsearch.common.settings.SettingsFilter) RestMainAction(org.elasticsearch.rest.action.RestMainAction) RestHandler(org.elasticsearch.rest.RestHandler) SettingsModule(org.elasticsearch.common.settings.SettingsModule) Supplier(java.util.function.Supplier) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Settings(org.elasticsearch.common.settings.Settings) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings)

Example 13 with ClusterSettings

use of org.elasticsearch.common.settings.ClusterSettings in project elasticsearch by elastic.

the class ActionModuleTests method testPluginCanRegisterRestHandler.

public void testPluginCanRegisterRestHandler() {
    class FakeHandler implements RestHandler {

        FakeHandler(RestController restController) {
            restController.registerHandler(Method.GET, "/_dummy", this);
        }

        @Override
        public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
        }
    }
    ActionPlugin registersFakeHandler = new ActionPlugin() {

        @Override
        public List<RestHandler> getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver, Supplier<DiscoveryNodes> nodesInCluster) {
            return singletonList(new FakeHandler(restController));
        }
    };
    SettingsModule settings = new SettingsModule(Settings.EMPTY);
    ThreadPool threadPool = new TestThreadPool(getTestName());
    try {
        ActionModule actionModule = new ActionModule(false, settings.getSettings(), new IndexNameExpressionResolver(Settings.EMPTY), settings.getIndexScopedSettings(), settings.getClusterSettings(), settings.getSettingsFilter(), threadPool, singletonList(registersFakeHandler), null, null);
        actionModule.initRestHandlers(null);
        // At this point the easiest way to confirm that a handler is loaded is to try to register another one on top of it and to fail
        Exception e = expectThrows(IllegalArgumentException.class, () -> actionModule.getRestController().registerHandler(Method.GET, "/_dummy", null));
        assertThat(e.getMessage(), startsWith("Path [/_dummy] already has a value [" + FakeHandler.class.getName()));
    } finally {
        threadPool.shutdown();
    }
}
Also used : NodeClient(org.elasticsearch.client.node.NodeClient) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) ActionPlugin(org.elasticsearch.plugins.ActionPlugin) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) RestController(org.elasticsearch.rest.RestController) RestChannel(org.elasticsearch.rest.RestChannel) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) IOException(java.io.IOException) SettingsFilter(org.elasticsearch.common.settings.SettingsFilter) RestRequest(org.elasticsearch.rest.RestRequest) RestHandler(org.elasticsearch.rest.RestHandler) SettingsModule(org.elasticsearch.common.settings.SettingsModule) Supplier(java.util.function.Supplier) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Settings(org.elasticsearch.common.settings.Settings) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings)

Example 14 with ClusterSettings

use of org.elasticsearch.common.settings.ClusterSettings in project elasticsearch by elastic.

the class ClusterModuleTests method testRegisterAllocationDeciderDuplicate.

public void testRegisterAllocationDeciderDuplicate() {
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new ClusterModule(Settings.EMPTY, clusterService, Collections.<ClusterPlugin>singletonList(new ClusterPlugin() {

        @Override
        public Collection<AllocationDecider> createAllocationDeciders(Settings settings, ClusterSettings clusterSettings) {
            return Collections.singletonList(new EnableAllocationDecider(settings, clusterSettings));
        }
    })));
    assertEquals(e.getMessage(), "Cannot specify allocation decider [" + EnableAllocationDecider.class.getName() + "] twice");
}
Also used : ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) ClusterPlugin(org.elasticsearch.plugins.ClusterPlugin) EnableAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider) Collection(java.util.Collection) Settings(org.elasticsearch.common.settings.Settings) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings)

Example 15 with ClusterSettings

use of org.elasticsearch.common.settings.ClusterSettings in project elasticsearch by elastic.

the class ClusterModuleTests method testAllocationDeciderOrder.

// makes sure that the allocation deciders are setup in the correct order, such that the
// slower allocation deciders come last and we can exit early if there is a NO decision without
// running them. If the order of the deciders is changed for a valid reason, the order should be
// changed in the test too.
public void testAllocationDeciderOrder() {
    List<Class<? extends AllocationDecider>> expectedDeciders = Arrays.asList(MaxRetryAllocationDecider.class, ReplicaAfterPrimaryActiveAllocationDecider.class, RebalanceOnlyWhenActiveAllocationDecider.class, ClusterRebalanceAllocationDecider.class, ConcurrentRebalanceAllocationDecider.class, EnableAllocationDecider.class, NodeVersionAllocationDecider.class, SnapshotInProgressAllocationDecider.class, FilterAllocationDecider.class, SameShardAllocationDecider.class, DiskThresholdDecider.class, ThrottlingAllocationDecider.class, ShardsLimitAllocationDecider.class, AwarenessAllocationDecider.class);
    Collection<AllocationDecider> deciders = ClusterModule.createAllocationDeciders(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), Collections.emptyList());
    Iterator<AllocationDecider> iter = deciders.iterator();
    int idx = 0;
    while (iter.hasNext()) {
        AllocationDecider decider = iter.next();
        assertSame(decider.getClass(), expectedDeciders.get(idx++));
    }
}
Also used : ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) NodeVersionAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.NodeVersionAllocationDecider) FilterAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.FilterAllocationDecider) ThrottlingAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ThrottlingAllocationDecider) AwarenessAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.AwarenessAllocationDecider) ReplicaAfterPrimaryActiveAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ReplicaAfterPrimaryActiveAllocationDecider) SameShardAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.SameShardAllocationDecider) MaxRetryAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider) ConcurrentRebalanceAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ConcurrentRebalanceAllocationDecider) SnapshotInProgressAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.SnapshotInProgressAllocationDecider) ClusterRebalanceAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ClusterRebalanceAllocationDecider) EnableAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider) ShardsLimitAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ShardsLimitAllocationDecider) RebalanceOnlyWhenActiveAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.RebalanceOnlyWhenActiveAllocationDecider) AllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.AllocationDecider)

Aggregations

ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)109 Settings (org.elasticsearch.common.settings.Settings)58 ClusterState (org.elasticsearch.cluster.ClusterState)50 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)30 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)25 Matchers.containsString (org.hamcrest.Matchers.containsString)25 Test (org.junit.Test)25 MetaData (org.elasticsearch.cluster.metadata.MetaData)21 BalancedShardsAllocator (org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator)21 AllocationService (org.elasticsearch.cluster.routing.allocation.AllocationService)20 ClusterInfo (org.elasticsearch.cluster.ClusterInfo)18 DiskUsage (org.elasticsearch.cluster.DiskUsage)18 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)18 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)18 TestGatewayAllocator (org.elasticsearch.test.gateway.TestGatewayAllocator)18 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)17 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)15 RoutingNode (org.elasticsearch.cluster.routing.RoutingNode)14 HashSet (java.util.HashSet)13 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)13