Esempio n. 1
0
 /**
  * This method handles submitting and then waiting for the request from the server. It uses the
  * ClientRequest API to actually write the request and then read back the response. This
  * implementation will not block for a response from the server.
  *
  * @param <T> Return type
  * @param clientRequest ClientRequest implementation used to write the request and read the
  *     response
  * @param operationName Simple string representing the type of request
  * @return Data returned by the individual requests
  */
 private <T> void requestAsync(
     ClientRequest<T> delegate,
     NonblockingStoreCallback callback,
     long timeoutMs,
     String operationName) {
   pool.submitAsync(this.destination, delegate, callback, timeoutMs, operationName);
 }
Esempio n. 2
0
  /**
   * This method handles submitting and then waiting for the request from the server. It uses the
   * ClientRequest API to actually write the request and then read back the response. This
   * implementation will block for a response from the server.
   *
   * @param <T> Return type
   * @param clientRequest ClientRequest implementation used to write the request and read the
   *     response
   * @param operationName Simple string representing the type of request
   * @return Data returned by the individual requests
   */
  private <T> void requestAsync(
      ClientRequest<T> delegate,
      NonblockingStoreCallback callback,
      long timeoutMs,
      String operationName) {
    ClientRequestExecutor clientRequestExecutor = null;

    try {
      clientRequestExecutor = pool.checkout(destination);
    } catch (Exception e) {
      // If we can't check out a socket from the pool, we'll usually get
      // either an IOException (subclass) or an UnreachableStoreException
      // error. However, in the case of asynchronous calls, we want the
      // error to be reported via our callback, not returned to the caller
      // directly.
      if (!(e instanceof UnreachableStoreException))
        e = new UnreachableStoreException("Failure in " + operationName + ": " + e.getMessage(), e);

      try {
        callback.requestComplete(e, 0);
      } catch (Exception ex) {
        if (logger.isEnabledFor(Level.WARN)) logger.warn(ex, ex);
      }

      return;
    }

    NonblockingStoreCallbackClientRequest<T> clientRequest =
        new NonblockingStoreCallbackClientRequest<T>(delegate, clientRequestExecutor, callback);
    clientRequestExecutor.addClientRequest(clientRequest, timeoutMs);
  }
Esempio n. 3
0
  /**
   * This method handles submitting and then waiting for the request from the server. It uses the
   * ClientRequest API to actually write the request and then read back the response. This
   * implementation will block for a response from the server.
   *
   * @param <T> Return type
   * @param clientRequest ClientRequest implementation used to write the request and read the
   *     response
   * @param operationName Simple string representing the type of request
   * @return Data returned by the individual requests
   */
  private <T> T request(ClientRequest<T> delegate, String operationName) {
    ClientRequestExecutor clientRequestExecutor = pool.checkout(destination);

    try {
      BlockingClientRequest<T> blockingClientRequest =
          new BlockingClientRequest<T>(delegate, timeoutMs);
      clientRequestExecutor.addClientRequest(blockingClientRequest, timeoutMs);
      blockingClientRequest.await();
      return blockingClientRequest.getResult();
    } catch (InterruptedException e) {
      throw new UnreachableStoreException(
          "Failure in " + operationName + " on " + destination + ": " + e.getMessage(), e);
    } catch (IOException e) {
      clientRequestExecutor.close();
      throw new UnreachableStoreException(
          "Failure in " + operationName + " on " + destination + ": " + e.getMessage(), e);
    } finally {
      pool.checkin(destination, clientRequestExecutor);
    }
  }
Esempio n. 4
0
  /**
   * This method handles submitting and then waiting for the request from the server. It uses the
   * ClientRequest API to actually write the request and then read back the response. This
   * implementation will block for a response from the server.
   *
   * @param <T> Return type
   * @param clientRequest ClientRequest implementation used to write the request and read the
   *     response
   * @param operationName Simple string representing the type of request
   * @return Data returned by the individual requests
   */
  private <T> T request(ClientRequest<T> delegate, String operationName) {
    long startTimeMs = -1;
    long startTimeNs = -1;

    if (logger.isDebugEnabled()) {
      startTimeMs = System.currentTimeMillis();
    }
    ClientRequestExecutor clientRequestExecutor = pool.checkout(destination);
    String debugMsgStr = "";

    startTimeNs = System.nanoTime();

    BlockingClientRequest<T> blockingClientRequest = null;
    try {
      blockingClientRequest = new BlockingClientRequest<T>(delegate, timeoutMs);
      clientRequestExecutor.addClientRequest(
          blockingClientRequest, timeoutMs, System.nanoTime() - startTimeNs);

      boolean awaitResult = blockingClientRequest.await();

      if (awaitResult == false) {
        blockingClientRequest.timeOut();
      }

      if (logger.isDebugEnabled()) debugMsgStr += "success";

      return blockingClientRequest.getResult();
    } catch (InterruptedException e) {

      if (logger.isDebugEnabled()) debugMsgStr += "unreachable: " + e.getMessage();

      throw new UnreachableStoreException(
          "Failure in " + operationName + " on " + destination + ": " + e.getMessage(), e);
    } catch (UnreachableStoreException e) {
      clientRequestExecutor.close();

      if (logger.isDebugEnabled()) debugMsgStr += "failure: " + e.getMessage();

      throw new UnreachableStoreException(
          "Failure in " + operationName + " on " + destination + ": " + e.getMessage(),
          e.getCause());
    } finally {
      if (blockingClientRequest != null && !blockingClientRequest.isComplete()) {
        // close the executor if we timed out
        clientRequestExecutor.close();
      }
      // Record operation time
      long opTimeNs = Utils.elapsedTimeNs(startTimeNs, System.nanoTime());
      if (stats != null) {
        stats.recordSyncOpTimeNs(destination, opTimeNs);
      }
      if (logger.isDebugEnabled()) {
        logger.debug(
            "Sync request end, type: "
                + operationName
                + " requestRef: "
                + System.identityHashCode(delegate)
                + " totalTimeNs: "
                + opTimeNs
                + " start time: "
                + startTimeMs
                + " end time: "
                + System.currentTimeMillis()
                + " client:"
                + clientRequestExecutor.getSocketChannel().socket().getLocalAddress()
                + ":"
                + clientRequestExecutor.getSocketChannel().socket().getLocalPort()
                + " server: "
                + clientRequestExecutor.getSocketChannel().socket().getRemoteSocketAddress()
                + " outcome: "
                + debugMsgStr);
      }

      pool.checkin(destination, clientRequestExecutor);
    }
  }