Example #1
0
        private void PublishApplicationFinishedEvent(ApplicationFinishedEvent @event)
        {
            TimelineEntity entity = CreateApplicationEntity(@event.GetApplicationId());
            TimelineEvent  tEvent = new TimelineEvent();

            tEvent.SetEventType(ApplicationMetricsConstants.FinishedEventType);
            tEvent.SetTimestamp(@event.GetTimestamp());
            IDictionary <string, object> eventInfo = new Dictionary <string, object>();

            eventInfo[ApplicationMetricsConstants.DiagnosticsInfoEventInfo] = @event.GetDiagnosticsInfo
                                                                                  ();
            eventInfo[ApplicationMetricsConstants.FinalStatusEventInfo] = @event.GetFinalApplicationStatus
                                                                              ().ToString();
            eventInfo[ApplicationMetricsConstants.StateEventInfo] = @event.GetYarnApplicationState
                                                                        ().ToString();
            if (@event.GetLatestApplicationAttemptId() != null)
            {
                eventInfo[ApplicationMetricsConstants.LatestAppAttemptEventInfo] = @event.GetLatestApplicationAttemptId
                                                                                       ().ToString();
            }
            RMAppMetrics appMetrics = @event.GetAppMetrics();

            entity.AddOtherInfo(ApplicationMetricsConstants.AppCpuMetrics, appMetrics.GetVcoreSeconds
                                    ());
            entity.AddOtherInfo(ApplicationMetricsConstants.AppMemMetrics, appMetrics.GetMemorySeconds
                                    ());
            tEvent.SetEventInfo(eventInfo);
            entity.AddEvent(tEvent);
            PutEntity(entity);
        }
Example #2
0
            /// <summary>create a summary of the application's runtime.</summary>
            /// <param name="app">
            ///
            /// <see cref="Org.Apache.Hadoop.Yarn.Server.Resourcemanager.Rmapp.RMApp"/>
            /// whose summary is to be created, cannot
            /// be <code>null</code>.
            /// </param>
            public static RMAppManager.ApplicationSummary.SummaryBuilder CreateAppSummary(RMApp
                                                                                          app)
            {
                string       trackingUrl = "N/A";
                string       host        = "N/A";
                RMAppAttempt attempt     = app.GetCurrentAppAttempt();

                if (attempt != null)
                {
                    trackingUrl = attempt.GetTrackingUrl();
                    host        = attempt.GetHost();
                }
                RMAppMetrics metrics = app.GetRMAppMetrics();

                RMAppManager.ApplicationSummary.SummaryBuilder summary = new RMAppManager.ApplicationSummary.SummaryBuilder
                                                                             ().Add("appId", app.GetApplicationId()).Add("name", app.GetName()).Add("user", app
                                                                                                                                                    .GetUser()).Add("queue", app.GetQueue()).Add("state", app.GetState()).Add("trackingUrl"
                                                                                                                                                                                                                              , trackingUrl).Add("appMasterHost", host).Add("startTime", app.GetStartTime()).Add
                                                                             ("finishTime", app.GetFinishTime()).Add("finalStatus", app.GetFinalApplicationStatus
                                                                                                                         ()).Add("memorySeconds", metrics.GetMemorySeconds()).Add("vcoreSeconds", metrics
                                                                                                                                                                                  .GetVcoreSeconds()).Add("preemptedAMContainers", metrics.GetNumAMContainersPreempted
                                                                                                                                                                                                              ()).Add("preemptedNonAMContainers", metrics.GetNumNonAMContainersPreempted()).Add
                                                                             ("preemptedResources", metrics.GetResourcePreempted()).Add("applicationType", app
                                                                                                                                        .GetApplicationType());
                return(summary);
            }
Example #3
0
        public virtual void TestEscapeApplicationSummary()
        {
            RMApp app = Org.Mockito.Mockito.Mock <RMAppImpl>();

            Org.Mockito.Mockito.When(app.GetApplicationId()).ThenReturn(ApplicationId.NewInstance
                                                                            (100L, 1));
            Org.Mockito.Mockito.When(app.GetName()).ThenReturn("Multiline\n\n\r\rAppName");
            Org.Mockito.Mockito.When(app.GetUser()).ThenReturn("Multiline\n\n\r\rUserName");
            Org.Mockito.Mockito.When(app.GetQueue()).ThenReturn("Multiline\n\n\r\rQueueName");
            Org.Mockito.Mockito.When(app.GetState()).ThenReturn(RMAppState.Running);
            Org.Mockito.Mockito.When(app.GetApplicationType()).ThenReturn("MAPREDUCE");
            RMAppMetrics metrics = new RMAppMetrics(Org.Apache.Hadoop.Yarn.Api.Records.Resource
                                                    .NewInstance(1234, 56), 10, 1, 16384, 64);

            Org.Mockito.Mockito.When(app.GetRMAppMetrics()).ThenReturn(metrics);
            RMAppManager.ApplicationSummary.SummaryBuilder summary = RMAppManager.ApplicationSummary
                                                                     .CreateAppSummary(app);
            string msg = summary.ToString();

            Log.Info("summary: " + msg);
            NUnit.Framework.Assert.IsFalse(msg.Contains("\n"));
            NUnit.Framework.Assert.IsFalse(msg.Contains("\r"));
            string escaped = "\\n\\n\\r\\r";

            NUnit.Framework.Assert.IsTrue(msg.Contains("Multiline" + escaped + "AppName"));
            NUnit.Framework.Assert.IsTrue(msg.Contains("Multiline" + escaped + "UserName"));
            NUnit.Framework.Assert.IsTrue(msg.Contains("Multiline" + escaped + "QueueName"));
            NUnit.Framework.Assert.IsTrue(msg.Contains("memorySeconds=16384"));
            NUnit.Framework.Assert.IsTrue(msg.Contains("vcoreSeconds=64"));
            NUnit.Framework.Assert.IsTrue(msg.Contains("preemptedAMContainers=1"));
            NUnit.Framework.Assert.IsTrue(msg.Contains("preemptedNonAMContainers=10"));
            NUnit.Framework.Assert.IsTrue(msg.Contains("preemptedResources=<memory:1234\\, vCores:56>"
                                                       ));
            NUnit.Framework.Assert.IsTrue(msg.Contains("applicationType=MAPREDUCE"));
        }
 public ApplicationFinishedEvent(ApplicationId appId, string diagnosticsInfo, FinalApplicationStatus
                                 appStatus, YarnApplicationState state, ApplicationAttemptId latestAppAttemptId,
                                 long finishedTime, RMAppMetrics appMetrics)
     : base(SystemMetricsEventType.AppFinished, finishedTime)
 {
     this.appId              = appId;
     this.diagnosticsInfo    = diagnosticsInfo;
     this.appStatus          = appStatus;
     this.latestAppAttemptId = latestAppAttemptId;
     this.state              = state;
     this.appMetrics         = appMetrics;
 }
        /// <exception cref="System.Exception"/>
        public virtual void TestUsageWithOneAttemptAndOneContainer()
        {
            MockRM rm = new MockRM(conf);

            rm.Start();
            MockNM nm = new MockNM("127.0.0.1:1234", 15120, rm.GetResourceTrackerService());

            nm.RegisterNode();
            RMApp        app0         = rm.SubmitApp(200);
            RMAppMetrics rmAppMetrics = app0.GetRMAppMetrics();

            NUnit.Framework.Assert.IsTrue("Before app submittion, memory seconds should have been 0 but was "
                                          + rmAppMetrics.GetMemorySeconds(), rmAppMetrics.GetMemorySeconds() == 0);
            NUnit.Framework.Assert.IsTrue("Before app submission, vcore seconds should have been 0 but was "
                                          + rmAppMetrics.GetVcoreSeconds(), rmAppMetrics.GetVcoreSeconds() == 0);
            RMAppAttempt attempt0 = app0.GetCurrentAppAttempt();

            nm.NodeHeartbeat(true);
            MockAM am0 = rm.SendAMLaunched(attempt0.GetAppAttemptId());

            am0.RegisterAppAttempt();
            RMContainer rmContainer = rm.GetResourceScheduler().GetRMContainer(attempt0.GetMasterContainer
                                                                                   ().GetId());
            // Allow metrics to accumulate.
            int sleepInterval       = 1000;
            int cumulativeSleepTime = 0;

            while (rmAppMetrics.GetMemorySeconds() <= 0 && cumulativeSleepTime < 5000)
            {
                Sharpen.Thread.Sleep(sleepInterval);
                cumulativeSleepTime += sleepInterval;
            }
            rmAppMetrics = app0.GetRMAppMetrics();
            NUnit.Framework.Assert.IsTrue("While app is running, memory seconds should be >0 but is "
                                          + rmAppMetrics.GetMemorySeconds(), rmAppMetrics.GetMemorySeconds() > 0);
            NUnit.Framework.Assert.IsTrue("While app is running, vcore seconds should be >0 but is "
                                          + rmAppMetrics.GetVcoreSeconds(), rmAppMetrics.GetVcoreSeconds() > 0);
            MockRM.FinishAMAndVerifyAppState(app0, rm, nm, am0);
            AggregateAppResourceUsage ru = CalculateContainerResourceMetrics(rmContainer);

            rmAppMetrics = app0.GetRMAppMetrics();
            NUnit.Framework.Assert.AreEqual("Unexcpected MemorySeconds value", ru.GetMemorySeconds
                                                (), rmAppMetrics.GetMemorySeconds());
            NUnit.Framework.Assert.AreEqual("Unexpected VcoreSeconds value", ru.GetVcoreSeconds
                                                (), rmAppMetrics.GetVcoreSeconds());
            rm.Stop();
        }
Example #6
0
        public virtual void TestAppBlockRenderWithNullCurrentAppAttempt()
        {
            ApplicationId AppId = ApplicationId.NewInstance(1234L, 0);
            Injector      injector;
            // init app
            RMApp app = Org.Mockito.Mockito.Mock <RMApp>();

            Org.Mockito.Mockito.When(app.GetTrackingUrl()).ThenReturn("http://host:123");
            Org.Mockito.Mockito.When(app.GetState()).ThenReturn(RMAppState.Failed);
            Org.Mockito.Mockito.When(app.GetApplicationId()).ThenReturn(AppId);
            Org.Mockito.Mockito.When(app.GetApplicationType()).ThenReturn("Type");
            Org.Mockito.Mockito.When(app.GetUser()).ThenReturn("user");
            Org.Mockito.Mockito.When(app.GetName()).ThenReturn("Name");
            Org.Mockito.Mockito.When(app.GetQueue()).ThenReturn("queue");
            Org.Mockito.Mockito.When(app.GetDiagnostics()).ThenReturn(new StringBuilder());
            Org.Mockito.Mockito.When(app.GetFinalApplicationStatus()).ThenReturn(FinalApplicationStatus
                                                                                 .Failed);
            Org.Mockito.Mockito.When(app.GetFinalApplicationStatus()).ThenReturn(FinalApplicationStatus
                                                                                 .Failed);
            Org.Mockito.Mockito.When(app.GetStartTime()).ThenReturn(0L);
            Org.Mockito.Mockito.When(app.GetFinishTime()).ThenReturn(0L);
            Org.Mockito.Mockito.When(app.CreateApplicationState()).ThenReturn(YarnApplicationState
                                                                              .Failed);
            RMAppMetrics appMetrics = new RMAppMetrics(Resource.NewInstance(0, 0), 0, 0, 0, 0
                                                       );

            Org.Mockito.Mockito.When(app.GetRMAppMetrics()).ThenReturn(appMetrics);
            // initialize RM Context, and create RMApp, without creating RMAppAttempt
            RMContext rmContext = TestRMWebApp.MockRMContext(15, 1, 2, 8);

            rmContext.GetRMApps()[AppId] = app;
            injector = WebAppTests.CreateMockInjector <RMContext>(rmContext, new _Module_76(rmContext
                                                                                            ));
            AppBlock instance = injector.GetInstance <AppBlock>();

            instance.Set(YarnWebParams.ApplicationId, AppId.ToString());
            instance.Render();
        }
Example #7
0
        protected override void CreateApplicationMetricsTable(HtmlBlock.Block html)
        {
            RMApp        rmApp      = this.rm.GetRMContext().GetRMApps()[appID];
            RMAppMetrics appMetrics = rmApp == null ? null : rmApp.GetRMAppMetrics();
            // Get attempt metrics and fields, it is possible currentAttempt of RMApp is
            // null. In that case, we will assume resource preempted and number of Non
            // AM container preempted on that attempt is 0
            RMAppAttemptMetrics attemptMetrics;

            if (rmApp == null || null == rmApp.GetCurrentAppAttempt())
            {
                attemptMetrics = null;
            }
            else
            {
                attemptMetrics = rmApp.GetCurrentAppAttempt().GetRMAppAttemptMetrics();
            }
            Org.Apache.Hadoop.Yarn.Api.Records.Resource attemptResourcePreempted = attemptMetrics
                                                                                   == null?Resources.None() : attemptMetrics.GetResourcePreempted();

            int attemptNumNonAMContainerPreempted = attemptMetrics == null ? 0 : attemptMetrics
                                                    .GetNumNonAMContainersPreempted();

            Hamlet.DIV <Org.Apache.Hadoop.Yarn.Webapp.Hamlet.Hamlet> pdiv = html.(typeof(InfoBlock
                                                                                         )).Div(JQueryUI.InfoWrap);
            Info("Application Overview").Clear();
            Info("Application Metrics").("Total Resource Preempted:", appMetrics == null ? "N/A"
                                 : appMetrics.GetResourcePreempted()).("Total Number of Non-AM Containers Preempted:"
                                                                       , appMetrics == null ? "N/A" : appMetrics.GetNumNonAMContainersPreempted()).("Total Number of AM Containers Preempted:"
                                                                                                                                                    , appMetrics == null ? "N/A" : appMetrics.GetNumAMContainersPreempted()).("Resource Preempted from Current Attempt:"
                                                                                                                                                                                                                              , attemptResourcePreempted).("Number of Non-AM Containers Preempted from Current Attempt:"
                                                                                                                                                                                                                                                           , attemptNumNonAMContainerPreempted).("Aggregate Resource Allocation:", string.Format
                                                                                                                                                                                                                                                                                                     ("%d MB-seconds, %d vcore-seconds", appMetrics == null ? "N/A" : appMetrics.GetMemorySeconds
                                                                                                                                                                                                                                                                                                         (), appMetrics == null ? "N/A" : appMetrics.GetVcoreSeconds()));
            pdiv.();
        }
Example #8
0
 public AppInfo(ResourceManager rm, RMApp app, bool hasAccess, string schemePrefix
                )
 {
     // these are ok for any user to see
     // these are only allowed if acls allow
     // preemption info fields
     // JAXB needs this
     this.schemePrefix = schemePrefix;
     if (app != null)
     {
         string trackingUrl = app.GetTrackingUrl();
         this.state = app.CreateApplicationState();
         this.trackingUrlIsNotReady = trackingUrl == null || trackingUrl.IsEmpty() || YarnApplicationState
                                      .New == this.state || YarnApplicationState.NewSaving == this.state || YarnApplicationState
                                      .Submitted == this.state || YarnApplicationState.Accepted == this.state;
         this.trackingUI = this.trackingUrlIsNotReady ? "UNASSIGNED" : (app.GetFinishTime(
                                                                            ) == 0 ? "ApplicationMaster" : "History");
         if (!trackingUrlIsNotReady)
         {
             this.trackingUrl       = WebAppUtils.GetURLWithScheme(schemePrefix, trackingUrl);
             this.trackingUrlPretty = this.trackingUrl;
         }
         else
         {
             this.trackingUrlPretty = "UNASSIGNED";
         }
         this.applicationId   = app.GetApplicationId();
         this.applicationType = app.GetApplicationType();
         this.appIdNum        = app.GetApplicationId().GetId().ToString();
         this.id          = app.GetApplicationId().ToString();
         this.user        = app.GetUser().ToString();
         this.name        = app.GetName().ToString();
         this.queue       = app.GetQueue().ToString();
         this.progress    = app.GetProgress() * 100;
         this.diagnostics = app.GetDiagnostics().ToString();
         if (diagnostics == null || diagnostics.IsEmpty())
         {
             this.diagnostics = string.Empty;
         }
         if (app.GetApplicationTags() != null && !app.GetApplicationTags().IsEmpty())
         {
             this.applicationTags = Joiner.On(',').Join(app.GetApplicationTags());
         }
         this.finalStatus = app.GetFinalApplicationStatus();
         this.clusterId   = ResourceManager.GetClusterTimeStamp();
         if (hasAccess)
         {
             this.startedTime  = app.GetStartTime();
             this.finishedTime = app.GetFinishTime();
             this.elapsedTime  = Times.Elapsed(app.GetStartTime(), app.GetFinishTime());
             RMAppAttempt attempt = app.GetCurrentAppAttempt();
             if (attempt != null)
             {
                 Container masterContainer = attempt.GetMasterContainer();
                 if (masterContainer != null)
                 {
                     this.amContainerLogsExist = true;
                     this.amContainerLogs      = WebAppUtils.GetRunningLogURL(schemePrefix + masterContainer
                                                                              .GetNodeHttpAddress(), ConverterUtils.ToString(masterContainer.GetId()), app.GetUser
                                                                                  ());
                     this.amHostHttpAddress = masterContainer.GetNodeHttpAddress();
                 }
                 ApplicationResourceUsageReport resourceReport = attempt.GetApplicationResourceUsageReport
                                                                     ();
                 if (resourceReport != null)
                 {
                     Resource usedResources = resourceReport.GetUsedResources();
                     allocatedMB       = usedResources.GetMemory();
                     allocatedVCores   = usedResources.GetVirtualCores();
                     runningContainers = resourceReport.GetNumUsedContainers();
                 }
                 resourceRequests = ((AbstractYarnScheduler)rm.GetRMContext().GetScheduler()).GetPendingResourceRequestsForAttempt
                                        (attempt.GetAppAttemptId());
             }
         }
         // copy preemption info fields
         RMAppMetrics appMetrics = app.GetRMAppMetrics();
         numAMContainerPreempted    = appMetrics.GetNumAMContainersPreempted();
         preemptedResourceMB        = appMetrics.GetResourcePreempted().GetMemory();
         numNonAMContainerPreempted = appMetrics.GetNumNonAMContainersPreempted();
         preemptedResourceVCores    = appMetrics.GetResourcePreempted().GetVirtualCores();
         memorySeconds = appMetrics.GetMemorySeconds();
         vcoreSeconds  = appMetrics.GetVcoreSeconds();
     }
 }
        /// <exception cref="System.Exception"/>
        private void AmRestartTests(bool keepRunningContainers)
        {
            MockRM rm = new MockRM(conf);

            rm.Start();
            RMApp app = rm.SubmitApp(200, "name", "user", new Dictionary <ApplicationAccessType
                                                                          , string>(), false, "default", -1, null, "MAPREDUCE", false, keepRunningContainers
                                     );
            MockNM nm = new MockNM("127.0.0.1:1234", 10240, rm.GetResourceTrackerService());

            nm.RegisterNode();
            MockAM am0           = MockRM.LaunchAndRegisterAM(app, rm, nm);
            int    NumContainers = 1;

            // allocate NUM_CONTAINERS containers
            am0.Allocate("127.0.0.1", 1024, NumContainers, new AList <ContainerId>());
            nm.NodeHeartbeat(true);
            // wait for containers to be allocated.
            IList <Container> containers = am0.Allocate(new AList <ResourceRequest>(), new AList
                                                        <ContainerId>()).GetAllocatedContainers();

            while (containers.Count != NumContainers)
            {
                nm.NodeHeartbeat(true);
                Sharpen.Collections.AddAll(containers, am0.Allocate(new AList <ResourceRequest>(),
                                                                    new AList <ContainerId>()).GetAllocatedContainers());
                Sharpen.Thread.Sleep(200);
            }
            // launch the 2nd container.
            ContainerId containerId2 = ContainerId.NewContainerId(am0.GetApplicationAttemptId
                                                                      (), 2);

            nm.NodeHeartbeat(am0.GetApplicationAttemptId(), containerId2.GetContainerId(), ContainerState
                             .Running);
            rm.WaitForState(nm, containerId2, RMContainerState.Running);
            // Capture the containers here so the metrics can be calculated after the
            // app has completed.
            ICollection <RMContainer> rmContainers = rm.scheduler.GetSchedulerAppInfo(am0.GetApplicationAttemptId
                                                                                          ()).GetLiveContainers();
            // fail the first app attempt by sending CONTAINER_FINISHED event without
            // registering.
            ContainerId amContainerId = app.GetCurrentAppAttempt().GetMasterContainer().GetId
                                            ();

            nm.NodeHeartbeat(am0.GetApplicationAttemptId(), amContainerId.GetContainerId(), ContainerState
                             .Complete);
            am0.WaitForState(RMAppAttemptState.Failed);
            long memorySeconds = 0;
            long vcoreSeconds  = 0;

            // Calculate container usage metrics for first attempt.
            if (keepRunningContainers)
            {
                // Only calculate the usage for the one container that has completed.
                foreach (RMContainer c in rmContainers)
                {
                    if (c.GetContainerId().Equals(amContainerId))
                    {
                        AggregateAppResourceUsage ru = CalculateContainerResourceMetrics(c);
                        memorySeconds += ru.GetMemorySeconds();
                        vcoreSeconds  += ru.GetVcoreSeconds();
                    }
                    else
                    {
                        // The remaining container should be RUNNING.
                        NUnit.Framework.Assert.IsTrue("After first attempt failed, remaining container "
                                                      + "should still be running. ", c.GetContainerState().Equals(ContainerState.Running
                                                                                                                  ));
                    }
                }
            }
            else
            {
                // If keepRunningContainers is false, all live containers should now
                // be completed. Calculate the resource usage metrics for all of them.
                foreach (RMContainer c in rmContainers)
                {
                    AggregateAppResourceUsage ru = CalculateContainerResourceMetrics(c);
                    memorySeconds += ru.GetMemorySeconds();
                    vcoreSeconds  += ru.GetVcoreSeconds();
                }
            }
            // wait for app to start a new attempt.
            rm.WaitForState(app.GetApplicationId(), RMAppState.Accepted);
            // assert this is a new AM.
            RMAppAttempt attempt2 = app.GetCurrentAppAttempt();

            NUnit.Framework.Assert.IsFalse(attempt2.GetAppAttemptId().Equals(am0.GetApplicationAttemptId
                                                                                 ()));
            // launch the new AM
            nm.NodeHeartbeat(true);
            MockAM am1 = rm.SendAMLaunched(attempt2.GetAppAttemptId());

            am1.RegisterAppAttempt();
            // allocate NUM_CONTAINERS containers
            am1.Allocate("127.0.0.1", 1024, NumContainers, new AList <ContainerId>());
            nm.NodeHeartbeat(true);
            // wait for containers to be allocated.
            containers = am1.Allocate(new AList <ResourceRequest>(), new AList <ContainerId>())
                         .GetAllocatedContainers();
            while (containers.Count != NumContainers)
            {
                nm.NodeHeartbeat(true);
                Sharpen.Collections.AddAll(containers, am1.Allocate(new AList <ResourceRequest>(),
                                                                    new AList <ContainerId>()).GetAllocatedContainers());
                Sharpen.Thread.Sleep(200);
            }
            rm.WaitForState(app.GetApplicationId(), RMAppState.Running);
            // Capture running containers for later use by metrics calculations.
            rmContainers = rm.scheduler.GetSchedulerAppInfo(attempt2.GetAppAttemptId()).GetLiveContainers
                               ();
            // complete container by sending the container complete event which has
            // earlier attempt's attemptId
            amContainerId = app.GetCurrentAppAttempt().GetMasterContainer().GetId();
            nm.NodeHeartbeat(am0.GetApplicationAttemptId(), amContainerId.GetContainerId(), ContainerState
                             .Complete);
            MockRM.FinishAMAndVerifyAppState(app, rm, nm, am1);
            // Calculate container usage metrics for second attempt.
            foreach (RMContainer c_1 in rmContainers)
            {
                AggregateAppResourceUsage ru = CalculateContainerResourceMetrics(c_1);
                memorySeconds += ru.GetMemorySeconds();
                vcoreSeconds  += ru.GetVcoreSeconds();
            }
            RMAppMetrics rmAppMetrics = app.GetRMAppMetrics();

            NUnit.Framework.Assert.AreEqual("Unexcpected MemorySeconds value", memorySeconds,
                                            rmAppMetrics.GetMemorySeconds());
            NUnit.Framework.Assert.AreEqual("Unexpected VcoreSeconds value", vcoreSeconds, rmAppMetrics
                                            .GetVcoreSeconds());
            rm.Stop();
            return;
        }
        /// <exception cref="System.Exception"/>
        public virtual void TestUsageWithMultipleContainersAndRMRestart()
        {
            // Set max attempts to 1 so that when the first attempt fails, the app
            // won't try to start a new one.
            conf.SetInt(YarnConfiguration.RmAmMaxAttempts, 1);
            conf.SetBoolean(YarnConfiguration.RecoveryEnabled, true);
            conf.SetBoolean(YarnConfiguration.RmWorkPreservingRecoveryEnabled, false);
            MemoryRMStateStore memStore = new MemoryRMStateStore();

            memStore.Init(conf);
            MockRM rm0 = new MockRM(conf, memStore);

            rm0.Start();
            MockNM nm = new MockNM("127.0.0.1:1234", 65536, rm0.GetResourceTrackerService());

            nm.RegisterNode();
            RMApp app0 = rm0.SubmitApp(200);

            rm0.WaitForState(app0.GetApplicationId(), RMAppState.Accepted);
            RMAppAttempt         attempt0   = app0.GetCurrentAppAttempt();
            ApplicationAttemptId attemptId0 = attempt0.GetAppAttemptId();

            rm0.WaitForState(attemptId0, RMAppAttemptState.Scheduled);
            nm.NodeHeartbeat(true);
            rm0.WaitForState(attemptId0, RMAppAttemptState.Allocated);
            MockAM am0 = rm0.SendAMLaunched(attempt0.GetAppAttemptId());

            am0.RegisterAppAttempt();
            int NumContainers = 2;

            am0.Allocate("127.0.0.1", 1000, NumContainers, new AList <ContainerId>());
            nm.NodeHeartbeat(true);
            IList <Container> conts = am0.Allocate(new AList <ResourceRequest>(), new AList <ContainerId
                                                                                             >()).GetAllocatedContainers();

            while (conts.Count != NumContainers)
            {
                nm.NodeHeartbeat(true);
                Sharpen.Collections.AddAll(conts, am0.Allocate(new AList <ResourceRequest>(), new
                                                               AList <ContainerId>()).GetAllocatedContainers());
                Sharpen.Thread.Sleep(500);
            }
            // launch the 2nd and 3rd containers.
            foreach (Container c in conts)
            {
                nm.NodeHeartbeat(attempt0.GetAppAttemptId(), c.GetId().GetContainerId(), ContainerState
                                 .Running);
                rm0.WaitForState(nm, c.GetId(), RMContainerState.Running);
            }
            // Get the RMContainers for all of the live containers, to be used later
            // for metrics calculations and comparisons.
            ICollection <RMContainer> rmContainers = rm0.scheduler.GetSchedulerAppInfo(attempt0
                                                                                       .GetAppAttemptId()).GetLiveContainers();
            // Allow metrics to accumulate.
            int sleepInterval       = 1000;
            int cumulativeSleepTime = 0;

            while (app0.GetRMAppMetrics().GetMemorySeconds() <= 0 && cumulativeSleepTime < 5000
                   )
            {
                Sharpen.Thread.Sleep(sleepInterval);
                cumulativeSleepTime += sleepInterval;
            }
            // Stop all non-AM containers
            foreach (Container c_1 in conts)
            {
                if (c_1.GetId().GetContainerId() == 1)
                {
                    continue;
                }
                nm.NodeHeartbeat(attempt0.GetAppAttemptId(), c_1.GetId().GetContainerId(), ContainerState
                                 .Complete);
                rm0.WaitForState(nm, c_1.GetId(), RMContainerState.Completed);
            }
            // After all other containers have completed, manually complete the master
            // container in order to trigger a save to the state store of the resource
            // usage metrics. This will cause the attempt to fail, and, since the max
            // attempt retries is 1, the app will also fail. This is intentional so
            // that all containers will complete prior to saving.
            ContainerId cId = ContainerId.NewContainerId(attempt0.GetAppAttemptId(), 1);

            nm.NodeHeartbeat(attempt0.GetAppAttemptId(), cId.GetContainerId(), ContainerState
                             .Complete);
            rm0.WaitForState(nm, cId, RMContainerState.Completed);
            // Check that the container metrics match those from the app usage report.
            long memorySeconds = 0;
            long vcoreSeconds  = 0;

            foreach (RMContainer c_2 in rmContainers)
            {
                AggregateAppResourceUsage ru = CalculateContainerResourceMetrics(c_2);
                memorySeconds += ru.GetMemorySeconds();
                vcoreSeconds  += ru.GetVcoreSeconds();
            }
            RMAppMetrics metricsBefore = app0.GetRMAppMetrics();

            NUnit.Framework.Assert.AreEqual("Unexcpected MemorySeconds value", memorySeconds,
                                            metricsBefore.GetMemorySeconds());
            NUnit.Framework.Assert.AreEqual("Unexpected VcoreSeconds value", vcoreSeconds, metricsBefore
                                            .GetVcoreSeconds());
            // create new RM to represent RM restart. Load up the state store.
            MockRM rm1 = new MockRM(conf, memStore);

            rm1.Start();
            RMApp app0After = rm1.GetRMContext().GetRMApps()[app0.GetApplicationId()];
            // Compare container resource usage metrics from before and after restart.
            RMAppMetrics metricsAfter = app0After.GetRMAppMetrics();

            NUnit.Framework.Assert.AreEqual("Vcore seconds were not the same after RM Restart"
                                            , metricsBefore.GetVcoreSeconds(), metricsAfter.GetVcoreSeconds());
            NUnit.Framework.Assert.AreEqual("Memory seconds were not the same after RM Restart"
                                            , metricsBefore.GetMemorySeconds(), metricsAfter.GetMemorySeconds());
            rm0.Stop();
            rm0.Close();
            rm1.Stop();
            rm1.Close();
        }