Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retry for batchGet #986

Merged
merged 2 commits into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import io.grpc.ManagedChannel;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.apache.log4j.Logger;
import org.tikv.kvproto.Coprocessor;
import org.tikv.kvproto.Coprocessor.KeyRange;
Expand Down Expand Up @@ -166,28 +167,50 @@ private boolean isGetSuccess(BackOffer backOffer, GetResponse resp)
return true;
}

// TODO: batch get should consider key range split
public List<KvPair> batchGet(BackOffer backOffer, Iterable<ByteString> keys, long version) {
Supplier<BatchGetRequest> request =
() ->
BatchGetRequest.newBuilder()
.setContext(region.getContext())
.addAllKeys(keys)
.setVersion(version)
.build();
KVErrorHandler<BatchGetResponse> handler =
new KVErrorHandler<>(
regionManager,
this,
region,
resp -> resp.hasRegionError() ? resp.getRegionError() : null);
BatchGetResponse resp =
callWithRetry(backOffer, TikvGrpc.METHOD_KV_BATCH_GET, request, handler);
return doBatchGet(resp, backOffer);
public List<KvPair> batchGet(BackOffer backOffer, List<ByteString> keys, long version) {
List<KvPair> result = new ArrayList<>();
while (true) {
// re-split keys
Map<TiRegion, List<ByteString>> map =
keys.stream().collect(Collectors.groupingBy(regionManager::getRegionByKey));
boolean ok = true;
for (Map.Entry<TiRegion, List<ByteString>> entry : map.entrySet()) {
TiRegion newRegion = entry.getKey();
if (!newRegion.equals(region)) {
RegionStoreClient newRegionStoreClient =
new RegionStoreClientBuilder(conf, this.channelFactory, this.regionManager)
.build(newRegion);
result.addAll(newRegionStoreClient.batchGet(backOffer, entry.getValue(), version));
} else {
Supplier<BatchGetRequest> request =
() ->
BatchGetRequest.newBuilder()
.setContext(region.getContext())
.addAllKeys(entry.getValue())
.setVersion(version)
.build();
KVErrorHandler<BatchGetResponse> handler =
new KVErrorHandler<>(
regionManager,
this,
region,
resp -> resp.hasRegionError() ? resp.getRegionError() : null);
BatchGetResponse resp =
callWithRetry(backOffer, TikvGrpc.METHOD_KV_BATCH_GET, request, handler);
if (isBatchGetSuccess(backOffer, resp)) {
result.addAll(resp.getPairsList());
} else {
ok = false;
}
}
}
if (ok) {
return result;
}
}
}

// TODO: deal with resolve locks and region errors
private List<KvPair> doBatchGet(BatchGetResponse resp, BackOffer bo) {
private boolean isBatchGetSuccess(BackOffer bo, BatchGetResponse resp) {
if (resp == null) {
this.regionManager.onRequestFail(region);
throw new TiClientInternalException("BatchGetResponse failed without a cause");
Expand All @@ -213,11 +236,10 @@ private List<KvPair> doBatchGet(BatchGetResponse resp, BackOffer bo) {
if (!ok) {
// if not resolve all locks, we wait and retry
bo.doBackOff(BoTxnLockFast, new KeyException((resp.getPairsList().get(0).getError())));
return false;
}

// FIXME: we should retry
}
return resp.getPairsList();
return true;
}

public List<KvPair> scan(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.pingcap.tikv.region.TiRegion;
import java.io.IOException;
import org.junit.Before;
import org.tikv.kvproto.Kvrpcpb;
import org.tikv.kvproto.Metapb;
import org.tikv.kvproto.Pdpb;

Expand All @@ -42,7 +41,12 @@ public void setUp() throws IOException {
.addPeers(Metapb.Peer.newBuilder().setId(11).setStoreId(13))
.build();

region = new TiRegion(r, r.getPeers(0), Kvrpcpb.IsolationLevel.RC, Kvrpcpb.CommandPri.Low);
region =
new TiRegion(
r,
r.getPeers(0),
session.getConf().getIsolationLevel(),
session.getConf().getCommandPriority());
pdServer.addGetRegionResp(Pdpb.GetRegionResponse.newBuilder().setRegion(r).build());
server = new KVMockServer();
port = server.start(region);
Expand Down