Search in sources :

Example 1 with RunningAppContext

use of org.apache.hadoop.mapreduce.v2.app.MRAppMaster.RunningAppContext in project hadoop by apache.

the class MRAppMaster method serviceInit.

@Override
protected void serviceInit(final Configuration conf) throws Exception {
    // create the job classloader if enabled
    createJobClassLoader(conf);
    conf.setBoolean(Dispatcher.DISPATCHER_EXIT_ON_ERROR_KEY, true);
    initJobCredentialsAndUGI(conf);
    dispatcher = createDispatcher();
    addIfService(dispatcher);
    taskAttemptFinishingMonitor = createTaskAttemptFinishingMonitor(dispatcher.getEventHandler());
    addIfService(taskAttemptFinishingMonitor);
    context = new RunningAppContext(conf, taskAttemptFinishingMonitor);
    // Job name is the same as the app name util we support DAG of jobs
    // for an app later
    appName = conf.get(MRJobConfig.JOB_NAME, "<missing app name>");
    conf.setInt(MRJobConfig.APPLICATION_ATTEMPT_ID, appAttemptID.getAttemptId());
    newApiCommitter = false;
    jobId = MRBuilderUtils.newJobId(appAttemptID.getApplicationId(), appAttemptID.getApplicationId().getId());
    int numReduceTasks = conf.getInt(MRJobConfig.NUM_REDUCES, 0);
    if ((numReduceTasks > 0 && conf.getBoolean("mapred.reducer.new-api", false)) || (numReduceTasks == 0 && conf.getBoolean("mapred.mapper.new-api", false))) {
        newApiCommitter = true;
        LOG.info("Using mapred newApiCommitter.");
    }
    boolean copyHistory = false;
    committer = createOutputCommitter(conf);
    try {
        String user = UserGroupInformation.getCurrentUser().getShortUserName();
        Path stagingDir = MRApps.getStagingAreaDir(conf, user);
        FileSystem fs = getFileSystem(conf);
        boolean stagingExists = fs.exists(stagingDir);
        Path startCommitFile = MRApps.getStartJobCommitFile(conf, user, jobId);
        boolean commitStarted = fs.exists(startCommitFile);
        Path endCommitSuccessFile = MRApps.getEndJobCommitSuccessFile(conf, user, jobId);
        boolean commitSuccess = fs.exists(endCommitSuccessFile);
        Path endCommitFailureFile = MRApps.getEndJobCommitFailureFile(conf, user, jobId);
        boolean commitFailure = fs.exists(endCommitFailureFile);
        if (!stagingExists) {
            isLastAMRetry = true;
            LOG.info("Attempt num: " + appAttemptID.getAttemptId() + " is last retry: " + isLastAMRetry + " because the staging dir doesn't exist.");
            errorHappenedShutDown = true;
            forcedState = JobStateInternal.ERROR;
            shutDownMessage = "Staging dir does not exist " + stagingDir;
            LOG.fatal(shutDownMessage);
        } else if (commitStarted) {
            //A commit was started so this is the last time, we just need to know
            // what result we will use to notify, and how we will unregister
            errorHappenedShutDown = true;
            isLastAMRetry = true;
            LOG.info("Attempt num: " + appAttemptID.getAttemptId() + " is last retry: " + isLastAMRetry + " because a commit was started.");
            copyHistory = true;
            if (commitSuccess) {
                shutDownMessage = "Job commit succeeded in a prior MRAppMaster attempt " + "before it crashed. Recovering.";
                forcedState = JobStateInternal.SUCCEEDED;
            } else if (commitFailure) {
                shutDownMessage = "Job commit failed in a prior MRAppMaster attempt " + "before it crashed. Not retrying.";
                forcedState = JobStateInternal.FAILED;
            } else {
                if (isCommitJobRepeatable()) {
                    // cleanup previous half done commits if committer supports
                    // repeatable job commit.
                    errorHappenedShutDown = false;
                    cleanupInterruptedCommit(conf, fs, startCommitFile);
                } else {
                    //The commit is still pending, commit error
                    shutDownMessage = "Job commit from a prior MRAppMaster attempt is " + "potentially in progress. Preventing multiple commit executions";
                    forcedState = JobStateInternal.ERROR;
                }
            }
        }
    } catch (IOException e) {
        throw new YarnRuntimeException("Error while initializing", e);
    }
    if (errorHappenedShutDown) {
        NoopEventHandler eater = new NoopEventHandler();
        //We do not have a JobEventDispatcher in this path
        dispatcher.register(JobEventType.class, eater);
        EventHandler<JobHistoryEvent> historyService = null;
        if (copyHistory) {
            historyService = createJobHistoryHandler(context);
            dispatcher.register(org.apache.hadoop.mapreduce.jobhistory.EventType.class, historyService);
        } else {
            dispatcher.register(org.apache.hadoop.mapreduce.jobhistory.EventType.class, eater);
        }
        if (copyHistory) {
            // Now that there's a FINISHING state for application on RM to give AMs
            // plenty of time to clean up after unregister it's safe to clean staging
            // directory after unregistering with RM. So, we start the staging-dir
            // cleaner BEFORE the ContainerAllocator so that on shut-down,
            // ContainerAllocator unregisters first and then the staging-dir cleaner
            // deletes staging directory.
            addService(createStagingDirCleaningService());
        }
        // service to allocate containers from RM (if non-uber) or to fake it (uber)
        containerAllocator = createContainerAllocator(null, context);
        addIfService(containerAllocator);
        dispatcher.register(ContainerAllocator.EventType.class, containerAllocator);
        if (copyHistory) {
            // Add the JobHistoryEventHandler last so that it is properly stopped first.
            // This will guarantee that all history-events are flushed before AM goes
            // ahead with shutdown.
            // Note: Even though JobHistoryEventHandler is started last, if any
            // component creates a JobHistoryEvent in the meanwhile, it will be just be
            // queued inside the JobHistoryEventHandler 
            addIfService(historyService);
            JobHistoryCopyService cpHist = new JobHistoryCopyService(appAttemptID, dispatcher.getEventHandler());
            addIfService(cpHist);
        }
    } else {
        //service to handle requests from JobClient
        clientService = createClientService(context);
        // Init ClientService separately so that we stop it separately, since this
        // service needs to wait some time before it stops so clients can know the
        // final states
        clientService.init(conf);
        containerAllocator = createContainerAllocator(clientService, context);
        //service to handle the output committer
        committerEventHandler = createCommitterEventHandler(context, committer);
        addIfService(committerEventHandler);
        //policy handling preemption requests from RM
        callWithJobClassLoader(conf, new Action<Void>() {

            public Void call(Configuration conf) {
                preemptionPolicy = createPreemptionPolicy(conf);
                preemptionPolicy.init(context);
                return null;
            }
        });
        //service to handle requests to TaskUmbilicalProtocol
        taskAttemptListener = createTaskAttemptListener(context, preemptionPolicy);
        addIfService(taskAttemptListener);
        //service to log job history events
        EventHandler<JobHistoryEvent> historyService = createJobHistoryHandler(context);
        dispatcher.register(org.apache.hadoop.mapreduce.jobhistory.EventType.class, historyService);
        this.jobEventDispatcher = new JobEventDispatcher();
        //register the event dispatchers
        dispatcher.register(JobEventType.class, jobEventDispatcher);
        dispatcher.register(TaskEventType.class, new TaskEventDispatcher());
        dispatcher.register(TaskAttemptEventType.class, new TaskAttemptEventDispatcher());
        dispatcher.register(CommitterEventType.class, committerEventHandler);
        if (conf.getBoolean(MRJobConfig.MAP_SPECULATIVE, false) || conf.getBoolean(MRJobConfig.REDUCE_SPECULATIVE, false)) {
            //optional service to speculate on task attempts' progress
            speculator = createSpeculator(conf, context);
            addIfService(speculator);
        }
        speculatorEventDispatcher = new SpeculatorEventDispatcher(conf);
        dispatcher.register(Speculator.EventType.class, speculatorEventDispatcher);
        // Now that there's a FINISHING state for application on RM to give AMs
        // plenty of time to clean up after unregister it's safe to clean staging
        // directory after unregistering with RM. So, we start the staging-dir
        // cleaner BEFORE the ContainerAllocator so that on shut-down,
        // ContainerAllocator unregisters first and then the staging-dir cleaner
        // deletes staging directory.
        addService(createStagingDirCleaningService());
        // service to allocate containers from RM (if non-uber) or to fake it (uber)
        addIfService(containerAllocator);
        dispatcher.register(ContainerAllocator.EventType.class, containerAllocator);
        // corresponding service to launch allocated containers via NodeManager
        containerLauncher = createContainerLauncher(context);
        addIfService(containerLauncher);
        dispatcher.register(ContainerLauncher.EventType.class, containerLauncher);
        // Add the JobHistoryEventHandler last so that it is properly stopped first.
        // This will guarantee that all history-events are flushed before AM goes
        // ahead with shutdown.
        // Note: Even though JobHistoryEventHandler is started last, if any
        // component creates a JobHistoryEvent in the meanwhile, it will be just be
        // queued inside the JobHistoryEventHandler 
        addIfService(historyService);
    }
    super.serviceInit(conf);
}
Also used : JobHistoryCopyService(org.apache.hadoop.mapreduce.jobhistory.JobHistoryCopyService) Configuration(org.apache.hadoop.conf.Configuration) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) JobHistoryEvent(org.apache.hadoop.mapreduce.jobhistory.JobHistoryEvent) ContainerLauncher(org.apache.hadoop.mapreduce.v2.app.launcher.ContainerLauncher) LocalContainerLauncher(org.apache.hadoop.mapred.LocalContainerLauncher) FileSystem(org.apache.hadoop.fs.FileSystem) Path(org.apache.hadoop.fs.Path) IOException(java.io.IOException) ContainerAllocator(org.apache.hadoop.mapreduce.v2.app.rm.ContainerAllocator) RMContainerAllocator(org.apache.hadoop.mapreduce.v2.app.rm.RMContainerAllocator) LocalContainerAllocator(org.apache.hadoop.mapreduce.v2.app.local.LocalContainerAllocator) DefaultSpeculator(org.apache.hadoop.mapreduce.v2.app.speculate.DefaultSpeculator) Speculator(org.apache.hadoop.mapreduce.v2.app.speculate.Speculator) YarnRuntimeException(org.apache.hadoop.yarn.exceptions.YarnRuntimeException)

Example 2 with RunningAppContext

use of org.apache.hadoop.mapreduce.v2.app.MRAppMaster.RunningAppContext in project hadoop by apache.

the class TestKillAMPreemptionPolicy method getRunningAppContext.

private RunningAppContext getRunningAppContext() {
    RunningAppContext mActxt = mock(RunningAppContext.class);
    @SuppressWarnings("unchecked") EventHandler<Event> eventHandler = mock(EventHandler.class);
    when(mActxt.getEventHandler()).thenReturn(eventHandler);
    return mActxt;
}
Also used : RunningAppContext(org.apache.hadoop.mapreduce.v2.app.MRAppMaster.RunningAppContext) Event(org.apache.hadoop.yarn.event.Event) TaskAttemptEvent(org.apache.hadoop.mapreduce.v2.app.job.event.TaskAttemptEvent) JobCounterUpdateEvent(org.apache.hadoop.mapreduce.v2.app.job.event.JobCounterUpdateEvent)

Example 3 with RunningAppContext

use of org.apache.hadoop.mapreduce.v2.app.MRAppMaster.RunningAppContext in project hadoop by apache.

the class RMCommunicator method doUnregistration.

@VisibleForTesting
protected void doUnregistration() throws YarnException, IOException, InterruptedException {
    FinalApplicationStatus finishState = FinalApplicationStatus.UNDEFINED;
    JobImpl jobImpl = (JobImpl) job;
    if (jobImpl.getInternalState() == JobStateInternal.SUCCEEDED) {
        finishState = FinalApplicationStatus.SUCCEEDED;
    } else if (jobImpl.getInternalState() == JobStateInternal.KILLED || (jobImpl.getInternalState() == JobStateInternal.RUNNING && isSignalled)) {
        finishState = FinalApplicationStatus.KILLED;
    } else if (jobImpl.getInternalState() == JobStateInternal.FAILED || jobImpl.getInternalState() == JobStateInternal.ERROR) {
        finishState = FinalApplicationStatus.FAILED;
    }
    StringBuffer sb = new StringBuffer();
    for (String s : job.getDiagnostics()) {
        sb.append(s).append("\n");
    }
    LOG.info("Setting job diagnostics to " + sb.toString());
    String historyUrl = MRWebAppUtil.getApplicationWebURLOnJHSWithScheme(getConfig(), context.getApplicationID());
    LOG.info("History url is " + historyUrl);
    FinishApplicationMasterRequest request = FinishApplicationMasterRequest.newInstance(finishState, sb.toString(), historyUrl);
    try {
        while (true) {
            FinishApplicationMasterResponse response = scheduler.finishApplicationMaster(request);
            if (response.getIsUnregistered()) {
                // When excepting ClientService, other services are already stopped,
                // it is safe to let clients know the final states. ClientService
                // should wait for some time so clients have enough time to know the
                // final states.
                RunningAppContext raContext = (RunningAppContext) context;
                raContext.markSuccessfulUnregistration();
                break;
            }
            LOG.info("Waiting for application to be successfully unregistered.");
            Thread.sleep(rmPollInterval);
        }
    } catch (ApplicationMasterNotRegisteredException e) {
        // RM might have restarted or failed over and so lost the fact that AM had
        // registered before.
        register();
        doUnregistration();
    }
}
Also used : ApplicationMasterNotRegisteredException(org.apache.hadoop.yarn.exceptions.ApplicationMasterNotRegisteredException) JobImpl(org.apache.hadoop.mapreduce.v2.app.job.impl.JobImpl) FinalApplicationStatus(org.apache.hadoop.yarn.api.records.FinalApplicationStatus) RunningAppContext(org.apache.hadoop.mapreduce.v2.app.MRAppMaster.RunningAppContext) FinishApplicationMasterResponse(org.apache.hadoop.yarn.api.protocolrecords.FinishApplicationMasterResponse) FinishApplicationMasterRequest(org.apache.hadoop.yarn.api.protocolrecords.FinishApplicationMasterRequest) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with RunningAppContext

use of org.apache.hadoop.mapreduce.v2.app.MRAppMaster.RunningAppContext in project hadoop by apache.

the class MRAppMaster method createJob.

/** Create and initialize (but don't start) a single job. 
   * @param forcedState a state to force the job into or null for normal operation. 
   * @param diagnostic a diagnostic message to include with the job.
   */
protected Job createJob(Configuration conf, JobStateInternal forcedState, String diagnostic) {
    // create single job
    Job newJob = new JobImpl(jobId, appAttemptID, conf, dispatcher.getEventHandler(), taskAttemptListener, jobTokenSecretManager, jobCredentials, clock, completedTasksFromPreviousRun, metrics, committer, newApiCommitter, currentUser.getUserName(), appSubmitTime, amInfos, context, forcedState, diagnostic);
    ((RunningAppContext) context).jobs.put(newJob.getID(), newJob);
    dispatcher.register(JobFinishEvent.Type.class, createJobFinishEventHandler());
    return newJob;
}
Also used : JobImpl(org.apache.hadoop.mapreduce.v2.app.job.impl.JobImpl) JobFinishEvent(org.apache.hadoop.mapreduce.v2.app.job.event.JobFinishEvent) Job(org.apache.hadoop.mapreduce.v2.app.job.Job)

Example 5 with RunningAppContext

use of org.apache.hadoop.mapreduce.v2.app.MRAppMaster.RunningAppContext in project hadoop by apache.

the class JHEventHandlerForSigtermTest method mockAppContext.

private AppContext mockAppContext(Class<? extends AppContext> contextClass, ApplicationId appId, boolean isLastAMRetry) {
    JobId jobId = TypeConverter.toYarn(TypeConverter.fromYarn(appId));
    AppContext mockContext = mock(contextClass);
    Job mockJob = mockJob();
    when(mockContext.getJob(jobId)).thenReturn(mockJob);
    when(mockContext.getApplicationID()).thenReturn(appId);
    when(mockContext.isLastAMRetry()).thenReturn(isLastAMRetry);
    if (mockContext instanceof RunningAppContext) {
        when(((RunningAppContext) mockContext).getTimelineClient()).thenReturn(TimelineClient.createTimelineClient());
        when(((RunningAppContext) mockContext).getTimelineV2Client()).thenReturn(TimelineV2Client.createTimelineClient(ApplicationId.newInstance(0, 1)));
    }
    return mockContext;
}
Also used : RunningAppContext(org.apache.hadoop.mapreduce.v2.app.MRAppMaster.RunningAppContext) RunningAppContext(org.apache.hadoop.mapreduce.v2.app.MRAppMaster.RunningAppContext) AppContext(org.apache.hadoop.mapreduce.v2.app.AppContext) Job(org.apache.hadoop.mapreduce.v2.app.job.Job) JobId(org.apache.hadoop.mapreduce.v2.api.records.JobId)

Aggregations

RunningAppContext (org.apache.hadoop.mapreduce.v2.app.MRAppMaster.RunningAppContext)5 FileSystem (org.apache.hadoop.fs.FileSystem)2 Path (org.apache.hadoop.fs.Path)2 JobId (org.apache.hadoop.mapreduce.v2.api.records.JobId)2 Job (org.apache.hadoop.mapreduce.v2.app.job.Job)2 JobCounterUpdateEvent (org.apache.hadoop.mapreduce.v2.app.job.event.JobCounterUpdateEvent)2 TaskAttemptEvent (org.apache.hadoop.mapreduce.v2.app.job.event.TaskAttemptEvent)2 JobImpl (org.apache.hadoop.mapreduce.v2.app.job.impl.JobImpl)2 ApplicationId (org.apache.hadoop.yarn.api.records.ApplicationId)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Configuration (org.apache.hadoop.conf.Configuration)1 LocalContainerLauncher (org.apache.hadoop.mapred.LocalContainerLauncher)1 JobHistoryCopyService (org.apache.hadoop.mapreduce.jobhistory.JobHistoryCopyService)1 JobHistoryEvent (org.apache.hadoop.mapreduce.jobhistory.JobHistoryEvent)1 TaskType (org.apache.hadoop.mapreduce.v2.api.records.TaskType)1 AppContext (org.apache.hadoop.mapreduce.v2.app.AppContext)1 JobFinishEvent (org.apache.hadoop.mapreduce.v2.app.job.event.JobFinishEvent)1 ContainerLauncher (org.apache.hadoop.mapreduce.v2.app.launcher.ContainerLauncher)1