public static <R, S, P extends Message, Q extends Message, T extends Message> CompletableFuture<S>
    sum(AsyncTable<?> table, ColumnInterpreter<R, S, P, Q, T> ci, Scan scan) {
    CompletableFuture<S> future = new CompletableFuture<>();
    AggregateRequest req;
    try {
      req = validateArgAndGetPB(scan, ci, false, true);
    } catch (IOException e) {
      future.completeExceptionally(e);
      return future;
    }
    AbstractAggregationCallback<S> callback =
      new AbstractAggregationCallback<>(future, req, AggregateService::getSum) {
        private S sum;
        private final Object lock = new Object();

        @Override
        protected void aggregate(RegionInfo region, AggregateResponse resp) throws IOException {
          if (resp.getFirstPartCount() > 0) {
            S s = getPromotedValueFromProto(ci, resp, 0);
            synchronized (lock) {
              sum = ci.add(sum, s);
            }
          }
        }

        @Override
        protected S getFinalResult() {
          synchronized (lock) {
            return sum;
          }
        }
      };
    table
      .coprocessorService(AggregateService::newStub,
        (stub, controller, rpcCallback) -> stub.getSum(controller, req, rpcCallback), callback)
      .fromRow(nullToEmpty(scan.getStartRow()), scan.includeStartRow())
      .toRow(nullToEmpty(scan.getStopRow()), scan.includeStopRow()).execute();
    return future;
  }
This client class is for invoking the aggregate functions deployed on the Region Server side via
the AggregateService. This class will implement the supporting functionality for
summing/processing the individual results obtained from the AggregateService for each region.
hbase-endpoint/src/main/java/org/apache/hadoop/hbase/client/coprocessor/AsyncAggregationClient.java