public static boolean validateCSConfiguration(
          final Configuration oldConfParam, final Configuration newConf,
          final RMContext rmContext) throws IOException {
    // ensure that the oldConf is deep copied
    Configuration oldConf = new Configuration(oldConfParam);
    QueueMetrics.setConfigurationValidation(oldConf, true);
    QueueMetrics.setConfigurationValidation(newConf, true);

    CapacityScheduler liveScheduler = (CapacityScheduler) rmContext.getScheduler();
    CapacityScheduler newCs = new CapacityScheduler();
    try {
      //TODO: extract all the validation steps and replace reinitialize with
      //the specific validation steps
      newCs.setConf(oldConf);
      newCs.setRMContext(rmContext);
      newCs.init(oldConf);
      newCs.addNodes(liveScheduler.getAllNodes());
      newCs.reinitialize(newConf, rmContext, true);
      return true;
    } finally {
      newCs.stop();
    }
  }
@Test
  public void testValidateDoesNotModifyTheDefaultMetricsSystem() throws Exception {
    try {
      YarnConfiguration conf = new YarnConfiguration(CapacitySchedulerConfigGeneratorForTest
          .createBasicCSConfiguration());
      conf.setClass(YarnConfiguration.RM_SCHEDULER, CapacityScheduler.class,
          ResourceScheduler.class);
      mockRM = new MockRM(conf);

      cs = (CapacityScheduler) mockRM.getResourceScheduler();
      mockRM.start();
      cs.start();
      RMContext rmContext = mockRM.getRMContext();
      Configuration oldConfig = cs.getConfig();

      final Map<String, QueueMetrics> cache = QueueMetrics.getQueueMetrics();
      final MetricsSystem ms = DefaultMetricsSystem.instance();

      QueueMetrics origQM1 = cache.get("root.test1");
      QueueMetrics origQM2 = cache.get("root.test2");
      Assert.assertNotNull("Original queues should be found in the cache", origQM1);
      Assert.assertNotNull("Original queues should be found in the cache", origQM2);

      QueueMetrics origPQM1 = cache.get("default.root.test1");
      QueueMetrics origPQM2 = cache.get("default.root.test2");
      Assert.assertNotNull("Original queues should be found in the cache (PartitionQueueMetrics)",
          origPQM1);
      Assert.assertNotNull("Original queues should be found in the cache (PartitionQueueMetrics)",
          origPQM2);

      MetricsSource origMS1 =
          ms.getSource("QueueMetrics,q0=root,q1=test1");
      MetricsSource origMS2 =
          ms.getSource("QueueMetrics,q0=root,q1=test2");
      Assert.assertNotNull("Original queues should be found in the Metrics System",
          origMS1);
      Assert.assertNotNull("Original queues should be found in the Metrics System",
          origMS2);

      MetricsSource origPMS1 = ms
          .getSource("PartitionQueueMetrics,partition=,q0=root,q1=test1");
      MetricsSource origPMS2 = ms
          .getSource("PartitionQueueMetrics,partition=,q0=root,q1=test2");
      Assert.assertNotNull(
          "Original queues should be found in Metrics System (PartitionQueueMetrics)", origPMS1);
      Assert.assertNotNull(
          "Original queues should be found in Metrics System (PartitionQueueMetrics)", origPMS2);

      Configuration newConfig = new Configuration(oldConfig);
      newConfig
          .set("yarn.scheduler.capacity.root.queues", "test1, test2, test3");
      newConfig
          .set("yarn.scheduler.capacity.root.test3.state", "RUNNING");
      newConfig
          .set("yarn.scheduler.capacity.root.test3.capacity", "30");
      newConfig
          .set("yarn.scheduler.capacity.root.test1.capacity", "20");

      boolean isValidConfig = CapacitySchedulerConfigValidator
          .validateCSConfiguration(oldConfig, newConfig, rmContext);
      Assert.assertTrue(isValidConfig);

      Assert.assertFalse("Validated new queue should not be in the cache",
          cache.containsKey("root.test3"));
      Assert.assertFalse("Validated new queue should not be in the cache (PartitionQueueMetrics)",
          cache.containsKey("default.root.test3"));
      Assert.assertNull("Validated new queue should not be in the Metrics System",
          ms.getSource("QueueMetrics,q0=root,q1=test3"));
      Assert.assertNull(
          "Validated new queue should not be in Metrics System (PartitionQueueMetrics)",
          ms
              .getSource("PartitionQueueMetrics,partition=,q0=root,q1=test3"));

      // Config validation should not change the existing
      // objects in the cache and the metrics system
      Assert.assertEquals(origQM1, cache.get("root.test1"));
      Assert.assertEquals(origQM2, cache.get("root.test2"));
      Assert.assertEquals(origPQM1, cache.get("default.root.test1"));
      Assert.assertEquals(origPQM1, cache.get("default.root.test1"));
      Assert.assertEquals(origMS1,
          ms.getSource("QueueMetrics,q0=root,q1=test1"));
      Assert.assertEquals(origMS2,
          ms.getSource("QueueMetrics,q0=root,q1=test2"));
      Assert.assertEquals(origPMS1,
          ms.getSource("PartitionQueueMetrics,partition=,q0=root,q1=test1"));
      Assert.assertEquals(origPMS2,
          ms.getSource("PartitionQueueMetrics,partition=,q0=root,q1=test2"));
    } finally {
      mockRM.stop();
    }
  }