Skip to content
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
35 changes: 35 additions & 0 deletions src/main/java/org/tikv/common/TiSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,44 @@ public TiSession(TiConfiguration conf) {
if (this.enableGrpcForward) {
logger.info("enable grpc forward for high available");
}
warmUp();
logger.info("TiSession initialized in " + conf.getKvMode() + " mode");
}

private synchronized void warmUp() {
long warmUpStartTime = System.currentTimeMillis();
try {
this.client = getPDClient();
this.regionManager = getRegionManager();
List<Metapb.Store> stores = this.client.getAllStores(ConcreteBackOffer.newGetBackOff());
// warm up store cache
for (Metapb.Store store : stores) {
this.regionManager.updateStore(
null,
new TiStore(this.client.getStore(ConcreteBackOffer.newGetBackOff(), store.getId())));
}
ByteString startKey = ByteString.EMPTY;

do {
TiRegion region = regionManager.getRegionByKey(startKey);
startKey = region.getEndKey();
} while (!startKey.isEmpty());

RawKVClient rawKVClient = createRawClient();
ByteString exampleKey = ByteString.EMPTY;
ByteString prev = rawKVClient.get(exampleKey);
rawKVClient.delete(exampleKey);
rawKVClient.putIfAbsent(exampleKey, prev);
rawKVClient.put(exampleKey, prev);
} catch (Exception e) {
// ignore error
logger.info("warm up fails, ignored ", e);
} finally {
logger.info(
String.format("warm up duration %d ms", System.currentTimeMillis() - warmUpStartTime));
}
}

@VisibleForTesting
public static TiSession create(TiConfiguration conf) {
return new TiSession(conf);
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/tikv/common/region/RegionCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,12 @@ public synchronized boolean updateStore(TiStore oldStore, TiStore newStore) {
if (!newStore.isValid()) {
return false;
}
if (oldStore == null) {
storeCache.put(newStore.getId(), newStore);
return true;
}
TiStore originStore = storeCache.get(oldStore.getId());
if (originStore == oldStore) {
if (originStore.equals(oldStore)) {
storeCache.put(newStore.getId(), newStore);
oldStore.markInvalid();
return true;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/tikv/common/region/RegionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ private TiStore getStoreByIdWithBackOff(long id, BackOffer backOffer) {
TiStore store = cache.getStoreById(id);
if (store == null) {
store = new TiStore(pdClient.getStore(backOffer, id));
} else {
return store;
}
// if we did not get store info from pd, remove store from cache
if (store.getStore() == null) {
Expand Down