Skip to content

Commit

Permalink
pd backoff use tikv.rawkv.default_backoff_in_ms
Browse files Browse the repository at this point in the history
Signed-off-by: marsishandsome <marsishandsome@gmail.com>
  • Loading branch information
marsishandsome committed Oct 21, 2021
1 parent ed5af81 commit 7f54c2c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
12 changes: 8 additions & 4 deletions src/main/java/org/tikv/common/region/RegionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public ReadOnlyPDClient getPDClient() {
}

public TiRegion getRegionByKey(ByteString key) {
return getRegionByKey(key, ConcreteBackOffer.newGetBackOff());
return getRegionByKey(key, defaultBackOff());
}

public TiRegion getRegionByKey(ByteString key, BackOffer backOffer) {
Expand Down Expand Up @@ -118,7 +118,7 @@ public TiRegion getRegionByKey(ByteString key, BackOffer backOffer) {
// Consider region A, B. After merge of (A, B) -> A, region ID B does not exist.
// This request is unrecoverable.
public TiRegion getRegionById(long regionId) {
BackOffer backOffer = ConcreteBackOffer.newGetBackOff();
BackOffer backOffer = defaultBackOff();
TiRegion region = cache.getRegionById(regionId);
if (region == null) {
Pair<Metapb.Region, Metapb.Peer> regionAndLeader =
Expand All @@ -138,7 +138,7 @@ public Pair<TiRegion, TiStore> getRegionStorePairByKey(ByteString key) {
}

public Pair<TiRegion, TiStore> getRegionStorePairByKey(ByteString key, TiStoreType storeType) {
return getRegionStorePairByKey(key, storeType, ConcreteBackOffer.newGetBackOff());
return getRegionStorePairByKey(key, storeType, defaultBackOff());
}

public Pair<TiRegion, TiStore> getRegionStorePairByKey(
Expand Down Expand Up @@ -210,7 +210,7 @@ public TiStore getStoreById(long id, BackOffer backOffer) {
}

public TiStore getStoreById(long id) {
return getStoreById(id, ConcreteBackOffer.newGetBackOff());
return getStoreById(id, defaultBackOff());
}

public void onRegionStale(TiRegion region) {
Expand Down Expand Up @@ -258,4 +258,8 @@ public void invalidateStore(long storeId) {
public void invalidateRegion(TiRegion region) {
cache.invalidateRegion(region);
}

private BackOffer defaultBackOff() {
return ConcreteBackOffer.newCustomBackOff(conf.getRawKVDefaultBackoffInMS());
}
}
24 changes: 22 additions & 2 deletions src/main/java/org/tikv/common/region/RegionStoreClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -1284,19 +1284,39 @@ public synchronized RegionStoreClient build(ByteString key) throws GrpcException
return build(key, TiStoreType.TiKV);
}

public synchronized RegionStoreClient build(ByteString key, BackOffer backOffer)
throws GrpcException {
return build(key, TiStoreType.TiKV, backOffer);
}

public synchronized RegionStoreClient build(ByteString key, TiStoreType storeType)
throws GrpcException {
Pair<TiRegion, TiStore> pair = regionManager.getRegionStorePairByKey(key, storeType);
return build(key, storeType, defaultBackOff());
}

public synchronized RegionStoreClient build(
ByteString key, TiStoreType storeType, BackOffer backOffer) throws GrpcException {
Pair<TiRegion, TiStore> pair =
regionManager.getRegionStorePairByKey(key, storeType, backOffer);
return build(pair.first, pair.second, storeType);
}

public synchronized RegionStoreClient build(TiRegion region) throws GrpcException {
TiStore store = regionManager.getStoreById(region.getLeader().getStoreId());
return build(region, defaultBackOff());
}

public synchronized RegionStoreClient build(TiRegion region, BackOffer backOffer)
throws GrpcException {
TiStore store = regionManager.getStoreById(region.getLeader().getStoreId(), backOffer);
return build(region, store, TiStoreType.TiKV);
}

public RegionManager getRegionManager() {
return regionManager;
}

private BackOffer defaultBackOff() {
return ConcreteBackOffer.newCustomBackOff(conf.getRawKVDefaultBackoffInMS());
}
}
}
16 changes: 8 additions & 8 deletions src/main/java/org/tikv/raw/RawKVClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private void put(ByteString key, ByteString value, long ttl, boolean atomic) {
try {
BackOffer backOffer = defaultBackOff();
while (true) {
RegionStoreClient client = clientBuilder.build(key);
RegionStoreClient client = clientBuilder.build(key, backOffer);
try {
client.rawPut(backOffer, key, value, ttl, atomic);
RAW_REQUEST_SUCCESS.labels(label).inc();
Expand Down Expand Up @@ -176,7 +176,7 @@ public ByteString putIfAbsent(ByteString key, ByteString value, long ttl) {
try {
BackOffer backOffer = defaultBackOff();
while (true) {
RegionStoreClient client = clientBuilder.build(key);
RegionStoreClient client = clientBuilder.build(key, backOffer);
try {
ByteString result = client.rawPutIfAbsent(backOffer, key, value, ttl);
RAW_REQUEST_SUCCESS.labels(label).inc();
Expand Down Expand Up @@ -257,7 +257,7 @@ public ByteString get(ByteString key) {
try {
BackOffer backOffer = defaultBackOff();
while (true) {
RegionStoreClient client = clientBuilder.build(key);
RegionStoreClient client = clientBuilder.build(key, backOffer);
try {
ByteString result = client.rawGet(defaultBackOff(), key);
RAW_REQUEST_SUCCESS.labels(label).inc();
Expand Down Expand Up @@ -343,7 +343,7 @@ public Long getKeyTTL(ByteString key) {
try {
BackOffer backOffer = defaultBackOff();
while (true) {
RegionStoreClient client = clientBuilder.build(key);
RegionStoreClient client = clientBuilder.build(key, backOffer);
try {
Long result = client.rawGetKeyTTL(defaultBackOff(), key);
RAW_REQUEST_SUCCESS.labels(label).inc();
Expand Down Expand Up @@ -561,7 +561,7 @@ private void delete(ByteString key, boolean atomic) {
try {
BackOffer backOffer = defaultBackOff();
while (true) {
RegionStoreClient client = clientBuilder.build(key);
RegionStoreClient client = clientBuilder.build(key, backOffer);
try {
client.rawDelete(defaultBackOff(), key, atomic);
RAW_REQUEST_SUCCESS.labels(label).inc();
Expand Down Expand Up @@ -707,7 +707,7 @@ private List<KvPair> doSendBatchGet(BackOffer backOffer, List<ByteString> keys)

private Pair<List<Batch>, List<KvPair>> doSendBatchGetInBatchesWithRetry(
BackOffer backOffer, Batch batch) {
RegionStoreClient client = clientBuilder.build(batch.getRegion());
RegionStoreClient client = clientBuilder.build(batch.getRegion(), backOffer);
try {
List<KvPair> partialResult = client.rawBatchGet(backOffer, batch.getKeys());
return Pair.create(new ArrayList<>(), partialResult);
Expand Down Expand Up @@ -748,7 +748,7 @@ private void doSendBatchDelete(BackOffer backOffer, List<ByteString> keys, boole

private List<Batch> doSendBatchDeleteInBatchesWithRetry(
BackOffer backOffer, Batch batch, boolean atomic) {
RegionStoreClient client = clientBuilder.build(batch.getRegion());
RegionStoreClient client = clientBuilder.build(batch.getRegion(), backOffer);
try {
client.rawBatchDelete(backOffer, batch.getKeys(), atomic);
return new ArrayList<>();
Expand Down Expand Up @@ -798,7 +798,7 @@ private void doSendDeleteRange(BackOffer backOffer, ByteString startKey, ByteStr
}

private List<DeleteRange> doSendDeleteRangeWithRetry(BackOffer backOffer, DeleteRange range) {
try (RegionStoreClient client = clientBuilder.build(range.getRegion())) {
try (RegionStoreClient client = clientBuilder.build(range.getRegion(), backOffer)) {
client.setTimeout(conf.getScanTimeout());
client.rawDeleteRange(backOffer, range.getStartKey(), range.getEndKey());
return new ArrayList<>();
Expand Down

0 comments on commit 7f54c2c

Please sign in to comment.