Ejemplo n.º 1
0
 /**
  * Gets a stream to write data to a block. The stream can only be backed by Alluxio storage.
  *
  * @param blockId the block to write
  * @param blockSize the standard block size to write, or -1 if the block already exists (and this
  *     stream is just storing the block in Alluxio again)
  * @param address the address of the worker to write the block to, fails if the worker cannot
  *     serve the request
  * @return a {@link BufferedBlockOutStream} which can be used to write data to the block in a
  *     streaming fashion
  * @throws IOException if the block cannot be written
  */
 public BufferedBlockOutStream getOutStream(long blockId, long blockSize, WorkerNetAddress address)
     throws IOException {
   if (blockSize == -1) {
     try (CloseableResource<BlockMasterClient> blockMasterClientResource =
         mContext.acquireMasterClientResource()) {
       blockSize = blockMasterClientResource.get().getBlockInfo(blockId).getLength();
     } catch (AlluxioException e) {
       throw new IOException(e);
     }
   }
   // No specified location to write to.
   if (address == null) {
     throw new RuntimeException(ExceptionMessage.NO_WORKER_AVAILABLE.getMessage());
   }
   // Location is local.
   if (NetworkAddressUtils.getLocalHostName(ClientContext.getConf()).equals(address.getHost())) {
     if (mContext.hasLocalWorker()) {
       return new LocalBlockOutStream(blockId, blockSize);
     } else {
       throw new IOException(ExceptionMessage.NO_LOCAL_WORKER.getMessage("write"));
     }
   }
   // Location is specified and it is remote.
   return new RemoteBlockOutStream(blockId, blockSize, address);
 }
Ejemplo n.º 2
0
 /**
  * Obtains a client for a remote based on the given network address. Illegal argument exception is
  * thrown if the hostname is the local hostname. Runtime exception is thrown if the client cannot
  * be created with a connection to the hostname.
  *
  * @param address the address of the worker
  * @return a worker client with a connection to the specified hostname
  */
 private BlockWorkerClient acquireRemoteWorkerClient(WorkerNetAddress address) {
   // If we couldn't find a worker, crash.
   if (address == null) {
     // TODO(calvin): Better exception usage.
     throw new RuntimeException(ExceptionMessage.NO_WORKER_AVAILABLE.getMessage());
   }
   Preconditions.checkArgument(
       !address.getHost().equals(NetworkAddressUtils.getLocalHostName()),
       PreconditionMessage.REMOTE_CLIENT_BUT_LOCAL_HOSTNAME);
   long clientId = IdUtils.getRandomNonNegativeLong();
   return new RetryHandlingBlockWorkerClient(
       address, ClientContext.getBlockClientExecutorService(), clientId, false);
 }
Ejemplo n.º 3
0
 /**
  * Obtains a client for a worker with the given address.
  *
  * @param address the address of the worker to get a client to
  * @return a {@link BlockWorkerClient} connected to the worker with the given hostname
  * @throws IOException if no Alluxio worker is available for the given hostname
  */
 public BlockWorkerClient acquireWorkerClient(WorkerNetAddress address) throws IOException {
   BlockWorkerClient client;
   if (address == null) {
     throw new RuntimeException(ExceptionMessage.NO_WORKER_AVAILABLE.getMessage());
   }
   if (address.getHost().equals(NetworkAddressUtils.getLocalHostName())) {
     client = acquireLocalWorkerClient(address);
     if (client == null) {
       throw new IOException(ExceptionMessage.NO_WORKER_AVAILABLE_ON_ADDRESS.getMessage(address));
     }
   } else {
     client = acquireRemoteWorkerClient(address);
   }
   return client;
 }
Ejemplo n.º 4
0
  /**
   * 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);
  }
Ejemplo n.º 5
0
 /**
  * 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);
   }
 }