Search in sources :

Example 51 with TimeValue

use of io.crate.common.unit.TimeValue in project crate by crate.

the class RamAccountingQueueSinkTest method testTimedRamAccountingQueueSink.

@Test
public void testTimedRamAccountingQueueSink() throws Exception {
    ConcurrentLinkedQueue<NoopLog> q = new ConcurrentLinkedQueue<>();
    RamAccountingQueue<NoopLog> ramAccountingQueue = new RamAccountingQueue<>(q, breaker(), NOOP_ESTIMATOR);
    TimeValue timeValue = TimeValue.timeValueSeconds(1L);
    ScheduledFuture<?> task = TimeBasedQEviction.scheduleTruncate(1000L, 1000L, q, scheduler, timeValue);
    logSink = new QueueSink<>(ramAccountingQueue, () -> {
        task.cancel(false);
        ramAccountingQueue.release();
    });
    for (int j = 0; j < 100; j++) {
        logSink.add(new NoopLog());
    }
    assertThat(ramAccountingQueue.size(), is(100));
    Thread.sleep(2000L);
    assertThat(ramAccountingQueue.size(), is(0));
}
Also used : ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) TimeValue(io.crate.common.unit.TimeValue) Test(org.junit.Test)

Example 52 with TimeValue

use of io.crate.common.unit.TimeValue in project crate by crate.

the class TransportDistributedResultAction method retryOrFailureResponse.

private CompletableFuture<DistributedResultResponse> retryOrFailureResponse(DistributedResultRequest request, @Nullable Iterator<TimeValue> retryDelay) {
    if (retryDelay == null) {
        retryDelay = backoffPolicy.iterator();
    }
    if (retryDelay.hasNext()) {
        TimeValue delay = retryDelay.next();
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("scheduling retry to start node operation for jobId: {} in {}ms", request.jobId(), delay.getMillis());
        }
        NodeOperationRunnable operationRunnable = new NodeOperationRunnable(request, retryDelay);
        scheduler.schedule(operationRunnable::run, delay.getMillis(), TimeUnit.MILLISECONDS);
        return operationRunnable;
    } else {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("Received a result for job={} but couldn't find a RootTask for it", request.jobId());
        }
        List<String> excludedNodeIds = Collections.singletonList(clusterService.localNode().getId());
        /* The upstream (DistributingConsumer) forwards failures to other downstreams and eventually considers its job done.
             * But it cannot inform the handler-merge about a failure because the JobResponse is sent eagerly.
             *
             * The handler local-merge would get stuck if not all its upstreams send their requests, so we need to invoke
             * a kill to make sure that doesn't happen.
             */
        KillJobsRequest killRequest = new KillJobsRequest(List.of(request.jobId()), User.CRATE_USER.name(), "Received data for job=" + request.jobId() + " but there is no job context present. " + "This can happen due to bad network latency or if individual nodes are unresponsive due to high load");
        killJobsAction.broadcast(killRequest, new ActionListener<>() {

            @Override
            public void onResponse(Long numKilled) {
            }

            @Override
            public void onFailure(Exception e) {
                LOGGER.debug("Could not kill " + request.jobId(), e);
            }
        }, excludedNodeIds);
        return CompletableFuture.failedFuture(new TaskMissing(TaskMissing.Type.ROOT, request.jobId()));
    }
}
Also used : TaskMissing(io.crate.exceptions.TaskMissing) KillJobsRequest(io.crate.execution.jobs.kill.KillJobsRequest) TimeValue(io.crate.common.unit.TimeValue) JobKilledException(io.crate.exceptions.JobKilledException)

Example 53 with TimeValue

use of io.crate.common.unit.TimeValue 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 54 with TimeValue

use of io.crate.common.unit.TimeValue in project crate by crate.

the class Coordinator method startElectionScheduler.

private void startElectionScheduler() {
    assert electionScheduler == null : electionScheduler;
    if (getLocalNode().isMasterEligibleNode() == false) {
        return;
    }
    // TODO variable grace period
    final TimeValue gracePeriod = TimeValue.ZERO;
    electionScheduler = electionSchedulerFactory.startElectionScheduler(gracePeriod, new Runnable() {

        @Override
        public void run() {
            synchronized (mutex) {
                if (mode == Mode.CANDIDATE) {
                    final ClusterState lastAcceptedState = coordinationState.get().getLastAcceptedState();
                    if (localNodeMayWinElection(lastAcceptedState) == false) {
                        LOGGER.trace("skip prevoting as local node may not win election: {}", lastAcceptedState.coordinationMetadata());
                        return;
                    }
                    if (prevotingRound != null) {
                        prevotingRound.close();
                    }
                    prevotingRound = preVoteCollector.start(lastAcceptedState, getDiscoveredNodes());
                }
            }
        }

        @Override
        public String toString() {
            return "scheduling of new prevoting round";
        }
    });
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) TimeValue(io.crate.common.unit.TimeValue)

Example 55 with TimeValue

use of io.crate.common.unit.TimeValue in project crate by crate.

the class Publication method logIncompleteNodes.

void logIncompleteNodes(Level level) {
    final String message = publicationTargets.stream().filter(PublicationTarget::isActive).map(publicationTarget -> publicationTarget.getDiscoveryNode() + " [" + publicationTarget.getState() + "]").collect(Collectors.joining(", "));
    if (message.isEmpty() == false) {
        final TimeValue elapsedTime = TimeValue.timeValueMillis(currentTimeSupplier.getAsLong() - startTime);
        logger.log(level, "after [{}] publication of cluster state version [{}] is still waiting for {}", elapsedTime, publishRequest.getAcceptedState().version(), message);
    }
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) LongSupplier(java.util.function.LongSupplier) Level(org.apache.logging.log4j.Level) Set(java.util.Set) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Collectors(java.util.stream.Collectors) AckListener(org.elasticsearch.cluster.coordination.ClusterStatePublisher.AckListener) ArrayList(java.util.ArrayList) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) List(java.util.List) Logger(org.apache.logging.log4j.Logger) TimeValue(io.crate.common.unit.TimeValue) Optional(java.util.Optional) TransportResponse(org.elasticsearch.transport.TransportResponse) LogManager(org.apache.logging.log4j.LogManager) TransportException(org.elasticsearch.transport.TransportException) ActionListener(org.elasticsearch.action.ActionListener) TimeValue(io.crate.common.unit.TimeValue)

Aggregations

TimeValue (io.crate.common.unit.TimeValue)75 Test (org.junit.Test)23 ClusterState (org.elasticsearch.cluster.ClusterState)20 IOException (java.io.IOException)17 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)12 ActionListener (org.elasticsearch.action.ActionListener)12 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)11 ArrayList (java.util.ArrayList)10 ThreadPool (org.elasticsearch.threadpool.ThreadPool)10 ElasticsearchException (org.elasticsearch.ElasticsearchException)9 Settings (org.elasticsearch.common.settings.Settings)9 Logger (org.apache.logging.log4j.Logger)8 ClusterStateUpdateTask (org.elasticsearch.cluster.ClusterStateUpdateTask)8 ClusterService (org.elasticsearch.cluster.service.ClusterService)8 List (java.util.List)7 LogManager (org.apache.logging.log4j.LogManager)7 Version (org.elasticsearch.Version)7 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)6 ClusterStateObserver (org.elasticsearch.cluster.ClusterStateObserver)6 StreamInput (org.elasticsearch.common.io.stream.StreamInput)6