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 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. 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> 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);
    }
  }