Search in sources :

Example 31 with ClusterStateUpdateTask

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

the class DelayedAllocationServiceTests method testDelayedUnassignedScheduleRerouteAfterDelayedReroute.

/**
     * This tests that a new delayed reroute is scheduled right after a delayed reroute was run
     */
public void testDelayedUnassignedScheduleRerouteAfterDelayedReroute() throws Exception {
    TimeValue shortDelaySetting = timeValueMillis(100);
    TimeValue longDelaySetting = TimeValue.timeValueSeconds(1);
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder("short_delay").settings(settings(Version.CURRENT).put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), shortDelaySetting)).numberOfShards(1).numberOfReplicas(1)).put(IndexMetaData.builder("long_delay").settings(settings(Version.CURRENT).put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), longDelaySetting)).numberOfShards(1).numberOfReplicas(1)).build();
    ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(RoutingTable.builder().addAsNew(metaData.index("short_delay")).addAsNew(metaData.index("long_delay")).build()).nodes(DiscoveryNodes.builder().add(newNode("node0", singleton(DiscoveryNode.Role.MASTER))).localNodeId("node0").masterNodeId("node0").add(newNode("node1")).add(newNode("node2")).add(newNode("node3")).add(newNode("node4"))).build();
    // allocate shards
    clusterState = allocationService.reroute(clusterState, "reroute");
    // start primaries
    clusterState = allocationService.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    // start replicas
    clusterState = allocationService.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    assertThat("all shards should be started", clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(4));
    // find replica of short_delay
    ShardRouting shortDelayReplica = null;
    for (ShardRouting shardRouting : clusterState.getRoutingTable().allShards("short_delay")) {
        if (shardRouting.primary() == false) {
            shortDelayReplica = shardRouting;
            break;
        }
    }
    assertNotNull(shortDelayReplica);
    // find replica of long_delay
    ShardRouting longDelayReplica = null;
    for (ShardRouting shardRouting : clusterState.getRoutingTable().allShards("long_delay")) {
        if (shardRouting.primary() == false) {
            longDelayReplica = shardRouting;
            break;
        }
    }
    assertNotNull(longDelayReplica);
    final long baseTimestampNanos = System.nanoTime();
    // remove node of shortDelayReplica and node of longDelayReplica and reroute
    ClusterState clusterStateBeforeNodeLeft = clusterState;
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).remove(shortDelayReplica.currentNodeId()).remove(longDelayReplica.currentNodeId())).build();
    // make sure both replicas are marked as delayed (i.e. not reallocated)
    allocationService.setNanoTimeOverride(baseTimestampNanos);
    clusterState = allocationService.deassociateDeadNodes(clusterState, true, "reroute");
    final ClusterState stateWithDelayedShards = clusterState;
    assertEquals(2, UnassignedInfo.getNumberOfDelayedUnassigned(stateWithDelayedShards));
    RoutingNodes.UnassignedShards.UnassignedIterator iter = stateWithDelayedShards.getRoutingNodes().unassigned().iterator();
    assertEquals(baseTimestampNanos, iter.next().unassignedInfo().getUnassignedTimeInNanos());
    assertEquals(baseTimestampNanos, iter.next().unassignedInfo().getUnassignedTimeInNanos());
    // mock ClusterService.submitStateUpdateTask() method
    CountDownLatch latch1 = new CountDownLatch(1);
    AtomicReference<ClusterStateUpdateTask> clusterStateUpdateTask1 = new AtomicReference<>();
    doAnswer(invocationOnMock -> {
        clusterStateUpdateTask1.set((ClusterStateUpdateTask) invocationOnMock.getArguments()[1]);
        latch1.countDown();
        return null;
    }).when(clusterService).submitStateUpdateTask(eq(CLUSTER_UPDATE_TASK_SOURCE), any(ClusterStateUpdateTask.class));
    assertNull(delayedAllocationService.delayedRerouteTask.get());
    long delayUntilClusterChangeEvent = TimeValue.timeValueNanos(randomInt((int) shortDelaySetting.nanos() - 1)).nanos();
    long clusterChangeEventTimestampNanos = baseTimestampNanos + delayUntilClusterChangeEvent;
    delayedAllocationService.setNanoTimeOverride(clusterChangeEventTimestampNanos);
    delayedAllocationService.clusterChanged(new ClusterChangedEvent("fake node left", stateWithDelayedShards, clusterStateBeforeNodeLeft));
    // check that delayed reroute task was created and registered with the proper settings
    DelayedAllocationService.DelayedRerouteTask firstDelayedRerouteTask = delayedAllocationService.delayedRerouteTask.get();
    assertNotNull(firstDelayedRerouteTask);
    assertFalse(firstDelayedRerouteTask.cancelScheduling.get());
    assertThat(firstDelayedRerouteTask.baseTimestampNanos, equalTo(clusterChangeEventTimestampNanos));
    assertThat(firstDelayedRerouteTask.nextDelay.nanos(), equalTo(UnassignedInfo.findNextDelayedAllocation(clusterChangeEventTimestampNanos, stateWithDelayedShards)));
    assertThat(firstDelayedRerouteTask.nextDelay.nanos(), equalTo(shortDelaySetting.nanos() - (clusterChangeEventTimestampNanos - baseTimestampNanos)));
    // check that submitStateUpdateTask() was invoked on the cluster service mock
    assertTrue(latch1.await(30, TimeUnit.SECONDS));
    verify(clusterService).submitStateUpdateTask(eq(CLUSTER_UPDATE_TASK_SOURCE), eq(clusterStateUpdateTask1.get()));
    // advance the time on the allocation service to a timestamp that happened after the delayed scheduling
    long nanoTimeForReroute = clusterChangeEventTimestampNanos + shortDelaySetting.nanos() + timeValueMillis(randomInt(50)).nanos();
    allocationService.setNanoTimeOverride(nanoTimeForReroute);
    // apply cluster state
    ClusterState stateWithOnlyOneDelayedShard = clusterStateUpdateTask1.get().execute(stateWithDelayedShards);
    // check that shard is not delayed anymore
    assertEquals(1, UnassignedInfo.getNumberOfDelayedUnassigned(stateWithOnlyOneDelayedShard));
    // check that task is now removed
    assertNull(delayedAllocationService.delayedRerouteTask.get());
    // mock ClusterService.submitStateUpdateTask() method again
    CountDownLatch latch2 = new CountDownLatch(1);
    AtomicReference<ClusterStateUpdateTask> clusterStateUpdateTask2 = new AtomicReference<>();
    doAnswer(invocationOnMock -> {
        clusterStateUpdateTask2.set((ClusterStateUpdateTask) invocationOnMock.getArguments()[1]);
        latch2.countDown();
        return null;
    }).when(clusterService).submitStateUpdateTask(eq(CLUSTER_UPDATE_TASK_SOURCE), any(ClusterStateUpdateTask.class));
    // simulate calling listener (cluster change event)
    delayUntilClusterChangeEvent = timeValueMillis(randomInt(50)).nanos();
    clusterChangeEventTimestampNanos = nanoTimeForReroute + delayUntilClusterChangeEvent;
    delayedAllocationService.setNanoTimeOverride(clusterChangeEventTimestampNanos);
    delayedAllocationService.clusterChanged(new ClusterChangedEvent(CLUSTER_UPDATE_TASK_SOURCE, stateWithOnlyOneDelayedShard, stateWithDelayedShards));
    // check that new delayed reroute task was created and registered with the proper settings
    DelayedAllocationService.DelayedRerouteTask secondDelayedRerouteTask = delayedAllocationService.delayedRerouteTask.get();
    assertNotNull(secondDelayedRerouteTask);
    assertFalse(secondDelayedRerouteTask.cancelScheduling.get());
    assertThat(secondDelayedRerouteTask.baseTimestampNanos, equalTo(clusterChangeEventTimestampNanos));
    assertThat(secondDelayedRerouteTask.nextDelay.nanos(), equalTo(UnassignedInfo.findNextDelayedAllocation(clusterChangeEventTimestampNanos, stateWithOnlyOneDelayedShard)));
    assertThat(secondDelayedRerouteTask.nextDelay.nanos(), equalTo(longDelaySetting.nanos() - (clusterChangeEventTimestampNanos - baseTimestampNanos)));
    // check that submitStateUpdateTask() was invoked on the cluster service mock
    assertTrue(latch2.await(30, TimeUnit.SECONDS));
    verify(clusterService).submitStateUpdateTask(eq(CLUSTER_UPDATE_TASK_SOURCE), eq(clusterStateUpdateTask2.get()));
    // advance the time on the allocation service to a timestamp that happened after the delayed scheduling
    nanoTimeForReroute = clusterChangeEventTimestampNanos + longDelaySetting.nanos() + timeValueMillis(randomInt(50)).nanos();
    allocationService.setNanoTimeOverride(nanoTimeForReroute);
    // apply cluster state
    ClusterState stateWithNoDelayedShards = clusterStateUpdateTask2.get().execute(stateWithOnlyOneDelayedShard);
    // check that shard is not delayed anymore
    assertEquals(0, UnassignedInfo.getNumberOfDelayedUnassigned(stateWithNoDelayedShards));
    // check that task is now removed
    assertNull(delayedAllocationService.delayedRerouteTask.get());
    // simulate calling listener (cluster change event)
    delayedAllocationService.setNanoTimeOverride(nanoTimeForReroute + timeValueMillis(randomInt(50)).nanos());
    delayedAllocationService.clusterChanged(new ClusterChangedEvent(CLUSTER_UPDATE_TASK_SOURCE, stateWithNoDelayedShards, stateWithOnlyOneDelayedShard));
    // check that no new task is scheduled
    assertNull(delayedAllocationService.delayedRerouteTask.get());
    // check that no further cluster state update was submitted
    verifyNoMoreInteractions(clusterService);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) AtomicReference(java.util.concurrent.atomic.AtomicReference) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) CountDownLatch(java.util.concurrent.CountDownLatch) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 32 with ClusterStateUpdateTask

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

the class DedicatedClusterSnapshotRestoreIT method updateClusterState.

private void updateClusterState(final ClusterStateUpdater updater) throws InterruptedException {
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    final ClusterService clusterService = internalCluster().getInstance(ClusterService.class);
    clusterService.submitStateUpdateTask("test", new ClusterStateUpdateTask() {

        @Override
        public ClusterState execute(ClusterState currentState) throws Exception {
            return updater.execute(currentState);
        }

        @Override
        public void onFailure(String source, @Nullable Exception e) {
            countDownLatch.countDown();
        }

        @Override
        public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
            countDownLatch.countDown();
        }
    });
    countDownLatch.await();
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterService(org.elasticsearch.cluster.service.ClusterService) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) RepositoryMissingException(org.elasticsearch.repositories.RepositoryMissingException) IOException(java.io.IOException)

Example 33 with ClusterStateUpdateTask

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

the class TransportSchemaUpdateAction method updateTemplate.

private CompletableFuture<AcknowledgedResponse> updateTemplate(ImmutableOpenMap<String, IndexTemplateMetadata> templates, String indexName, String mappingSource, TimeValue timeout) {
    CompletableFuture<AcknowledgedResponse> future = new CompletableFuture<>();
    String templateName = PartitionName.templateName(indexName);
    Map<String, Object> newMapping;
    try {
        XContentParser parser = JsonXContent.JSON_XCONTENT.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, mappingSource);
        newMapping = parser.map();
        if (newMappingAlreadyApplied(templates.get(templateName), newMapping)) {
            return CompletableFuture.completedFuture(new AcknowledgedResponse(true));
        }
    } catch (Exception e) {
        return CompletableFuture.failedFuture(e);
    }
    clusterService.submitStateUpdateTask("update-template-mapping", new ClusterStateUpdateTask(Priority.HIGH) {

        @Override
        public ClusterState execute(ClusterState currentState) throws Exception {
            return updateTemplate(xContentRegistry, currentState, templateName, newMapping);
        }

        @Override
        public TimeValue timeout() {
            return timeout;
        }

        @Override
        public void onFailure(String source, Exception e) {
            future.completeExceptionally(e);
        }

        @Override
        public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
            future.complete(new AcknowledgedResponse(true));
        }
    });
    return future;
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) CompletableFuture(java.util.concurrent.CompletableFuture) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) XContentParser(org.elasticsearch.common.xcontent.XContentParser) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException) IOException(java.io.IOException) TimeValue(io.crate.common.unit.TimeValue)

Example 34 with ClusterStateUpdateTask

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

the class TransportAddVotingConfigExclusionsAction method masterOperation.

@Override
protected void masterOperation(AddVotingConfigExclusionsRequest request, ClusterState state, ActionListener<AddVotingConfigExclusionsResponse> listener) throws Exception {
    // throws IAE if no nodes matched or maximum exceeded
    resolveVotingConfigExclusionsAndCheckMaximum(request, state);
    clusterService.submitStateUpdateTask("add-voting-config-exclusions", new ClusterStateUpdateTask(Priority.URGENT) {

        private Set<VotingConfigExclusion> resolvedExclusions;

        @Override
        public ClusterState execute(ClusterState currentState) {
            assert resolvedExclusions == null : resolvedExclusions;
            resolvedExclusions = resolveVotingConfigExclusionsAndCheckMaximum(request, currentState);
            final CoordinationMetadata.Builder builder = CoordinationMetadata.builder(currentState.coordinationMetadata());
            resolvedExclusions.forEach(builder::addVotingConfigExclusion);
            final Metadata newMetadata = Metadata.builder(currentState.metadata()).coordinationMetadata(builder.build()).build();
            final ClusterState newState = ClusterState.builder(currentState).metadata(newMetadata).build();
            assert newState.getVotingConfigExclusions().size() <= MAXIMUM_VOTING_CONFIG_EXCLUSIONS_SETTING.get(currentState.metadata().settings());
            return newState;
        }

        @Override
        public void onFailure(String source, Exception e) {
            listener.onFailure(e);
        }

        @Override
        public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
            final ClusterStateObserver observer = new ClusterStateObserver(clusterService, request.getTimeout(), logger);
            final Set<String> excludedNodeIds = resolvedExclusions.stream().map(VotingConfigExclusion::getNodeId).collect(Collectors.toSet());
            final Predicate<ClusterState> allNodesRemoved = clusterState -> {
                final Set<String> votingConfigNodeIds = clusterState.getLastCommittedConfiguration().getNodeIds();
                return excludedNodeIds.stream().noneMatch(votingConfigNodeIds::contains);
            };
            final Listener clusterStateListener = new Listener() {

                @Override
                public void onNewClusterState(ClusterState state) {
                    listener.onResponse(new AddVotingConfigExclusionsResponse());
                }

                @Override
                public void onClusterServiceClose() {
                    listener.onFailure(new ElasticsearchException("cluster service closed while waiting for voting config exclusions " + resolvedExclusions + " to take effect"));
                }

                @Override
                public void onTimeout(TimeValue timeout) {
                    listener.onFailure(new ElasticsearchTimeoutException("timed out waiting for voting config exclusions " + resolvedExclusions + " to take effect"));
                }
            };
            if (allNodesRemoved.test(newState)) {
                clusterStateListener.onNewClusterState(newState);
            } else {
                observer.waitForNextChange(clusterStateListener, allNodesRemoved);
            }
        }
    });
}
Also used : VotingConfigExclusion(org.elasticsearch.cluster.coordination.CoordinationMetadata.VotingConfigExclusion) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) Set(java.util.Set) Listener(org.elasticsearch.cluster.ClusterStateObserver.Listener) ActionListener(org.elasticsearch.action.ActionListener) CoordinationMetadata(org.elasticsearch.cluster.coordination.CoordinationMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) IOException(java.io.IOException) Predicate(java.util.function.Predicate) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) TimeValue(io.crate.common.unit.TimeValue)

Example 35 with ClusterStateUpdateTask

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

the class Coordinator method scheduleReconfigurationIfNeeded.

private void scheduleReconfigurationIfNeeded() {
    assert Thread.holdsLock(mutex) : "Coordinator mutex not held";
    assert mode == Mode.LEADER : mode;
    assert currentPublication.isPresent() == false : "Expected no publication in progress";
    final ClusterState state = getLastAcceptedState();
    if (improveConfiguration(state) != state && reconfigurationTaskScheduled.compareAndSet(false, true)) {
        LOGGER.trace("scheduling reconfiguration");
        masterService.submitStateUpdateTask("reconfigure", new ClusterStateUpdateTask(Priority.URGENT) {

            @Override
            public ClusterState execute(ClusterState currentState) {
                reconfigurationTaskScheduled.set(false);
                synchronized (mutex) {
                    return improveConfiguration(currentState);
                }
            }

            @Override
            public void onFailure(String source, Exception e) {
                reconfigurationTaskScheduled.set(false);
                LOGGER.debug("reconfiguration failed", e);
            }
        });
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) IOException(java.io.IOException)

Aggregations

ClusterState (org.elasticsearch.cluster.ClusterState)45 ClusterStateUpdateTask (org.elasticsearch.cluster.ClusterStateUpdateTask)45 CountDownLatch (java.util.concurrent.CountDownLatch)21 Matchers.containsString (org.hamcrest.Matchers.containsString)15 ArrayList (java.util.ArrayList)13 ClusterService (org.elasticsearch.cluster.service.ClusterService)13 IOException (java.io.IOException)12 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)12 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)12 List (java.util.List)11 Logger (org.apache.logging.log4j.Logger)9 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)8 SnapshotDeletionsInProgress (org.elasticsearch.cluster.SnapshotDeletionsInProgress)8 SnapshotsInProgress (org.elasticsearch.cluster.SnapshotsInProgress)8 FailedToCommitClusterStateException (org.elasticsearch.cluster.coordination.FailedToCommitClusterStateException)8 Priority (org.elasticsearch.common.Priority)8 Settings (org.elasticsearch.common.settings.Settings)8 Set (java.util.Set)7 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)7 RepositoryMissingException (org.elasticsearch.repositories.RepositoryMissingException)7