Java Code Examples for org.apache.hadoop.hdfs.protocol.ClientProtocol#getBlockLocations()

The following examples show how to use org.apache.hadoop.hdfs.protocol.ClientProtocol#getBlockLocations() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileDataServlet.java    From RDFS with Apache License 2.0 6 votes vote down vote up
/** Select a datanode to service this request, which is the first one
 * in the returned array. The rest of the elements in the datanode
 * are possible candidates if the first one fails.
 * Currently, this looks at no more than the first five blocks of a file,
 * selecting a datanode randomly from the most represented.
 */
private static DatanodeInfo[] pickSrcDatanode(FileStatus i,
    ClientProtocol nnproxy) throws IOException {
  // a race condition can happen by initializing a static member this way.
  // A proper fix should make JspHelper a singleton. Since it doesn't affect 
  // correctness, we leave it as is for now.
  if (jspHelper == null)
    jspHelper = new JspHelper();
  final LocatedBlocks blks = nnproxy.getBlockLocations(
      i.getPath().toUri().getPath(), 0, 1);
  if (i.getLen() == 0 || blks.getLocatedBlocks().size() <= 0) {
    // pick a random datanode
    return new DatanodeInfo[] { jspHelper.randomNode() };
  }
  return jspHelper.bestNode(blks);
}
 
Example 2
Source File: FileDataServlet.java    From hadoop-gpu with Apache License 2.0 6 votes vote down vote up
/** Select a datanode to service this request.
 * Currently, this looks at no more than the first five blocks of a file,
 * selecting a datanode randomly from the most represented.
 */
private static DatanodeID pickSrcDatanode(FileStatus i,
    ClientProtocol nnproxy) throws IOException {
  // a race condition can happen by initializing a static member this way.
  // A proper fix should make JspHelper a singleton. Since it doesn't affect 
  // correctness, we leave it as is for now.
  if (jspHelper == null)
    jspHelper = new JspHelper();
  final LocatedBlocks blks = nnproxy.getBlockLocations(
      i.getPath().toUri().getPath(), 0, 1);
  if (i.getLen() == 0 || blks.getLocatedBlocks().size() <= 0) {
    // pick a random datanode
    return jspHelper.randomNode();
  }
  return jspHelper.bestNode(blks.get(0));
}
 
Example 3
Source File: FileDataServlet.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/** Create a redirection URL */
private URL createRedirectURL(String path, String encodedPath, HdfsFileStatus status, 
    UserGroupInformation ugi, ClientProtocol nnproxy, HttpServletRequest request, String dt)
    throws IOException {
  String scheme = request.getScheme();
  final LocatedBlocks blks = nnproxy.getBlockLocations(
      status.getFullPath(new Path(path)).toUri().getPath(), 0, 1);
  final Configuration conf = NameNodeHttpServer.getConfFromContext(
      getServletContext());
  final DatanodeID host = pickSrcDatanode(blks, status, conf);
  final String hostname;
  if (host instanceof DatanodeInfo) {
    hostname = host.getHostName();
  } else {
    hostname = host.getIpAddr();
  }

  int port = "https".equals(scheme) ? host.getInfoSecurePort() : host
      .getInfoPort();

  String dtParam = "";
  if (dt != null) {
    dtParam = JspHelper.getDelegationTokenUrlParam(dt);
  }

  // Add namenode address to the url params
  NameNode nn = NameNodeHttpServer.getNameNodeFromContext(
      getServletContext());
  String addr = nn.getNameNodeAddressHostPortString();
  String addrParam = JspHelper.getUrlParam(JspHelper.NAMENODE_ADDRESS, addr);
  
  return new URL(scheme, hostname, port,
      "/streamFile" + encodedPath + '?' +
      "ugi=" + ServletUtil.encodeQueryValue(ugi.getShortUserName()) +
      dtParam + addrParam);
}
 
Example 4
Source File: TestInterDatanodeProtocol.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
public static LocatedBlock getLastLocatedBlock(
    ClientProtocol namenode, String src
) throws IOException {
  //get block info for the last block
  LocatedBlocks locations = namenode.getBlockLocations(src, 0, Long.MAX_VALUE);
  List<LocatedBlock> blocks = locations.getLocatedBlocks();
  DataNode.LOG.info("blocks.size()=" + blocks.size());
  assertTrue(blocks.size() > 0);

  return blocks.get(blocks.size() - 1);
}
 
Example 5
Source File: TestInjectionForSimulatedStorage.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
private void waitForBlockReplication(String filename, 
                                     ClientProtocol namenode,
                                     int expected, long maxWaitSec) 
                                     throws IOException {
  long start = System.currentTimeMillis();
  
  //wait for all the blocks to be replicated;
  LOG.info("Checking for block replication for " + filename);
  
  LocatedBlocks blocks = namenode.getBlockLocations(filename, 0, Long.MAX_VALUE);
  assertEquals(numBlocks, blocks.locatedBlockCount());
  
  for (int i = 0; i < numBlocks; ++i) {
    LOG.info("Checking for block:" + (i+1));
    while (true) { // Loop to check for block i (usually when 0 is done all will be done
      blocks = namenode.getBlockLocations(filename, 0, Long.MAX_VALUE);
      assertEquals(numBlocks, blocks.locatedBlockCount());
      LocatedBlock block = blocks.get(i);
      int actual = block.getLocations().length;
      if ( actual == expected ) {
        LOG.info("Got enough replicas for " + (i+1) + "th block " + block.getBlock() +
            ", got " + actual + ".");
        break;
      }
      LOG.info("Not enough replicas for " + (i+1) + "th block " + block.getBlock() +
                             " yet. Expecting " + expected + ", got " + 
                             actual + ".");
    
      if (maxWaitSec > 0 && 
          (System.currentTimeMillis() - start) > (maxWaitSec * 1000)) {
        throw new IOException("Timedout while waiting for all blocks to " +
                              " be replicated for " + filename);
      }
    
      try {
        Thread.sleep(500);
      } catch (InterruptedException ignored) {}
    }
  }
}
 
Example 6
Source File: DFSClient.java    From RDFS with Apache License 2.0 5 votes vote down vote up
private static LocatedBlocks callGetBlockLocations(
    ClientProtocol namenode,
    String src, long start, long length, boolean supportMetaInfo) throws IOException {
  try {
    if (supportMetaInfo) {
      return namenode.openAndFetchMetaInfo(src, start, length);
    }
    return namenode.getBlockLocations(src, start, length);
  } catch(RemoteException re) {
    throw re.unwrapRemoteException(AccessControlException.class,
                                  FileNotFoundException.class);
  }
}
 
Example 7
Source File: TestInjectionForSimulatedStorage.java    From RDFS with Apache License 2.0 5 votes vote down vote up
private void waitForBlockReplication(String filename, 
                                     ClientProtocol namenode,
                                     int expected, long maxWaitSec) 
                                     throws IOException {
  long start = System.currentTimeMillis();
  
  //wait for all the blocks to be replicated;
  LOG.info("Checking for block replication for " + filename);
  
  LocatedBlocks blocks = namenode.getBlockLocations(filename, 0, Long.MAX_VALUE);
  assertEquals(numBlocks, blocks.locatedBlockCount());
  
  for (int i = 0; i < numBlocks; ++i) {
    LOG.info("Checking for block:" + (i+1));
    while (true) { // Loop to check for block i (usually when 0 is done all will be done
      blocks = namenode.getBlockLocations(filename, 0, Long.MAX_VALUE);
      assertEquals(numBlocks, blocks.locatedBlockCount());
      LocatedBlock block = blocks.get(i);
      int actual = block.getLocations().length;
      if ( actual == expected ) {
        LOG.info("Got enough replicas for " + (i+1) + "th block " + block.getBlock() +
            ", got " + actual + ".");
        break;
      }
      LOG.info("Not enough replicas for " + (i+1) + "th block " + block.getBlock() +
                             " yet. Expecting " + expected + ", got " + 
                             actual + ".");
    
      if (maxWaitSec > 0 && 
          (System.currentTimeMillis() - start) > (maxWaitSec * 1000)) {
        throw new IOException("Timedout while waiting for all blocks to " +
                              " be replicated for " + filename);
      }
    
      try {
        Thread.sleep(500);
      } catch (InterruptedException ignored) {}
    }
  }
}
 
Example 8
Source File: TestNameNodeIdempotence.java    From RDFS with Apache License 2.0 5 votes vote down vote up
/**
 * Test addBlock() name-node RPC is idempotent
 */
public void testIdepotentCallsAddBlock() throws IOException {
  ClientProtocol nn = cluster.getNameNode();
  FileSystem fs = cluster.getFileSystem();
  DFSClient dfsclient = ((DistributedFileSystem) fs).dfs;

  String src = "/testNameNodeFingerprintSent1.txt";
  // Path f = new Path(src);

  DFSOutputStream dos = (DFSOutputStream) dfsclient.create(src, true,
      (short) 1, 512L);

  FSDataOutputStream a_out = new FSDataOutputStream(dos); // fs.create(f);

  for (int i = 0; i < 512; i++) {
    a_out.writeBytes("bc");
  }
  a_out.flush();

  LocatedBlocks lb = nn.getBlockLocations(src, 256, 257);
  LocatedBlock lb1 = nn.addBlockAndFetchMetaInfo(src, dfsclient.clientName,
      null, null, 512L, lb.getLocatedBlocks().get(lb.locatedBlockCount() - 1)
          .getBlock());
  LocatedBlock lb2 = nn.addBlockAndFetchMetaInfo(src, dfsclient.clientName,
      null, null, 512L, lb.getLocatedBlocks().get(lb.locatedBlockCount() - 1)
          .getBlock());
  TestCase.assertTrue("blocks: " + lb1.getBlock() + " and " + lb2.getBlock(),
      lb1.getBlock().equals(lb2.getBlock()));
}
 
Example 9
Source File: TestInterDatanodeProtocol.java    From big-c with Apache License 2.0 5 votes vote down vote up
public static LocatedBlock getLastLocatedBlock(
    ClientProtocol namenode, String src) throws IOException {
  //get block info for the last block
  LocatedBlocks locations = namenode.getBlockLocations(src, 0, Long.MAX_VALUE);
  List<LocatedBlock> blocks = locations.getLocatedBlocks();
  DataNode.LOG.info("blocks.size()=" + blocks.size());
  assertTrue(blocks.size() > 0);

  return blocks.get(blocks.size() - 1);
}
 
Example 10
Source File: TestInjectionForSimulatedStorage.java    From big-c with Apache License 2.0 5 votes vote down vote up
private void waitForBlockReplication(String filename, 
                                     ClientProtocol namenode,
                                     int expected, long maxWaitSec) 
                                     throws IOException {
  long start = Time.monotonicNow();
  
  //wait for all the blocks to be replicated;
  LOG.info("Checking for block replication for " + filename);
  
  LocatedBlocks blocks = namenode.getBlockLocations(filename, 0, Long.MAX_VALUE);
  assertEquals(numBlocks, blocks.locatedBlockCount());
  
  for (int i = 0; i < numBlocks; ++i) {
    LOG.info("Checking for block:" + (i+1));
    while (true) { // Loop to check for block i (usually when 0 is done all will be done
      blocks = namenode.getBlockLocations(filename, 0, Long.MAX_VALUE);
      assertEquals(numBlocks, blocks.locatedBlockCount());
      LocatedBlock block = blocks.get(i);
      int actual = block.getLocations().length;
      if ( actual == expected ) {
        LOG.info("Got enough replicas for " + (i+1) + "th block " + block.getBlock() +
            ", got " + actual + ".");
        break;
      }
      LOG.info("Not enough replicas for " + (i+1) + "th block " + block.getBlock() +
                             " yet. Expecting " + expected + ", got " + 
                             actual + ".");
    
      if (maxWaitSec > 0 && 
          (Time.monotonicNow() - start) > (maxWaitSec * 1000)) {
        throw new IOException("Timedout while waiting for all blocks to " +
                              " be replicated for " + filename);
      }
    
      try {
        Thread.sleep(500);
      } catch (InterruptedException ignored) {}
    }
  }
}
 
Example 11
Source File: FileDataServlet.java    From big-c with Apache License 2.0 5 votes vote down vote up
/** Create a redirection URL */
private URL createRedirectURL(String path, String encodedPath, HdfsFileStatus status, 
    UserGroupInformation ugi, ClientProtocol nnproxy, HttpServletRequest request, String dt)
    throws IOException {
  String scheme = request.getScheme();
  final LocatedBlocks blks = nnproxy.getBlockLocations(
      status.getFullPath(new Path(path)).toUri().getPath(), 0, 1);
  final Configuration conf = NameNodeHttpServer.getConfFromContext(
      getServletContext());
  final DatanodeID host = pickSrcDatanode(blks, status, conf);
  final String hostname;
  if (host instanceof DatanodeInfo) {
    hostname = host.getHostName();
  } else {
    hostname = host.getIpAddr();
  }

  int port = "https".equals(scheme) ? host.getInfoSecurePort() : host
      .getInfoPort();

  String dtParam = "";
  if (dt != null) {
    dtParam = JspHelper.getDelegationTokenUrlParam(dt);
  }

  // Add namenode address to the url params
  NameNode nn = NameNodeHttpServer.getNameNodeFromContext(
      getServletContext());
  String addr = nn.getNameNodeAddressHostPortString();
  String addrParam = JspHelper.getUrlParam(JspHelper.NAMENODE_ADDRESS, addr);
  
  return new URL(scheme, hostname, port,
      "/streamFile" + encodedPath + '?' +
      "ugi=" + ServletUtil.encodeQueryValue(ugi.getShortUserName()) +
      dtParam + addrParam);
}
 
Example 12
Source File: DFSClient.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * @see ClientProtocol#getBlockLocations(String, long, long)
 */
static LocatedBlocks callGetBlockLocations(ClientProtocol namenode,
    String src, long start, long length) 
    throws IOException {
  try {
    return namenode.getBlockLocations(src, start, length);
  } catch(RemoteException re) {
    throw re.unwrapRemoteException(AccessControlException.class,
                                   FileNotFoundException.class,
                                   UnresolvedPathException.class);
  }
}
 
Example 13
Source File: TestReplication.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private void waitForBlockReplication(String filename, 
                                     ClientProtocol namenode,
                                     int expected, long maxWaitSec) 
                                     throws IOException {
  long start = Time.monotonicNow();
  
  //wait for all the blocks to be replicated;
  LOG.info("Checking for block replication for " + filename);
  while (true) {
    boolean replOk = true;
    LocatedBlocks blocks = namenode.getBlockLocations(filename, 0, 
                                                      Long.MAX_VALUE);
    
    for (Iterator<LocatedBlock> iter = blocks.getLocatedBlocks().iterator();
         iter.hasNext();) {
      LocatedBlock block = iter.next();
      int actual = block.getLocations().length;
      if ( actual < expected ) {
        LOG.info("Not enough replicas for " + block.getBlock()
            + " yet. Expecting " + expected + ", got " + actual + ".");
        replOk = false;
        break;
      }
    }
    
    if (replOk) {
      return;
    }
    
    if (maxWaitSec > 0 && 
        (Time.monotonicNow() - start) > (maxWaitSec * 1000)) {
      throw new IOException("Timedout while waiting for all blocks to " +
                            " be replicated for " + filename);
    }
    
    try {
      Thread.sleep(500);
    } catch (InterruptedException ignored) {}
  }
}
 
Example 14
Source File: TestInterDatanodeProtocol.java    From hadoop with Apache License 2.0 5 votes vote down vote up
public static LocatedBlock getLastLocatedBlock(
    ClientProtocol namenode, String src) throws IOException {
  //get block info for the last block
  LocatedBlocks locations = namenode.getBlockLocations(src, 0, Long.MAX_VALUE);
  List<LocatedBlock> blocks = locations.getLocatedBlocks();
  DataNode.LOG.info("blocks.size()=" + blocks.size());
  assertTrue(blocks.size() > 0);

  return blocks.get(blocks.size() - 1);
}
 
Example 15
Source File: TestInjectionForSimulatedStorage.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private void waitForBlockReplication(String filename, 
                                     ClientProtocol namenode,
                                     int expected, long maxWaitSec) 
                                     throws IOException {
  long start = Time.monotonicNow();
  
  //wait for all the blocks to be replicated;
  LOG.info("Checking for block replication for " + filename);
  
  LocatedBlocks blocks = namenode.getBlockLocations(filename, 0, Long.MAX_VALUE);
  assertEquals(numBlocks, blocks.locatedBlockCount());
  
  for (int i = 0; i < numBlocks; ++i) {
    LOG.info("Checking for block:" + (i+1));
    while (true) { // Loop to check for block i (usually when 0 is done all will be done
      blocks = namenode.getBlockLocations(filename, 0, Long.MAX_VALUE);
      assertEquals(numBlocks, blocks.locatedBlockCount());
      LocatedBlock block = blocks.get(i);
      int actual = block.getLocations().length;
      if ( actual == expected ) {
        LOG.info("Got enough replicas for " + (i+1) + "th block " + block.getBlock() +
            ", got " + actual + ".");
        break;
      }
      LOG.info("Not enough replicas for " + (i+1) + "th block " + block.getBlock() +
                             " yet. Expecting " + expected + ", got " + 
                             actual + ".");
    
      if (maxWaitSec > 0 && 
          (Time.monotonicNow() - start) > (maxWaitSec * 1000)) {
        throw new IOException("Timedout while waiting for all blocks to " +
                              " be replicated for " + filename);
      }
    
      try {
        Thread.sleep(500);
      } catch (InterruptedException ignored) {}
    }
  }
}
 
Example 16
Source File: DFSClient.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * @see ClientProtocol#getBlockLocations(String, long, long)
 */
static LocatedBlocks callGetBlockLocations(ClientProtocol namenode,
    String src, long start, long length) 
    throws IOException {
  try {
    return namenode.getBlockLocations(src, start, length);
  } catch(RemoteException re) {
    throw re.unwrapRemoteException(AccessControlException.class,
                                   FileNotFoundException.class,
                                   UnresolvedPathException.class);
  }
}
 
Example 17
Source File: TestFileCorruption.java    From RDFS with Apache License 2.0 4 votes vote down vote up
/**
 * check if listCorruptFileBlocks() returns the right number of
 * corrupt files if there are two corrupt files with the same name
 * in different directories
 */
public void test2CorruptFilesWithSameName() throws Exception {
  MiniDFSCluster cluster = null;
  Random random = new Random();
  
  try {
    Configuration conf = new Configuration();
    // datanode scans directories
    conf.setInt("dfs.datanode.directoryscan.interval", 1);
    // datanode sends block reports 
    conf.setInt("dfs.blockreport.intervalMsec", 3 * 1000);
    conf.setBoolean("dfs.permissions", false);
    cluster = new MiniDFSCluster(conf, 1, true, null);
    FileSystem fs = cluster.getFileSystem();
    
    assertTrue("fs is not a DFS", fs instanceof DistributedFileSystem);
    DistributedFileSystem dfs = (DistributedFileSystem) fs;

    Path file1 = new Path("/srcdat12/test2file.test");
    Path file2 = new Path("/srcdat13/test2file.test");
    // create two files with the same name
    DFSTestUtil.createFile(fs, file1, 1L, (short)1, 1L);
    DFSTestUtil.createFile(fs, file2, 1L, (short)1, 1L);

    // fetch bad file list from namenode. There should be none.
    ClientProtocol namenode = DFSClient.createNamenode(conf);
    String[] badFiles = DFSUtil.getCorruptFiles(dfs);
    assertTrue("Namenode has " + badFiles.length +
               " corrupt files. Expecting None.",
        badFiles.length == 0);

    // Now deliberately corrupt one block in each file
    Path[] files = {file1, file2};
    for (Path file: files) {
      LocatedBlocks fileBlocks = 
        namenode.getBlockLocations(file.toString(), 0, 1L);
      LocatedBlock block = fileBlocks.get(0);
      File data_dir = 
        new File(TEST_ROOT_DIR, "dfs/data/");
      File dir1 = cluster.getBlockDirectory("data"+(2 * 0 + 1));
      File dir2 = cluster.getBlockDirectory("data"+(2 * 0 + 2));
      if (!(dir1.isDirectory() && dir2.isDirectory())) {
        throw new IOException("data directories not found for data node 0: " +
                              dir1.toString() + " " + dir2.toString());
      }

      File[] dirs = new File[2];
      dirs[0] = dir1; 
      dirs[1] = dir2;
      for (File dir: dirs) {
        File[] blockFiles = dir.listFiles();
        if ((blockFiles == null) || (blockFiles.length == 0)) {
          throw 
            new IOException("no blocks found in data node's data directory");
        }

        for (File blockFile: blockFiles) {
          if ((blockFile.getName().
               startsWith("blk_" + block.getBlock().getBlockId())) &&
              (!blockFile.getName().endsWith(".meta"))) {
            blockFile.delete();
          }
        }
      }
      LocatedBlock[] toReport = { block };
      namenode.reportBadBlocks(toReport);
    }

    // fetch bad file list from namenode. There should be 2.
    badFiles = DFSUtil.getCorruptFiles(dfs);
    assertTrue("Namenode has " + badFiles.length + " bad files. Expecting 2.",
        badFiles.length == 2);
  } finally {
    if (cluster != null) {
      cluster.shutdown(); 
    }
  }
}
 
Example 18
Source File: TestFavoredNodes.java    From RDFS with Apache License 2.0 4 votes vote down vote up
@Test
public void testCrossFileSystemAddBlock() throws Exception {
  // Create source file.
  String fileName = "/testCrossFileSystemAddBlock";
  DFSTestUtil.createFile(cluster.getFileSystem(), new Path(fileName),
      (long) FILE_SIZE, (short) 3, (long) 0);

  // Create RPC connections
  ClientProtocol dstNamenode = DFSClient.createRPCNamenode(
      NameNode.getAddress(remoteCluster.getFileSystem().getUri()
          .getAuthority()), remoteConf,
      UnixUserGroupInformation.login(remoteConf, true)).getProxy();
  ClientProtocol srcNamenode = DFSClient.createRPCNamenode(
      NameNode.getAddress(cluster.getFileSystem().getUri().getAuthority()),
      conf, UnixUserGroupInformation.login(conf, true)).getProxy();

  // Create destination file.
  String dstFile = "/dst" + fileName;
  FileStatus srcStat = cluster.getFileSystem().getFileStatus(
      new Path(fileName));
  String clientName = "testClient";
  dstNamenode.create(dstFile, srcStat.getPermission(), clientName, true,
      true, srcStat.getReplication(), srcStat.getBlockSize());
  FSNamesystem dstNamesystem = remoteCluster.getNameNode().getNamesystem();

  LocatedBlocks lbks = srcNamenode.getBlockLocations(fileName, 0,
      Long.MAX_VALUE);
  for (LocatedBlock lbk : lbks.getLocatedBlocks()) {
    DatanodeInfo[] locs = lbk.getLocations();
    int slice = r.nextInt(locs.length);
    LocatedBlock dstlbk = dstNamenode.addBlock(dstFile, clientName, null,
        Arrays.copyOfRange(locs, 0, slice + 1));
    DatanodeInfo[] dstlocs = dstlbk.getLocations();
    List<String> dstlocHostnames = new ArrayList<String>(dstlocs.length);
    for (DatanodeInfo dstloc : dstlocs) {
      dstlocHostnames.add(dstloc.getHostName());
    }
    assertEquals(conf.getInt("dfs.replication", 3), dstlocs.length);
    for (int i = 0; i <= slice; i++) {
      assertTrue("Expected " + locs[i].getHostName() + " was not found",
          dstlocHostnames.contains(locs[i].getHostName()));
      // Allows us to make the namenode think that these blocks have been
      // successfully written to the datanode, helps us to add the next block
      // without completing the previous block.
      dstNamesystem.blocksMap.addNode(dstlbk.getBlock(),
          dstNamesystem.getDatanode(dstlocs[i]), srcStat.getReplication());
    }
  }
}
 
Example 19
Source File: TestReplication.java    From hadoop-gpu with Apache License 2.0 4 votes vote down vote up
private void waitForBlockReplication(String filename, 
                                     ClientProtocol namenode,
                                     int expected, long maxWaitSec) 
                                     throws IOException {
  long start = System.currentTimeMillis();
  
  //wait for all the blocks to be replicated;
  LOG.info("Checking for block replication for " + filename);
  int iters = 0;
  while (true) {
    boolean replOk = true;
    LocatedBlocks blocks = namenode.getBlockLocations(filename, 0, 
                                                      Long.MAX_VALUE);
    
    for (Iterator<LocatedBlock> iter = blocks.getLocatedBlocks().iterator();
         iter.hasNext();) {
      LocatedBlock block = iter.next();
      int actual = block.getLocations().length;
      if ( actual < expected ) {
        if (true || iters > 0) {
          LOG.info("Not enough replicas for " + block.getBlock() +
                             " yet. Expecting " + expected + ", got " + 
                             actual + ".");
        }
        replOk = false;
        break;
      }
    }
    
    if (replOk) {
      return;
    }
    
    iters++;
    
    if (maxWaitSec > 0 && 
        (System.currentTimeMillis() - start) > (maxWaitSec * 1000)) {
      throw new IOException("Timedout while waiting for all blocks to " +
                            " be replicated for " + filename);
    }
    
    try {
      Thread.sleep(500);
    } catch (InterruptedException ignored) {}
  }
}
 
Example 20
Source File: TestReplication.java    From RDFS with Apache License 2.0 4 votes vote down vote up
private void waitForBlockReplication(String filename, 
                                     ClientProtocol namenode,
                                     int expected, long maxWaitSec,
                                     boolean isUnderConstruction) 
                                     throws IOException {
  long start = System.currentTimeMillis();
  
  //wait for all the blocks to be replicated;
  LOG.info("Checking for block replication for " + filename);
  int iters = 0;
  while (true) {
    boolean replOk = true;
    LocatedBlocks blocks = namenode.getBlockLocations(filename, 0, 
                                                      Long.MAX_VALUE);
    
    for (Iterator<LocatedBlock> iter = blocks.getLocatedBlocks().iterator();
         iter.hasNext();) {
      LocatedBlock block = iter.next();
      if (isUnderConstruction && !iter.hasNext()) {
        break;  // do not check the last block
      }
      int actual = block.getLocations().length;
      if ( actual < expected ) {
        if (true || iters > 0) {
          LOG.info("Not enough replicas for " + block.getBlock() +
                             " yet. Expecting " + expected + ", got " + 
                             actual + ".");
        }
        replOk = false;
        break;
      }
    }
    
    if (replOk) {
      return;
    }
    
    iters++;
    
    if (maxWaitSec > 0 && 
        (System.currentTimeMillis() - start) > (maxWaitSec * 1000)) {
      throw new IOException("Timedout while waiting for all blocks to " +
                            " be replicated for " + filename);
    }
    
    try {
      Thread.sleep(500);
    } catch (InterruptedException ignored) {}
  }
}