/// <exception cref="Org.Apache.Hadoop.Yarn.Exceptions.YarnException"/>
        /// <exception cref="System.IO.IOException"/>
        public virtual FinishApplicationMasterResponse FinishApplicationMaster(FinishApplicationMasterRequest
                                                                               request)
        {
            ApplicationAttemptId applicationAttemptId = AuthorizeRequest().GetApplicationAttemptId
                                                            ();
            ApplicationId appId = applicationAttemptId.GetApplicationId();
            RMApp         rmApp = rmContext.GetRMApps()[applicationAttemptId.GetApplicationId()];

            // checking whether the app exits in RMStateStore at first not to throw
            // ApplicationDoesNotExistInCacheException before and after
            // RM work-preserving restart.
            if (rmApp.IsAppFinalStateStored())
            {
                Log.Info(rmApp.GetApplicationId() + " unregistered successfully. ");
                return(FinishApplicationMasterResponse.NewInstance(true));
            }
            ApplicationMasterService.AllocateResponseLock Lock = responseMap[applicationAttemptId
                                                                 ];
            if (Lock == null)
            {
                ThrowApplicationDoesNotExistInCacheException(applicationAttemptId);
            }
            // Allow only one thread in AM to do finishApp at a time.
            lock (Lock)
            {
                if (!HasApplicationMasterRegistered(applicationAttemptId))
                {
                    string message = "Application Master is trying to unregister before registering for: "
                                     + appId;
                    Log.Error(message);
                    RMAuditLogger.LogFailure(this.rmContext.GetRMApps()[appId].GetUser(), RMAuditLogger.AuditConstants
                                             .UnregisterAm, string.Empty, "ApplicationMasterService", message, appId, applicationAttemptId
                                             );
                    throw new ApplicationMasterNotRegisteredException(message);
                }
                this.amLivelinessMonitor.ReceivedPing(applicationAttemptId);
                rmContext.GetDispatcher().GetEventHandler().Handle(new RMAppAttemptUnregistrationEvent
                                                                       (applicationAttemptId, request.GetTrackingUrl(), request.GetFinalApplicationStatus
                                                                           (), request.GetDiagnostics()));
                // For UnmanagedAMs, return true so they don't retry
                return(FinishApplicationMasterResponse.NewInstance(rmApp.GetApplicationSubmissionContext
                                                                       ().GetUnmanagedAM()));
            }
        }
 public virtual FinishApplicationMasterResponse CreateFakeFinishApplicationMasterResponse
     ()
 {
     return(FinishApplicationMasterResponse.NewInstance(true));
 }