TestCapacitySchedulerConf.java
上传用户:quxuerui
上传日期:2018-01-08
资源大小:41811k
文件大小:16k
源码类别:

网格计算

开发平台:

Java

  1. /** Licensed to the Apache Software Foundation (ASF) under one
  2.  * or more contributor license agreements.  See the NOTICE file
  3.  * distributed with this work for additional information
  4.  * regarding copyright ownership.  The ASF licenses this file
  5.  * to you under the Apache License, Version 2.0 (the
  6.  * "License"); you may not use this file except in compliance
  7.  * with the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.hadoop.mapred;
  18. import java.io.BufferedWriter;
  19. import java.io.IOException;
  20. import java.io.File;
  21. import java.io.FileWriter;
  22. import java.io.PrintWriter;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import junit.framework.TestCase;
  26. import org.apache.hadoop.fs.Path;
  27. public class TestCapacitySchedulerConf extends TestCase {
  28.   private static String testDataDir = System.getProperty("test.build.data");
  29.   private static String testConfFile;
  30.   
  31.   private Map<String, String> defaultProperties;
  32.   private CapacitySchedulerConf testConf;
  33.   private PrintWriter writer;
  34.   
  35.   static {
  36.     if (testDataDir == null) {
  37.       testDataDir = ".";
  38.     } else {
  39.       new File(testDataDir).mkdirs();
  40.     }
  41.     testConfFile = new File(testDataDir, "test-conf.xml").getAbsolutePath();
  42.   }
  43.   
  44.   public TestCapacitySchedulerConf() {
  45.     defaultProperties = setupQueueProperties(
  46.         new String[] { "guaranteed-capacity", 
  47.                        "reclaim-time-limit",
  48.                        "supports-priority",
  49.                        "minimum-user-limit-percent",
  50.                        "maximum-initialized-jobs-per-user"}, 
  51.         new String[] { "100", 
  52.                         "300",
  53.                         "false", 
  54.                         "100",
  55.                         "2" }
  56.                       );
  57.   }
  58.   
  59.   public void setUp() throws IOException {
  60.     openFile();
  61.   }
  62.   
  63.   public void tearDown() throws IOException {
  64.     File confFile = new File(testConfFile);
  65.     if (confFile.exists()) {
  66.       confFile.delete();  
  67.     }
  68.   }
  69.   
  70.   public void testDefaults() {
  71.     testConf = new CapacitySchedulerConf();
  72.     Map<String, Map<String, String>> queueDetails
  73.                             = new HashMap<String, Map<String,String>>();
  74.     queueDetails.put("default", defaultProperties);
  75.     checkQueueProperties(testConf, queueDetails);
  76.   }
  77.   
  78.   public void testQueues() {
  79.     Map<String, String> q1Props = setupQueueProperties(
  80.         new String[] { "guaranteed-capacity", 
  81.                        "reclaim-time-limit",
  82.                        "supports-priority",
  83.                        "minimum-user-limit-percent",
  84.                        "maximum-initialized-jobs-per-user"}, 
  85.         new String[] { "10", 
  86.                         "600",
  87.                         "true",
  88.                         "25",
  89.                         "4"}
  90.                       );
  91.     Map<String, String> q2Props = setupQueueProperties(
  92.         new String[] { "guaranteed-capacity", 
  93.                        "reclaim-time-limit",
  94.                        "supports-priority",
  95.                        "minimum-user-limit-percent",
  96.                        "maximum-initialized-jobs-per-user"}, 
  97.         new String[] { "100", 
  98.                         "6000",
  99.                         "false", 
  100.                         "50",
  101.                         "1"}
  102.                       );
  103.     startConfig();
  104.     writeQueueDetails("default", q1Props);
  105.     writeQueueDetails("research", q2Props);
  106.     endConfig();
  107.     testConf = new CapacitySchedulerConf(new Path(testConfFile));
  108.     Map<String, Map<String, String>> queueDetails
  109.               = new HashMap<String, Map<String,String>>();
  110.     queueDetails.put("default", q1Props);
  111.     queueDetails.put("research", q2Props);
  112.     checkQueueProperties(testConf, queueDetails);
  113.   }
  114.   
  115.   public void testQueueWithDefaultProperties() {
  116.     Map<String, String> q1Props = setupQueueProperties(
  117.         new String[] { "guaranteed-capacity", 
  118.                        "minimum-user-limit-percent" }, 
  119.         new String[] { "20", 
  120.                         "75" }
  121.                       );
  122.     startConfig();
  123.     writeQueueDetails("default", q1Props);
  124.     endConfig();
  125.     testConf = new CapacitySchedulerConf(new Path(testConfFile));
  126.     Map<String, Map<String, String>> queueDetails
  127.               = new HashMap<String, Map<String,String>>();
  128.     Map<String, String> expProperties = new HashMap<String, String>();
  129.     for (String key : q1Props.keySet()) {
  130.       expProperties.put(key, q1Props.get(key));
  131.     }
  132.     expProperties.put("reclaim-time-limit", "300");
  133.     expProperties.put("supports-priority", "false");
  134.     expProperties.put("maximum-initialized-jobs-per-user", "2");
  135.     queueDetails.put("default", expProperties);
  136.     checkQueueProperties(testConf, queueDetails);
  137.   }
  138.   public void testReload() throws IOException {
  139.     // use the setup in the test case testQueues as a base...
  140.     testQueues();
  141.     
  142.     // write new values to the file...
  143.     Map<String, String> q1Props = setupQueueProperties(
  144.         new String[] { "guaranteed-capacity", 
  145.                        "reclaim-time-limit",
  146.                        "supports-priority",
  147.                        "minimum-user-limit-percent" }, 
  148.         new String[] { "20.5", 
  149.                         "600",
  150.                         "true", 
  151.                         "40" }
  152.                       );
  153.     Map<String, String> q2Props = setupQueueProperties(
  154.         new String[] { "guaranteed-capacity", 
  155.                        "reclaim-time-limit",
  156.                        "supports-priority",
  157.                        "minimum-user-limit-percent" }, 
  158.         new String[] { "100", 
  159.                         "3000",
  160.                         "false",
  161.                         "50" }
  162.                       );
  163.     openFile();
  164.     startConfig();
  165.     writeDefaultConfiguration();
  166.     writeQueueDetails("default", q1Props);
  167.     writeQueueDetails("production", q2Props);
  168.     endConfig();
  169.     testConf.reloadConfiguration();
  170.     Map<String, Map<String, String>> queueDetails 
  171.                       = new HashMap<String, Map<String, String>>();
  172.     queueDetails.put("default", q1Props);
  173.     queueDetails.put("production", q2Props);
  174.     checkQueueProperties(testConf, queueDetails);
  175.   }
  176.   public void testQueueWithUserDefinedDefaultProperties() throws IOException {
  177.     openFile();
  178.     startConfig();
  179.     writeUserDefinedDefaultConfiguration();
  180.     endConfig();
  181.     Map<String, String> q1Props = setupQueueProperties(
  182.         new String[] { "guaranteed-capacity", 
  183.                        "reclaim-time-limit",
  184.                        "supports-priority",
  185.                        "minimum-user-limit-percent" }, 
  186.         new String[] { "-1", 
  187.                         "800",
  188.                         "true", 
  189.                         "50" }
  190.                       );
  191.     Map<String, String> q2Props = setupQueueProperties(
  192.         new String[] { "guaranteed-capacity", 
  193.                        "reclaim-time-limit",
  194.                        "supports-priority",
  195.                        "minimum-user-limit-percent" }, 
  196.         new String[] { "-1", 
  197.                         "800",
  198.                         "true",
  199.                         "50" }
  200.                       );
  201.     
  202.     testConf = new CapacitySchedulerConf(new Path(testConfFile));
  203.     Map<String, Map<String, String>> queueDetails
  204.               = new HashMap<String, Map<String,String>>();
  205.     
  206.     queueDetails.put("default", q1Props);
  207.     queueDetails.put("production", q2Props);
  208.     
  209.     checkQueueProperties(testConf, queueDetails);
  210.   }
  211.   
  212.   public void testQueueWithDefaultPropertiesOverriden() throws IOException {
  213.     openFile();
  214.     startConfig();
  215.     writeUserDefinedDefaultConfiguration();
  216.     Map<String, String> q1Props = setupQueueProperties(
  217.         new String[] { "guaranteed-capacity", 
  218.                        "reclaim-time-limit",
  219.                        "supports-priority",
  220.                        "minimum-user-limit-percent" }, 
  221.         new String[] { "-1", 
  222.                         "800",
  223.                         "true", 
  224.                         "50" }
  225.                       );
  226.     Map<String, String> q2Props = setupQueueProperties(
  227.         new String[] { "guaranteed-capacity", 
  228.                        "supports-priority",
  229.                        "minimum-user-limit-percent" }, 
  230.         new String[] { "40", 
  231.                         "true",
  232.                         "50" }
  233.                       );
  234.     Map<String, String> q3Props = setupQueueProperties(
  235.         new String[] { "guaranteed-capacity", 
  236.                        "reclaim-time-limit",
  237.                        "supports-priority",
  238.                        "minimum-user-limit-percent" }, 
  239.         new String[] { "40", 
  240.                        "500",
  241.                         "true",
  242.                         "50" }
  243.                       );
  244.     writeQueueDetails("production", q2Props);
  245.     writeQueueDetails("test", q3Props);
  246.     endConfig();
  247.     testConf = new CapacitySchedulerConf(new Path(testConfFile));
  248.     Map<String, Map<String, String>> queueDetails
  249.               = new HashMap<String, Map<String,String>>();
  250.     q2Props.put("reclaim-time-limit", "800");
  251.     queueDetails.put("default", q1Props);
  252.     queueDetails.put("production", q2Props);
  253.     queueDetails.put("test", q3Props);
  254.     checkQueueProperties(testConf, queueDetails);
  255.   }
  256.   
  257.   public void testInvalidUserLimit() throws IOException {
  258.     openFile();
  259.     startConfig();
  260.     Map<String, String> q1Props = setupQueueProperties(
  261.         new String[] { "guaranteed-capacity", 
  262.                        "reclaim-time-limit",
  263.                        "supports-priority",
  264.                        "minimum-user-limit-percent" }, 
  265.         new String[] { "-1", 
  266.                         "800",
  267.                         "true", 
  268.                         "-50" }
  269.                       );
  270.     writeQueueDetails("default", q1Props);
  271.     endConfig();
  272.     try {
  273.       testConf = new CapacitySchedulerConf(new Path(testConfFile));
  274.       testConf.getMinimumUserLimitPercent("default");
  275.       fail("Expect Invalid user limit to raise Exception");
  276.     }catch(IllegalArgumentException e) {
  277.       assertTrue(true);
  278.     }
  279.   }
  280.   public void testInvalidReclaimTimeLimit() throws IOException {
  281.     openFile();
  282.     startConfig();
  283.     Map<String, String> q1Props = setupQueueProperties(
  284.         new String[] { "guaranteed-capacity", 
  285.                        "reclaim-time-limit",
  286.                        "supports-priority",
  287.                        "minimum-user-limit-percent" }, 
  288.         new String[] { "-1", 
  289.                         "-800",
  290.                         "true", 
  291.                         "50" }
  292.                       );
  293.     writeQueueDetails("default", q1Props);
  294.     endConfig();
  295.     try {
  296.       testConf = new CapacitySchedulerConf(new Path(testConfFile));
  297.       testConf.getReclaimTimeLimit("default");
  298.       fail("Expect Invalid reclaim time limit to raise Exception");
  299.     }catch(IllegalArgumentException e) {
  300.       assertTrue(true);
  301.     }
  302.   }
  303.   
  304.   public void testInitializationPollerProperties() 
  305.     throws Exception {
  306.     /*
  307.      * Test case to check properties of poller when no configuration file
  308.      * is present.
  309.      */
  310.     testConf = new CapacitySchedulerConf();
  311.     long pollingInterval = testConf.getSleepInterval();
  312.     int maxWorker = testConf.getMaxWorkerThreads();
  313.     assertTrue("Invalid polling interval ",pollingInterval > 0);
  314.     assertTrue("Invalid working thread pool size" , maxWorker > 0);
  315.     
  316.     //test case for custom values configured for initialization 
  317.     //poller.
  318.     openFile();
  319.     startConfig();
  320.     writeProperty("mapred.capacity-scheduler.init-worker-threads", "1");
  321.     writeProperty("mapred.capacity-scheduler.init-poll-interval", "1");
  322.     endConfig();
  323.     
  324.     testConf = new CapacitySchedulerConf(new Path(testConfFile));
  325.     
  326.     pollingInterval = testConf.getSleepInterval();
  327.     
  328.     maxWorker = testConf.getMaxWorkerThreads();
  329.     
  330.     assertEquals("Invalid polling interval ",pollingInterval ,1);
  331.     assertEquals("Invalid working thread pool size" , maxWorker, 1);
  332.     
  333.     //Test case for invalid values configured for initialization
  334.     //poller
  335.     openFile();
  336.     startConfig();
  337.     writeProperty("mapred.capacity-scheduler.init-worker-threads", "0");
  338.     writeProperty("mapred.capacity-scheduler.init-poll-interval", "0");
  339.     endConfig();
  340.     
  341.     testConf = new CapacitySchedulerConf(new Path(testConfFile));
  342.     
  343.     try {
  344.       pollingInterval = testConf.getSleepInterval();
  345.       fail("Polling interval configured is illegal");
  346.     } catch (IllegalArgumentException e) {}
  347.     try {
  348.       maxWorker = testConf.getMaxWorkerThreads();
  349.       fail("Max worker thread configured is illegal");
  350.     } catch (IllegalArgumentException e) {}
  351.   }
  352.   
  353.   public void testInvalidReclaimCapacityInterval() throws IOException {
  354.     openFile();
  355.     startConfig();
  356.     Map<String, String> q1Props = setupQueueProperties(
  357.         new String[] { "guaranteed-capacity", 
  358.                        "reclaim-time-limit",
  359.                        "supports-priority",
  360.                        "minimum-user-limit-percent" }, 
  361.         new String[] { "-1", 
  362.                         "-800",
  363.                         "true", 
  364.                         "50" }
  365.                       );
  366.     writeQueueDetails("default", q1Props);
  367.     writeProperty("mapred.capacity-scheduler.reclaimCapacity.interval", "0");
  368.     endConfig();
  369.     try {
  370.       testConf = new CapacitySchedulerConf(new Path(testConfFile));
  371.       testConf.getReclaimCapacityInterval();
  372.       fail("Expect Invalid reclaim capacity interval raise Exception");
  373.     }catch(IllegalArgumentException e) {
  374.       assertTrue(true);
  375.     }
  376.   }
  377.   
  378.   private void checkQueueProperties(
  379.                         CapacitySchedulerConf testConf,
  380.                         Map<String, Map<String, String>> queueDetails) {
  381.     for (String queueName : queueDetails.keySet()) {
  382.       Map<String, String> map = queueDetails.get(queueName);
  383.       assertEquals(Float.parseFloat(map.get("guaranteed-capacity")),
  384.            testConf.getGuaranteedCapacity(queueName));
  385.       assertEquals(Integer.parseInt(map.get("minimum-user-limit-percent")),
  386.           testConf.getMinimumUserLimitPercent(queueName));
  387.       assertEquals(Integer.parseInt(map.get("reclaim-time-limit")),
  388.           testConf.getReclaimTimeLimit(queueName));
  389.       assertEquals(Boolean.parseBoolean(map.get("supports-priority")),
  390.           testConf.isPrioritySupported(queueName));
  391.     }
  392.   }
  393.   
  394.   private Map<String, String> setupQueueProperties(String[] keys, 
  395.                                                 String[] values) {
  396.     HashMap<String, String> map = new HashMap<String, String>();
  397.     for(int i=0; i<keys.length; i++) {
  398.       map.put(keys[i], values[i]);
  399.     }
  400.     return map;
  401.   }
  402.   private void openFile() throws IOException {
  403.     
  404.     if (testDataDir != null) {
  405.       File f = new File(testDataDir);
  406.       f.mkdirs();
  407.     }
  408.     FileWriter fw = new FileWriter(testConfFile);
  409.     BufferedWriter bw = new BufferedWriter(fw);
  410.     writer = new PrintWriter(bw);
  411.   }
  412.   
  413.   private void startConfig() {
  414.     writer.println("<?xml version="1.0"?>");
  415.     writer.println("<configuration>");
  416.   }
  417.   
  418.   private void writeQueueDetails(String queue, Map<String, String> props) {
  419.     for (String key : props.keySet()) {
  420.       writer.println("<property>");
  421.       writer.println("<name>mapred.capacity-scheduler.queue." 
  422.                         + queue + "." + key +
  423.                     "</name>");
  424.       writer.println("<value>"+props.get(key)+"</value>");
  425.       writer.println("</property>");
  426.     }
  427.   }
  428.   
  429.   
  430.   private void writeDefaultConfiguration() {
  431.     writeProperty("mapred.capacity-scheduler.default-reclaim-time-limit"
  432.         , "300");
  433.     writeProperty("mapred.capacity-scheduler.default-supports-priority"
  434.         , "false");
  435.     writeProperty("mapred.capacity-scheduler.default-minimum-user-limit-percent"
  436.         , "100");  
  437.   }
  438.   
  439.   
  440.   private void writeUserDefinedDefaultConfiguration() {
  441.     writeProperty("mapred.capacity-scheduler.default-reclaim-time-limit"
  442.         , "800");
  443.     writeProperty("mapred.capacity-scheduler.default-supports-priority"
  444.         , "true");
  445.     writeProperty("mapred.capacity-scheduler.default-minimum-user-limit-percent"
  446.         , "50"); 
  447.   }
  448.   
  449.   
  450.   private void writeProperty(String name, String value) {
  451.     writer.println("<property>");
  452.     writer.println("<name> " + name + "</name>");
  453.     writer.println("<value>"+ value+"</value>");
  454.     writer.println("</property>");
  455.     
  456.   }
  457.   
  458.   private void endConfig() {
  459.     writer.println("</configuration>");
  460.     writer.close();
  461.   }
  462.   
  463. }