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());
}
}
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());
}
}
Aggregations