Example usage for com.google.common.collect ImmutableRangeMap subRangeMap

List of usage examples for com.google.common.collect ImmutableRangeMap subRangeMap

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableRangeMap subRangeMap.

Prototype

@Override
    public ImmutableRangeMap<K, V> subRangeMap(final Range<K> range) 

Source Link

Usage

From source file:org.apache.drill.exec.store.AffinityCalculator.java

/**
 * For a given RowGroup, calculate how many bytes are available on each on drillbit endpoint
 *
 * @param rowGroup the RowGroup to calculate endpoint bytes for
 *///from   w ww .  jav  a2s. c  o m
public void setEndpointBytes(ParquetGroupScan.RowGroupInfo rowGroup) {
    Stopwatch watch = new Stopwatch();
    watch.start();
    String fileName = rowGroup.getPath();
    if (!blockMapMap.containsKey(fileName)) {
        buildBlockMap(fileName);
    }

    ImmutableRangeMap<Long, BlockLocation> blockMap = blockMapMap.get(fileName);
    HashMap<String, Long> hostMap = new HashMap<>();
    HashMap<DrillbitEndpoint, Long> endpointByteMap = new HashMap();
    long start = rowGroup.getStart();
    long end = start + rowGroup.getLength();
    Range<Long> rowGroupRange = Range.closedOpen(start, end);

    // Find submap of ranges that intersect with the rowGroup
    ImmutableRangeMap<Long, BlockLocation> subRangeMap = blockMap.subRangeMap(rowGroupRange);

    // Iterate through each block in this submap and get the host for the block location
    for (Map.Entry<Range<Long>, BlockLocation> block : subRangeMap.asMapOfRanges().entrySet()) {
        String[] hosts;
        Range<Long> blockRange = block.getKey();
        try {
            hosts = block.getValue().getHosts();
        } catch (IOException ioe) {
            throw new RuntimeException("Failed to get hosts for block location", ioe);
        }
        Range<Long> intersection = rowGroupRange.intersection(blockRange);
        long bytes = intersection.upperEndpoint() - intersection.lowerEndpoint();

        // For each host in the current block location, add the intersecting bytes to the corresponding endpoint
        for (String host : hosts) {
            DrillbitEndpoint endpoint = getDrillBitEndpoint(host);
            if (endpointByteMap.containsKey(endpoint)) {
                endpointByteMap.put(endpoint, endpointByteMap.get(endpoint) + bytes);
            } else {
                if (endpoint != null)
                    endpointByteMap.put(endpoint, bytes);
            }
        }
    }

    rowGroup.setEndpointBytes(endpointByteMap);
    rowGroup.setMaxBytes(endpointByteMap.size() > 0 ? Collections.max(endpointByteMap.values()) : 0);
    logger.debug("Row group ({},{}) max bytes {}", rowGroup.getPath(), rowGroup.getStart(),
            rowGroup.getMaxBytes());
    watch.stop();
    logger.debug("Took {} ms to set endpoint bytes", watch.elapsed(TimeUnit.MILLISECONDS));
}

From source file:org.apache.drill.exec.store.schedule.BlockMapBuilder.java

/**
 * For a given FileWork, calculate how many bytes are available on each on drillbit endpoint
 *
 * @param work the FileWork to calculate endpoint bytes for
 * @throws IOException/*from  www .  j  a v  a 2s. c o  m*/
 */
public EndpointByteMap getEndpointByteMap(FileWork work) throws IOException {
    Stopwatch watch = new Stopwatch();
    watch.start();
    Path fileName = new Path(work.getPath());

    ImmutableRangeMap<Long, BlockLocation> blockMap = getBlockMap(fileName);
    EndpointByteMapImpl endpointByteMap = new EndpointByteMapImpl();
    long start = work.getStart();
    long end = start + work.getLength();
    Range<Long> rowGroupRange = Range.closedOpen(start, end);

    // Find submap of ranges that intersect with the rowGroup
    ImmutableRangeMap<Long, BlockLocation> subRangeMap = blockMap.subRangeMap(rowGroupRange);

    // Iterate through each block in this submap and get the host for the block location
    for (Map.Entry<Range<Long>, BlockLocation> block : subRangeMap.asMapOfRanges().entrySet()) {
        String[] hosts;
        Range<Long> blockRange = block.getKey();
        try {
            hosts = block.getValue().getHosts();
        } catch (IOException ioe) {
            throw new RuntimeException("Failed to get hosts for block location", ioe);
        }
        Range<Long> intersection = rowGroupRange.intersection(blockRange);
        long bytes = intersection.upperEndpoint() - intersection.lowerEndpoint();

        // For each host in the current block location, add the intersecting bytes to the corresponding endpoint
        for (String host : hosts) {
            DrillbitEndpoint endpoint = getDrillBitEndpoint(host);
            if (endpoint != null) {
                endpointByteMap.add(endpoint, bytes);
            } else {
                logger.info("Failure finding Drillbit running on host {}.  Skipping affinity to that host.",
                        host);
            }
        }
    }

    logger.debug("FileWork group ({},{}) max bytes {}", work.getPath(), work.getStart(),
            endpointByteMap.getMaxBytes());

    logger.debug("Took {} ms to set endpoint bytes", watch.stop().elapsed(TimeUnit.MILLISECONDS));
    return endpointByteMap;
}