Search in sources :

Example 11 with ClusterChangedEvent

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

the class ScriptContextTests method makeScriptService.

ScriptService makeScriptService() throws Exception {
    Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).put(ScriptService.SCRIPT_AUTO_RELOAD_ENABLED_SETTING.getKey(), "false").put("script." + PLUGIN_NAME + "_custom_globally_disabled_op", "false").put("script.engine." + MockScriptEngine.NAME + ".inline." + PLUGIN_NAME + "_custom_exp_disabled_op", "false").build();
    MockScriptEngine scriptEngine = new MockScriptEngine(MockScriptEngine.NAME, Collections.singletonMap("1", script -> "1"));
    ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections.singletonList(scriptEngine));
    List<ScriptContext.Plugin> customContexts = Arrays.asList(new ScriptContext.Plugin(PLUGIN_NAME, "custom_op"), new ScriptContext.Plugin(PLUGIN_NAME, "custom_exp_disabled_op"), new ScriptContext.Plugin(PLUGIN_NAME, "custom_globally_disabled_op"));
    ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(customContexts);
    ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
    ScriptService scriptService = new ScriptService(settings, new Environment(settings), null, scriptEngineRegistry, scriptContextRegistry, scriptSettings);
    ClusterState empty = ClusterState.builder(new ClusterName("_name")).build();
    ScriptMetaData smd = empty.metaData().custom(ScriptMetaData.TYPE);
    smd = ScriptMetaData.putStoredScript(smd, "1", new StoredScriptSource(MockScriptEngine.NAME, "1", Collections.emptyMap()));
    MetaData.Builder mdb = MetaData.builder(empty.getMetaData()).putCustom(ScriptMetaData.TYPE, smd);
    ClusterState stored = ClusterState.builder(empty).metaData(mdb).build();
    scriptService.clusterChanged(new ClusterChangedEvent("test", stored, empty));
    return scriptService;
}
Also used : MetaData(org.elasticsearch.cluster.metadata.MetaData) ClusterState(org.elasticsearch.cluster.ClusterState) Arrays(java.util.Arrays) List(java.util.List) Settings(org.elasticsearch.common.settings.Settings) Environment(org.elasticsearch.env.Environment) ClusterName(org.elasticsearch.cluster.ClusterName) ESTestCase(org.elasticsearch.test.ESTestCase) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) MetaData(org.elasticsearch.cluster.metadata.MetaData) Environment(org.elasticsearch.env.Environment) ClusterName(org.elasticsearch.cluster.ClusterName) Settings(org.elasticsearch.common.settings.Settings)

Example 12 with ClusterChangedEvent

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

the class ClusterService method publishAndApplyChanges.

private void publishAndApplyChanges(TaskInputs taskInputs, TaskOutputs taskOutputs) {
    ClusterState previousClusterState = taskOutputs.previousClusterState;
    ClusterState newClusterState = taskOutputs.newClusterState;
    ClusterChangedEvent clusterChangedEvent = new ClusterChangedEvent(taskInputs.summary, newClusterState, previousClusterState);
    // new cluster state, notify all listeners
    final DiscoveryNodes.Delta nodesDelta = clusterChangedEvent.nodesDelta();
    if (nodesDelta.hasChanges() && logger.isInfoEnabled()) {
        String summary = nodesDelta.shortSummary();
        if (summary.length() > 0) {
            logger.info("{}, reason: {}", summary, taskInputs.summary);
        }
    }
    final Discovery.AckListener ackListener = newClusterState.nodes().isLocalNodeElectedMaster() ? taskOutputs.createAckListener(threadPool, newClusterState) : null;
    nodeConnectionsService.connectToNodes(newClusterState.nodes());
    // we don't want to notify
    if (newClusterState.nodes().isLocalNodeElectedMaster()) {
        logger.debug("publishing cluster state version [{}]", newClusterState.version());
        try {
            clusterStatePublisher.accept(clusterChangedEvent, ackListener);
        } catch (Discovery.FailedToCommitClusterStateException t) {
            final long version = newClusterState.version();
            logger.warn((Supplier<?>) () -> new ParameterizedMessage("failing [{}]: failed to commit cluster state version [{}]", taskInputs.summary, version), t);
            // ensure that list of connected nodes in NodeConnectionsService is in-sync with the nodes of the current cluster state
            nodeConnectionsService.connectToNodes(previousClusterState.nodes());
            nodeConnectionsService.disconnectFromNodesExcept(previousClusterState.nodes());
            taskOutputs.publishingFailed(t);
            return;
        }
    }
    logger.debug("applying cluster state version {}", newClusterState.version());
    try {
        // nothing to do until we actually recover from the gateway or any other block indicates we need to disable persistency
        if (clusterChangedEvent.state().blocks().disableStatePersistence() == false && clusterChangedEvent.metaDataChanged()) {
            final Settings incomingSettings = clusterChangedEvent.state().metaData().settings();
            clusterSettings.applySettings(incomingSettings);
        }
    } catch (Exception ex) {
        logger.warn("failed to apply cluster settings", ex);
    }
    logger.debug("set local cluster state to version {}", newClusterState.version());
    callClusterStateAppliers(newClusterState, clusterChangedEvent);
    nodeConnectionsService.disconnectFromNodesExcept(newClusterState.nodes());
    updateState(css -> newClusterState);
    Stream.concat(clusterStateListeners.stream(), timeoutClusterStateListeners.stream()).forEach(listener -> {
        try {
            logger.trace("calling [{}] with change to version [{}]", listener, newClusterState.version());
            listener.clusterChanged(clusterChangedEvent);
        } catch (Exception ex) {
            logger.warn("failed to notify ClusterStateListener", ex);
        }
    });
    //manual ack only from the master at the end of the publish
    if (newClusterState.nodes().isLocalNodeElectedMaster()) {
        try {
            ackListener.onNodeAck(newClusterState.nodes().getLocalNode(), null);
        } catch (Exception e) {
            final DiscoveryNode localNode = newClusterState.nodes().getLocalNode();
            logger.debug((Supplier<?>) () -> new ParameterizedMessage("error while processing ack for master node [{}]", localNode), e);
        }
    }
    taskOutputs.processedDifferentClusterState(previousClusterState, newClusterState);
    if (newClusterState.nodes().isLocalNodeElectedMaster()) {
        try {
            taskOutputs.clusterStatePublished(clusterChangedEvent);
        } catch (Exception e) {
            logger.error((Supplier<?>) () -> new ParameterizedMessage("exception thrown while notifying executor of new cluster state publication [{}]", taskInputs.summary), e);
        }
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Discovery(org.elasticsearch.discovery.Discovery) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) ProcessClusterEventTimeoutException(org.elasticsearch.cluster.metadata.ProcessClusterEventTimeoutException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) Settings(org.elasticsearch.common.settings.Settings) DiscoverySettings(org.elasticsearch.discovery.DiscoverySettings) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings)

Example 13 with ClusterChangedEvent

use of org.elasticsearch.cluster.ClusterChangedEvent in project crate by crate.

the class DocTableInfo method getRouting.

@Override
public Routing getRouting(final WhereClause whereClause, @Nullable final String preference) {
    Routing routing = getRouting(clusterService.state(), whereClause, preference, new ArrayList<ShardId>(0));
    if (routing != null)
        return routing;
    ClusterStateObserver observer = new ClusterStateObserver(clusterService, routingFetchTimeout, logger);
    final SettableFuture<Routing> routingSettableFuture = SettableFuture.create();
    observer.waitForNextChange(new FetchRoutingListener(routingSettableFuture, whereClause, preference), new ClusterStateObserver.ChangePredicate() {

        @Override
        public boolean apply(ClusterState previousState, ClusterState.ClusterStateStatus previousStatus, ClusterState newState, ClusterState.ClusterStateStatus newStatus) {
            return validate(newState);
        }

        @Override
        public boolean apply(ClusterChangedEvent changedEvent) {
            return validate(changedEvent.state());
        }

        private boolean validate(ClusterState state) {
            final Map<String, Map<String, List<Integer>>> locations = new TreeMap<>();
            GroupShardsIterator shardIterators;
            try {
                shardIterators = getShardIterators(whereClause, preference, state);
            } catch (IndexNotFoundException e) {
                return true;
            }
            final List<ShardId> missingShards = new ArrayList<>(0);
            fillLocationsFromShardIterators(locations, shardIterators, missingShards);
            return missingShards.isEmpty();
        }
    });
    try {
        return routingSettableFuture.get();
    } catch (ExecutionException e) {
        throw Throwables.propagate(e.getCause());
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) UnavailableShardsException(io.crate.exceptions.UnavailableShardsException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ColumnUnknownException(io.crate.exceptions.ColumnUnknownException) ShardId(org.elasticsearch.index.shard.ShardId) GroupShardsIterator(org.elasticsearch.cluster.routing.GroupShardsIterator) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 14 with ClusterChangedEvent

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

the class TransportRestoreSnapshotAction method masterOperation.

@Override
protected void masterOperation(final RestoreSnapshotRequest request, final ClusterState state, final ActionListener<RestoreSnapshotResponse> listener) {
    RestoreService.RestoreRequest restoreRequest = new RestoreService.RestoreRequest(request.repository(), request.snapshot(), request.indices(), request.indicesOptions(), request.renamePattern(), request.renameReplacement(), request.settings(), request.masterNodeTimeout(), request.includeGlobalState(), request.partial(), request.includeAliases(), request.indexSettings(), request.ignoreIndexSettings(), "restore_snapshot[" + request.snapshot() + "]");
    restoreService.restoreSnapshot(restoreRequest, new ActionListener<RestoreCompletionResponse>() {

        @Override
        public void onResponse(RestoreCompletionResponse restoreCompletionResponse) {
            if (restoreCompletionResponse.getRestoreInfo() == null && request.waitForCompletion()) {
                final Snapshot snapshot = restoreCompletionResponse.getSnapshot();
                ClusterStateListener clusterStateListener = new ClusterStateListener() {

                    @Override
                    public void clusterChanged(ClusterChangedEvent changedEvent) {
                        final RestoreInProgress.Entry prevEntry = restoreInProgress(changedEvent.previousState(), snapshot);
                        final RestoreInProgress.Entry newEntry = restoreInProgress(changedEvent.state(), snapshot);
                        if (prevEntry == null) {
                            // When there is a master failure after a restore has been started, this listener might not be registered
                            // on the current master and as such it might miss some intermediary cluster states due to batching.
                            // Clean up listener in that case and acknowledge completion of restore operation to client.
                            clusterService.removeListener(this);
                            listener.onResponse(new RestoreSnapshotResponse(null));
                        } else if (newEntry == null) {
                            clusterService.removeListener(this);
                            ImmutableOpenMap<ShardId, RestoreInProgress.ShardRestoreStatus> shards = prevEntry.shards();
                            assert prevEntry.state().completed() : "expected completed snapshot state but was " + prevEntry.state();
                            assert RestoreService.completed(shards) : "expected all restore entries to be completed";
                            RestoreInfo ri = new RestoreInfo(prevEntry.snapshot().getSnapshotId().getName(), prevEntry.indices(), shards.size(), shards.size() - RestoreService.failedShards(shards));
                            RestoreSnapshotResponse response = new RestoreSnapshotResponse(ri);
                            logger.debug("restore of [{}] completed", snapshot);
                            listener.onResponse(response);
                        } else {
                        // restore not completed yet, wait for next cluster state update
                        }
                    }
                };
                clusterService.addListener(clusterStateListener);
            } else {
                listener.onResponse(new RestoreSnapshotResponse(restoreCompletionResponse.getRestoreInfo()));
            }
        }

        @Override
        public void onFailure(Exception t) {
            listener.onFailure(t);
        }
    });
}
Also used : RestoreService(org.elasticsearch.snapshots.RestoreService) RestoreCompletionResponse(org.elasticsearch.snapshots.RestoreService.RestoreCompletionResponse) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) ClusterStateListener(org.elasticsearch.cluster.ClusterStateListener) ShardId(org.elasticsearch.index.shard.ShardId) Snapshot(org.elasticsearch.snapshots.Snapshot) RestoreInfo(org.elasticsearch.snapshots.RestoreInfo)

Example 15 with ClusterChangedEvent

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

the class GatewayMetaStateTests method generateCloseEvent.

ClusterChangedEvent generateCloseEvent(boolean masterEligible) {
    //ridiculous settings to make sure we don't run into uninitialized because fo default
    AllocationService strategy = createAllocationService(Settings.builder().put("cluster.routing.allocation.node_concurrent_recoveries", 100).put(ClusterRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ALLOW_REBALANCE_SETTING.getKey(), "always").put("cluster.routing.allocation.cluster_concurrent_rebalance", 100).put("cluster.routing.allocation.node_initial_primaries_recoveries", 100).build());
    ClusterState newClusterState, previousClusterState;
    MetaData metaDataIndexCreated = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(5).numberOfReplicas(2)).build();
    RoutingTable routingTableIndexCreated = RoutingTable.builder().addAsNew(metaDataIndexCreated.index("test")).build();
    // assign all shards
    ClusterState init = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaDataIndexCreated).routingTable(routingTableIndexCreated).nodes(generateDiscoveryNodes(masterEligible)).build();
    RoutingTable routingTableInitializing = strategy.reroute(init, "reroute").routingTable();
    ClusterState temp = ClusterState.builder(init).routingTable(routingTableInitializing).build();
    RoutingTable routingTableStarted = strategy.applyStartedShards(temp, temp.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable();
    // create new meta data either with version changed or not
    MetaData metaDataStarted = MetaData.builder().put(init.metaData().index("test"), true).build();
    // create the cluster states with meta data and routing tables as computed before
    MetaData metaDataClosed = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).state(IndexMetaData.State.CLOSE).numberOfShards(5).numberOfReplicas(2)).version(metaDataStarted.version() + 1).build();
    previousClusterState = ClusterState.builder(init).metaData(metaDataStarted).routingTable(routingTableStarted).nodes(generateDiscoveryNodes(masterEligible)).build();
    newClusterState = ClusterState.builder(previousClusterState).routingTable(routingTableIndexCreated).metaData(metaDataClosed).version(previousClusterState.getVersion() + 1).build();
    ClusterChangedEvent event = new ClusterChangedEvent("test", newClusterState, previousClusterState);
    assertThat(event.state().version(), equalTo(event.previousState().version() + 1));
    return event;
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) MetaData(org.elasticsearch.cluster.metadata.MetaData) TestCustomMetaData(org.elasticsearch.test.TestCustomMetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService)

Aggregations

ClusterChangedEvent (org.elasticsearch.cluster.ClusterChangedEvent)27 ClusterState (org.elasticsearch.cluster.ClusterState)20 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)8 MetaData (org.elasticsearch.cluster.metadata.MetaData)8 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)7 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)7 Settings (org.elasticsearch.common.settings.Settings)7 Discovery (org.elasticsearch.discovery.Discovery)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 ClusterStateListener (org.elasticsearch.cluster.ClusterStateListener)4 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)4 TimeValue (org.elasticsearch.common.unit.TimeValue)4 DiscoverySettings (org.elasticsearch.discovery.DiscoverySettings)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)3 ClusterStateUpdateTask (org.elasticsearch.cluster.ClusterStateUpdateTask)3 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)3 AllocationService (org.elasticsearch.cluster.routing.allocation.AllocationService)3 ShardId (org.elasticsearch.index.shard.ShardId)3