Search in sources :

Example 1 with TooManyRequestsException

use of io.cdap.cdap.common.TooManyRequestsException in project cdap by caskdata.

the class ProgramLifecycleService method runInternal.

/**
 * Runs a Program without authorization.
 *
 * Note that this method should only be called through internal service, it does not have auth check for starting the
 * program.
 *
 * @param programId the {@link ProgramId program} to run
 * @param userArgs user arguments
 * @param sysArgs system arguments
 * @param debug whether to start as a debug run
 * @return {@link RunId}
 * @throws IOException if there is an error starting the program
 * @throws NotFoundException if the namespace, application, or program is not found
 * @throws ProfileConflictException if the profile is disabled
 * @throws Exception if there were other exceptions
 */
public RunId runInternal(ProgramId programId, Map<String, String> userArgs, Map<String, String> sysArgs, boolean debug) throws Exception {
    RunId runId = RunIds.generate();
    ProgramOptions programOptions = createProgramOptions(programId, userArgs, sysArgs, debug);
    ProgramDescriptor programDescriptor = store.loadProgram(programId);
    String userId = SecurityRequestContext.getUserId();
    userId = userId == null ? "" : userId;
    checkCapability(programDescriptor);
    ProgramRunId programRunId = programId.run(runId);
    RunRecordMonitorService.Count count = runRecordMonitorService.addRequestAndGetCount(programRunId);
    boolean done = false;
    try {
        if (maxConcurrentRuns >= 0 && maxConcurrentRuns < count.getLaunchingCount() + count.getRunningCount()) {
            String msg = String.format("Program %s cannot start because the maximum of %d outstanding runs is allowed", programId, maxConcurrentRuns);
            LOG.info(msg);
            TooManyRequestsException e = new TooManyRequestsException(msg);
            programStateWriter.reject(programRunId, programOptions, programDescriptor, userId, e);
            throw e;
        }
        if (maxConcurrentLaunching >= 0 && maxConcurrentLaunching < count.getLaunchingCount()) {
            String msg = String.format("Program %s cannot start because the maximum of %d concurrent " + "provisioning/starting runs is allowed", programId, maxConcurrentLaunching);
            LOG.info(msg);
            TooManyRequestsException e = new TooManyRequestsException(msg);
            programStateWriter.reject(programRunId, programOptions, programDescriptor, userId, e);
            throw e;
        }
        LOG.info("Attempt to run {} program {} as user {} with arguments {}", programId.getType(), programId.getProgram(), authenticationContext.getPrincipal().getName(), userArgs);
        provisionerNotifier.provisioning(programRunId, programOptions, programDescriptor, userId);
        done = true;
    } finally {
        if (!done) {
            runRecordMonitorService.removeRequest(programRunId, false);
        }
    }
    return runId;
}
Also used : TooManyRequestsException(io.cdap.cdap.common.TooManyRequestsException) ProgramDescriptor(io.cdap.cdap.app.program.ProgramDescriptor) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) RunId(org.apache.twill.api.RunId) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) SimpleProgramOptions(io.cdap.cdap.internal.app.runtime.SimpleProgramOptions) ProgramOptions(io.cdap.cdap.app.runtime.ProgramOptions)

Aggregations

ProgramDescriptor (io.cdap.cdap.app.program.ProgramDescriptor)1 ProgramOptions (io.cdap.cdap.app.runtime.ProgramOptions)1 TooManyRequestsException (io.cdap.cdap.common.TooManyRequestsException)1 SimpleProgramOptions (io.cdap.cdap.internal.app.runtime.SimpleProgramOptions)1 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)1 RunId (org.apache.twill.api.RunId)1