Search in sources :

Example 26 with Operation

use of com.hazelcast.spi.impl.operationservice.Operation in project hazelcast by hazelcast.

the class JobProxy method doExportSnapshot.

private JobStateSnapshot doExportSnapshot(String name, boolean cancelJob) {
    checkNotLightJob("export snapshot");
    JetServiceBackend jetServiceBackend = container().getService(JetServiceBackend.SERVICE_NAME);
    try {
        Operation operation = jetServiceBackend.createExportSnapshotOperation(getId(), name, cancelJob);
        invokeOp(operation).get();
    } catch (Exception e) {
        throw rethrow(e);
    }
    return jetServiceBackend.getJet().getJobStateSnapshot(name);
}
Also used : GetJobConfigOperation(com.hazelcast.jet.impl.operation.GetJobConfigOperation) TerminateJobOperation(com.hazelcast.jet.impl.operation.TerminateJobOperation) GetJobSubmissionTimeOperation(com.hazelcast.jet.impl.operation.GetJobSubmissionTimeOperation) JoinSubmittedJobOperation(com.hazelcast.jet.impl.operation.JoinSubmittedJobOperation) GetJobMetricsOperation(com.hazelcast.jet.impl.operation.GetJobMetricsOperation) Operation(com.hazelcast.spi.impl.operationservice.Operation) GetJobSuspensionCauseOperation(com.hazelcast.jet.impl.operation.GetJobSuspensionCauseOperation) GetJobStatusOperation(com.hazelcast.jet.impl.operation.GetJobStatusOperation) ResumeJobOperation(com.hazelcast.jet.impl.operation.ResumeJobOperation) SubmitJobOperation(com.hazelcast.jet.impl.operation.SubmitJobOperation)

Example 27 with Operation

use of com.hazelcast.spi.impl.operationservice.Operation in project hazelcast by hazelcast.

the class MasterContext method invokeOnParticipants.

/**
 * @param completionCallback      a consumer that will receive a collection
 *                                of member-response pairs, one for each
 *                                member, after all have been received. The
 *                                response value will be either the response
 *                                (including a null response) or an
 *                                exception thrown from the operation (the
 *                                pairs themselves will never be null); size
 *                                will be equal to participant count
 * @param individualCallback      A callback that will be called after each
 *                                individual participant completes
 * @param retryOnTimeoutException if true, operations that threw {@link
 *                                com.hazelcast.core.OperationTimeoutException}
 *                                will be retried
 */
void invokeOnParticipants(Function<ExecutionPlan, Operation> operationCtor, @Nullable Consumer<Collection<Map.Entry<MemberInfo, Object>>> completionCallback, @Nullable BiConsumer<Address, Object> individualCallback, boolean retryOnTimeoutException) {
    ConcurrentMap<MemberInfo, Object> responses = new ConcurrentHashMap<>();
    AtomicInteger remainingCount = new AtomicInteger(executionPlanMap.size());
    for (Entry<MemberInfo, ExecutionPlan> entry : executionPlanMap.entrySet()) {
        MemberInfo memberInfo = entry.getKey();
        Supplier<Operation> opSupplier = () -> operationCtor.apply(entry.getValue());
        invokeOnParticipant(memberInfo, opSupplier, completionCallback, individualCallback, retryOnTimeoutException, responses, remainingCount);
    }
}
Also used : ExecutionPlan(com.hazelcast.jet.impl.execution.init.ExecutionPlan) MemberInfo(com.hazelcast.internal.cluster.MemberInfo) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StartExecutionOperation(com.hazelcast.jet.impl.operation.StartExecutionOperation) Operation(com.hazelcast.spi.impl.operationservice.Operation) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 28 with Operation

use of com.hazelcast.spi.impl.operationservice.Operation in project hazelcast by hazelcast.

the class MasterContext method invokeOnParticipant.

private void invokeOnParticipant(MemberInfo memberInfo, Supplier<Operation> operationSupplier, @Nullable Consumer<Collection<Map.Entry<MemberInfo, Object>>> completionCallback, @Nullable BiConsumer<Address, Object> individualCallback, boolean retryOnTimeoutException, ConcurrentMap<MemberInfo, Object> collectedResponses, AtomicInteger remainingCount) {
    Operation operation = operationSupplier.get();
    InternalCompletableFuture<Object> future = nodeEngine.getOperationService().createInvocationBuilder(JetServiceBackend.SERVICE_NAME, operation, memberInfo.getAddress()).invoke();
    future.whenCompleteAsync(withTryCatch(logger, (r, throwable) -> {
        Object response = r != null ? r : throwable != null ? peel(throwable) : NULL_OBJECT;
        if (retryOnTimeoutException && throwable instanceof OperationTimeoutException) {
            logger.warning("Retrying " + operation.getClass().getName() + " that failed with " + OperationTimeoutException.class.getSimpleName() + " in " + jobIdString());
            invokeOnParticipant(memberInfo, operationSupplier, completionCallback, individualCallback, retryOnTimeoutException, collectedResponses, remainingCount);
            return;
        }
        if (individualCallback != null) {
            individualCallback.accept(memberInfo.getAddress(), throwable != null ? peel(throwable) : r);
        }
        Object oldResponse = collectedResponses.put(memberInfo, response);
        assert oldResponse == null : "Duplicate response for " + memberInfo.getAddress() + ". Old=" + oldResponse + ", new=" + response;
        if (remainingCount.decrementAndGet() == 0 && completionCallback != null) {
            completionCallback.accept(collectedResponses.entrySet().stream().map(e -> e.getValue() == NULL_OBJECT ? entry(e.getKey(), null) : e).collect(Collectors.toList()));
        }
    }));
}
Also used : Address(com.hazelcast.cluster.Address) NOT_RUNNING(com.hazelcast.jet.core.JobStatus.NOT_RUNNING) CompletableFuture(java.util.concurrent.CompletableFuture) StartExecutionOperation(com.hazelcast.jet.impl.operation.StartExecutionOperation) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ConcurrentMap(java.util.concurrent.ConcurrentMap) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) Util.jobNameAndExecutionId(com.hazelcast.jet.impl.util.Util.jobNameAndExecutionId) ILogger(com.hazelcast.logging.ILogger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Operation(com.hazelcast.spi.impl.operationservice.Operation) Util.entry(com.hazelcast.jet.Util.entry) ExceptionUtil.withTryCatch(com.hazelcast.jet.impl.util.ExceptionUtil.withTryCatch) MemberInfo(com.hazelcast.internal.cluster.MemberInfo) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) SUSPENDED(com.hazelcast.jet.core.JobStatus.SUSPENDED) JobStatus(com.hazelcast.jet.core.JobStatus) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) NodeEngineImpl(com.hazelcast.spi.impl.NodeEngineImpl) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Collectors.toConcurrentMap(java.util.stream.Collectors.toConcurrentMap) Collection(java.util.Collection) JobConfig(com.hazelcast.jet.config.JobConfig) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Util.idToString(com.hazelcast.jet.Util.idToString) ExceptionUtil.peel(com.hazelcast.jet.impl.util.ExceptionUtil.peel) ExecutionPlan(com.hazelcast.jet.impl.execution.init.ExecutionPlan) OperationTimeoutException(com.hazelcast.core.OperationTimeoutException) Entry(java.util.Map.Entry) OperationTimeoutException(com.hazelcast.core.OperationTimeoutException) StartExecutionOperation(com.hazelcast.jet.impl.operation.StartExecutionOperation) Operation(com.hazelcast.spi.impl.operationservice.Operation)

Example 29 with Operation

use of com.hazelcast.spi.impl.operationservice.Operation in project hazelcast by hazelcast.

the class MasterSnapshotContext method tryBeginSnapshot.

void tryBeginSnapshot() {
    mc.coordinationService().submitToCoordinatorThread(() -> {
        boolean isTerminal;
        String snapshotMapName;
        CompletableFuture<Void> future;
        mc.lock();
        long localExecutionId;
        try {
            if (mc.jobStatus() != RUNNING) {
                logger.fine("Not beginning snapshot, " + mc.jobIdString() + " is not RUNNING, but " + mc.jobStatus());
                return;
            }
            if (snapshotInProgress) {
                logger.fine("Not beginning snapshot since one is already in progress " + mc.jobIdString());
                return;
            }
            if (terminalSnapshotFuture.isDone()) {
                logger.fine("Not beginning snapshot since terminal snapshot is already completed " + mc.jobIdString());
                return;
            }
            Tuple3<String, Boolean, CompletableFuture<Void>> requestedSnapshot = snapshotQueue.poll();
            if (requestedSnapshot == null) {
                return;
            }
            snapshotInProgress = true;
            snapshotMapName = requestedSnapshot.f0();
            assert requestedSnapshot.f1() != null;
            isTerminal = requestedSnapshot.f1();
            future = requestedSnapshot.f2();
            mc.jobExecutionRecord().startNewSnapshot(snapshotMapName);
            localExecutionId = mc.executionId();
        } finally {
            mc.unlock();
        }
        mc.writeJobExecutionRecord(false);
        long newSnapshotId = mc.jobExecutionRecord().ongoingSnapshotId();
        boolean isExport = snapshotMapName != null;
        int snapshotFlags = SnapshotFlags.create(isTerminal, isExport);
        String finalMapName = isExport ? exportedSnapshotMapName(snapshotMapName) : snapshotDataMapName(mc.jobId(), mc.jobExecutionRecord().ongoingDataMapIndex());
        mc.nodeEngine().getHazelcastInstance().getMap(finalMapName).clear();
        logFine(logger, "Starting snapshot %d for %s, flags: %s, writing to: %s", newSnapshotId, jobNameAndExecutionId(mc.jobName(), localExecutionId), SnapshotFlags.toString(snapshotFlags), snapshotMapName);
        Function<ExecutionPlan, Operation> factory = plan -> new SnapshotPhase1Operation(mc.jobId(), localExecutionId, newSnapshotId, finalMapName, snapshotFlags);
        // Need to take a copy of executionId: we don't cancel the scheduled task when the execution
        // finalizes. If a new execution is started in the meantime, we'll use the execution ID to detect it.
        mc.invokeOnParticipants(factory, responses -> onSnapshotPhase1Complete(responses, localExecutionId, newSnapshotId, finalMapName, snapshotFlags, future), null, true);
    });
}
Also used : SnapshotPhase2Operation(com.hazelcast.jet.impl.operation.SnapshotPhase2Operation) LoggingUtil(com.hazelcast.jet.impl.util.LoggingUtil) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) ExecutionNotFoundException(com.hazelcast.jet.impl.exception.ExecutionNotFoundException) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) JetException(com.hazelcast.jet.JetException) Util.jobNameAndExecutionId(com.hazelcast.jet.impl.util.Util.jobNameAndExecutionId) ILogger(com.hazelcast.logging.ILogger) Operation(com.hazelcast.spi.impl.operationservice.Operation) ExceptionUtil.withTryCatch(com.hazelcast.jet.impl.util.ExceptionUtil.withTryCatch) MemberInfo(com.hazelcast.internal.cluster.MemberInfo) Map(java.util.Map) SnapshotPhase1Operation(com.hazelcast.jet.impl.operation.SnapshotPhase1Operation) LinkedList(java.util.LinkedList) EXPORTED_SNAPSHOTS_PREFIX(com.hazelcast.jet.impl.JobRepository.EXPORTED_SNAPSHOTS_PREFIX) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Tuple3(com.hazelcast.jet.datamodel.Tuple3) SnapshotFlags(com.hazelcast.jet.impl.execution.SnapshotFlags) Collection(java.util.Collection) JobRepository.snapshotDataMapName(com.hazelcast.jet.impl.JobRepository.snapshotDataMapName) ExecutionException(java.util.concurrent.ExecutionException) JobRepository.exportedSnapshotMapName(com.hazelcast.jet.impl.JobRepository.exportedSnapshotMapName) SnapshotPhase1Result(com.hazelcast.jet.impl.operation.SnapshotPhase1Operation.SnapshotPhase1Result) Tuple3.tuple3(com.hazelcast.jet.datamodel.Tuple3.tuple3) List(java.util.List) Util.idToString(com.hazelcast.jet.Util.idToString) LoggingUtil.logFine(com.hazelcast.jet.impl.util.LoggingUtil.logFine) ExecutionPlan(com.hazelcast.jet.impl.execution.init.ExecutionPlan) Entry(java.util.Map.Entry) RUNNING(com.hazelcast.jet.core.JobStatus.RUNNING) Queue(java.util.Queue) SnapshotStats(com.hazelcast.jet.impl.JobExecutionRecord.SnapshotStats) IMap(com.hazelcast.map.IMap) SnapshotPhase1Operation(com.hazelcast.jet.impl.operation.SnapshotPhase1Operation) Util.idToString(com.hazelcast.jet.Util.idToString) SnapshotPhase2Operation(com.hazelcast.jet.impl.operation.SnapshotPhase2Operation) Operation(com.hazelcast.spi.impl.operationservice.Operation) SnapshotPhase1Operation(com.hazelcast.jet.impl.operation.SnapshotPhase1Operation) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionPlan(com.hazelcast.jet.impl.execution.init.ExecutionPlan)

Example 30 with Operation

use of com.hazelcast.spi.impl.operationservice.Operation in project hazelcast by hazelcast.

the class MasterSnapshotContext method onSnapshotPhase1Complete.

/**
 * @param responses collected responses from the members
 * @param snapshotMapName the IMap name to which the snapshot is written
 * @param snapshotFlags flags of the snapshot
 * @param future a future to be completed when the phase-2 is fully completed
 */
private void onSnapshotPhase1Complete(Collection<Map.Entry<MemberInfo, Object>> responses, long executionId, long snapshotId, String snapshotMapName, int snapshotFlags, @Nullable CompletableFuture<Void> future) {
    mc.coordinationService().submitToCoordinatorThread(() -> {
        SnapshotPhase1Result mergedResult = new SnapshotPhase1Result();
        List<CompletableFuture<Void>> missingResponses = new ArrayList<>();
        for (Map.Entry<MemberInfo, Object> entry : responses) {
            // the response is either SnapshotOperationResult or an exception, see #invokeOnParticipants() method
            Object response = entry.getValue();
            if (response instanceof Throwable) {
                // all the responses to an array, and we'll wait for them later.
                if (response instanceof ExecutionNotFoundException) {
                    missingResponses.add(mc.startOperationResponses().get(entry.getKey().getAddress()));
                    continue;
                }
                response = new SnapshotPhase1Result(0, 0, 0, (Throwable) response);
            }
            mergedResult.merge((SnapshotPhase1Result) response);
        }
        if (!missingResponses.isEmpty()) {
            LoggingUtil.logFine(logger, "%s will wait for %d responses to StartExecutionOperation in " + "onSnapshotPhase1Complete()", mc.jobIdString(), missingResponses.size());
        }
        // In a typical case `missingResponses` will be empty. It will be non-empty if some member completed
        // its execution and some other did not, or near the completion of a job, e.g. after a failure.
        // `allOf` for an empty array returns a completed future immediately.
        // Another edge case is that we'll be waiting for a response to start operation from a next execution,
        // which can happen much later - we could handle it, but we ignore it: when it arrives, we'll find a
        // changed executionId and ignore the response. It also doesn't occupy a thread - we're using a future.
        CompletableFuture.allOf(missingResponses.toArray(new CompletableFuture[0])).whenComplete(withTryCatch(logger, (r, t) -> onSnapshotPhase1CompleteWithStartResponses(responses, executionId, snapshotId, snapshotMapName, snapshotFlags, future, mergedResult, missingResponses)));
    });
}
Also used : SnapshotPhase2Operation(com.hazelcast.jet.impl.operation.SnapshotPhase2Operation) LoggingUtil(com.hazelcast.jet.impl.util.LoggingUtil) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) ExecutionNotFoundException(com.hazelcast.jet.impl.exception.ExecutionNotFoundException) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) JetException(com.hazelcast.jet.JetException) Util.jobNameAndExecutionId(com.hazelcast.jet.impl.util.Util.jobNameAndExecutionId) ILogger(com.hazelcast.logging.ILogger) Operation(com.hazelcast.spi.impl.operationservice.Operation) ExceptionUtil.withTryCatch(com.hazelcast.jet.impl.util.ExceptionUtil.withTryCatch) MemberInfo(com.hazelcast.internal.cluster.MemberInfo) Map(java.util.Map) SnapshotPhase1Operation(com.hazelcast.jet.impl.operation.SnapshotPhase1Operation) LinkedList(java.util.LinkedList) EXPORTED_SNAPSHOTS_PREFIX(com.hazelcast.jet.impl.JobRepository.EXPORTED_SNAPSHOTS_PREFIX) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Tuple3(com.hazelcast.jet.datamodel.Tuple3) SnapshotFlags(com.hazelcast.jet.impl.execution.SnapshotFlags) Collection(java.util.Collection) JobRepository.snapshotDataMapName(com.hazelcast.jet.impl.JobRepository.snapshotDataMapName) ExecutionException(java.util.concurrent.ExecutionException) JobRepository.exportedSnapshotMapName(com.hazelcast.jet.impl.JobRepository.exportedSnapshotMapName) SnapshotPhase1Result(com.hazelcast.jet.impl.operation.SnapshotPhase1Operation.SnapshotPhase1Result) Tuple3.tuple3(com.hazelcast.jet.datamodel.Tuple3.tuple3) List(java.util.List) Util.idToString(com.hazelcast.jet.Util.idToString) LoggingUtil.logFine(com.hazelcast.jet.impl.util.LoggingUtil.logFine) ExecutionPlan(com.hazelcast.jet.impl.execution.init.ExecutionPlan) Entry(java.util.Map.Entry) RUNNING(com.hazelcast.jet.core.JobStatus.RUNNING) Queue(java.util.Queue) SnapshotStats(com.hazelcast.jet.impl.JobExecutionRecord.SnapshotStats) IMap(com.hazelcast.map.IMap) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionNotFoundException(com.hazelcast.jet.impl.exception.ExecutionNotFoundException) MemberInfo(com.hazelcast.internal.cluster.MemberInfo) SnapshotPhase1Result(com.hazelcast.jet.impl.operation.SnapshotPhase1Operation.SnapshotPhase1Result) ArrayList(java.util.ArrayList) Map(java.util.Map) IMap(com.hazelcast.map.IMap)

Aggregations

Operation (com.hazelcast.spi.impl.operationservice.Operation)271 Test (org.junit.Test)80 QuickTest (com.hazelcast.test.annotation.QuickTest)79 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)59 OperationService (com.hazelcast.spi.impl.operationservice.OperationService)56 Address (com.hazelcast.cluster.Address)31 HazelcastInstance (com.hazelcast.core.HazelcastInstance)25 Data (com.hazelcast.internal.serialization.Data)24 Future (java.util.concurrent.Future)24 Member (com.hazelcast.cluster.Member)22 ArrayList (java.util.ArrayList)21 NodeEngine (com.hazelcast.spi.impl.NodeEngine)18 NodeEngineImpl (com.hazelcast.spi.impl.NodeEngineImpl)17 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)17 AssertTask (com.hazelcast.test.AssertTask)15 ILogger (com.hazelcast.logging.ILogger)14 UrgentSystemOperation (com.hazelcast.spi.impl.operationservice.UrgentSystemOperation)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 Config (com.hazelcast.config.Config)12 CompletableFuture (java.util.concurrent.CompletableFuture)12