Search in sources :

Example 6 with RpcCallback

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

the class TestAsyncTableTracing method setUp.

@Before
public void setUp() throws IOException {
    stub = mock(ClientService.Interface.class);
    AtomicInteger scanNextCalled = new AtomicInteger(0);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ScanRequest req = invocation.getArgument(1);
            RpcCallback<ScanResponse> done = invocation.getArgument(2);
            if (!req.hasScannerId()) {
                done.run(ScanResponse.newBuilder().setScannerId(1).setTtl(800).setMoreResultsInRegion(true).setMoreResults(true).build());
            } else {
                if (req.hasCloseScanner() && req.getCloseScanner()) {
                    done.run(ScanResponse.getDefaultInstance());
                } else {
                    Cell cell = CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setType(Type.Put).setRow(Bytes.toBytes(scanNextCalled.incrementAndGet())).setFamily(Bytes.toBytes("cf")).setQualifier(Bytes.toBytes("cq")).setValue(Bytes.toBytes("v")).build();
                    Result result = Result.create(Arrays.asList(cell));
                    ScanResponse.Builder builder = ScanResponse.newBuilder().setScannerId(1).setTtl(800).addResults(ProtobufUtil.toResult(result));
                    if (req.getLimitOfRows() == 1) {
                        builder.setMoreResultsInRegion(false).setMoreResults(false);
                    } else {
                        builder.setMoreResultsInRegion(true).setMoreResults(true);
                    }
                    ForkJoinPool.commonPool().execute(() -> done.run(builder.build()));
                }
            }
            return null;
        }
    }).when(stub).scan(any(HBaseRpcController.class), any(ScanRequest.class), any());
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ClientProtos.MultiRequest req = invocation.getArgument(1);
            ClientProtos.MultiResponse.Builder builder = ClientProtos.MultiResponse.newBuilder();
            for (ClientProtos.RegionAction regionAction : req.getRegionActionList()) {
                RegionActionResult.Builder raBuilder = RegionActionResult.newBuilder();
                for (ClientProtos.Action ignored : regionAction.getActionList()) {
                    raBuilder.addResultOrException(ResultOrException.newBuilder().setResult(ProtobufUtil.toResult(new Result())));
                }
                builder.addRegionActionResult(raBuilder);
            }
            ClientProtos.MultiResponse resp = builder.build();
            RpcCallback<ClientProtos.MultiResponse> done = invocation.getArgument(2);
            ForkJoinPool.commonPool().execute(() -> done.run(resp));
            return null;
        }
    }).when(stub).multi(any(HBaseRpcController.class), any(ClientProtos.MultiRequest.class), any());
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            MutationProto req = ((MutateRequest) invocation.getArgument(1)).getMutation();
            MutateResponse resp;
            switch(req.getMutateType()) {
                case INCREMENT:
                    ColumnValue value = req.getColumnValue(0);
                    QualifierValue qvalue = value.getQualifierValue(0);
                    Cell cell = CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setType(Type.Put).setRow(req.getRow().toByteArray()).setFamily(value.getFamily().toByteArray()).setQualifier(qvalue.getQualifier().toByteArray()).setValue(qvalue.getValue().toByteArray()).build();
                    resp = MutateResponse.newBuilder().setResult(ProtobufUtil.toResult(Result.create(Arrays.asList(cell)))).build();
                    break;
                default:
                    resp = MutateResponse.getDefaultInstance();
                    break;
            }
            RpcCallback<MutateResponse> done = invocation.getArgument(2);
            ForkJoinPool.commonPool().execute(() -> done.run(resp));
            return null;
        }
    }).when(stub).mutate(any(HBaseRpcController.class), any(MutateRequest.class), any());
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            RpcCallback<GetResponse> done = invocation.getArgument(2);
            ForkJoinPool.commonPool().execute(() -> done.run(GetResponse.getDefaultInstance()));
            return null;
        }
    }).when(stub).get(any(HBaseRpcController.class), any(GetRequest.class), any());
    final User user = UserProvider.instantiate(CONF).getCurrent();
    conn = new AsyncConnectionImpl(CONF, new DoNothingConnectionRegistry(CONF), "test", null, user) {

        @Override
        AsyncRegionLocator getLocator() {
            AsyncRegionLocator locator = mock(AsyncRegionLocator.class);
            Answer<CompletableFuture<HRegionLocation>> answer = new Answer<CompletableFuture<HRegionLocation>>() {

                @Override
                public CompletableFuture<HRegionLocation> answer(InvocationOnMock invocation) throws Throwable {
                    TableName tableName = invocation.getArgument(0);
                    RegionInfo info = RegionInfoBuilder.newBuilder(tableName).build();
                    ServerName serverName = ServerName.valueOf("rs", 16010, 12345);
                    HRegionLocation loc = new HRegionLocation(info, serverName);
                    return CompletableFuture.completedFuture(loc);
                }
            };
            doAnswer(answer).when(locator).getRegionLocation(any(TableName.class), any(byte[].class), any(RegionLocateType.class), anyLong());
            doAnswer(answer).when(locator).getRegionLocation(any(TableName.class), any(byte[].class), anyInt(), any(RegionLocateType.class), anyLong());
            return locator;
        }

        @Override
        ClientService.Interface getRegionServerStub(ServerName serverName) throws IOException {
            return stub;
        }
    };
    table = conn.getTable(TableName.valueOf("table"), ForkJoinPool.commonPool());
}
Also used : User(org.apache.hadoop.hbase.security.User) MutationProto(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto) RegionActionResult(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.RegionActionResult) MutateResponse(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutateResponse) CompletableFuture(java.util.concurrent.CompletableFuture) HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) GetRequest(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.GetRequest) RpcCallback(org.apache.hbase.thirdparty.com.google.protobuf.RpcCallback) Cell(org.apache.hadoop.hbase.Cell) QualifierValue(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto.ColumnValue.QualifierValue) MutateRequest(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutateRequest) IOException(java.io.IOException) ScanRequest(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ScanRequest) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) TableName(org.apache.hadoop.hbase.TableName) HBaseRpcController(org.apache.hadoop.hbase.ipc.HBaseRpcController) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ServerName(org.apache.hadoop.hbase.ServerName) ColumnValue(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto.ColumnValue) ClientProtos(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos) Before(org.junit.Before)

Example 7 with RpcCallback

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

the class AggregationClient method max.

/**
 * It gives the maximum value of a column for a given column family for the
 * given range. In case qualifier is null, a max of all values for the given
 * family is returned.
 * @param table table to scan.
 * @param ci the user's ColumnInterpreter implementation
 * @param scan the HBase scan object to use to read data from HBase
 * @return max val &lt;&gt;
 * @throws Throwable The caller is supposed to handle the exception as they are thrown
 *           &amp; propagated to it.
 */
public <R, S, P extends Message, Q extends Message, T extends Message> R max(final Table table, final ColumnInterpreter<R, S, P, Q, T> ci, final Scan scan) throws Throwable {
    final AggregateRequest requestArg = validateArgAndGetPB(scan, ci, false);
    class MaxCallBack implements Batch.Callback<R> {

        R max = null;

        R getMax() {
            return max;
        }

        @Override
        public synchronized void update(byte[] region, byte[] row, R result) {
            max = (max == null || (result != null && ci.compare(max, result) < 0)) ? result : max;
        }
    }
    MaxCallBack aMaxCallBack = new MaxCallBack();
    table.coprocessorService(AggregateService.class, scan.getStartRow(), scan.getStopRow(), new Batch.Call<AggregateService, R>() {

        @Override
        public R call(AggregateService instance) throws IOException {
            RpcController controller = new AggregationClientRpcController();
            CoprocessorRpcUtils.BlockingRpcCallback<AggregateResponse> rpcCallback = new CoprocessorRpcUtils.BlockingRpcCallback<>();
            instance.getMax(controller, requestArg, rpcCallback);
            AggregateResponse response = rpcCallback.get();
            if (controller.failed()) {
                throw new IOException(controller.errorText());
            }
            if (response.getFirstPartCount() > 0) {
                ByteString b = response.getFirstPart(0);
                Q q = getParsedGenericInstance(ci.getClass(), 3, b);
                return ci.getCellValueFromProto(q);
            }
            return null;
        }
    }, aMaxCallBack);
    return aMaxCallBack.getMax();
}
Also used : AggregateRequest(org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateRequest) ByteString(org.apache.hbase.thirdparty.com.google.protobuf.ByteString) AggregateResponse(org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateResponse) IOException(java.io.IOException) RpcController(org.apache.hbase.thirdparty.com.google.protobuf.RpcController) RpcCallback(org.apache.hbase.thirdparty.com.google.protobuf.RpcCallback) CoprocessorRpcUtils(org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils) AggregateService(org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateService)

Example 8 with RpcCallback

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

the class AggregationClient method getMedianArgs.

/**
 * It helps locate the region with median for a given column whose weight
 * is specified in an optional column.
 * From individual regions, it obtains sum of values and sum of weights.
 * @param table table to scan.
 * @param ci the user's ColumnInterpreter implementation
 * @param scan the HBase scan object to use to read data from HBase
 * @return pair whose first element is a map between start row of the region
 *   and (sum of values, sum of weights) for the region, the second element is
 *   (sum of values, sum of weights) for all the regions chosen
 * @throws Throwable The caller is supposed to handle the exception as they are thrown
 *           &amp; propagated to it.
 */
private <R, S, P extends Message, Q extends Message, T extends Message> Pair<NavigableMap<byte[], List<S>>, List<S>> getMedianArgs(final Table table, final ColumnInterpreter<R, S, P, Q, T> ci, final Scan scan) throws Throwable {
    final AggregateRequest requestArg = validateArgAndGetPB(scan, ci, false);
    final NavigableMap<byte[], List<S>> map = new TreeMap<>(Bytes.BYTES_COMPARATOR);
    class StdCallback implements Batch.Callback<List<S>> {

        S sumVal = null, sumWeights = null;

        public synchronized Pair<NavigableMap<byte[], List<S>>, List<S>> getMedianParams() {
            List<S> l = new ArrayList<>(2);
            l.add(sumVal);
            l.add(sumWeights);
            Pair<NavigableMap<byte[], List<S>>, List<S>> p = new Pair<>(map, l);
            return p;
        }

        @Override
        public synchronized void update(byte[] region, byte[] row, List<S> result) {
            map.put(row, result);
            sumVal = ci.add(sumVal, result.get(0));
            sumWeights = ci.add(sumWeights, result.get(1));
        }
    }
    StdCallback stdCallback = new StdCallback();
    table.coprocessorService(AggregateService.class, scan.getStartRow(), scan.getStopRow(), new Batch.Call<AggregateService, List<S>>() {

        @Override
        public List<S> call(AggregateService instance) throws IOException {
            RpcController controller = new AggregationClientRpcController();
            CoprocessorRpcUtils.BlockingRpcCallback<AggregateResponse> rpcCallback = new CoprocessorRpcUtils.BlockingRpcCallback<>();
            instance.getMedian(controller, requestArg, rpcCallback);
            AggregateResponse response = rpcCallback.get();
            if (controller.failed()) {
                throw new IOException(controller.errorText());
            }
            List<S> list = new ArrayList<>();
            for (int i = 0; i < response.getFirstPartCount(); i++) {
                ByteString b = response.getFirstPart(i);
                T t = getParsedGenericInstance(ci.getClass(), 4, b);
                S s = ci.getPromotedValueFromProto(t);
                list.add(s);
            }
            return list;
        }
    }, stdCallback);
    return stdCallback.getMedianParams();
}
Also used : AggregateRequest(org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateRequest) NavigableMap(java.util.NavigableMap) ByteString(org.apache.hbase.thirdparty.com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) AggregateResponse(org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateResponse) ArrayList(java.util.ArrayList) List(java.util.List) Pair(org.apache.hadoop.hbase.util.Pair) IOException(java.io.IOException) TreeMap(java.util.TreeMap) RpcController(org.apache.hbase.thirdparty.com.google.protobuf.RpcController) RpcCallback(org.apache.hbase.thirdparty.com.google.protobuf.RpcCallback) CoprocessorRpcUtils(org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils) AggregateService(org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateService)

Example 9 with RpcCallback

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

the class TestAsyncTableRpcPriority method setUp.

@Before
public void setUp() throws IOException {
    stub = mock(ClientService.Interface.class);
    AtomicInteger scanNextCalled = new AtomicInteger(0);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ScanRequest req = invocation.getArgument(1);
            RpcCallback<ScanResponse> done = invocation.getArgument(2);
            if (!req.hasScannerId()) {
                done.run(ScanResponse.newBuilder().setScannerId(1).setTtl(800).setMoreResultsInRegion(true).setMoreResults(true).build());
            } else {
                if (req.hasCloseScanner() && req.getCloseScanner()) {
                    done.run(ScanResponse.getDefaultInstance());
                } else {
                    Cell cell = CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setType(Type.Put).setRow(Bytes.toBytes(scanNextCalled.incrementAndGet())).setFamily(Bytes.toBytes("cf")).setQualifier(Bytes.toBytes("cq")).setValue(Bytes.toBytes("v")).build();
                    Result result = Result.create(Arrays.asList(cell));
                    done.run(ScanResponse.newBuilder().setScannerId(1).setTtl(800).setMoreResultsInRegion(true).setMoreResults(true).addResults(ProtobufUtil.toResult(result)).build());
                }
            }
            return null;
        }
    }).when(stub).scan(any(HBaseRpcController.class), any(ScanRequest.class), any());
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ClientProtos.MultiResponse resp = ClientProtos.MultiResponse.newBuilder().addRegionActionResult(RegionActionResult.newBuilder().addResultOrException(ResultOrException.newBuilder().setResult(ProtobufUtil.toResult(new Result())))).build();
            RpcCallback<ClientProtos.MultiResponse> done = invocation.getArgument(2);
            done.run(resp);
            return null;
        }
    }).when(stub).multi(any(HBaseRpcController.class), any(ClientProtos.MultiRequest.class), any());
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            MutationProto req = ((MutateRequest) invocation.getArgument(1)).getMutation();
            MutateResponse resp;
            switch(req.getMutateType()) {
                case INCREMENT:
                    ColumnValue value = req.getColumnValue(0);
                    QualifierValue qvalue = value.getQualifierValue(0);
                    Cell cell = CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setType(Type.Put).setRow(req.getRow().toByteArray()).setFamily(value.getFamily().toByteArray()).setQualifier(qvalue.getQualifier().toByteArray()).setValue(qvalue.getValue().toByteArray()).build();
                    resp = MutateResponse.newBuilder().setResult(ProtobufUtil.toResult(Result.create(Arrays.asList(cell)))).build();
                    break;
                default:
                    resp = MutateResponse.getDefaultInstance();
                    break;
            }
            RpcCallback<MutateResponse> done = invocation.getArgument(2);
            done.run(resp);
            return null;
        }
    }).when(stub).mutate(any(HBaseRpcController.class), any(MutateRequest.class), any());
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            RpcCallback<GetResponse> done = invocation.getArgument(2);
            done.run(GetResponse.getDefaultInstance());
            return null;
        }
    }).when(stub).get(any(HBaseRpcController.class), any(GetRequest.class), any());
    conn = new AsyncConnectionImpl(CONF, new DoNothingConnectionRegistry(CONF), "test", null, UserProvider.instantiate(CONF).getCurrent()) {

        @Override
        AsyncRegionLocator getLocator() {
            AsyncRegionLocator locator = mock(AsyncRegionLocator.class);
            Answer<CompletableFuture<HRegionLocation>> answer = new Answer<CompletableFuture<HRegionLocation>>() {

                @Override
                public CompletableFuture<HRegionLocation> answer(InvocationOnMock invocation) throws Throwable {
                    TableName tableName = invocation.getArgument(0);
                    RegionInfo info = RegionInfoBuilder.newBuilder(tableName).build();
                    ServerName serverName = ServerName.valueOf("rs", 16010, 12345);
                    HRegionLocation loc = new HRegionLocation(info, serverName);
                    return CompletableFuture.completedFuture(loc);
                }
            };
            doAnswer(answer).when(locator).getRegionLocation(any(TableName.class), any(byte[].class), any(RegionLocateType.class), anyLong());
            doAnswer(answer).when(locator).getRegionLocation(any(TableName.class), any(byte[].class), anyInt(), any(RegionLocateType.class), anyLong());
            return locator;
        }

        @Override
        ClientService.Interface getRegionServerStub(ServerName serverName) throws IOException {
            return stub;
        }
    };
}
Also used : MutationProto(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto) RegionActionResult(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.RegionActionResult) MutateResponse(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutateResponse) CompletableFuture(java.util.concurrent.CompletableFuture) HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) GetRequest(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.GetRequest) RpcCallback(org.apache.hbase.thirdparty.com.google.protobuf.RpcCallback) Cell(org.apache.hadoop.hbase.Cell) QualifierValue(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto.ColumnValue.QualifierValue) MutateRequest(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutateRequest) IOException(java.io.IOException) ScanRequest(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ScanRequest) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) TableName(org.apache.hadoop.hbase.TableName) HBaseRpcController(org.apache.hadoop.hbase.ipc.HBaseRpcController) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ServerName(org.apache.hadoop.hbase.ServerName) ColumnValue(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto.ColumnValue) ClientProtos(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos) Before(org.junit.Before)

Aggregations

IOException (java.io.IOException)9 RpcCallback (org.apache.hbase.thirdparty.com.google.protobuf.RpcCallback)9 CoprocessorRpcUtils (org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils)7 AggregateRequest (org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateRequest)7 AggregateResponse (org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateResponse)7 AggregateService (org.apache.hadoop.hbase.shaded.protobuf.generated.AggregateProtos.AggregateService)7 RpcController (org.apache.hbase.thirdparty.com.google.protobuf.RpcController)7 ByteString (org.apache.hbase.thirdparty.com.google.protobuf.ByteString)6 ByteBuffer (java.nio.ByteBuffer)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 Pair (org.apache.hadoop.hbase.util.Pair)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Cell (org.apache.hadoop.hbase.Cell)2 HRegionLocation (org.apache.hadoop.hbase.HRegionLocation)2 ServerName (org.apache.hadoop.hbase.ServerName)2 TableName (org.apache.hadoop.hbase.TableName)2 HBaseRpcController (org.apache.hadoop.hbase.ipc.HBaseRpcController)2