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

网格计算

开发平台:

Java

  1. /**
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  *     http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing, software
  13.  * distributed under the License is distributed on an "AS IS" BASIS,
  14.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.  * See the License for the specific language governing permissions and
  16.  * limitations under the License.
  17.  */
  18. package org.apache.hadoop.mapred;
  19. import org.apache.hadoop.fs.BlockLocation;
  20. import org.apache.hadoop.net.NetworkTopology;
  21. import junit.framework.TestCase;
  22. public class TestGetSplitHosts extends TestCase {
  23.   public void testGetSplitHosts() throws Exception {
  24.     int numBlocks = 3;
  25.     int block1Size = 100, block2Size = 150, block3Size = 75;
  26.     int fileSize = block1Size + block2Size + block3Size;
  27.     int replicationFactor = 3;
  28.     NetworkTopology clusterMap = new NetworkTopology();
  29.     
  30.     BlockLocation[] bs = new BlockLocation[numBlocks];
  31.     
  32.     String [] block1Hosts = {"host1","host2","host3"};
  33.     String [] block1Names = {"host1:100","host2:100","host3:100"};
  34.     String [] block1Racks = {"/rack1/","/rack1/","/rack2/"};
  35.     String [] block1Paths = new String[replicationFactor];
  36.     
  37.     for (int i = 0; i < replicationFactor; i++) { 
  38.       block1Paths[i] = block1Racks[i]+block1Names[i];
  39.     }
  40.     
  41.     bs[0] = new BlockLocation(block1Names,block1Hosts,
  42.                               block1Paths,0,block1Size);
  43.     String [] block2Hosts = {"host4","host5","host6"};
  44.     String [] block2Names = {"host4:100","host5:100","host6:100"};
  45.     String [] block2Racks = {"/rack2/","/rack3/","/rack3/"};
  46.     String [] block2Paths = new String[replicationFactor];
  47.     
  48.     for (int i = 0; i < replicationFactor; i++) { 
  49.       block2Paths[i] = block2Racks[i]+block2Names[i];
  50.     }
  51.     bs[1] = new BlockLocation(block2Names,block2Hosts,
  52.                               block2Paths,block1Size,block2Size);
  53.     String [] block3Hosts = {"host1","host7","host8"};
  54.     String [] block3Names = {"host1:100","host7:100","host8:100"};
  55.     String [] block3Racks = {"/rack1/","/rack4/","/rack4/"};
  56.     String [] block3Paths = new String[replicationFactor];
  57.     
  58.     for (int i = 0; i < replicationFactor; i++) { 
  59.       block3Paths[i] = block3Racks[i]+block3Names[i];
  60.     }
  61.     bs[2] = new BlockLocation(block3Names,block3Hosts,
  62.                               block3Paths,block1Size+block2Size,
  63.                               block3Size);
  64.     
  65.     SequenceFileInputFormat< String, String> sif = 
  66.       new SequenceFileInputFormat<String,String>();
  67.     String [] hosts = sif.getSplitHosts(bs, 0, fileSize, clusterMap);
  68.     // Contributions By Racks are
  69.     // Rack1   175       
  70.     // Rack2   275       
  71.     // Rack3   150       
  72.     // So, Rack2 hosts, host4 and host 3 should be returned
  73.     // even if their individual contribution is not the highest
  74.     assertTrue (hosts.length == replicationFactor);
  75.     assertTrue(hosts[0].equalsIgnoreCase("host4"));
  76.     assertTrue(hosts[1].equalsIgnoreCase("host3"));
  77.     assertTrue(hosts[2].equalsIgnoreCase("host1"));
  78.     
  79.     
  80.     // Now Create the blocks without topology information
  81.     bs[0] = new BlockLocation(block1Names,block1Hosts,0,block1Size);
  82.     bs[1] = new BlockLocation(block2Names,block2Hosts,block1Size,block2Size);
  83.     bs[2] = new BlockLocation(block3Names,block3Hosts,block1Size+block2Size,
  84.                                block3Size);
  85.     hosts = sif.getSplitHosts(bs, 0, fileSize, clusterMap);
  86.     
  87.     // host1 makes the highest contribution among all hosts
  88.     // So, that should be returned before others
  89.     
  90.     assertTrue (hosts.length == replicationFactor);
  91.     assertTrue(hosts[0].equalsIgnoreCase("host1"));
  92.   }
  93. }