Example #1
0
        /// <exception cref="System.Exception"/>
        public virtual void TestAppOnMultiNode()
        {
            Logger rootLogger = LogManager.GetRootLogger();

            rootLogger.SetLevel(Level.Debug);
            conf.Set("yarn.scheduler.capacity.node-locality-delay", "-1");
            MockRM rm = new MockRM(conf);

            rm.Start();
            MockNM nm1 = rm.RegisterNode("h1:1234", 5120);
            MockNM nm2 = rm.RegisterNode("h2:5678", 10240);
            RMApp  app = rm.SubmitApp(2000);

            //kick the scheduling
            nm1.NodeHeartbeat(true);
            RMAppAttempt attempt = app.GetCurrentAppAttempt();
            MockAM       am      = rm.SendAMLaunched(attempt.GetAppAttemptId());

            am.RegisterAppAttempt();
            //request for containers
            int request = 13;

            am.Allocate("h1", 1000, request, new AList <ContainerId>());
            //kick the scheduler
            IList <Container> conts = am.Allocate(new AList <ResourceRequest>(), new AList <ContainerId
                                                                                            >()).GetAllocatedContainers();
            int contReceived = conts.Count;

            while (contReceived < 3)
            {
                //only 3 containers are available on node1
                nm1.NodeHeartbeat(true);
                Sharpen.Collections.AddAll(conts, am.Allocate(new AList <ResourceRequest>(), new AList
                                                              <ContainerId>()).GetAllocatedContainers());
                contReceived = conts.Count;
                Log.Info("Got " + contReceived + " containers. Waiting to get " + 3);
                Sharpen.Thread.Sleep(WaitSleepMs);
            }
            NUnit.Framework.Assert.AreEqual(3, conts.Count);
            //send node2 heartbeat
            conts = am.Allocate(new AList <ResourceRequest>(), new AList <ContainerId>()).GetAllocatedContainers
                        ();
            contReceived = conts.Count;
            while (contReceived < 10)
            {
                nm2.NodeHeartbeat(true);
                Sharpen.Collections.AddAll(conts, am.Allocate(new AList <ResourceRequest>(), new AList
                                                              <ContainerId>()).GetAllocatedContainers());
                contReceived = conts.Count;
                Log.Info("Got " + contReceived + " containers. Waiting to get " + 10);
                Sharpen.Thread.Sleep(WaitSleepMs);
            }
            NUnit.Framework.Assert.AreEqual(10, conts.Count);
            am.UnregisterAppAttempt();
            nm1.NodeHeartbeat(attempt.GetAppAttemptId(), 1, ContainerState.Complete);
            am.WaitForState(RMAppAttemptState.Finished);
            rm.Stop();
        }
        /// <exception cref="System.Exception"/>
        public virtual void TestAppCleanupWhenRMRestartedBeforeAppFinished()
        {
            conf.SetInt(YarnConfiguration.RmAmMaxAttempts, 1);
            MemoryRMStateStore memStore = new MemoryRMStateStore();

            memStore.Init(conf);
            // start RM
            MockRM rm1 = new MockRM(conf, memStore);

            rm1.Start();
            MockNM nm1 = new MockNM("127.0.0.1:1234", 1024, rm1.GetResourceTrackerService());

            nm1.RegisterNode();
            MockNM nm2 = new MockNM("127.0.0.1:5678", 1024, rm1.GetResourceTrackerService());

            nm2.RegisterNode();
            // create app and launch the AM
            RMApp  app0 = rm1.SubmitApp(200);
            MockAM am0  = LaunchAM(app0, rm1, nm1);
            // alloc another container on nm2
            AllocateResponse allocResponse = am0.Allocate(Arrays.AsList(ResourceRequest.NewInstance
                                                                            (Priority.NewInstance(1), "*", Resource.NewInstance(1024, 0), 1)), null);

            while (null == allocResponse.GetAllocatedContainers() || allocResponse.GetAllocatedContainers
                       ().IsEmpty())
            {
                nm2.NodeHeartbeat(true);
                allocResponse = am0.Allocate(null, null);
                Sharpen.Thread.Sleep(1000);
            }
            // start new RM
            MockRM rm2 = new MockRM(conf, memStore);

            rm2.Start();
            // nm1/nm2 register to rm2, and do a heartbeat
            nm1.SetResourceTrackerService(rm2.GetResourceTrackerService());
            nm1.RegisterNode(Arrays.AsList(NMContainerStatus.NewInstance(ContainerId.NewContainerId
                                                                             (am0.GetApplicationAttemptId(), 1), ContainerState.Complete, Resource.NewInstance
                                                                             (1024, 1), string.Empty, 0, Priority.NewInstance(0), 1234)), Arrays.AsList(app0.
                                                                                                                                                        GetApplicationId()));
            nm2.SetResourceTrackerService(rm2.GetResourceTrackerService());
            nm2.RegisterNode(Arrays.AsList(app0.GetApplicationId()));
            // assert app state has been saved.
            rm2.WaitForState(app0.GetApplicationId(), RMAppState.Failed);
            // wait for application cleanup message received on NM1
            WaitForAppCleanupMessageRecved(nm1, app0.GetApplicationId());
            // wait for application cleanup message received on NM2
            WaitForAppCleanupMessageRecved(nm2, app0.GetApplicationId());
            rm1.Stop();
            rm2.Stop();
        }
Example #3
0
        /// <exception cref="System.Exception"/>
        protected internal virtual void AllocateContainersAndValidateNMTokens(MockAM am,
                                                                              AList <Container> containersReceived, int totalContainerRequested, Dictionary <string
                                                                                                                                                             , Token> nmTokens, MockNM nm)
        {
            AList <ContainerId>     releaseContainerList = new AList <ContainerId>();
            AllocateResponse        response;
            AList <ResourceRequest> resourceRequest = new AList <ResourceRequest>();

            while (containersReceived.Count < totalContainerRequested)
            {
                nm.NodeHeartbeat(true);
                Log.Info("requesting containers..");
                response = am.Allocate(resourceRequest, releaseContainerList);
                Sharpen.Collections.AddAll(containersReceived, response.GetAllocatedContainers());
                if (!response.GetNMTokens().IsEmpty())
                {
                    foreach (NMToken nmToken in response.GetNMTokens())
                    {
                        string nodeId = nmToken.GetNodeId().ToString();
                        if (nmTokens.Contains(nodeId))
                        {
                            NUnit.Framework.Assert.Fail("Duplicate NMToken received for : " + nodeId);
                        }
                        nmTokens[nodeId] = nmToken.GetToken();
                    }
                }
                Log.Info("Got " + containersReceived.Count + " containers. Waiting to get " + totalContainerRequested
                         );
                Sharpen.Thread.Sleep(WaitSleepMs);
            }
        }
Example #4
0
        // Test even if AM container is allocated with containerId not equal to 1, the
        // following allocate requests from AM should be able to retrieve the
        // corresponding NM Token.
        /// <exception cref="System.Exception"/>
        public virtual void TestNMTokenSentForNormalContainer()
        {
            conf.Set(YarnConfiguration.RmScheduler, typeof(CapacityScheduler).GetCanonicalName
                         ());
            MockRM rm = new MockRM(conf);

            rm.Start();
            MockNM       nm1     = rm.RegisterNode("h1:1234", 5120);
            RMApp        app     = rm.SubmitApp(2000);
            RMAppAttempt attempt = app.GetCurrentAppAttempt();
            // Call getNewContainerId to increase container Id so that the AM container
            // Id doesn't equal to one.
            CapacityScheduler cs = (CapacityScheduler)rm.GetResourceScheduler();

            cs.GetApplicationAttempt(attempt.GetAppAttemptId()).GetNewContainerId();
            // kick the scheduling
            nm1.NodeHeartbeat(true);
            MockAM am = MockRM.LaunchAM(app, rm, nm1);

            // am container Id not equal to 1.
            NUnit.Framework.Assert.IsTrue(attempt.GetMasterContainer().GetId().GetContainerId
                                              () != 1);
            // NMSecretManager doesn't record the node on which the am is allocated.
            NUnit.Framework.Assert.IsFalse(rm.GetRMContext().GetNMTokenSecretManager().IsApplicationAttemptNMTokenPresent
                                               (attempt.GetAppAttemptId(), nm1.GetNodeId()));
            am.RegisterAppAttempt();
            rm.WaitForState(app.GetApplicationId(), RMAppState.Running);
            int NumContainers            = 1;
            IList <Container> containers = new AList <Container>();
            // nmTokens keeps track of all the nmTokens issued in the allocate call.
            IList <NMToken> expectedNMTokens = new AList <NMToken>();

            // am1 allocate 1 container on nm1.
            while (true)
            {
                AllocateResponse response = am.Allocate("127.0.0.1", 2000, NumContainers, new AList
                                                        <ContainerId>());
                nm1.NodeHeartbeat(true);
                Sharpen.Collections.AddAll(containers, response.GetAllocatedContainers());
                Sharpen.Collections.AddAll(expectedNMTokens, response.GetNMTokens());
                if (containers.Count == NumContainers)
                {
                    break;
                }
                Sharpen.Thread.Sleep(200);
                System.Console.Out.WriteLine("Waiting for container to be allocated.");
            }
            NodeId nodeId = expectedNMTokens[0].GetNodeId();

            // NMToken is sent for the allocated container.
            NUnit.Framework.Assert.AreEqual(nm1.GetNodeId(), nodeId);
        }
Example #5
0
        public virtual void TestAllocateContainerOnNodeWithoutOffSwitchSpecified()
        {
            Logger rootLogger = LogManager.GetRootLogger();

            rootLogger.SetLevel(Level.Debug);
            MockRM rm = new MockRM(conf);

            rm.Start();
            MockNM nm1  = rm.RegisterNode("127.0.0.1:1234", 6 * Gb);
            RMApp  app1 = rm.SubmitApp(2048);

            // kick the scheduling, 2 GB given to AM1, remaining 4GB on nm1
            nm1.NodeHeartbeat(true);
            RMAppAttempt attempt1 = app1.GetCurrentAppAttempt();
            MockAM       am1      = rm.SendAMLaunched(attempt1.GetAppAttemptId());

            am1.RegisterAppAttempt();
            // add request for containers
            IList <ResourceRequest> requests = new AList <ResourceRequest>();

            requests.AddItem(am1.CreateResourceReq("127.0.0.1", 1 * Gb, 1, 1));
            requests.AddItem(am1.CreateResourceReq("/default-rack", 1 * Gb, 1, 1));
            am1.Allocate(requests, null);
            // send the request
            try
            {
                // kick the schedule
                nm1.NodeHeartbeat(true);
            }
            catch (ArgumentNullException)
            {
                NUnit.Framework.Assert.Fail("NPE when allocating container on node but " + "forget to set off-switch request should be handled"
                                            );
            }
            rm.Stop();
        }
        /// <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();
        }
        // The test verifies processing of NMContainerStatuses which are sent during
        // NM registration.
        // 1. Start the cluster-RM,NM,Submit app with 1024MB,Launch & register AM
        // 2. AM sends ResourceRequest for 1 container with memory 2048MB.
        // 3. Verify for number of container allocated by RM
        // 4. Verify Memory Usage by cluster, it should be 3072. AM memory + requested
        // memory. 1024 + 2048=3072
        // 5. Re-register NM by sending completed container status
        // 6. Verify for Memory Used, it should be 1024
        // 7. Send AM heatbeat to RM. Allocated response should contain completed
        // container.
        /// <exception cref="System.Exception"/>
        public virtual void TestProcessingNMContainerStatusesOnNMRestart()
        {
            conf.SetInt(YarnConfiguration.RmAmMaxAttempts, 1);
            MemoryRMStateStore memStore = new MemoryRMStateStore();

            memStore.Init(conf);
            // 1. Start the cluster-RM,NM,Submit app with 1024MB,Launch & register AM
            MockRM rm1 = new MockRM(conf, memStore);

            rm1.Start();
            int    nmMemory        = 8192;
            int    amMemory        = 1024;
            int    containerMemory = 2048;
            MockNM nm1             = new MockNM("127.0.0.1:1234", nmMemory, rm1.GetResourceTrackerService
                                                    ());

            nm1.RegisterNode();
            RMApp  app0 = rm1.SubmitApp(amMemory);
            MockAM am0  = MockRM.LaunchAndRegisterAM(app0, rm1, nm1);
            // 2. AM sends ResourceRequest for 1 container with memory 2048MB.
            int noOfContainers = 1;
            IList <Container> allocateContainers = am0.AllocateAndWaitForContainers(noOfContainers
                                                                                    , containerMemory, nm1);

            // 3. Verify for number of container allocated by RM
            NUnit.Framework.Assert.AreEqual(noOfContainers, allocateContainers.Count);
            Container container = allocateContainers[0];

            nm1.NodeHeartbeat(am0.GetApplicationAttemptId(), 1, ContainerState.Running);
            nm1.NodeHeartbeat(am0.GetApplicationAttemptId(), container.GetId().GetContainerId
                                  (), ContainerState.Running);
            rm1.WaitForState(app0.GetApplicationId(), RMAppState.Running);
            // 4. Verify Memory Usage by cluster, it should be 3072. AM memory +
            // requested memory. 1024 + 2048=3072
            ResourceScheduler rs = rm1.GetRMContext().GetScheduler();
            int allocatedMB      = rs.GetRootQueueMetrics().GetAllocatedMB();

            NUnit.Framework.Assert.AreEqual(amMemory + containerMemory, allocatedMB);
            // 5. Re-register NM by sending completed container status
            IList <NMContainerStatus> nMContainerStatusForApp = CreateNMContainerStatusForApp(
                am0);

            nm1.RegisterNode(nMContainerStatusForApp, Arrays.AsList(app0.GetApplicationId()));
            WaitForClusterMemory(nm1, rs, amMemory);
            // 6. Verify for Memory Used, it should be 1024
            NUnit.Framework.Assert.AreEqual(amMemory, rs.GetRootQueueMetrics().GetAllocatedMB
                                                ());
            // 7. Send AM heatbeat to RM. Allocated response should contain completed
            // container
            AllocateRequest req = AllocateRequest.NewInstance(0, 0F, new AList <ResourceRequest
                                                                                >(), new AList <ContainerId>(), null);
            AllocateResponse        allocate = am0.Allocate(req);
            IList <ContainerStatus> completedContainersStatuses = allocate.GetCompletedContainersStatuses
                                                                      ();

            NUnit.Framework.Assert.AreEqual(noOfContainers, completedContainersStatuses.Count
                                            );
            // Application clean up should happen Cluster memory used is 0
            nm1.NodeHeartbeat(am0.GetApplicationAttemptId(), 1, ContainerState.Complete);
            WaitForClusterMemory(nm1, rs, 0);
            rm1.Stop();
        }
        public virtual void TestAppCleanup()
        {
            Logger rootLogger = LogManager.GetRootLogger();

            rootLogger.SetLevel(Level.Debug);
            MockRM rm = new MockRM();

            rm.Start();
            MockNM nm1 = rm.RegisterNode("127.0.0.1:1234", 5000);
            RMApp  app = rm.SubmitApp(2000);

            //kick the scheduling
            nm1.NodeHeartbeat(true);
            RMAppAttempt attempt = app.GetCurrentAppAttempt();
            MockAM       am      = rm.SendAMLaunched(attempt.GetAppAttemptId());

            am.RegisterAppAttempt();
            //request for containers
            int request = 2;

            am.Allocate("127.0.0.1", 1000, request, new AList <ContainerId>());
            //kick the scheduler
            nm1.NodeHeartbeat(true);
            IList <Container> conts = am.Allocate(new AList <ResourceRequest>(), new AList <ContainerId
                                                                                            >()).GetAllocatedContainers();
            int contReceived = conts.Count;
            int waitCount    = 0;

            while (contReceived < request && waitCount++ < 200)
            {
                Log.Info("Got " + contReceived + " containers. Waiting to get " + request);
                Sharpen.Thread.Sleep(100);
                conts = am.Allocate(new AList <ResourceRequest>(), new AList <ContainerId>()).GetAllocatedContainers
                            ();
                contReceived += conts.Count;
                nm1.NodeHeartbeat(true);
            }
            NUnit.Framework.Assert.AreEqual(request, contReceived);
            am.UnregisterAppAttempt();
            NodeHeartbeatResponse resp = nm1.NodeHeartbeat(attempt.GetAppAttemptId(), 1, ContainerState
                                                           .Complete);

            am.WaitForState(RMAppAttemptState.Finished);
            //currently only containers are cleaned via this
            //AM container is cleaned via container launcher
            resp = nm1.NodeHeartbeat(true);
            IList <ContainerId>   containersToCleanup = resp.GetContainersToCleanup();
            IList <ApplicationId> appsToCleanup       = resp.GetApplicationsToCleanup();
            int numCleanedContainers = containersToCleanup.Count;
            int numCleanedApps       = appsToCleanup.Count;

            waitCount = 0;
            while ((numCleanedContainers < 2 || numCleanedApps < 1) && waitCount++ < 200)
            {
                Log.Info("Waiting to get cleanup events.. cleanedConts: " + numCleanedContainers
                         + " cleanedApps: " + numCleanedApps);
                Sharpen.Thread.Sleep(100);
                resp = nm1.NodeHeartbeat(true);
                IList <ContainerId>   deltaContainersToCleanup = resp.GetContainersToCleanup();
                IList <ApplicationId> deltaAppsToCleanup       = resp.GetApplicationsToCleanup();
                // Add the deltas to the global list
                Sharpen.Collections.AddAll(containersToCleanup, deltaContainersToCleanup);
                Sharpen.Collections.AddAll(appsToCleanup, deltaAppsToCleanup);
                // Update counts now
                numCleanedContainers = containersToCleanup.Count;
                numCleanedApps       = appsToCleanup.Count;
            }
            NUnit.Framework.Assert.AreEqual(1, appsToCleanup.Count);
            NUnit.Framework.Assert.AreEqual(app.GetApplicationId(), appsToCleanup[0]);
            NUnit.Framework.Assert.AreEqual(1, numCleanedApps);
            NUnit.Framework.Assert.AreEqual(2, numCleanedContainers);
            rm.Stop();
        }
        public virtual void TestContainerCleanup()
        {
            Logger rootLogger = LogManager.GetRootLogger();

            rootLogger.SetLevel(Level.Debug);
            DrainDispatcher dispatcher = new DrainDispatcher();
            MockRM          rm         = new _MockRM_167(this, dispatcher);

            rm.Start();
            MockNM nm1 = rm.RegisterNode("127.0.0.1:1234", 5000);
            RMApp  app = rm.SubmitApp(2000);

            //kick the scheduling
            nm1.NodeHeartbeat(true);
            RMAppAttempt attempt = app.GetCurrentAppAttempt();
            MockAM       am      = rm.SendAMLaunched(attempt.GetAppAttemptId());

            am.RegisterAppAttempt();
            //request for containers
            int request = 2;

            am.Allocate("127.0.0.1", 1000, request, new AList <ContainerId>());
            dispatcher.Await();
            //kick the scheduler
            nm1.NodeHeartbeat(true);
            IList <Container> conts = am.Allocate(new AList <ResourceRequest>(), new AList <ContainerId
                                                                                            >()).GetAllocatedContainers();
            int contReceived = conts.Count;
            int waitCount    = 0;

            while (contReceived < request && waitCount++ < 200)
            {
                Log.Info("Got " + contReceived + " containers. Waiting to get " + request);
                Sharpen.Thread.Sleep(100);
                conts = am.Allocate(new AList <ResourceRequest>(), new AList <ContainerId>()).GetAllocatedContainers
                            ();
                dispatcher.Await();
                contReceived += conts.Count;
                nm1.NodeHeartbeat(true);
            }
            NUnit.Framework.Assert.AreEqual(request, contReceived);
            // Release a container.
            AList <ContainerId> release = new AList <ContainerId>();

            release.AddItem(conts[0].GetId());
            am.Allocate(new AList <ResourceRequest>(), release);
            dispatcher.Await();
            // Send one more heartbeat with a fake running container. This is to
            // simulate the situation that can happen if the NM reports that container
            // is running in the same heartbeat when the RM asks it to clean it up.
            IDictionary <ApplicationId, IList <ContainerStatus> > containerStatuses = new Dictionary
                                                                                      <ApplicationId, IList <ContainerStatus> >();
            AList <ContainerStatus> containerStatusList = new AList <ContainerStatus>();

            containerStatusList.AddItem(BuilderUtils.NewContainerStatus(conts[0].GetId(), ContainerState
                                                                        .Running, "nothing", 0));
            containerStatuses[app.GetApplicationId()] = containerStatusList;
            NodeHeartbeatResponse resp = nm1.NodeHeartbeat(containerStatuses, true);

            WaitForContainerCleanup(dispatcher, nm1, resp);
            // Now to test the case when RM already gave cleanup, and NM suddenly
            // realizes that the container is running.
            Log.Info("Testing container launch much after release and " + "NM getting cleanup"
                     );
            containerStatuses.Clear();
            containerStatusList.Clear();
            containerStatusList.AddItem(BuilderUtils.NewContainerStatus(conts[0].GetId(), ContainerState
                                                                        .Running, "nothing", 0));
            containerStatuses[app.GetApplicationId()] = containerStatusList;
            resp = nm1.NodeHeartbeat(containerStatuses, true);
            // The cleanup list won't be instantaneous as it is given out by scheduler
            // and not RMNodeImpl.
            WaitForContainerCleanup(dispatcher, nm1, resp);
            rm.Stop();
        }
        /// <exception cref="System.Exception"/>
        public virtual void TestallocateBeforeAMRegistration()
        {
            Logger rootLogger = LogManager.GetRootLogger();
            bool   thrown     = false;

            rootLogger.SetLevel(Level.Debug);
            MockRM rm = new MockRM();

            rm.Start();
            MockNM nm1 = rm.RegisterNode("h1:1234", 5000);
            RMApp  app = rm.SubmitApp(2000);

            // kick the scheduling
            nm1.NodeHeartbeat(true);
            RMAppAttempt attempt = app.GetCurrentAppAttempt();
            MockAM       am      = rm.SendAMLaunched(attempt.GetAppAttemptId());
            // request for containers
            int request         = 2;
            AllocateResponse ar = null;

            try
            {
                ar = am.Allocate("h1", 1000, request, new AList <ContainerId>());
                NUnit.Framework.Assert.Fail();
            }
            catch (ApplicationMasterNotRegisteredException)
            {
            }
            // kick the scheduler
            nm1.NodeHeartbeat(true);
            AllocateResponse amrs = null;

            try
            {
                amrs = am.Allocate(new AList <ResourceRequest>(), new AList <ContainerId>());
                NUnit.Framework.Assert.Fail();
            }
            catch (ApplicationMasterNotRegisteredException)
            {
            }
            am.RegisterAppAttempt();
            try
            {
                am.RegisterAppAttempt(false);
                NUnit.Framework.Assert.Fail();
            }
            catch (Exception e)
            {
                NUnit.Framework.Assert.AreEqual("Application Master is already registered : " + attempt
                                                .GetAppAttemptId().GetApplicationId(), e.Message);
            }
            // Simulate an AM that was disconnected and app attempt was removed
            // (responseMap does not contain attemptid)
            am.UnregisterAppAttempt();
            nm1.NodeHeartbeat(attempt.GetAppAttemptId(), 1, ContainerState.Complete);
            am.WaitForState(RMAppAttemptState.Finished);
            try
            {
                amrs = am.Allocate(new AList <ResourceRequest>(), new AList <ContainerId>());
                NUnit.Framework.Assert.Fail();
            }
            catch (ApplicationAttemptNotFoundException)
            {
            }
        }
Example #12
0
        /// <exception cref="System.Exception"/>
        public virtual void TestProgressFilter()
        {
            MockRM rm = new MockRM(conf);

            rm.Start();
            // Register node1
            MockNM nm1 = rm.RegisterNode("127.0.0.1:1234", 6 * Gb);
            // Submit an application
            RMApp app1 = rm.SubmitApp(2048);

            nm1.NodeHeartbeat(true);
            RMAppAttempt attempt1 = app1.GetCurrentAppAttempt();
            MockAM       am1      = rm.SendAMLaunched(attempt1.GetAppAttemptId());

            am1.RegisterAppAttempt();
            AllocateRequestPBImpl   allocateRequest = new AllocateRequestPBImpl();
            IList <ContainerId>     release         = new AList <ContainerId>();
            IList <ResourceRequest> ask             = new AList <ResourceRequest>();

            allocateRequest.SetReleaseList(release);
            allocateRequest.SetAskList(ask);
            allocateRequest.SetProgress(float.PositiveInfinity);
            am1.Allocate(allocateRequest);
            while (attempt1.GetProgress() != 1)
            {
                Log.Info("Waiting for allocate event to be handled ...");
                Sharpen.Thread.Sleep(100);
            }
            allocateRequest.SetProgress(float.NaN);
            am1.Allocate(allocateRequest);
            while (attempt1.GetProgress() != 0)
            {
                Log.Info("Waiting for allocate event to be handled ...");
                Sharpen.Thread.Sleep(100);
            }
            allocateRequest.SetProgress((float)9);
            am1.Allocate(allocateRequest);
            while (attempt1.GetProgress() != 1)
            {
                Log.Info("Waiting for allocate event to be handled ...");
                Sharpen.Thread.Sleep(100);
            }
            allocateRequest.SetProgress(float.NegativeInfinity);
            am1.Allocate(allocateRequest);
            while (attempt1.GetProgress() != 0)
            {
                Log.Info("Waiting for allocate event to be handled ...");
                Sharpen.Thread.Sleep(100);
            }
            allocateRequest.SetProgress((float)0.5);
            am1.Allocate(allocateRequest);
            while (attempt1.GetProgress() != 0.5)
            {
                Log.Info("Waiting for allocate event to be handled ...");
                Sharpen.Thread.Sleep(100);
            }
            allocateRequest.SetProgress((float)-1);
            am1.Allocate(allocateRequest);
            while (attempt1.GetProgress() != 0)
            {
                Log.Info("Waiting for allocate event to be handled ...");
                Sharpen.Thread.Sleep(100);
            }
        }
Example #13
0
        /// <exception cref="System.Exception"/>
        public virtual void TestNMToken()
        {
            MockRM rm = new MockRM(conf);

            try
            {
                rm.Start();
                MockNM nm1 = rm.RegisterNode("h1:1234", 10000);
                NMTokenSecretManagerInRM nmTokenSecretManager = rm.GetRMContext().GetNMTokenSecretManager
                                                                    ();
                // submitting new application
                RMApp app = rm.SubmitApp(1000);
                // start scheduling.
                nm1.NodeHeartbeat(true);
                // Starting application attempt and launching
                // It should get registered with NMTokenSecretManager.
                RMAppAttempt attempt = app.GetCurrentAppAttempt();
                MockAM       am      = rm.SendAMLaunched(attempt.GetAppAttemptId());
                NUnit.Framework.Assert.IsTrue(nmTokenSecretManager.IsApplicationAttemptRegistered
                                                  (attempt.GetAppAttemptId()));
                // This will register application master.
                am.RegisterAppAttempt();
                AList <Container>          containersReceivedForNM1 = new AList <Container>();
                IList <ContainerId>        releaseContainerList     = new AList <ContainerId>();
                Dictionary <string, Token> nmTokens = new Dictionary <string, Token>();
                // initially requesting 2 containers.
                AllocateResponse response = am.Allocate("h1", 1000, 2, releaseContainerList);
                NUnit.Framework.Assert.AreEqual(0, response.GetAllocatedContainers().Count);
                AllocateContainersAndValidateNMTokens(am, containersReceivedForNM1, 2, nmTokens,
                                                      nm1);
                NUnit.Framework.Assert.AreEqual(1, nmTokens.Count);
                // requesting 2 more containers.
                response = am.Allocate("h1", 1000, 2, releaseContainerList);
                NUnit.Framework.Assert.AreEqual(0, response.GetAllocatedContainers().Count);
                AllocateContainersAndValidateNMTokens(am, containersReceivedForNM1, 4, nmTokens,
                                                      nm1);
                NUnit.Framework.Assert.AreEqual(1, nmTokens.Count);
                // We will be simulating NM restart so restarting newly added h2:1234
                // NM 2 now registers.
                MockNM nm2 = rm.RegisterNode("h2:1234", 10000);
                nm2.NodeHeartbeat(true);
                AList <Container> containersReceivedForNM2 = new AList <Container>();
                response = am.Allocate("h2", 1000, 2, releaseContainerList);
                NUnit.Framework.Assert.AreEqual(0, response.GetAllocatedContainers().Count);
                AllocateContainersAndValidateNMTokens(am, containersReceivedForNM2, 2, nmTokens,
                                                      nm2);
                NUnit.Framework.Assert.AreEqual(2, nmTokens.Count);
                // Simulating NM-2 restart.
                nm2 = rm.RegisterNode("h2:1234", 10000);
                // Wait for reconnect to make it through the RM and create a new RMNode
                IDictionary <NodeId, RMNode> nodes = rm.GetRMContext().GetRMNodes();
                while (nodes[nm2.GetNodeId()].GetLastNodeHeartBeatResponse().GetResponseId() > 0)
                {
                    Sharpen.Thread.Sleep(WaitSleepMs);
                }
                int interval = 40;
                // Wait for nm Token to be cleared.
                while (nmTokenSecretManager.IsApplicationAttemptNMTokenPresent(attempt.GetAppAttemptId
                                                                                   (), nm2.GetNodeId()) && interval-- > 0)
                {
                    Log.Info("waiting for nmToken to be cleared for : " + nm2.GetNodeId());
                    Sharpen.Thread.Sleep(WaitSleepMs);
                }
                NUnit.Framework.Assert.IsTrue(nmTokenSecretManager.IsApplicationAttemptRegistered
                                                  (attempt.GetAppAttemptId()));
                // removing NMToken for h2:1234
                Sharpen.Collections.Remove(nmTokens, nm2.GetNodeId().ToString());
                NUnit.Framework.Assert.AreEqual(1, nmTokens.Count);
                // We should again receive the NMToken.
                response = am.Allocate("h2", 1000, 2, releaseContainerList);
                NUnit.Framework.Assert.AreEqual(0, response.GetAllocatedContainers().Count);
                AllocateContainersAndValidateNMTokens(am, containersReceivedForNM2, 4, nmTokens,
                                                      nm2);
                NUnit.Framework.Assert.AreEqual(2, nmTokens.Count);
                // Now rolling over NMToken masterKey. it should resend the NMToken in
                // next allocate call.
                NUnit.Framework.Assert.IsTrue(nmTokenSecretManager.IsApplicationAttemptNMTokenPresent
                                                  (attempt.GetAppAttemptId(), nm1.GetNodeId()));
                NUnit.Framework.Assert.IsTrue(nmTokenSecretManager.IsApplicationAttemptNMTokenPresent
                                                  (attempt.GetAppAttemptId(), nm2.GetNodeId()));
                nmTokenSecretManager.RollMasterKey();
                nmTokenSecretManager.ActivateNextMasterKey();
                NUnit.Framework.Assert.IsFalse(nmTokenSecretManager.IsApplicationAttemptNMTokenPresent
                                                   (attempt.GetAppAttemptId(), nm1.GetNodeId()));
                NUnit.Framework.Assert.IsFalse(nmTokenSecretManager.IsApplicationAttemptNMTokenPresent
                                                   (attempt.GetAppAttemptId(), nm2.GetNodeId()));
                // It should not remove application attempt entry.
                NUnit.Framework.Assert.IsTrue(nmTokenSecretManager.IsApplicationAttemptRegistered
                                                  (attempt.GetAppAttemptId()));
                nmTokens.Clear();
                NUnit.Framework.Assert.AreEqual(0, nmTokens.Count);
                // We should again receive the NMToken.
                response = am.Allocate("h2", 1000, 1, releaseContainerList);
                NUnit.Framework.Assert.AreEqual(0, response.GetAllocatedContainers().Count);
                AllocateContainersAndValidateNMTokens(am, containersReceivedForNM2, 5, nmTokens,
                                                      nm2);
                NUnit.Framework.Assert.AreEqual(1, nmTokens.Count);
                NUnit.Framework.Assert.IsTrue(nmTokenSecretManager.IsApplicationAttemptNMTokenPresent
                                                  (attempt.GetAppAttemptId(), nm2.GetNodeId()));
                // After AM is finished making sure that nmtoken entry for app
                NUnit.Framework.Assert.IsTrue(nmTokenSecretManager.IsApplicationAttemptRegistered
                                                  (attempt.GetAppAttemptId()));
                am.UnregisterAppAttempt();
                // marking all the containers as finished.
                foreach (Container container in containersReceivedForNM1)
                {
                    nm1.NodeHeartbeat(attempt.GetAppAttemptId(), container.GetId().GetContainerId(),
                                      ContainerState.Complete);
                }
                foreach (Container container_1 in containersReceivedForNM2)
                {
                    nm2.NodeHeartbeat(attempt.GetAppAttemptId(), container_1.GetId().GetContainerId()
                                      , ContainerState.Complete);
                }
                nm1.NodeHeartbeat(am.GetApplicationAttemptId(), 1, ContainerState.Complete);
                am.WaitForState(RMAppAttemptState.Finished);
                NUnit.Framework.Assert.IsFalse(nmTokenSecretManager.IsApplicationAttemptRegistered
                                                   (attempt.GetAppAttemptId()));
            }
            finally
            {
                rm.Stop();
            }
        }