Search in sources :

Example 1 with RpcCallContext

use of org.apache.hadoop.hbase.ipc.RpcCallContext in project hbase by apache.

the class VersionInfoUtil method currentClientHasMinimumVersion.

public static boolean currentClientHasMinimumVersion(int major, int minor) {
    RpcCallContext call = RpcServer.getCurrentCall();
    HBaseProtos.VersionInfo versionInfo = call != null ? call.getClientVersionInfo() : null;
    return hasMinimumVersion(versionInfo, major, minor);
}
Also used : RpcCallContext(org.apache.hadoop.hbase.ipc.RpcCallContext) HBaseProtos(org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos)

Example 2 with RpcCallContext

use of org.apache.hadoop.hbase.ipc.RpcCallContext in project hbase by apache.

the class RSRpcServices method scan.

/**
 * Scan data in a table.
 *
 * @param controller the RPC controller
 * @param request the scan request
 * @throws ServiceException
 */
@Override
public ScanResponse scan(final RpcController controller, final ScanRequest request) throws ServiceException {
    if (controller != null && !(controller instanceof HBaseRpcController)) {
        throw new UnsupportedOperationException("We only do " + "HBaseRpcControllers! FIX IF A PROBLEM: " + controller);
    }
    if (!request.hasScannerId() && !request.hasScan()) {
        throw new ServiceException(new DoNotRetryIOException("Missing required input: scannerId or scan"));
    }
    try {
        checkOpen();
    } catch (IOException e) {
        if (request.hasScannerId()) {
            String scannerName = toScannerName(request.getScannerId());
            if (LOG.isDebugEnabled()) {
                LOG.debug("Server shutting down and client tried to access missing scanner " + scannerName);
            }
            final LeaseManager leaseManager = server.getLeaseManager();
            if (leaseManager != null) {
                try {
                    leaseManager.cancelLease(scannerName);
                } catch (LeaseException le) {
                    // No problem, ignore
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Un-able to cancel lease of scanner. It could already be closed.");
                    }
                }
            }
        }
        throw new ServiceException(e);
    }
    requestCount.increment();
    rpcScanRequestCount.increment();
    RegionScannerHolder rsh;
    ScanResponse.Builder builder = ScanResponse.newBuilder();
    String scannerName;
    try {
        if (request.hasScannerId()) {
            // The downstream projects such as AsyncHBase in OpenTSDB need this value. See HBASE-18000
            // for more details.
            long scannerId = request.getScannerId();
            builder.setScannerId(scannerId);
            scannerName = toScannerName(scannerId);
            rsh = getRegionScanner(request);
        } else {
            Pair<String, RegionScannerHolder> scannerNameAndRSH = newRegionScanner(request, builder);
            scannerName = scannerNameAndRSH.getFirst();
            rsh = scannerNameAndRSH.getSecond();
        }
    } catch (IOException e) {
        if (e == SCANNER_ALREADY_CLOSED) {
            // the old client will still send a close request to us. Just ignore it and return.
            return builder.build();
        }
        throw new ServiceException(e);
    }
    if (rsh.fullRegionScan) {
        rpcFullScanRequestCount.increment();
    }
    HRegion region = rsh.r;
    LeaseManager.Lease lease;
    try {
        // Remove lease while its being processed in server; protects against case
        // where processing of request takes > lease expiration time. or null if none found.
        lease = server.getLeaseManager().removeLease(scannerName);
    } catch (LeaseException e) {
        throw new ServiceException(e);
    }
    if (request.hasRenew() && request.getRenew()) {
        // add back and return
        addScannerLeaseBack(lease);
        try {
            checkScanNextCallSeq(request, rsh);
        } catch (OutOfOrderScannerNextException e) {
            throw new ServiceException(e);
        }
        return builder.build();
    }
    OperationQuota quota;
    try {
        quota = getRpcQuotaManager().checkQuota(region, OperationQuota.OperationType.SCAN);
    } catch (IOException e) {
        addScannerLeaseBack(lease);
        throw new ServiceException(e);
    }
    try {
        checkScanNextCallSeq(request, rsh);
    } catch (OutOfOrderScannerNextException e) {
        addScannerLeaseBack(lease);
        throw new ServiceException(e);
    }
    // Now we have increased the next call sequence. If we give client an error, the retry will
    // never success. So we'd better close the scanner and return a DoNotRetryIOException to client
    // and then client will try to open a new scanner.
    boolean closeScanner = request.hasCloseScanner() ? request.getCloseScanner() : false;
    // this is scan.getCaching
    int rows;
    if (request.hasNumberOfRows()) {
        rows = request.getNumberOfRows();
    } else {
        rows = closeScanner ? 0 : 1;
    }
    RpcCallContext context = RpcServer.getCurrentCall().orElse(null);
    // now let's do the real scan.
    long maxQuotaResultSize = Math.min(maxScannerResultSize, quota.getReadAvailable());
    RegionScanner scanner = rsh.s;
    // this is the limit of rows for this scan, if we the number of rows reach this value, we will
    // close the scanner.
    int limitOfRows;
    if (request.hasLimitOfRows()) {
        limitOfRows = request.getLimitOfRows();
    } else {
        limitOfRows = -1;
    }
    MutableObject<Object> lastBlock = new MutableObject<>();
    boolean scannerClosed = false;
    try {
        List<Result> results = new ArrayList<>(Math.min(rows, 512));
        if (rows > 0) {
            boolean done = false;
            // Call coprocessor. Get region info from scanner.
            if (region.getCoprocessorHost() != null) {
                Boolean bypass = region.getCoprocessorHost().preScannerNext(scanner, results, rows);
                if (!results.isEmpty()) {
                    for (Result r : results) {
                        lastBlock.setValue(addSize(context, r, lastBlock.getValue()));
                    }
                }
                if (bypass != null && bypass.booleanValue()) {
                    done = true;
                }
            }
            if (!done) {
                scan((HBaseRpcController) controller, request, rsh, maxQuotaResultSize, rows, limitOfRows, results, builder, lastBlock, context);
            } else {
                builder.setMoreResultsInRegion(!results.isEmpty());
            }
        } else {
            // This is a open scanner call with numberOfRow = 0, so set more results in region to true.
            builder.setMoreResultsInRegion(true);
        }
        quota.addScanResult(results);
        addResults(builder, results, (HBaseRpcController) controller, RegionReplicaUtil.isDefaultReplica(region.getRegionInfo()), isClientCellBlockSupport(context));
        if (scanner.isFilterDone() && results.isEmpty()) {
            // If the scanner's filter - if any - is done with the scan
            // only set moreResults to false if the results is empty. This is used to keep compatible
            // with the old scan implementation where we just ignore the returned results if moreResults
            // is false. Can remove the isEmpty check after we get rid of the old implementation.
            builder.setMoreResults(false);
        }
        // have already set this flag.
        assert builder.hasMoreResultsInRegion();
        // yet.
        if (!builder.hasMoreResults()) {
            builder.setMoreResults(true);
        }
        if (builder.getMoreResults() && builder.getMoreResultsInRegion() && !results.isEmpty()) {
            // Record the last cell of the last result if it is a partial result
            // We need this to calculate the complete rows we have returned to client as the
            // mayHaveMoreCellsInRow is true does not mean that there will be extra cells for the
            // current row. We may filter out all the remaining cells for the current row and just
            // return the cells of the nextRow when calling RegionScanner.nextRaw. So here we need to
            // check for row change.
            Result lastResult = results.get(results.size() - 1);
            if (lastResult.mayHaveMoreCellsInRow()) {
                rsh.rowOfLastPartialResult = lastResult.getRow();
            } else {
                rsh.rowOfLastPartialResult = null;
            }
        }
        if (!builder.getMoreResults() || !builder.getMoreResultsInRegion() || closeScanner) {
            scannerClosed = true;
            closeScanner(region, scanner, scannerName, context);
        }
        return builder.build();
    } catch (IOException e) {
        try {
            // scanner is closed here
            scannerClosed = true;
            // The scanner state might be left in a dirty state, so we will tell the Client to
            // fail this RPC and close the scanner while opening up another one from the start of
            // row that the client has last seen.
            closeScanner(region, scanner, scannerName, context);
            // the client. See ClientScanner code to see how it deals with these special exceptions.
            if (e instanceof DoNotRetryIOException) {
                throw e;
            }
            // DoNotRetryIOException. This can avoid the retry in ClientScanner.
            if (e instanceof FileNotFoundException) {
                throw new DoNotRetryIOException(e);
            }
            // a special exception to save an RPC.
            if (VersionInfoUtil.hasMinimumVersion(context.getClientVersionInfo(), 1, 4)) {
                // 1.4.0+ clients know how to handle
                throw new ScannerResetException("Scanner is closed on the server-side", e);
            } else {
                // older clients do not know about SRE. Just throw USE, which they will handle
                throw new UnknownScannerException("Throwing UnknownScannerException to reset the client" + " scanner state for clients older than 1.3.", e);
            }
        } catch (IOException ioe) {
            throw new ServiceException(ioe);
        }
    } finally {
        if (!scannerClosed) {
            // the closeCallBack will be set in closeScanner so here we only care about shippedCallback
            if (context != null) {
                context.setCallBack(rsh.shippedCallback);
            } else {
                // When context != null, adding back the lease will be done in callback set above.
                addScannerLeaseBack(lease);
            }
        }
        quota.close();
    }
}
Also used : DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) ScannerResetException(org.apache.hadoop.hbase.exceptions.ScannerResetException) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) ByteString(org.apache.hbase.thirdparty.com.google.protobuf.ByteString) RegionActionResult(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.RegionActionResult) Result(org.apache.hadoop.hbase.client.Result) CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OutOfOrderScannerNextException(org.apache.hadoop.hbase.exceptions.OutOfOrderScannerNextException) MutableObject(org.apache.commons.lang3.mutable.MutableObject) RpcCallContext(org.apache.hadoop.hbase.ipc.RpcCallContext) Lease(org.apache.hadoop.hbase.regionserver.LeaseManager.Lease) ScanResponse(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ScanResponse) OperationQuota(org.apache.hadoop.hbase.quotas.OperationQuota) IOException(java.io.IOException) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) HBaseIOException(org.apache.hadoop.hbase.HBaseIOException) UncheckedIOException(java.io.UncheckedIOException) UnknownScannerException(org.apache.hadoop.hbase.UnknownScannerException) HBaseRpcController(org.apache.hadoop.hbase.ipc.HBaseRpcController) ServiceException(org.apache.hbase.thirdparty.com.google.protobuf.ServiceException) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Example 3 with RpcCallContext

use of org.apache.hadoop.hbase.ipc.RpcCallContext in project hbase by apache.

the class RSRpcServices method multi.

/**
 * Execute multiple actions on a table: get, mutate, and/or execCoprocessor
 * @param rpcc the RPC controller
 * @param request the multi request
 * @throws ServiceException
 */
@Override
public MultiResponse multi(final RpcController rpcc, final MultiRequest request) throws ServiceException {
    try {
        checkOpen();
    } catch (IOException ie) {
        throw new ServiceException(ie);
    }
    checkBatchSizeAndLogLargeSize(request);
    // rpc controller is how we bring in data via the back door;  it is unprotobuf'ed data.
    // It is also the conduit via which we pass back data.
    HBaseRpcController controller = (HBaseRpcController) rpcc;
    CellScanner cellScanner = controller != null ? controller.cellScanner() : null;
    if (controller != null) {
        controller.setCellScanner(null);
    }
    long nonceGroup = request.hasNonceGroup() ? request.getNonceGroup() : HConstants.NO_NONCE;
    MultiResponse.Builder responseBuilder = MultiResponse.newBuilder();
    RegionActionResult.Builder regionActionResultBuilder = RegionActionResult.newBuilder();
    this.rpcMultiRequestCount.increment();
    this.requestCount.increment();
    ActivePolicyEnforcement spaceQuotaEnforcement = getSpaceQuotaManager().getActiveEnforcements();
    // MultiRequest#condition in case of checkAndMutate with RowMutations.
    if (request.hasCondition()) {
        if (request.getRegionActionList().isEmpty()) {
            // If the region action list is empty, do nothing.
            responseBuilder.setProcessed(true);
            return responseBuilder.build();
        }
        RegionAction regionAction = request.getRegionAction(0);
        // we can assume regionAction.getAtomic() is true here.
        assert regionAction.getAtomic();
        OperationQuota quota;
        HRegion region;
        RegionSpecifier regionSpecifier = regionAction.getRegion();
        try {
            region = getRegion(regionSpecifier);
            quota = getRpcQuotaManager().checkQuota(region, regionAction.getActionList());
        } catch (IOException e) {
            failRegionAction(responseBuilder, regionActionResultBuilder, regionAction, cellScanner, e);
            return responseBuilder.build();
        }
        try {
            boolean rejectIfFromClient = shouldRejectRequestsFromClient(region);
            // We only allow replication in standby state and it will not set the atomic flag.
            if (rejectIfFromClient) {
                failRegionAction(responseBuilder, regionActionResultBuilder, regionAction, cellScanner, new DoNotRetryIOException(region.getRegionInfo().getRegionNameAsString() + " is in STANDBY state"));
                return responseBuilder.build();
            }
            try {
                CheckAndMutateResult result = checkAndMutate(region, regionAction.getActionList(), cellScanner, request.getCondition(), nonceGroup, spaceQuotaEnforcement);
                responseBuilder.setProcessed(result.isSuccess());
                ClientProtos.ResultOrException.Builder resultOrExceptionOrBuilder = ClientProtos.ResultOrException.newBuilder();
                for (int i = 0; i < regionAction.getActionCount(); i++) {
                    // To unify the response format with doNonAtomicRegionMutation and read through
                    // client's AsyncProcess we have to add an empty result instance per operation
                    resultOrExceptionOrBuilder.clear();
                    resultOrExceptionOrBuilder.setIndex(i);
                    regionActionResultBuilder.addResultOrException(resultOrExceptionOrBuilder.build());
                }
            } catch (IOException e) {
                rpcServer.getMetrics().exception(e);
                // As it's an atomic operation with a condition, we may expect it's a global failure.
                regionActionResultBuilder.setException(ResponseConverter.buildException(e));
            }
        } finally {
            quota.close();
        }
        responseBuilder.addRegionActionResult(regionActionResultBuilder.build());
        ClientProtos.RegionLoadStats regionLoadStats = region.getLoadStatistics();
        if (regionLoadStats != null) {
            responseBuilder.setRegionStatistics(MultiRegionLoadStats.newBuilder().addRegion(regionSpecifier).addStat(regionLoadStats).build());
        }
        return responseBuilder.build();
    }
    // this will contain all the cells that we need to return. It's created later, if needed.
    List<CellScannable> cellsToReturn = null;
    RegionScannersCloseCallBack closeCallBack = null;
    RpcCallContext context = RpcServer.getCurrentCall().orElse(null);
    Map<RegionSpecifier, ClientProtos.RegionLoadStats> regionStats = new HashMap<>(request.getRegionActionCount());
    for (RegionAction regionAction : request.getRegionActionList()) {
        OperationQuota quota;
        HRegion region;
        RegionSpecifier regionSpecifier = regionAction.getRegion();
        regionActionResultBuilder.clear();
        try {
            region = getRegion(regionSpecifier);
            quota = getRpcQuotaManager().checkQuota(region, regionAction.getActionList());
        } catch (IOException e) {
            failRegionAction(responseBuilder, regionActionResultBuilder, regionAction, cellScanner, e);
            // For this region it's a failure.
            continue;
        }
        try {
            boolean rejectIfFromClient = shouldRejectRequestsFromClient(region);
            if (regionAction.hasCondition()) {
                // We only allow replication in standby state and it will not set the atomic flag.
                if (rejectIfFromClient) {
                    failRegionAction(responseBuilder, regionActionResultBuilder, regionAction, cellScanner, new DoNotRetryIOException(region.getRegionInfo().getRegionNameAsString() + " is in STANDBY state"));
                    continue;
                }
                try {
                    ClientProtos.ResultOrException.Builder resultOrExceptionOrBuilder = ClientProtos.ResultOrException.newBuilder();
                    if (regionAction.getActionCount() == 1) {
                        CheckAndMutateResult result = checkAndMutate(region, quota, regionAction.getAction(0).getMutation(), cellScanner, regionAction.getCondition(), nonceGroup, spaceQuotaEnforcement);
                        regionActionResultBuilder.setProcessed(result.isSuccess());
                        resultOrExceptionOrBuilder.setIndex(0);
                        if (result.getResult() != null) {
                            resultOrExceptionOrBuilder.setResult(ProtobufUtil.toResult(result.getResult()));
                        }
                        regionActionResultBuilder.addResultOrException(resultOrExceptionOrBuilder.build());
                    } else {
                        CheckAndMutateResult result = checkAndMutate(region, regionAction.getActionList(), cellScanner, regionAction.getCondition(), nonceGroup, spaceQuotaEnforcement);
                        regionActionResultBuilder.setProcessed(result.isSuccess());
                        for (int i = 0; i < regionAction.getActionCount(); i++) {
                            if (i == 0 && result.getResult() != null) {
                                // Set the result of the Increment/Append operations to the first element of the
                                // ResultOrException list
                                resultOrExceptionOrBuilder.setIndex(i);
                                regionActionResultBuilder.addResultOrException(resultOrExceptionOrBuilder.setResult(ProtobufUtil.toResult(result.getResult())).build());
                                continue;
                            }
                            // To unify the response format with doNonAtomicRegionMutation and read through
                            // client's AsyncProcess we have to add an empty result instance per operation
                            resultOrExceptionOrBuilder.clear();
                            resultOrExceptionOrBuilder.setIndex(i);
                            regionActionResultBuilder.addResultOrException(resultOrExceptionOrBuilder.build());
                        }
                    }
                } catch (IOException e) {
                    rpcServer.getMetrics().exception(e);
                    // As it's an atomic operation with a condition, we may expect it's a global failure.
                    regionActionResultBuilder.setException(ResponseConverter.buildException(e));
                }
            } else if (regionAction.hasAtomic() && regionAction.getAtomic()) {
                // We only allow replication in standby state and it will not set the atomic flag.
                if (rejectIfFromClient) {
                    failRegionAction(responseBuilder, regionActionResultBuilder, regionAction, cellScanner, new DoNotRetryIOException(region.getRegionInfo().getRegionNameAsString() + " is in STANDBY state"));
                    continue;
                }
                try {
                    doAtomicBatchOp(regionActionResultBuilder, region, quota, regionAction.getActionList(), cellScanner, nonceGroup, spaceQuotaEnforcement);
                    regionActionResultBuilder.setProcessed(true);
                    // We no longer use MultiResponse#processed. Instead, we use
                    // RegionActionResult#processed. This is for backward compatibility for old clients.
                    responseBuilder.setProcessed(true);
                } catch (IOException e) {
                    rpcServer.getMetrics().exception(e);
                    // As it's atomic, we may expect it's a global failure.
                    regionActionResultBuilder.setException(ResponseConverter.buildException(e));
                }
            } else {
                if (rejectIfFromClient && regionAction.getActionCount() > 0 && !isReplicationRequest(regionAction.getAction(0))) {
                    // fail if it is not a replication request
                    failRegionAction(responseBuilder, regionActionResultBuilder, regionAction, cellScanner, new DoNotRetryIOException(region.getRegionInfo().getRegionNameAsString() + " is in STANDBY state"));
                    continue;
                }
                // doNonAtomicRegionMutation manages the exception internally
                if (context != null && closeCallBack == null) {
                    // An RpcCallBack that creates a list of scanners that needs to perform callBack
                    // operation on completion of multiGets.
                    // Set this only once
                    closeCallBack = new RegionScannersCloseCallBack();
                    context.setCallBack(closeCallBack);
                }
                cellsToReturn = doNonAtomicRegionMutation(region, quota, regionAction, cellScanner, regionActionResultBuilder, cellsToReturn, nonceGroup, closeCallBack, context, spaceQuotaEnforcement);
            }
        } finally {
            quota.close();
        }
        responseBuilder.addRegionActionResult(regionActionResultBuilder.build());
        ClientProtos.RegionLoadStats regionLoadStats = region.getLoadStatistics();
        if (regionLoadStats != null) {
            regionStats.put(regionSpecifier, regionLoadStats);
        }
    }
    // Load the controller with the Cells to return.
    if (cellsToReturn != null && !cellsToReturn.isEmpty() && controller != null) {
        controller.setCellScanner(CellUtil.createCellScanner(cellsToReturn));
    }
    MultiRegionLoadStats.Builder builder = MultiRegionLoadStats.newBuilder();
    for (Entry<RegionSpecifier, ClientProtos.RegionLoadStats> stat : regionStats.entrySet()) {
        builder.addRegion(stat.getKey());
        builder.addStat(stat.getValue());
    }
    responseBuilder.setRegionStatistics(builder);
    return responseBuilder.build();
}
Also used : ActivePolicyEnforcement(org.apache.hadoop.hbase.quotas.ActivePolicyEnforcement) CellScannable(org.apache.hadoop.hbase.CellScannable) MultiResponse(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MultiResponse) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult) RegionAction(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.RegionAction) CellScanner(org.apache.hadoop.hbase.CellScanner) RegionSpecifier(org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.RegionSpecifier) MultiRegionLoadStats(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MultiRegionLoadStats) ResultOrException(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ResultOrException) RpcCallContext(org.apache.hadoop.hbase.ipc.RpcCallContext) OperationQuota(org.apache.hadoop.hbase.quotas.OperationQuota) IOException(java.io.IOException) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) HBaseIOException(org.apache.hadoop.hbase.HBaseIOException) UncheckedIOException(java.io.UncheckedIOException) RegionActionResult(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.RegionActionResult) HBaseRpcController(org.apache.hadoop.hbase.ipc.HBaseRpcController) ServiceException(org.apache.hbase.thirdparty.com.google.protobuf.ServiceException) MultiRegionLoadStats(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MultiRegionLoadStats) ClientProtos(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos)

Example 4 with RpcCallContext

use of org.apache.hadoop.hbase.ipc.RpcCallContext in project hbase by apache.

the class RSRpcServices method get.

/**
 * Get data from a table.
 *
 * @param controller the RPC controller
 * @param request the get request
 * @throws ServiceException
 */
@Override
public GetResponse get(final RpcController controller, final GetRequest request) throws ServiceException {
    long before = EnvironmentEdgeManager.currentTime();
    OperationQuota quota = null;
    HRegion region = null;
    try {
        checkOpen();
        requestCount.increment();
        rpcGetRequestCount.increment();
        region = getRegion(request.getRegion());
        rejectIfInStandByState(region);
        GetResponse.Builder builder = GetResponse.newBuilder();
        ClientProtos.Get get = request.getGet();
        // they are; its a problem for non-native clients like asynchbase. HBASE-20225.
        if (get.hasClosestRowBefore() && get.getClosestRowBefore()) {
            throw new UnknownProtocolException("Is this a pre-hbase-1.0.0 or asynchbase client? " + "Client is invoking getClosestRowBefore removed in hbase-2.0.0 replaced by " + "reverse Scan.");
        }
        Boolean existence = null;
        Result r = null;
        RpcCallContext context = RpcServer.getCurrentCall().orElse(null);
        quota = getRpcQuotaManager().checkQuota(region, OperationQuota.OperationType.GET);
        Get clientGet = ProtobufUtil.toGet(get);
        if (get.getExistenceOnly() && region.getCoprocessorHost() != null) {
            existence = region.getCoprocessorHost().preExists(clientGet);
        }
        if (existence == null) {
            if (context != null) {
                r = get(clientGet, (region), null, context);
            } else {
                // for test purpose
                r = region.get(clientGet);
            }
            if (get.getExistenceOnly()) {
                boolean exists = r.getExists();
                if (region.getCoprocessorHost() != null) {
                    exists = region.getCoprocessorHost().postExists(clientGet, exists);
                }
                existence = exists;
            }
        }
        if (existence != null) {
            ClientProtos.Result pbr = ProtobufUtil.toResult(existence, region.getRegionInfo().getReplicaId() != 0);
            builder.setResult(pbr);
        } else if (r != null) {
            ClientProtos.Result pbr;
            if (isClientCellBlockSupport(context) && controller instanceof HBaseRpcController && VersionInfoUtil.hasMinimumVersion(context.getClientVersionInfo(), 1, 3)) {
                pbr = ProtobufUtil.toResultNoData(r);
                ((HBaseRpcController) controller).setCellScanner(CellUtil.createCellScanner(r.rawCells()));
                addSize(context, r, null);
            } else {
                pbr = ProtobufUtil.toResult(r);
            }
            builder.setResult(pbr);
        }
        // r.cells is null when an table.exists(get) call
        if (r != null && r.rawCells() != null) {
            quota.addGetResult(r);
        }
        return builder.build();
    } catch (IOException ie) {
        throw new ServiceException(ie);
    } finally {
        final MetricsRegionServer metricsRegionServer = server.getMetrics();
        if (metricsRegionServer != null) {
            TableDescriptor td = region != null ? region.getTableDescriptor() : null;
            if (td != null) {
                metricsRegionServer.updateGet(td.getTableName(), EnvironmentEdgeManager.currentTime() - before);
            }
        }
        if (quota != null) {
            quota.close();
        }
    }
}
Also used : RpcCallContext(org.apache.hadoop.hbase.ipc.RpcCallContext) OperationQuota(org.apache.hadoop.hbase.quotas.OperationQuota) IOException(java.io.IOException) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) HBaseIOException(org.apache.hadoop.hbase.HBaseIOException) UncheckedIOException(java.io.UncheckedIOException) GetResponse(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.GetResponse) TableDescriptor(org.apache.hadoop.hbase.client.TableDescriptor) RegionActionResult(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.RegionActionResult) Result(org.apache.hadoop.hbase.client.Result) CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult) HBaseRpcController(org.apache.hadoop.hbase.ipc.HBaseRpcController) ServiceException(org.apache.hbase.thirdparty.com.google.protobuf.ServiceException) Get(org.apache.hadoop.hbase.client.Get) UnknownProtocolException(org.apache.hadoop.hbase.exceptions.UnknownProtocolException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClientProtos(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos)

Example 5 with RpcCallContext

use of org.apache.hadoop.hbase.ipc.RpcCallContext in project hbase by apache.

the class RSRpcServices method mutate.

/**
 * Mutate data in a table.
 *
 * @param rpcc the RPC controller
 * @param request the mutate request
 */
@Override
public MutateResponse mutate(final RpcController rpcc, final MutateRequest request) throws ServiceException {
    // rpc controller is how we bring in data via the back door;  it is unprotobuf'ed data.
    // It is also the conduit via which we pass back data.
    HBaseRpcController controller = (HBaseRpcController) rpcc;
    CellScanner cellScanner = controller != null ? controller.cellScanner() : null;
    OperationQuota quota = null;
    RpcCallContext context = RpcServer.getCurrentCall().orElse(null);
    // Clear scanner so we are not holding on to reference across call.
    if (controller != null) {
        controller.setCellScanner(null);
    }
    try {
        checkOpen();
        requestCount.increment();
        rpcMutateRequestCount.increment();
        HRegion region = getRegion(request.getRegion());
        rejectIfInStandByState(region);
        MutateResponse.Builder builder = MutateResponse.newBuilder();
        MutationProto mutation = request.getMutation();
        if (!region.getRegionInfo().isMetaRegion()) {
            server.getMemStoreFlusher().reclaimMemStoreMemory();
        }
        long nonceGroup = request.hasNonceGroup() ? request.getNonceGroup() : HConstants.NO_NONCE;
        quota = getRpcQuotaManager().checkQuota(region, OperationQuota.OperationType.MUTATE);
        ActivePolicyEnforcement spaceQuotaEnforcement = getSpaceQuotaManager().getActiveEnforcements();
        if (request.hasCondition()) {
            CheckAndMutateResult result = checkAndMutate(region, quota, mutation, cellScanner, request.getCondition(), nonceGroup, spaceQuotaEnforcement);
            builder.setProcessed(result.isSuccess());
            boolean clientCellBlockSupported = isClientCellBlockSupport(context);
            addResult(builder, result.getResult(), controller, clientCellBlockSupported);
            if (clientCellBlockSupported) {
                addSize(context, result.getResult(), null);
            }
        } else {
            Result r = null;
            Boolean processed = null;
            MutationType type = mutation.getMutateType();
            switch(type) {
                case APPEND:
                    // TODO: this doesn't actually check anything.
                    r = append(region, quota, mutation, cellScanner, nonceGroup, spaceQuotaEnforcement);
                    break;
                case INCREMENT:
                    // TODO: this doesn't actually check anything.
                    r = increment(region, quota, mutation, cellScanner, nonceGroup, spaceQuotaEnforcement);
                    break;
                case PUT:
                    put(region, quota, mutation, cellScanner, spaceQuotaEnforcement);
                    processed = Boolean.TRUE;
                    break;
                case DELETE:
                    delete(region, quota, mutation, cellScanner, spaceQuotaEnforcement);
                    processed = Boolean.TRUE;
                    break;
                default:
                    throw new DoNotRetryIOException("Unsupported mutate type: " + type.name());
            }
            if (processed != null) {
                builder.setProcessed(processed);
            }
            boolean clientCellBlockSupported = isClientCellBlockSupport(context);
            addResult(builder, r, controller, clientCellBlockSupported);
            if (clientCellBlockSupported) {
                addSize(context, r, null);
            }
        }
        return builder.build();
    } catch (IOException ie) {
        server.checkFileSystem();
        throw new ServiceException(ie);
    } finally {
        if (quota != null) {
            quota.close();
        }
    }
}
Also used : RpcCallContext(org.apache.hadoop.hbase.ipc.RpcCallContext) ActivePolicyEnforcement(org.apache.hadoop.hbase.quotas.ActivePolicyEnforcement) MutationType(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto.MutationType) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult) OperationQuota(org.apache.hadoop.hbase.quotas.OperationQuota) IOException(java.io.IOException) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) HBaseIOException(org.apache.hadoop.hbase.HBaseIOException) UncheckedIOException(java.io.UncheckedIOException) CellScanner(org.apache.hadoop.hbase.CellScanner) MutationProto(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto) RegionActionResult(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.RegionActionResult) Result(org.apache.hadoop.hbase.client.Result) CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult) MutateResponse(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutateResponse) HBaseRpcController(org.apache.hadoop.hbase.ipc.HBaseRpcController) ServiceException(org.apache.hbase.thirdparty.com.google.protobuf.ServiceException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Aggregations

RpcCallContext (org.apache.hadoop.hbase.ipc.RpcCallContext)5 IOException (java.io.IOException)4 UncheckedIOException (java.io.UncheckedIOException)4 DoNotRetryIOException (org.apache.hadoop.hbase.DoNotRetryIOException)4 HBaseIOException (org.apache.hadoop.hbase.HBaseIOException)4 CheckAndMutateResult (org.apache.hadoop.hbase.client.CheckAndMutateResult)4 HBaseRpcController (org.apache.hadoop.hbase.ipc.HBaseRpcController)4 OperationQuota (org.apache.hadoop.hbase.quotas.OperationQuota)4 RegionActionResult (org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.RegionActionResult)4 ServiceException (org.apache.hbase.thirdparty.com.google.protobuf.ServiceException)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Result (org.apache.hadoop.hbase.client.Result)3 CellScanner (org.apache.hadoop.hbase.CellScanner)2 ActivePolicyEnforcement (org.apache.hadoop.hbase.quotas.ActivePolicyEnforcement)2 ClientProtos (org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos)2 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 MutableObject (org.apache.commons.lang3.mutable.MutableObject)1