Search in sources :

Example 1 with ProvisioningOp

use of io.cdap.cdap.internal.provision.ProvisioningOp in project cdap by caskdata.

the class ProvisioningTask method executeOnce.

/**
 * Executes one iteration of subtask. It persists task info before each subtask such that this task
 * can be re-created from the task info stored in the ProvisionerStore.
 */
@Override
public final long executeOnce() throws Exception {
    RetryStrategy retryStrategy = getRetryStrategy();
    Map<ProvisioningOp.Status, ProvisioningSubtask> subTasks = getSubTasks();
    ProvisioningTaskInfo currentTaskInfo = persistTaskInfo(taskInfo, retryStrategy);
    ProvisioningOp.Status state = currentTaskInfo.getProvisioningOp().getStatus();
    if (state == ProvisioningOp.Status.CANCELLED) {
        LOG.debug("Cancelled {} task for program run {}.", initialTaskInfo.getProvisioningOp().getType(), programRunId);
        return -1L;
    }
    // Get the sub-task to execute
    ProvisioningSubtask subtask = subTasks.get(state);
    if (subtask == null) {
        // should never happen
        throw new IllegalStateException(String.format("Invalid state '%s' in provisioning task for program run '%s'. " + "This means there is a bug in provisioning state machine. " + "Please reach out to the development team.", state, programRunId));
    }
    if (subtask == EndSubtask.INSTANCE) {
        LOG.debug("Completed {} task for program run {}.", initialTaskInfo.getProvisioningOp().getType(), programRunId);
        return -1L;
    }
    if (subTaskStartTime == 0L) {
        subTaskStartTime = System.currentTimeMillis();
    }
    try {
        PROGRESS_LOG.debug("Executing {} subtask {} for program run {}.", currentTaskInfo.getProvisioningOp().getType(), state, programRunId);
        taskInfo = Retries.callWithInterruptibleRetries(() -> subtask.execute(currentTaskInfo), retryStrategy, t -> t instanceof RetryableProvisionException).orElse(null);
        PROGRESS_LOG.debug("Completed {} subtask {} for program run {}.", currentTaskInfo.getProvisioningOp().getType(), state, programRunId);
        // Nothing more to execute
        if (taskInfo == null) {
            LOG.debug("No more {} task for program run {}.", initialTaskInfo.getProvisioningOp().getType(), programRunId);
            return -1L;
        }
        ProvisioningOp.Status nextState = taskInfo.getProvisioningOp().getStatus();
        // If state doesn't change, determine the delay based on the polling strategy
        if (state == nextState) {
            if (subTaskPollingStrategy == null) {
                subTaskPollingStrategy = provisioner.getPollingStrategy(provisionerContext, taskInfo.getCluster());
            }
            return Math.max(0, subTaskPollingStrategy.nextPoll(subTaskExecNums++, subTaskStartTime));
        }
        // Otherwise, execute the next task immediately.
        subTaskPollingStrategy = null;
        subTaskStartTime = 0L;
        subTaskExecNums = 0;
        return 0;
    } catch (InterruptedException e) {
        throw e;
    } catch (Exception e) {
        LOG.error("{} task failed in {} state for program run {} due to {}.", currentTaskInfo.getProvisioningOp().getType(), state, programRunId, e.getMessage(), e);
        handleSubtaskFailure(currentTaskInfo, e);
        ProvisioningOp failureOp = new ProvisioningOp(currentTaskInfo.getProvisioningOp().getType(), ProvisioningOp.Status.FAILED);
        ProvisioningTaskInfo failureInfo = new ProvisioningTaskInfo(currentTaskInfo, failureOp, currentTaskInfo.getCluster());
        persistTaskInfo(failureInfo, retryStrategy);
        LOG.debug("Terminated {} task for program run {} due to exception.", initialTaskInfo.getProvisioningOp().getType(), programRunId);
        return -1L;
    }
}
Also used : RetryableProvisionException(io.cdap.cdap.runtime.spi.provisioner.RetryableProvisionException) ProvisioningTaskInfo(io.cdap.cdap.internal.provision.ProvisioningTaskInfo) ProvisioningOp(io.cdap.cdap.internal.provision.ProvisioningOp) RetryStrategy(io.cdap.cdap.common.service.RetryStrategy) RetryableProvisionException(io.cdap.cdap.runtime.spi.provisioner.RetryableProvisionException)

Example 2 with ProvisioningOp

use of io.cdap.cdap.internal.provision.ProvisioningOp in project cdap by caskdata.

the class ProvisioningSubtask method execute.

/**
 * Executes the subtask and returns the next subtask that should be executed if there is one.
 *
 * @param taskInfo information about the task being executed, including the current cluster state
 * @return task info to be sent to the next subtask if there is one
 * @throws Exception if there was an error executing the subtask
 */
public Optional<ProvisioningTaskInfo> execute(ProvisioningTaskInfo taskInfo) throws Exception {
    Cluster cluster = taskInfo.getCluster();
    Cluster nextCluster = execute(cluster);
    return transition.apply(nextCluster).map(nextState -> {
        ProvisioningOp nextOp = new ProvisioningOp(taskInfo.getProvisioningOp().getType(), nextState);
        return new ProvisioningTaskInfo(taskInfo, nextOp, nextCluster);
    });
}
Also used : ProvisioningTaskInfo(io.cdap.cdap.internal.provision.ProvisioningTaskInfo) Cluster(io.cdap.cdap.runtime.spi.provisioner.Cluster) ProvisioningOp(io.cdap.cdap.internal.provision.ProvisioningOp)

Aggregations

ProvisioningOp (io.cdap.cdap.internal.provision.ProvisioningOp)2 ProvisioningTaskInfo (io.cdap.cdap.internal.provision.ProvisioningTaskInfo)2 RetryStrategy (io.cdap.cdap.common.service.RetryStrategy)1 Cluster (io.cdap.cdap.runtime.spi.provisioner.Cluster)1 RetryableProvisionException (io.cdap.cdap.runtime.spi.provisioner.RetryableProvisionException)1