Search in sources :

Example 6 with ResultOrException

use of org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ResultOrException in project hbase by apache.

the class RSRpcServices method doNonAtomicRegionMutation.

/**
   * Run through the regionMutation <code>rm</code> and per Mutation, do the work, and then when
   * done, add an instance of a {@link ResultOrException} that corresponds to each Mutation.
   * @param region
   * @param actions
   * @param cellScanner
   * @param builder
   * @param cellsToReturn  Could be null. May be allocated in this method.  This is what this
   * method returns as a 'result'.
   * @param closeCallBack the callback to be used with multigets
   * @param context the current RpcCallContext
   * @return Return the <code>cellScanner</code> passed
   */
private List<CellScannable> doNonAtomicRegionMutation(final Region region, final OperationQuota quota, final RegionAction actions, final CellScanner cellScanner, final RegionActionResult.Builder builder, List<CellScannable> cellsToReturn, long nonceGroup, final RegionScannersCloseCallBack closeCallBack, RpcCallContext context) {
    // Gather up CONTIGUOUS Puts and Deletes in this mutations List.  Idea is that rather than do
    // one at a time, we instead pass them in batch.  Be aware that the corresponding
    // ResultOrException instance that matches each Put or Delete is then added down in the
    // doBatchOp call.  We should be staying aligned though the Put and Delete are deferred/batched
    List<ClientProtos.Action> mutations = null;
    long maxQuotaResultSize = Math.min(maxScannerResultSize, quota.getReadAvailable());
    IOException sizeIOE = null;
    Object lastBlock = null;
    ClientProtos.ResultOrException.Builder resultOrExceptionBuilder = ResultOrException.newBuilder();
    boolean hasResultOrException = false;
    for (ClientProtos.Action action : actions.getActionList()) {
        hasResultOrException = false;
        resultOrExceptionBuilder.clear();
        try {
            Result r = null;
            if (context != null && context.isRetryImmediatelySupported() && (context.getResponseCellSize() > maxQuotaResultSize || context.getResponseBlockSize() + context.getResponseExceptionSize() > maxQuotaResultSize)) {
                // change after the response size limit is reached.
                if (sizeIOE == null) {
                    // We don't need the stack un-winding do don't throw the exception.
                    // Throwing will kill the JVM's JIT.
                    //
                    // Instead just create the exception and then store it.
                    sizeIOE = new MultiActionResultTooLarge("Max size exceeded" + " CellSize: " + context.getResponseCellSize() + " BlockSize: " + context.getResponseBlockSize());
                    // Only report the exception once since there's only one request that
                    // caused the exception. Otherwise this number will dominate the exceptions count.
                    rpcServer.getMetrics().exception(sizeIOE);
                }
                // Now that there's an exception is known to be created
                // use it for the response.
                //
                // This will create a copy in the builder.
                hasResultOrException = true;
                NameBytesPair pair = ResponseConverter.buildException(sizeIOE);
                resultOrExceptionBuilder.setException(pair);
                context.incrementResponseExceptionSize(pair.getSerializedSize());
                resultOrExceptionBuilder.setIndex(action.getIndex());
                builder.addResultOrException(resultOrExceptionBuilder.build());
                if (cellScanner != null) {
                    skipCellsForMutation(action, cellScanner);
                }
                continue;
            }
            if (action.hasGet()) {
                long before = EnvironmentEdgeManager.currentTime();
                try {
                    Get get = ProtobufUtil.toGet(action.getGet());
                    if (context != null) {
                        r = get(get, ((HRegion) region), closeCallBack, context);
                    } else {
                        r = region.get(get);
                    }
                } finally {
                    if (regionServer.metricsRegionServer != null) {
                        regionServer.metricsRegionServer.updateGet(EnvironmentEdgeManager.currentTime() - before);
                    }
                }
            } else if (action.hasServiceCall()) {
                hasResultOrException = true;
                try {
                    com.google.protobuf.Message result = execServiceOnRegion(region, action.getServiceCall());
                    ClientProtos.CoprocessorServiceResult.Builder serviceResultBuilder = ClientProtos.CoprocessorServiceResult.newBuilder();
                    resultOrExceptionBuilder.setServiceResult(serviceResultBuilder.setValue(serviceResultBuilder.getValueBuilder().setName(result.getClass().getName()).setValue(UnsafeByteOperations.unsafeWrap(result.toByteArray()))));
                } catch (IOException ioe) {
                    rpcServer.getMetrics().exception(ioe);
                    NameBytesPair pair = ResponseConverter.buildException(ioe);
                    resultOrExceptionBuilder.setException(pair);
                    context.incrementResponseExceptionSize(pair.getSerializedSize());
                }
            } else if (action.hasMutation()) {
                MutationType type = action.getMutation().getMutateType();
                if (type != MutationType.PUT && type != MutationType.DELETE && mutations != null && !mutations.isEmpty()) {
                    // Flush out any Puts or Deletes already collected.
                    doBatchOp(builder, region, quota, mutations, cellScanner);
                    mutations.clear();
                }
                switch(type) {
                    case APPEND:
                        r = append(region, quota, action.getMutation(), cellScanner, nonceGroup);
                        break;
                    case INCREMENT:
                        r = increment(region, quota, action.getMutation(), cellScanner, nonceGroup);
                        break;
                    case PUT:
                    case DELETE:
                        // Collect the individual mutations and apply in a batch
                        if (mutations == null) {
                            mutations = new ArrayList<>(actions.getActionCount());
                        }
                        mutations.add(action);
                        break;
                    default:
                        throw new DoNotRetryIOException("Unsupported mutate type: " + type.name());
                }
            } else {
                throw new HBaseIOException("Unexpected Action type");
            }
            if (r != null) {
                ClientProtos.Result pbResult = null;
                if (isClientCellBlockSupport(context)) {
                    pbResult = ProtobufUtil.toResultNoData(r);
                    //  Hard to guess the size here.  Just make a rough guess.
                    if (cellsToReturn == null) {
                        cellsToReturn = new ArrayList<>();
                    }
                    cellsToReturn.add(r);
                } else {
                    pbResult = ProtobufUtil.toResult(r);
                }
                lastBlock = addSize(context, r, lastBlock);
                hasResultOrException = true;
                resultOrExceptionBuilder.setResult(pbResult);
            }
        // Could get to here and there was no result and no exception.  Presumes we added
        // a Put or Delete to the collecting Mutations List for adding later.  In this
        // case the corresponding ResultOrException instance for the Put or Delete will be added
        // down in the doBatchOp method call rather than up here.
        } catch (IOException ie) {
            rpcServer.getMetrics().exception(ie);
            hasResultOrException = true;
            NameBytesPair pair = ResponseConverter.buildException(ie);
            resultOrExceptionBuilder.setException(pair);
            context.incrementResponseExceptionSize(pair.getSerializedSize());
        }
        if (hasResultOrException) {
            // Propagate index.
            resultOrExceptionBuilder.setIndex(action.getIndex());
            builder.addResultOrException(resultOrExceptionBuilder.build());
        }
    }
    // Finish up any outstanding mutations
    if (mutations != null && !mutations.isEmpty()) {
        doBatchOp(builder, region, quota, mutations, cellScanner);
    }
    return cellsToReturn;
}
Also used : MultiActionResultTooLarge(org.apache.hadoop.hbase.MultiActionResultTooLarge) RegionAction(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.RegionAction) Action(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.Action) MutationType(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto.MutationType) Message(org.apache.hadoop.hbase.shaded.com.google.protobuf.Message) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) HBaseIOException(org.apache.hadoop.hbase.HBaseIOException) Action(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.Action) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) HBaseIOException(org.apache.hadoop.hbase.HBaseIOException) RegionActionResult(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.RegionActionResult) Result(org.apache.hadoop.hbase.client.Result) NameBytesPair(org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.NameBytesPair) Get(org.apache.hadoop.hbase.client.Get) MutableObject(org.apache.commons.lang.mutable.MutableObject) ResultOrException(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ResultOrException) ClientProtos(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos)

Example 7 with ResultOrException

use of org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ResultOrException in project hbase by apache.

the class RSRpcServices method doBatchOp.

/**
 * Execute a list of mutations.
 *
 * @param builder
 * @param region
 * @param mutations
 */
private void doBatchOp(final RegionActionResult.Builder builder, final HRegion region, final OperationQuota quota, final List<ClientProtos.Action> mutations, final CellScanner cells, long nonceGroup, ActivePolicyEnforcement spaceQuotaEnforcement, boolean atomic) throws IOException {
    Mutation[] mArray = new Mutation[mutations.size()];
    long before = EnvironmentEdgeManager.currentTime();
    boolean batchContainsPuts = false, batchContainsDelete = false;
    try {
        /**
         * HBASE-17924
         * mutationActionMap is a map to map the relation between mutations and actions
         * since mutation array may have been reoredered.In order to return the right
         * result or exception to the corresponding actions, We need to know which action
         * is the mutation belong to. We can't sort ClientProtos.Action array, since they
         * are bonded to cellscanners.
         */
        Map<Mutation, ClientProtos.Action> mutationActionMap = new HashMap<>();
        int i = 0;
        long nonce = HConstants.NO_NONCE;
        for (ClientProtos.Action action : mutations) {
            if (action.hasGet()) {
                throw new DoNotRetryIOException("Atomic put and/or delete only, not a Get=" + action.getGet());
            }
            MutationProto m = action.getMutation();
            Mutation mutation;
            switch(m.getMutateType()) {
                case PUT:
                    mutation = ProtobufUtil.toPut(m, cells);
                    batchContainsPuts = true;
                    break;
                case DELETE:
                    mutation = ProtobufUtil.toDelete(m, cells);
                    batchContainsDelete = true;
                    break;
                case INCREMENT:
                    mutation = ProtobufUtil.toIncrement(m, cells);
                    nonce = m.hasNonce() ? m.getNonce() : HConstants.NO_NONCE;
                    break;
                case APPEND:
                    mutation = ProtobufUtil.toAppend(m, cells);
                    nonce = m.hasNonce() ? m.getNonce() : HConstants.NO_NONCE;
                    break;
                default:
                    throw new DoNotRetryIOException("Invalid mutation type : " + m.getMutateType());
            }
            mutationActionMap.put(mutation, action);
            mArray[i++] = mutation;
            checkCellSizeLimit(region, mutation);
            // Check if a space quota disallows this mutation
            spaceQuotaEnforcement.getPolicyEnforcement(region).check(mutation);
            quota.addMutation(mutation);
        }
        if (!region.getRegionInfo().isMetaRegion()) {
            server.getMemStoreFlusher().reclaimMemStoreMemory();
        }
        // order is preserved as its expected from the client
        if (!atomic) {
            Arrays.sort(mArray, (v1, v2) -> Row.COMPARATOR.compare(v1, v2));
        }
        OperationStatus[] codes = region.batchMutate(mArray, atomic, nonceGroup, nonce);
        // result to the first element of the ResultOrException list
        if (atomic) {
            List<ResultOrException> resultOrExceptions = new ArrayList<>();
            List<Result> results = new ArrayList<>();
            for (i = 0; i < codes.length; i++) {
                if (codes[i].getResult() != null) {
                    results.add(codes[i].getResult());
                }
                if (i != 0) {
                    resultOrExceptions.add(getResultOrException(ClientProtos.Result.getDefaultInstance(), i));
                }
            }
            if (results.isEmpty()) {
                builder.addResultOrException(getResultOrException(ClientProtos.Result.getDefaultInstance(), 0));
            } else {
                // Merge the results of the Increment/Append operations
                List<Cell> cellList = new ArrayList<>();
                for (Result result : results) {
                    if (result.rawCells() != null) {
                        cellList.addAll(Arrays.asList(result.rawCells()));
                    }
                }
                Result result = Result.create(cellList);
                // Set the merged result of the Increment/Append operations to the first element of the
                // ResultOrException list
                builder.addResultOrException(getResultOrException(ProtobufUtil.toResult(result), 0));
            }
            builder.addAllResultOrException(resultOrExceptions);
            return;
        }
        for (i = 0; i < codes.length; i++) {
            Mutation currentMutation = mArray[i];
            ClientProtos.Action currentAction = mutationActionMap.get(currentMutation);
            int index = currentAction.hasIndex() ? currentAction.getIndex() : i;
            Exception e;
            switch(codes[i].getOperationStatusCode()) {
                case BAD_FAMILY:
                    e = new NoSuchColumnFamilyException(codes[i].getExceptionMsg());
                    builder.addResultOrException(getResultOrException(e, index));
                    break;
                case SANITY_CHECK_FAILURE:
                    e = new FailedSanityCheckException(codes[i].getExceptionMsg());
                    builder.addResultOrException(getResultOrException(e, index));
                    break;
                default:
                    e = new DoNotRetryIOException(codes[i].getExceptionMsg());
                    builder.addResultOrException(getResultOrException(e, index));
                    break;
                case SUCCESS:
                    builder.addResultOrException(getResultOrException(ClientProtos.Result.getDefaultInstance(), index));
                    break;
                case STORE_TOO_BUSY:
                    e = new RegionTooBusyException(codes[i].getExceptionMsg());
                    builder.addResultOrException(getResultOrException(e, index));
                    break;
            }
        }
    } finally {
        int processedMutationIndex = 0;
        for (Action mutation : mutations) {
            // The non-null mArray[i] means the cell scanner has been read.
            if (mArray[processedMutationIndex++] == null) {
                skipCellsForMutation(mutation, cells);
            }
        }
        updateMutationMetrics(region, before, batchContainsPuts, batchContainsDelete);
    }
}
Also used : RegionAction(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.RegionAction) Action(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.Action) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) ArrayList(java.util.ArrayList) Action(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.Action) 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) RegionTooBusyException(org.apache.hadoop.hbase.RegionTooBusyException) ResultOrException(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ResultOrException) Cell(org.apache.hadoop.hbase.Cell) ByteBufferExtendedCell(org.apache.hadoop.hbase.ByteBufferExtendedCell) FailedSanityCheckException(org.apache.hadoop.hbase.exceptions.FailedSanityCheckException) FailedSanityCheckException(org.apache.hadoop.hbase.exceptions.FailedSanityCheckException) ResultOrException(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ResultOrException) ScannerResetException(org.apache.hadoop.hbase.exceptions.ScannerResetException) OutOfOrderScannerNextException(org.apache.hadoop.hbase.exceptions.OutOfOrderScannerNextException) RegionTooBusyException(org.apache.hadoop.hbase.RegionTooBusyException) IOException(java.io.IOException) LeaseStillHeldException(org.apache.hadoop.hbase.regionserver.LeaseManager.LeaseStillHeldException) NotServingRegionException(org.apache.hadoop.hbase.NotServingRegionException) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) HBaseIOException(org.apache.hadoop.hbase.HBaseIOException) ServerNotRunningYetException(org.apache.hadoop.hbase.ipc.ServerNotRunningYetException) UnknownProtocolException(org.apache.hadoop.hbase.exceptions.UnknownProtocolException) ServiceException(org.apache.hbase.thirdparty.com.google.protobuf.ServiceException) UncheckedIOException(java.io.UncheckedIOException) UnknownScannerException(org.apache.hadoop.hbase.UnknownScannerException) FileNotFoundException(java.io.FileNotFoundException) BindException(java.net.BindException) DroppedSnapshotException(org.apache.hadoop.hbase.DroppedSnapshotException) Mutation(org.apache.hadoop.hbase.client.Mutation) ClientProtos(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos)

Aggregations

RegionActionResult (org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.RegionActionResult)7 ResultOrException (org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ResultOrException)7 ClientProtos (org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos)6 RegionAction (org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.RegionAction)6 IOException (java.io.IOException)4 DoNotRetryIOException (org.apache.hadoop.hbase.DoNotRetryIOException)4 HBaseIOException (org.apache.hadoop.hbase.HBaseIOException)4 CheckAndMutateResult (org.apache.hadoop.hbase.client.CheckAndMutateResult)4 Result (org.apache.hadoop.hbase.client.Result)4 UncheckedIOException (java.io.UncheckedIOException)3 Action (org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.Action)3 MultiResponse (org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MultiResponse)3 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 MultiActionResultTooLarge (org.apache.hadoop.hbase.MultiActionResultTooLarge)2 Get (org.apache.hadoop.hbase.client.Get)2 UnknownProtocolException (org.apache.hadoop.hbase.exceptions.UnknownProtocolException)2 MutationType (org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto.MutationType)2 HBaseProtos (org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos)2 NameBytesPair (org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.NameBytesPair)2