Search in sources :

Example 6 with Message

use of org.apache.hbase.thirdparty.com.google.protobuf.Message in project hbase by apache.

the class AsyncAggregationClient method max.

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

        private R max;

        @Override
        protected void aggregate(RegionInfo region, AggregateResponse resp) throws IOException {
            if (resp.getFirstPartCount() > 0) {
                R result = getCellValueFromProto(ci, resp, 0);
                if (max == null || (result != null && ci.compare(max, result) < 0)) {
                    max = result;
                }
            }
        }

        @Override
        protected R getFinalResult() {
            return max;
        }
    };
    table.<AggregateService, AggregateResponse>coprocessorService(AggregateService::newStub, (stub, controller, rpcCallback) -> stub.getMax(controller, req, rpcCallback), callback).fromRow(nullToEmpty(scan.getStartRow()), scan.includeStartRow()).toRow(nullToEmpty(scan.getStopRow()), scan.includeStopRow()).execute();
    return future;
}
Also used : AdvancedScanResultConsumer(org.apache.hadoop.hbase.client.AdvancedScanResultConsumer) FutureUtils.addListener(org.apache.hadoop.hbase.util.FutureUtils.addListener) CoprocessorCallback(org.apache.hadoop.hbase.client.AsyncTable.CoprocessorCallback) ColumnInterpreter(org.apache.hadoop.hbase.coprocessor.ColumnInterpreter) AggregateResponse(org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateResponse) Result(org.apache.hadoop.hbase.client.Result) CompletableFuture(java.util.concurrent.CompletableFuture) AggregateRequest(org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateRequest) Message(org.apache.hbase.thirdparty.com.google.protobuf.Message) HConstants(org.apache.hadoop.hbase.HConstants) Map(java.util.Map) AggregationHelper.validateArgAndGetPB(org.apache.hadoop.hbase.client.coprocessor.AggregationHelper.validateArgAndGetPB) NoSuchElementException(java.util.NoSuchElementException) Cell(org.apache.hadoop.hbase.Cell) Bytes(org.apache.hadoop.hbase.util.Bytes) ReflectionUtils(org.apache.hadoop.hbase.util.ReflectionUtils) IOException(java.io.IOException) NavigableSet(java.util.NavigableSet) NavigableMap(java.util.NavigableMap) AggregateService(org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateService) Scan(org.apache.hadoop.hbase.client.Scan) InterfaceAudience(org.apache.yetus.audience.InterfaceAudience) TreeMap(java.util.TreeMap) AggregationHelper.getParsedGenericInstance(org.apache.hadoop.hbase.client.coprocessor.AggregationHelper.getParsedGenericInstance) AsyncTable(org.apache.hadoop.hbase.client.AsyncTable) RegionInfo(org.apache.hadoop.hbase.client.RegionInfo) CompletableFuture(java.util.concurrent.CompletableFuture) AggregateRequest(org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateRequest) AggregateResponse(org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateResponse) RegionInfo(org.apache.hadoop.hbase.client.RegionInfo) IOException(java.io.IOException)

Example 7 with Message

use of org.apache.hbase.thirdparty.com.google.protobuf.Message in project hbase by apache.

the class AsyncAggregationClient method avg.

public static <R, S, P extends Message, Q extends Message, T extends Message> CompletableFuture<Double> avg(AsyncTable<?> table, ColumnInterpreter<R, S, P, Q, T> ci, Scan scan) {
    CompletableFuture<Double> future = new CompletableFuture<>();
    AggregateRequest req;
    try {
        req = validateArgAndGetPB(scan, ci, false);
    } catch (IOException e) {
        future.completeExceptionally(e);
        return future;
    }
    AbstractAggregationCallback<Double> callback = new AbstractAggregationCallback<Double>(future) {

        private S sum;

        long count = 0L;

        @Override
        protected void aggregate(RegionInfo region, AggregateResponse resp) throws IOException {
            if (resp.getFirstPartCount() > 0) {
                sum = ci.add(sum, getPromotedValueFromProto(ci, resp, 0));
                count += resp.getSecondPart().asReadOnlyByteBuffer().getLong();
            }
        }

        @Override
        protected Double getFinalResult() {
            return ci.divideForAvg(sum, count);
        }
    };
    table.<AggregateService, AggregateResponse>coprocessorService(AggregateService::newStub, (stub, controller, rpcCallback) -> stub.getAvg(controller, req, rpcCallback), callback).fromRow(nullToEmpty(scan.getStartRow()), scan.includeStartRow()).toRow(nullToEmpty(scan.getStopRow()), scan.includeStopRow()).execute();
    return future;
}
Also used : AdvancedScanResultConsumer(org.apache.hadoop.hbase.client.AdvancedScanResultConsumer) FutureUtils.addListener(org.apache.hadoop.hbase.util.FutureUtils.addListener) CoprocessorCallback(org.apache.hadoop.hbase.client.AsyncTable.CoprocessorCallback) ColumnInterpreter(org.apache.hadoop.hbase.coprocessor.ColumnInterpreter) AggregateResponse(org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateResponse) Result(org.apache.hadoop.hbase.client.Result) CompletableFuture(java.util.concurrent.CompletableFuture) AggregateRequest(org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateRequest) Message(org.apache.hbase.thirdparty.com.google.protobuf.Message) HConstants(org.apache.hadoop.hbase.HConstants) Map(java.util.Map) AggregationHelper.validateArgAndGetPB(org.apache.hadoop.hbase.client.coprocessor.AggregationHelper.validateArgAndGetPB) NoSuchElementException(java.util.NoSuchElementException) Cell(org.apache.hadoop.hbase.Cell) Bytes(org.apache.hadoop.hbase.util.Bytes) ReflectionUtils(org.apache.hadoop.hbase.util.ReflectionUtils) IOException(java.io.IOException) NavigableSet(java.util.NavigableSet) NavigableMap(java.util.NavigableMap) AggregateService(org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateService) Scan(org.apache.hadoop.hbase.client.Scan) InterfaceAudience(org.apache.yetus.audience.InterfaceAudience) TreeMap(java.util.TreeMap) AggregationHelper.getParsedGenericInstance(org.apache.hadoop.hbase.client.coprocessor.AggregationHelper.getParsedGenericInstance) AsyncTable(org.apache.hadoop.hbase.client.AsyncTable) RegionInfo(org.apache.hadoop.hbase.client.RegionInfo) CompletableFuture(java.util.concurrent.CompletableFuture) AggregateRequest(org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateRequest) AggregateResponse(org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateResponse) RegionInfo(org.apache.hadoop.hbase.client.RegionInfo) IOException(java.io.IOException)

Example 8 with Message

use of org.apache.hbase.thirdparty.com.google.protobuf.Message in project hbase by apache.

the class AsyncAggregationClient method sum.

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);
    } catch (IOException e) {
        future.completeExceptionally(e);
        return future;
    }
    AbstractAggregationCallback<S> callback = new AbstractAggregationCallback<S>(future) {

        private S sum;

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

        @Override
        protected S getFinalResult() {
            return sum;
        }
    };
    table.<AggregateService, AggregateResponse>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;
}
Also used : AdvancedScanResultConsumer(org.apache.hadoop.hbase.client.AdvancedScanResultConsumer) FutureUtils.addListener(org.apache.hadoop.hbase.util.FutureUtils.addListener) CoprocessorCallback(org.apache.hadoop.hbase.client.AsyncTable.CoprocessorCallback) ColumnInterpreter(org.apache.hadoop.hbase.coprocessor.ColumnInterpreter) AggregateResponse(org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateResponse) Result(org.apache.hadoop.hbase.client.Result) CompletableFuture(java.util.concurrent.CompletableFuture) AggregateRequest(org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateRequest) Message(org.apache.hbase.thirdparty.com.google.protobuf.Message) HConstants(org.apache.hadoop.hbase.HConstants) Map(java.util.Map) AggregationHelper.validateArgAndGetPB(org.apache.hadoop.hbase.client.coprocessor.AggregationHelper.validateArgAndGetPB) NoSuchElementException(java.util.NoSuchElementException) Cell(org.apache.hadoop.hbase.Cell) Bytes(org.apache.hadoop.hbase.util.Bytes) ReflectionUtils(org.apache.hadoop.hbase.util.ReflectionUtils) IOException(java.io.IOException) NavigableSet(java.util.NavigableSet) NavigableMap(java.util.NavigableMap) AggregateService(org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateService) Scan(org.apache.hadoop.hbase.client.Scan) InterfaceAudience(org.apache.yetus.audience.InterfaceAudience) TreeMap(java.util.TreeMap) AggregationHelper.getParsedGenericInstance(org.apache.hadoop.hbase.client.coprocessor.AggregationHelper.getParsedGenericInstance) AsyncTable(org.apache.hadoop.hbase.client.AsyncTable) RegionInfo(org.apache.hadoop.hbase.client.RegionInfo) CompletableFuture(java.util.concurrent.CompletableFuture) AggregateRequest(org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateRequest) AggregateResponse(org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateResponse) RegionInfo(org.apache.hadoop.hbase.client.RegionInfo) IOException(java.io.IOException)

Example 9 with Message

use of org.apache.hbase.thirdparty.com.google.protobuf.Message in project hbase by apache.

the class RpcServer method call.

/**
 * This is a server side method, which is invoked over RPC. On success
 * the return response has protobuf response payload. On failure, the
 * exception name and the stack trace are returned in the protobuf response.
 */
@Override
public Pair<Message, CellScanner> call(RpcCall call, MonitoredRPCHandler status) throws IOException {
    try {
        MethodDescriptor md = call.getMethod();
        Message param = call.getParam();
        status.setRPC(md.getName(), new Object[] { param }, call.getReceiveTime());
        // TODO: Review after we add in encoded data blocks.
        status.setRPCPacket(param);
        status.resume("Servicing call");
        // get an instance of the method arg type
        HBaseRpcController controller = new HBaseRpcControllerImpl(call.getCellScanner());
        controller.setCallTimeout(call.getTimeout());
        Message result = call.getService().callBlockingMethod(md, controller, param);
        long receiveTime = call.getReceiveTime();
        long startTime = call.getStartTime();
        long endTime = EnvironmentEdgeManager.currentTime();
        int processingTime = (int) (endTime - startTime);
        int qTime = (int) (startTime - receiveTime);
        int totalTime = (int) (endTime - receiveTime);
        if (LOG.isTraceEnabled()) {
            LOG.trace(CurCall.get().toString() + ", response " + TextFormat.shortDebugString(result) + " queueTime: " + qTime + " processingTime: " + processingTime + " totalTime: " + totalTime);
        }
        // Use the raw request call size for now.
        long requestSize = call.getSize();
        long responseSize = result.getSerializedSize();
        if (call.isClientCellBlockSupported()) {
            // Include the payload size in HBaseRpcController
            responseSize += call.getResponseCellSize();
        }
        metrics.dequeuedCall(qTime);
        metrics.processedCall(processingTime);
        metrics.totalCall(totalTime);
        metrics.receivedRequest(requestSize);
        metrics.sentResponse(responseSize);
        // log any RPC responses that are slower than the configured warn
        // response time or larger than configured warning size
        boolean tooSlow = (processingTime > warnResponseTime && warnResponseTime > -1);
        boolean tooLarge = (responseSize > warnResponseSize && warnResponseSize > -1);
        if (tooSlow || tooLarge) {
            final String userName = call.getRequestUserName().orElse(StringUtils.EMPTY);
            // when tagging, we let TooLarge trump TooSmall to keep output simple
            // note that large responses will often also be slow.
            logResponse(param, md.getName(), md.getName() + "(" + param.getClass().getName() + ")", tooLarge, tooSlow, status.getClient(), startTime, processingTime, qTime, responseSize, userName);
            if (this.namedQueueRecorder != null && this.isOnlineLogProviderEnabled) {
                // send logs to ring buffer owned by slowLogRecorder
                final String className = server == null ? StringUtils.EMPTY : server.getClass().getSimpleName();
                this.namedQueueRecorder.addRecord(new RpcLogDetails(call, param, status.getClient(), responseSize, className, tooSlow, tooLarge));
            }
        }
        return new Pair<>(result, controller.cellScanner());
    } catch (Throwable e) {
        // need to pass it over the wire.
        if (e instanceof ServiceException) {
            if (e.getCause() == null) {
                LOG.debug("Caught a ServiceException with null cause", e);
            } else {
                e = e.getCause();
            }
        }
        // increment the number of requests that were exceptions.
        metrics.exception(e);
        if (e instanceof LinkageError)
            throw new DoNotRetryIOException(e);
        if (e instanceof IOException)
            throw (IOException) e;
        LOG.error("Unexpected throwable object ", e);
        throw new IOException(e.getMessage(), e);
    }
}
Also used : Message(org.apache.hbase.thirdparty.com.google.protobuf.Message) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) RpcLogDetails(org.apache.hadoop.hbase.namequeues.RpcLogDetails) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) IOException(java.io.IOException) MethodDescriptor(org.apache.hbase.thirdparty.com.google.protobuf.Descriptors.MethodDescriptor) ServiceException(org.apache.hbase.thirdparty.com.google.protobuf.ServiceException) Pair(org.apache.hadoop.hbase.util.Pair)

Example 10 with Message

use of org.apache.hbase.thirdparty.com.google.protobuf.Message in project hbase by apache.

the class SimpleServerRpcConnection method readAndProcess.

/**
 * Read off the wire. If there is not enough data to read, update the connection state with what
 * we have and returns.
 * @return Returns -1 if failure (and caller will close connection), else zero or more.
 * @throws IOException
 * @throws InterruptedException
 */
public int readAndProcess() throws IOException, InterruptedException {
    // If we have not read the connection setup preamble, look to see if that is on the wire.
    if (!connectionPreambleRead) {
        int count = readPreamble();
        if (!connectionPreambleRead) {
            return count;
        }
    }
    // Try and read in an int. it will be length of the data to read (or -1 if a ping). We catch the
    // integer length into the 4-byte this.dataLengthBuffer.
    int count = read4Bytes();
    if (count < 0 || dataLengthBuffer.remaining() > 0) {
        return count;
    }
    // or it is a request.
    if (data == null) {
        dataLengthBuffer.flip();
        int dataLength = dataLengthBuffer.getInt();
        if (dataLength == RpcClient.PING_CALL_ID) {
            if (!useWrap) {
                // covers the !useSasl too
                dataLengthBuffer.clear();
                // ping message
                return 0;
            }
        }
        if (dataLength < 0) {
            // A data length of zero is legal.
            throw new DoNotRetryIOException("Unexpected data length " + dataLength + "!! from " + getHostAddress());
        }
        if (dataLength > this.rpcServer.maxRequestSize) {
            String msg = "RPC data length of " + dataLength + " received from " + getHostAddress() + " is greater than max allowed " + this.rpcServer.maxRequestSize + ". Set \"" + SimpleRpcServer.MAX_REQUEST_SIZE + "\" on server to override this limit (not recommended)";
            SimpleRpcServer.LOG.warn(msg);
            if (connectionHeaderRead && connectionPreambleRead) {
                incRpcCount();
                // Construct InputStream for the non-blocking SocketChannel
                // We need the InputStream because we want to read only the request header
                // instead of the whole rpc.
                ByteBuffer buf = ByteBuffer.allocate(1);
                InputStream is = new InputStream() {

                    @Override
                    public int read() throws IOException {
                        SimpleServerRpcConnection.this.rpcServer.channelRead(channel, buf);
                        buf.flip();
                        int x = buf.get();
                        buf.flip();
                        return x;
                    }
                };
                CodedInputStream cis = CodedInputStream.newInstance(is);
                int headerSize = cis.readRawVarint32();
                Message.Builder builder = RequestHeader.newBuilder();
                ProtobufUtil.mergeFrom(builder, cis, headerSize);
                RequestHeader header = (RequestHeader) builder.build();
                // Notify the client about the offending request
                SimpleServerCall reqTooBig = new SimpleServerCall(header.getCallId(), this.service, null, null, null, null, this, 0, this.addr, EnvironmentEdgeManager.currentTime(), 0, this.rpcServer.bbAllocator, this.rpcServer.cellBlockBuilder, null, responder);
                RequestTooBigException reqTooBigEx = new RequestTooBigException(msg);
                this.rpcServer.metrics.exception(reqTooBigEx);
                // Otherwise, throw a DoNotRetryIOException.
                if (VersionInfoUtil.hasMinimumVersion(connectionHeader.getVersionInfo(), RequestTooBigException.MAJOR_VERSION, RequestTooBigException.MINOR_VERSION)) {
                    reqTooBig.setResponse(null, null, reqTooBigEx, msg);
                } else {
                    reqTooBig.setResponse(null, null, new DoNotRetryIOException(msg), msg);
                }
                // In most cases we will write out the response directly. If not, it is still OK to just
                // close the connection without writing out the reqTooBig response. Do not try to write
                // out directly here, and it will cause deserialization error if the connection is slow
                // and we have a half writing response in the queue.
                reqTooBig.sendResponseIfReady();
            }
            // Close the connection
            return -1;
        }
        // Initialize this.data with a ByteBuff.
        // This call will allocate a ByteBuff to read request into and assign to this.data
        // Also when we use some buffer(s) from pool, it will create a CallCleanup instance also and
        // assign to this.callCleanup
        initByteBuffToReadInto(dataLength);
        // Increment the rpc count. This counter will be decreased when we write
        // the response. If we want the connection to be detected as idle properly, we
        // need to keep the inc / dec correct.
        incRpcCount();
    }
    count = channelDataRead(channel, data);
    if (count >= 0 && data.remaining() == 0) {
        // count==0 if dataLength == 0
        process();
    }
    return count;
}
Also used : Message(org.apache.hbase.thirdparty.com.google.protobuf.Message) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) CodedInputStream(org.apache.hbase.thirdparty.com.google.protobuf.CodedInputStream) InputStream(java.io.InputStream) CodedInputStream(org.apache.hbase.thirdparty.com.google.protobuf.CodedInputStream) RequestTooBigException(org.apache.hadoop.hbase.exceptions.RequestTooBigException) RequestHeader(org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos.RequestHeader) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Message (org.apache.hbase.thirdparty.com.google.protobuf.Message)28 IOException (java.io.IOException)19 CompletableFuture (java.util.concurrent.CompletableFuture)11 DoNotRetryIOException (org.apache.hadoop.hbase.DoNotRetryIOException)11 Result (org.apache.hadoop.hbase.client.Result)8 Map (java.util.Map)7 NavigableMap (java.util.NavigableMap)7 NavigableSet (java.util.NavigableSet)7 NoSuchElementException (java.util.NoSuchElementException)7 TreeMap (java.util.TreeMap)7 Cell (org.apache.hadoop.hbase.Cell)7 HConstants (org.apache.hadoop.hbase.HConstants)7 AdvancedScanResultConsumer (org.apache.hadoop.hbase.client.AdvancedScanResultConsumer)7 AsyncTable (org.apache.hadoop.hbase.client.AsyncTable)7 CoprocessorCallback (org.apache.hadoop.hbase.client.AsyncTable.CoprocessorCallback)7 RegionInfo (org.apache.hadoop.hbase.client.RegionInfo)7 Scan (org.apache.hadoop.hbase.client.Scan)7 AggregationHelper.getParsedGenericInstance (org.apache.hadoop.hbase.client.coprocessor.AggregationHelper.getParsedGenericInstance)7 AggregationHelper.validateArgAndGetPB (org.apache.hadoop.hbase.client.coprocessor.AggregationHelper.validateArgAndGetPB)7 ColumnInterpreter (org.apache.hadoop.hbase.coprocessor.ColumnInterpreter)7