Search in sources :

Example 26 with BlockLocation

use of org.apache.hadoop.fs.BlockLocation in project hadoop by apache.

the class AdlFileSystem method getFileBlockLocations.

@Override
public BlockLocation[] getFileBlockLocations(final FileStatus status, final long offset, final long length) throws IOException {
    if (status == null) {
        return null;
    }
    if ((offset < 0) || (length < 0)) {
        throw new IllegalArgumentException("Invalid start or len parameter");
    }
    if (status.getLen() < offset) {
        return new BlockLocation[0];
    }
    final String[] name = { "localhost" };
    final String[] host = { "localhost" };
    long blockSize = ADL_BLOCK_SIZE;
    int numberOfLocations = (int) (length / blockSize) + ((length % blockSize == 0) ? 0 : 1);
    BlockLocation[] locations = new BlockLocation[numberOfLocations];
    for (int i = 0; i < locations.length; i++) {
        long currentOffset = offset + (i * blockSize);
        long currentLength = Math.min(blockSize, offset + length - currentOffset);
        locations[i] = new BlockLocation(name, host, currentOffset, currentLength);
    }
    return locations;
}
Also used : BlockLocation(org.apache.hadoop.fs.BlockLocation)

Example 27 with BlockLocation

use of org.apache.hadoop.fs.BlockLocation in project hbase by apache.

the class FSRegionScanner method run.

@Override
public void run() {
    try {
        // empty the map for each region
        Map<String, AtomicInteger> blockCountMap = new HashMap<>();
        //get table name
        String tableName = regionPath.getParent().getName();
        int totalBlkCount = 0;
        // ignore null
        FileStatus[] cfList = fs.listStatus(regionPath, new FSUtils.FamilyDirFilter(fs));
        if (null == cfList) {
            return;
        }
        // for each cf, get all the blocks information
        for (FileStatus cfStatus : cfList) {
            if (!cfStatus.isDirectory()) {
                // skip because this is not a CF directory
                continue;
            }
            FileStatus[] storeFileLists = fs.listStatus(cfStatus.getPath());
            if (null == storeFileLists) {
                continue;
            }
            for (FileStatus storeFile : storeFileLists) {
                BlockLocation[] blkLocations = fs.getFileBlockLocations(storeFile, 0, storeFile.getLen());
                if (null == blkLocations) {
                    continue;
                }
                totalBlkCount += blkLocations.length;
                for (BlockLocation blk : blkLocations) {
                    for (String host : blk.getHosts()) {
                        AtomicInteger count = blockCountMap.get(host);
                        if (count == null) {
                            count = new AtomicInteger(0);
                            blockCountMap.put(host, count);
                        }
                        count.incrementAndGet();
                    }
                }
            }
        }
        if (regionToBestLocalityRSMapping != null) {
            int largestBlkCount = 0;
            String hostToRun = null;
            for (Map.Entry<String, AtomicInteger> entry : blockCountMap.entrySet()) {
                String host = entry.getKey();
                int tmp = entry.getValue().get();
                if (tmp > largestBlkCount) {
                    largestBlkCount = tmp;
                    hostToRun = host;
                }
            }
            // empty regions could make this null
            if (null == hostToRun) {
                return;
            }
            if (hostToRun.endsWith(".")) {
                hostToRun = hostToRun.substring(0, hostToRun.length() - 1);
            }
            String name = tableName + ":" + regionPath.getName();
            synchronized (regionToBestLocalityRSMapping) {
                regionToBestLocalityRSMapping.put(name, hostToRun);
            }
        }
        if (regionDegreeLocalityMapping != null && totalBlkCount > 0) {
            Map<String, Float> hostLocalityMap = new HashMap<>();
            for (Map.Entry<String, AtomicInteger> entry : blockCountMap.entrySet()) {
                String host = entry.getKey();
                if (host.endsWith(".")) {
                    host = host.substring(0, host.length() - 1);
                }
                // Locality is fraction of blocks local to this host.
                float locality = ((float) entry.getValue().get()) / totalBlkCount;
                hostLocalityMap.put(host, locality);
            }
            // Put the locality map into the result map, keyed by the encoded name
            // of the region.
            regionDegreeLocalityMapping.put(regionPath.getName(), hostLocalityMap);
        }
    } catch (IOException e) {
        LOG.warn("Problem scanning file system", e);
    } catch (RuntimeException e) {
        LOG.warn("Problem scanning file system", e);
    }
}
Also used : FileStatus(org.apache.hadoop.fs.FileStatus) HashMap(java.util.HashMap) IOException(java.io.IOException) BlockLocation(org.apache.hadoop.fs.BlockLocation) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) HashMap(java.util.HashMap) FSUtils(org.apache.hadoop.hbase.util.FSUtils)

Example 28 with BlockLocation

use of org.apache.hadoop.fs.BlockLocation in project hbase by apache.

the class FSUtils method computeHDFSBlocksDistribution.

/**
   * Compute HDFS blocks distribution of a given file, or a portion of the file
   * @param fs file system
   * @param status file status of the file
   * @param start start position of the portion
   * @param length length of the portion
   * @return The HDFS blocks distribution
   */
public static HDFSBlocksDistribution computeHDFSBlocksDistribution(final FileSystem fs, FileStatus status, long start, long length) throws IOException {
    HDFSBlocksDistribution blocksDistribution = new HDFSBlocksDistribution();
    BlockLocation[] blockLocations = fs.getFileBlockLocations(status, start, length);
    for (BlockLocation bl : blockLocations) {
        String[] hosts = bl.getHosts();
        long len = bl.getLength();
        blocksDistribution.addHostsAndBlockWeight(hosts, len);
    }
    return blocksDistribution;
}
Also used : BlockLocation(org.apache.hadoop.fs.BlockLocation) HDFSBlocksDistribution(org.apache.hadoop.hbase.HDFSBlocksDistribution)

Example 29 with BlockLocation

use of org.apache.hadoop.fs.BlockLocation in project hbase by apache.

the class FSUtils method addToHDFSBlocksDistribution.

/**
   * Update blocksDistribution with blockLocations
   * @param blocksDistribution the hdfs blocks distribution
   * @param blockLocations an array containing block location
   */
public static void addToHDFSBlocksDistribution(HDFSBlocksDistribution blocksDistribution, BlockLocation[] blockLocations) throws IOException {
    for (BlockLocation bl : blockLocations) {
        String[] hosts = bl.getHosts();
        long len = bl.getLength();
        blocksDistribution.addHostsAndBlockWeight(hosts, len);
    }
}
Also used : BlockLocation(org.apache.hadoop.fs.BlockLocation)

Example 30 with BlockLocation

use of org.apache.hadoop.fs.BlockLocation in project hbase by apache.

the class TestRegionFavoredNodes method testFavoredNodes.

@Test
public void testFavoredNodes() throws Exception {
    Assume.assumeTrue(createWithFavoredNode != null);
    // Get the addresses of the datanodes in the cluster.
    InetSocketAddress[] nodes = new InetSocketAddress[REGION_SERVERS];
    List<DataNode> datanodes = TEST_UTIL.getDFSCluster().getDataNodes();
    Method selfAddress;
    try {
        selfAddress = DataNode.class.getMethod("getSelfAddr");
    } catch (NoSuchMethodException ne) {
        selfAddress = DataNode.class.getMethod("getXferAddress");
    }
    for (int i = 0; i < REGION_SERVERS; i++) {
        nodes[i] = (InetSocketAddress) selfAddress.invoke(datanodes.get(i));
    }
    String[] nodeNames = new String[REGION_SERVERS];
    for (int i = 0; i < REGION_SERVERS; i++) {
        nodeNames[i] = nodes[i].getAddress().getHostAddress() + ":" + nodes[i].getPort();
    }
    // them as favored nodes through the region.
    for (int i = 0; i < REGION_SERVERS; i++) {
        HRegionServer server = TEST_UTIL.getHBaseCluster().getRegionServer(i);
        List<Region> regions = server.getOnlineRegions(TABLE_NAME);
        for (Region region : regions) {
            List<org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.ServerName> favoredNodes = new ArrayList<>(3);
            String encodedRegionName = region.getRegionInfo().getEncodedName();
            for (int j = 0; j < FAVORED_NODES_NUM; j++) {
                org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.ServerName.Builder b = org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.ServerName.newBuilder();
                b.setHostName(nodes[(i + j) % REGION_SERVERS].getAddress().getHostAddress());
                b.setPort(nodes[(i + j) % REGION_SERVERS].getPort());
                b.setStartCode(-1);
                favoredNodes.add(b.build());
            }
            server.updateRegionFavoredNodesMapping(encodedRegionName, favoredNodes);
        }
    }
    // get multiple files for each region.
    for (int i = 0; i < FLUSHES; i++) {
        TEST_UTIL.loadTable(table, COLUMN_FAMILY, false);
        TEST_UTIL.flush();
    }
    // they are consistent with the favored nodes for that region.
    for (int i = 0; i < REGION_SERVERS; i++) {
        HRegionServer server = TEST_UTIL.getHBaseCluster().getRegionServer(i);
        List<Region> regions = server.getOnlineRegions(TABLE_NAME);
        for (Region region : regions) {
            List<String> files = region.getStoreFileList(new byte[][] { COLUMN_FAMILY });
            for (String file : files) {
                FileStatus status = TEST_UTIL.getDFSCluster().getFileSystem().getFileStatus(new Path(new URI(file).getPath()));
                BlockLocation[] lbks = ((DistributedFileSystem) TEST_UTIL.getDFSCluster().getFileSystem()).getFileBlockLocations(status, 0, Long.MAX_VALUE);
                for (BlockLocation lbk : lbks) {
                    locations: for (String info : lbk.getNames()) {
                        for (int j = 0; j < FAVORED_NODES_NUM; j++) {
                            if (info.equals(nodeNames[(i + j) % REGION_SERVERS])) {
                                continue locations;
                            }
                        }
                        // This block was at a location that was not a favored location.
                        fail("Block location " + info + " not a favored node");
                    }
                }
            }
        }
    }
}
Also used : FileStatus(org.apache.hadoop.fs.FileStatus) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) BlockLocation(org.apache.hadoop.fs.BlockLocation) URI(java.net.URI) DataNode(org.apache.hadoop.hdfs.server.datanode.DataNode) Path(org.apache.hadoop.fs.Path) Method(java.lang.reflect.Method) DistributedFileSystem(org.apache.hadoop.hdfs.DistributedFileSystem) Test(org.junit.Test)

Aggregations

BlockLocation (org.apache.hadoop.fs.BlockLocation)87 Path (org.apache.hadoop.fs.Path)41 FileStatus (org.apache.hadoop.fs.FileStatus)30 Test (org.junit.Test)29 FileSystem (org.apache.hadoop.fs.FileSystem)16 Configuration (org.apache.hadoop.conf.Configuration)14 ArrayList (java.util.ArrayList)13 IOException (java.io.IOException)10 FSDataOutputStream (org.apache.hadoop.fs.FSDataOutputStream)10 DatanodeInfo (org.apache.hadoop.hdfs.protocol.DatanodeInfo)7 DataNode (org.apache.hadoop.hdfs.server.datanode.DataNode)7 InetSocketAddress (java.net.InetSocketAddress)5 FSDataInputStream (org.apache.hadoop.fs.FSDataInputStream)5 LocatedFileStatus (org.apache.hadoop.fs.LocatedFileStatus)5 HdfsConfiguration (org.apache.hadoop.hdfs.HdfsConfiguration)5 LocatedBlocks (org.apache.hadoop.hdfs.protocol.LocatedBlocks)5 IgfsBlockLocation (org.apache.ignite.igfs.IgfsBlockLocation)5 IgfsPath (org.apache.ignite.igfs.IgfsPath)5 HashMap (java.util.HashMap)4 Random (java.util.Random)4