/** * Gets a stream to read the data of a block. The stream is backed by Alluxio storage. * * @param blockId the block to read from * @return a {@link BlockInStream} which can be used to read the data in a streaming fashion * @throws IOException if the block does not exist */ public BufferedBlockInStream getInStream(long blockId) throws IOException { BlockInfo blockInfo; try (CloseableResource<BlockMasterClient> masterClientResource = mContext.acquireMasterClientResource()) { blockInfo = masterClientResource.get().getBlockInfo(blockId); } catch (AlluxioException e) { throw new IOException(e); } if (blockInfo.getLocations().isEmpty()) { throw new IOException("Block " + blockId + " is not available in Alluxio"); } // TODO(calvin): Get location via a policy. // Although blockInfo.locations are sorted by tier, we prefer reading from the local worker. // But when there is no local worker or there are no local blocks, we prefer the first // location in blockInfo.locations that is nearest to memory tier. // Assuming if there is no local worker, there are no local blocks in blockInfo.locations. // TODO(cc): Check mContext.hasLocalWorker before finding for a local block when the TODO // for hasLocalWorker is fixed. String localHostName = NetworkAddressUtils.getLocalHostName(ClientContext.getConf()); for (BlockLocation location : blockInfo.getLocations()) { WorkerNetAddress workerNetAddress = location.getWorkerAddress(); if (workerNetAddress.getHost().equals(localHostName)) { // There is a local worker and the block is local. try { return new LocalBlockInStream(blockId, blockInfo.getLength()); } catch (IOException e) { LOG.warn("Failed to open local stream for block " + blockId + ". " + e.getMessage()); // Getting a local stream failed, do not try again break; } } } // No local worker/block, get the first location since it's nearest to memory tier. WorkerNetAddress workerNetAddress = blockInfo.getLocations().get(0).getWorkerAddress(); return new RemoteBlockInStream(blockId, blockInfo.getLength(), workerNetAddress); }
/** * Attempts to promote a block in Alluxio space. If the block is not present, this method will * return without an error. If the block is present in multiple workers, only one worker will * receive the promotion request. * * @param blockId the id of the block to promote * @throws IOException if the block does not exist */ public void promote(long blockId) throws IOException { BlockInfo info; try (CloseableResource<BlockMasterClient> blockMasterClientResource = mContext.acquireMasterClientResource()) { info = blockMasterClientResource.get().getBlockInfo(blockId); } catch (AlluxioException e) { throw new IOException(e); } if (info.getLocations().isEmpty()) { // Nothing to promote return; } // Get the first worker address for now, as this will likely be the location being read from // TODO(calvin): Get this location via a policy (possibly location is a parameter to promote) WorkerNetAddress workerAddr = info.getLocations().get(0).getWorkerAddress(); BlockWorkerClient blockWorkerClient = mContext.acquireWorkerClient(workerAddr.getHost()); try { blockWorkerClient.promoteBlock(blockId); } catch (AlluxioException e) { throw new IOException(e); } finally { mContext.releaseWorkerClient(blockWorkerClient); } }