Search in sources :

Example 1 with Coprocessor

use of org.tikv.kvproto.Coprocessor in project client-java by tikv.

the class KVMockServer method coprocessor.

@Override
public void coprocessor(org.tikv.kvproto.Coprocessor.Request requestWrap, io.grpc.stub.StreamObserver<org.tikv.kvproto.Coprocessor.Response> responseObserver) {
    try {
        verifyContext(requestWrap.getContext());
        DAGRequest request = DAGRequest.parseFrom(requestWrap.getData());
        if (request.getStartTsFallback() == 0) {
            throw new Exception();
        }
        List<Coprocessor.KeyRange> keyRanges = requestWrap.getRangesList();
        Coprocessor.Response.Builder builderWrap = Coprocessor.Response.newBuilder();
        SelectResponse.Builder builder = SelectResponse.newBuilder();
        org.tikv.kvproto.Errorpb.Error.Builder errBuilder = org.tikv.kvproto.Errorpb.Error.newBuilder();
        for (Coprocessor.KeyRange keyRange : keyRanges) {
            Integer errorCode = errorMap.remove(keyRange.getStart());
            if (errorCode != null) {
                if (STALE_EPOCH == errorCode) {
                    errBuilder.setEpochNotMatch(EpochNotMatch.getDefaultInstance());
                } else if (NOT_LEADER == errorCode) {
                    errBuilder.setNotLeader(NotLeader.getDefaultInstance());
                } else {
                    errBuilder.setServerIsBusy(ServerIsBusy.getDefaultInstance());
                }
                builderWrap.setRegionError(errBuilder.build());
                break;
            } else {
                ByteString startKey = keyRange.getStart();
                SortedMap<Key, ByteString> kvs = dataMap.tailMap(toRawKey(startKey));
                builder.addAllChunks(kvs.entrySet().stream().filter(Objects::nonNull).filter(kv -> kv.getKey().compareTo(toRawKey(keyRange.getEnd())) <= 0).map(kv -> Chunk.newBuilder().setRowsData(kv.getValue()).build()).collect(Collectors.toList()));
            }
        }
        responseObserver.onNext(builderWrap.setData(builder.build().toByteString()).build());
        responseObserver.onCompleted();
    } catch (Exception e) {
        responseObserver.onError(Status.INTERNAL.asRuntimeException());
    }
}
Also used : NotLeader(org.tikv.kvproto.Errorpb.NotLeader) SelectResponse(com.pingcap.tidb.tipb.SelectResponse) Errorpb(org.tikv.kvproto.Errorpb) ServerIsBusy(org.tikv.kvproto.Errorpb.ServerIsBusy) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Error(org.tikv.kvproto.Errorpb.Error) Context(org.tikv.kvproto.Kvrpcpb.Context) ServerSocket(java.net.ServerSocket) TiRegion(org.tikv.common.region.TiRegion) StreamObserver(io.grpc.stub.StreamObserver) ImmutableList(com.google.common.collect.ImmutableList) HealthImplBase(io.grpc.health.v1.HealthGrpc.HealthImplBase) ServerBuilder(io.grpc.ServerBuilder) Map(java.util.Map) HealthCheckRequest(io.grpc.health.v1.HealthCheckRequest) Status(io.grpc.Status) Server(io.grpc.Server) Coprocessor(org.tikv.kvproto.Coprocessor) Key.toRawKey(org.tikv.common.key.Key.toRawKey) ServingStatus(io.grpc.health.v1.HealthCheckResponse.ServingStatus) Logger(org.slf4j.Logger) TikvGrpc(org.tikv.kvproto.TikvGrpc) Chunk(com.pingcap.tidb.tipb.Chunk) IOException(java.io.IOException) Kvrpcpb(org.tikv.kvproto.Kvrpcpb) Collectors(java.util.stream.Collectors) ByteString(com.google.protobuf.ByteString) DAGRequest(com.pingcap.tidb.tipb.DAGRequest) Objects(java.util.Objects) List(java.util.List) Key(org.tikv.common.key.Key) TreeMap(java.util.TreeMap) HealthCheckResponse(io.grpc.health.v1.HealthCheckResponse) EpochNotMatch(org.tikv.kvproto.Errorpb.EpochNotMatch) SortedMap(java.util.SortedMap) ByteString(com.google.protobuf.ByteString) Error(org.tikv.kvproto.Errorpb.Error) IOException(java.io.IOException) SelectResponse(com.pingcap.tidb.tipb.SelectResponse) HealthCheckResponse(io.grpc.health.v1.HealthCheckResponse) Coprocessor(org.tikv.kvproto.Coprocessor) DAGRequest(com.pingcap.tidb.tipb.DAGRequest) Objects(java.util.Objects) SelectResponse(com.pingcap.tidb.tipb.SelectResponse) Key.toRawKey(org.tikv.common.key.Key.toRawKey) Key(org.tikv.common.key.Key)

Example 2 with Coprocessor

use of org.tikv.kvproto.Coprocessor in project tispark by pingcap.

the class KVMockServer method coprocessor.

@Override
public void coprocessor(org.tikv.kvproto.Coprocessor.Request requestWrap, io.grpc.stub.StreamObserver<org.tikv.kvproto.Coprocessor.Response> responseObserver) {
    try {
        verifyContext(requestWrap.getContext());
        DAGRequest request = DAGRequest.parseFrom(requestWrap.getData());
        List<Coprocessor.KeyRange> keyRanges = requestWrap.getRangesList();
        Coprocessor.Response.Builder builderWrap = Coprocessor.Response.newBuilder();
        SelectResponse.Builder builder = SelectResponse.newBuilder();
        org.tikv.kvproto.Errorpb.Error.Builder errBuilder = org.tikv.kvproto.Errorpb.Error.newBuilder();
        for (Coprocessor.KeyRange keyRange : keyRanges) {
            Integer errorCode = errorMap.remove(keyRange.getStart());
            if (errorCode != null) {
                if (STALE_EPOCH == errorCode) {
                    errBuilder.setEpochNotMatch(Errorpb.EpochNotMatch.getDefaultInstance());
                } else if (NOT_LEADER == errorCode) {
                    errBuilder.setNotLeader(NotLeader.getDefaultInstance());
                } else {
                    errBuilder.setServerIsBusy(ServerIsBusy.getDefaultInstance());
                }
                builderWrap.setRegionError(errBuilder.build());
                break;
            } else {
                ByteString startKey = keyRange.getStart();
                SortedMap<Key, ByteString> kvs = dataMap.tailMap(toRawKey(startKey));
                builder.addAllChunks(kvs.entrySet().stream().filter(Objects::nonNull).filter(kv -> kv.getKey().compareTo(toRawKey(keyRange.getEnd())) <= 0).map(kv -> Chunk.newBuilder().setRowsData(kv.getValue()).build()).collect(Collectors.toList()));
            }
        }
        responseObserver.onNext(builderWrap.setData(builder.build().toByteString()).build());
        responseObserver.onCompleted();
    } catch (Exception e) {
        responseObserver.onError(Status.INTERNAL.asRuntimeException());
    }
}
Also used : NotLeader(org.tikv.kvproto.Errorpb.NotLeader) SelectResponse(com.pingcap.tidb.tipb.SelectResponse) Errorpb(org.tikv.kvproto.Errorpb) ServerIsBusy(org.tikv.kvproto.Errorpb.ServerIsBusy) HashMap(java.util.HashMap) Error(org.tikv.kvproto.Errorpb.Error) Context(org.tikv.kvproto.Kvrpcpb.Context) Key(com.pingcap.tikv.key.Key) ServerSocket(java.net.ServerSocket) ImmutableList(com.google.common.collect.ImmutableList) ServerBuilder(io.grpc.ServerBuilder) Map(java.util.Map) Status(io.grpc.Status) Server(io.grpc.Server) Coprocessor(org.tikv.kvproto.Coprocessor) Key.toRawKey(com.pingcap.tikv.key.Key.toRawKey) TikvGrpc(org.tikv.kvproto.TikvGrpc) Chunk(com.pingcap.tidb.tipb.Chunk) IOException(java.io.IOException) Kvrpcpb(org.tikv.kvproto.Kvrpcpb) TiRegion(com.pingcap.tikv.region.TiRegion) Collectors(java.util.stream.Collectors) ByteString(com.google.protobuf.ByteString) DAGRequest(com.pingcap.tidb.tipb.DAGRequest) Objects(java.util.Objects) List(java.util.List) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) ByteString(com.google.protobuf.ByteString) Error(org.tikv.kvproto.Errorpb.Error) IOException(java.io.IOException) SelectResponse(com.pingcap.tidb.tipb.SelectResponse) Coprocessor(org.tikv.kvproto.Coprocessor) DAGRequest(com.pingcap.tidb.tipb.DAGRequest) Objects(java.util.Objects) SelectResponse(com.pingcap.tidb.tipb.SelectResponse) Key(com.pingcap.tikv.key.Key) Key.toRawKey(com.pingcap.tikv.key.Key.toRawKey)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)2 ByteString (com.google.protobuf.ByteString)2 Chunk (com.pingcap.tidb.tipb.Chunk)2 DAGRequest (com.pingcap.tidb.tipb.DAGRequest)2 SelectResponse (com.pingcap.tidb.tipb.SelectResponse)2 Server (io.grpc.Server)2 ServerBuilder (io.grpc.ServerBuilder)2 Status (io.grpc.Status)2 IOException (java.io.IOException)2 ServerSocket (java.net.ServerSocket)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Objects (java.util.Objects)2 SortedMap (java.util.SortedMap)2 TreeMap (java.util.TreeMap)2 Collectors (java.util.stream.Collectors)2 Coprocessor (org.tikv.kvproto.Coprocessor)2 Errorpb (org.tikv.kvproto.Errorpb)2 Error (org.tikv.kvproto.Errorpb.Error)2