-
Notifications
You must be signed in to change notification settings - Fork 118
Fix pd request throws invalid store id #337
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
Merged
marsishandsome
merged 6 commits into
tikv:release-3.1
from
birdstorm:fix-pd-invalid-store-id
Nov 21, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
289c118
fix pd request throws invalid store id
birdstorm 251e6e9
format
birdstorm e54fa15
refactor
birdstorm 07fad51
resolve comments
birdstorm fe9c73f
fix test
marsishandsome 3a80b00
Merge branch 'release-3.1' into fix-pd-invalid-store-id
marsishandsome File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/main/java/org/tikv/common/exception/InvalidStoreException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* | ||
| * Copyright 2021 PingCAP, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.tikv.common.exception; | ||
|
|
||
| public class InvalidStoreException extends TiKVException { | ||
|
|
||
| public InvalidStoreException(long storeId) { | ||
| super(String.format("Invalid storeId: %d", storeId)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| import org.tikv.common.ReadOnlyPDClient; | ||
| import org.tikv.common.TiConfiguration; | ||
| import org.tikv.common.exception.GrpcException; | ||
| import org.tikv.common.exception.InvalidStoreException; | ||
| import org.tikv.common.exception.TiClientInternalException; | ||
| import org.tikv.common.util.BackOffer; | ||
| import org.tikv.common.util.ChannelFactory; | ||
|
|
@@ -152,9 +153,6 @@ public Pair<TiRegion, TiStore> getRegionStorePairByKey( | |
| if (storeType == TiStoreType.TiKV) { | ||
| Peer peer = region.getCurrentReplica(); | ||
| store = getStoreById(peer.getStoreId(), backOffer); | ||
| if (store == null) { | ||
| cache.clearAll(); | ||
| } | ||
| } else { | ||
| outerLoop: | ||
| for (Peer peer : region.getLearnerList()) { | ||
|
|
@@ -168,16 +166,11 @@ public Pair<TiRegion, TiStore> getRegionStorePairByKey( | |
| } | ||
| } | ||
| if (store == null) { | ||
| // clear the region cache so we may get the learner peer next time | ||
| // clear the region cache, so we may get the learner peer next time | ||
| cache.invalidateRegion(region); | ||
| } | ||
| } | ||
|
|
||
| if (store == null) { | ||
| throw new TiClientInternalException( | ||
| "Cannot find valid store on " + storeType + " for region " + region.toString()); | ||
| } | ||
|
|
||
| return Pair.create(region, store); | ||
| } | ||
|
|
||
|
|
@@ -200,13 +193,20 @@ private List<TiStore> getRegionStore(List<Metapb.Peer> peers, BackOffer backOffe | |
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public TiStore getStoreById(long id, BackOffer backOffer) { | ||
| private TiStore getStoreByIdWithBackOff(long id, BackOffer backOffer) { | ||
| try { | ||
| TiStore store = cache.getStoreById(id); | ||
| if (store == null) { | ||
| store = new TiStore(pdClient.getStore(backOffer, id)); | ||
| } | ||
| // if we did not get store info from pd, remove store from cache | ||
| if (store.getStore() == null) { | ||
| logger.warn(String.format("failed to get store %d from pd", id)); | ||
| return null; | ||
| } | ||
| // if the store is already tombstone, remove store from cache | ||
| if (store.getStore().getState().equals(StoreState.Tombstone)) { | ||
| logger.warn(String.format("store %d is tombstone", id)); | ||
| return null; | ||
| } | ||
| if (cache.putStore(id, store) && storeChecker != null) { | ||
|
|
@@ -222,6 +222,16 @@ public TiStore getStoreById(long id) { | |
| return getStoreById(id, defaultBackOff()); | ||
| } | ||
|
|
||
| public TiStore getStoreById(long id, BackOffer backOffer) { | ||
| TiStore store = getStoreByIdWithBackOff(id, backOffer); | ||
| if (store == null) { | ||
| logger.warn(String.format("failed to fetch store %d, the store may be missing", id)); | ||
| cache.clearAll(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should log info or warn messages for this critical operation. |
||
| throw new InvalidStoreException(id); | ||
| } | ||
| return store; | ||
| } | ||
|
|
||
| public void onRegionStale(TiRegion region) { | ||
| cache.invalidateRegion(region); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we directly check whether the instance is of
InvalidStoreException?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, should the error message be
Invalid storeId?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, the exception tikv returns is a grpc exception, it is a StatusRuntimeException that only contains "invalid store id xx" information in its message.