PlacementService implements IPlacementService { @Override public List<BaseNode> getPlacementPlan(IContainer container, ClusterCreate cluster, List<BaseNode> existedNodes, Map<String, List<String>> filteredHosts) { IPlacementPlanner planner = new PlacementPlanner(); List<BaseNode> badNodes = planner.getBadNodes(cluster, existedNodes); AuAssert.check(badNodes == null || badNodes.size() == 0); AuAssert.check(((Container) container).getTemplateNode() != null); planner.init(cluster, ((Container) container).getTemplateNode(), existedNodes, ((Container) container).getRackMap()); List<VirtualGroup> vGroups = planner.getVirtualGroups(existedNodes); List<VirtualGroup> referredGroups = new ArrayList<VirtualGroup>(); List<VirtualGroup> normalGroups = new ArrayList<VirtualGroup>(); for (VirtualGroup vGroup : vGroups) { if (vGroup.isReferred()) referredGroups.add(vGroup); else normalGroups.add(vGroup); } List<BaseNode> placedNodes = new ArrayList<BaseNode>(); Collections.sort(referredGroups, Collections.reverseOrder()); for (VirtualGroup vGroup : referredGroups) { placeVirtualGroupWithSnapshot(container, cluster, planner, vGroup, placedNodes, filteredHosts); } Collections.sort(normalGroups, Collections.reverseOrder()); for (VirtualGroup vGroup : normalGroups) { placeVirtualGroupWithSnapshot(container, cluster, planner, vGroup, placedNodes, filteredHosts); } if (existedNodes != null && existedNodes.size() != 0) AuAssert.check(placedNodes.size() == cluster.totalInstances() - existedNodes.size()); else AuAssert.check(placedNodes.size() == cluster.totalInstances()); return placedNodes; }  @Override List<BaseNode> getPlacementPlan(IContainer container,
         ClusterCreate cluster, List<BaseNode> existedNodes, Map<String, List<String>> filteredHosts); @Override List<BaseNode> getBadNodes(ClusterCreate cluster,
         List<BaseNode> existedNodes); @Override List<DiskSpec> getReplacementDisks(IContainer container,
         ClusterCreate spec, String groupName, String nodeName,
         String targetHost, List<DiskSpec> badDisks,
         Map<String, Integer> dsUsage);  }
@Test public void testDefaultCluster() throws Exception { ClusterCreate spec = TestPlacementUtil .getSimpleClusterSpec(TestPlacementUtil.SIMPLE_CLUSTER_SPEC); AbstractDatacenter dc = TestPlacementUtil .getAbstractDatacenter(TestPlacementUtil.DATACENTER_SPEC); Container container = new Container(dc); container.setTemplateNode(TestPlacementUtil.getTemplateNode()); PlacementService service = new PlacementService(); List<BaseNode> nodes = service.getPlacementPlan(container, spec, null, new HashMap<String, List<String>>()); TestPlacementUtil.validate(spec, nodes, false); }
@Test public void testClusterWithStrictAssociation() throws Exception { ClusterCreate spec = TestPlacementUtil .getSimpleClusterSpec(TestPlacementUtil.DC_SPLIT_CLUSTER_SPEC); AbstractDatacenter dc = TestPlacementUtil .getAbstractDatacenter(TestPlacementUtil.DATACENTER_SPEC); Container container = new Container(dc); container.setTemplateNode(TestPlacementUtil.getTemplateNode()); PlacementService service = new PlacementService(); List<BaseNode> nodes = service.getPlacementPlan(container, spec, null, new HashMap<String, List<String>>()); TestPlacementUtil.validate(spec, nodes, false); }
@Test public void testRRRackPolicy() throws Exception { ClusterCreate cluster = TestPlacementUtil .getSimpleClusterSpec(TestPlacementUtil.WITH_RACK_CLUSTER_SPEC); AbstractDatacenter dc = TestPlacementUtil .getAbstractDatacenter(TestPlacementUtil.RESIZE_DATACENTER_SPEC); Container container = new Container(dc); container.setTemplateNode(TestPlacementUtil.getTemplateNode()); container.addRackMap(cluster.getHostToRackMap()); PlacementService service = new PlacementService(); List<BaseNode> nodes = service.getPlacementPlan(container, cluster, null, new HashMap<String, List<String>>()); TestPlacementUtil.validate(cluster, nodes, false); Map<String, Integer> rackUsage = new HashMap<String, Integer>(); for (BaseNode node : nodes) { if (node.getGroupName().equals("data")) { if (!rackUsage.containsKey(node.getTargetRack())) { rackUsage.put(node.getTargetRack(), 0); } rackUsage.put(node.getTargetRack(), rackUsage.get(node.getTargetRack()) + 1); } } for (String rack : rackUsage.keySet()) { Assert.assertTrue(rackUsage.get(rack) == 1); } }
@Test public void testSameRackPolicy() throws Exception { ClusterCreate cluster = TestPlacementUtil .getSimpleClusterSpec(TestPlacementUtil.WITH_SAME_RACK_CLUSTER_SPEC); AbstractDatacenter dc = TestPlacementUtil .getAbstractDatacenter(TestPlacementUtil.RESIZE_DATACENTER_SPEC); Container container = new Container(dc); container.setTemplateNode(TestPlacementUtil.getTemplateNode()); container.addRackMap(cluster.getHostToRackMap()); PlacementService service = new PlacementService(); List<BaseNode> nodes = service.getPlacementPlan(container, cluster, null, new HashMap<String, List<String>>()); TestPlacementUtil.validate(cluster, nodes, false); Map<String, Integer> rackUsage = new HashMap<String, Integer>(); for (BaseNode node : nodes) { if (node.getGroupName().equals("data")) { if (!rackUsage.containsKey(node.getTargetRack())) { rackUsage.put(node.getTargetRack(), 0); } rackUsage.put(node.getTargetRack(), rackUsage.get(node.getTargetRack()) + 1); } } Assert.assertTrue(rackUsage.size() == 1); }
@Test public void testFilteredHosts() throws Exception { ClusterCreate spec = TestPlacementUtil .getSimpleClusterSpec(TestPlacementUtil.SIMPLE_CLUSTER_SPEC); AbstractDatacenter dc = TestPlacementUtil .getAbstractDatacenter(TestPlacementUtil.DATACENTER_SPEC); Container container = new Container(dc); container.setTemplateNode(TestPlacementUtil.getTemplateNode()); List<AbstractHost> allHosts = container.getAllHosts(); AbstractHost host = allHosts.get(0); container.removeHost(host.getName()); List<String> outOfSyncHosts = new ArrayList<String>(); outOfSyncHosts.add(host.getName()); List<String> noNetworkHosts = new ArrayList<String>(); noNetworkHosts.add(host.getName()); Map<String, List<String>> filteredHosts = new HashMap<String, List<String>>(); filteredHosts.put(PlacementUtil.OUT_OF_SYNC_HOSTS, outOfSyncHosts); filteredHosts.put(PlacementUtil.NETWORK_NAMES, new ArrayList<String>(Arrays.asList("VM Network"))); filteredHosts.put(PlacementUtil.NO_NETWORKS_HOSTS, noNetworkHosts); PlacementService service = new PlacementService(); try { List<BaseNode> nodes = service.getPlacementPlan(container, spec, null, filteredHosts); } catch (PlacementException e) { System.out.println(e.getMessage()); String[] strs = e.getMessage().split("\n"); Assert.assertEquals(strs[0], "No host available for node [hadoop-worker-4, hadoop-worker-5] that meets the placement policy requirements specified for the number of instances per host. Possible fixes:"); Assert.assertEquals(strs[1], "You must synchronize the time of the following hosts [10.1.1.1] with the Serengeti Management Server to use them."); Assert.assertEquals(strs[2], "You must add these hosts [10.1.1.1] to the network [VM Network] to use them."); } }
@Test public void testRRRackPolicyFailure() throws Exception { ClusterCreate cluster = TestPlacementUtil .getSimpleClusterSpec(TestPlacementUtil.RACK_FAILURE_CLUSTER_SPEC); AbstractDatacenter dc = TestPlacementUtil .getAbstractDatacenter(TestPlacementUtil.RACK_FAILURE_DATACENTER_SPEC); Container container = new Container(dc); container.setTemplateNode(TestPlacementUtil.getTemplateNode()); container.addRackMap(cluster.getHostToRackMap()); PlacementService service = new PlacementService(); try { List<BaseNode> nodes = service.getPlacementPlan(container, cluster, null, new HashMap<String, List<String>>()); } catch (PlacementException e) { System.out.println(e.getMessage()); String[] strs = e.getMessage().split("\n"); Assert.assertEquals(strs[0], "No host available on the racks [rack1] specified for the node [hadoop-data-0]. Review your topology rack-hosts mapping file and correct as necessary. Possible fixes:"); Assert.assertEquals(strs[1], "You must add datastores on these hosts [10.1.1.3, 10.1.1.4] to use them with the node group [data]."); } }