From 80c67ca8fc66e145b3489ef4aa7a439af764d247 Mon Sep 17 00:00:00 2001 From: iosmanthus Date: Wed, 8 Dec 2021 18:33:11 +0800 Subject: [PATCH 1/9] warm up RawKVClient while creating it Signed-off-by: iosmanthus --- src/main/java/org/tikv/common/TiSession.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/tikv/common/TiSession.java b/src/main/java/org/tikv/common/TiSession.java index f3f10c9ccef..c470215a89f 100644 --- a/src/main/java/org/tikv/common/TiSession.java +++ b/src/main/java/org/tikv/common/TiSession.java @@ -123,7 +123,12 @@ public static TiSession getInstance(TiConfiguration conf) { public RawKVClient createRawClient() { checkIsClosed(); - return new RawKVClient(this, this.getRegionStoreClientBuilder()); + RawKVClient rawClient = new RawKVClient(this, this.getRegionStoreClientBuilder()); + + // Warm up raw client to avoid a slow first call. + rawClient.get(ByteString.EMPTY); + + return rawClient; } public KVClient createKVClient() { From 0100efd1509effb5e6da54182c8ad6324e7413a5 Mon Sep 17 00:00:00 2001 From: birdstorm Date: Thu, 9 Dec 2021 22:05:42 +0800 Subject: [PATCH 2/9] try fix by load class Signed-off-by: birdstorm --- src/main/java/org/tikv/common/TiSession.java | 7 +- .../tikv/common/util/ClientClassLoader.java | 85 +++++++++++++++++++ src/main/java/org/tikv/raw/RawKVClient.java | 16 ++++ .../java/org/tikv/raw/RawKVClientTest.java | 6 ++ 4 files changed, 108 insertions(+), 6 deletions(-) create mode 100644 src/main/java/org/tikv/common/util/ClientClassLoader.java diff --git a/src/main/java/org/tikv/common/TiSession.java b/src/main/java/org/tikv/common/TiSession.java index c470215a89f..f3f10c9ccef 100644 --- a/src/main/java/org/tikv/common/TiSession.java +++ b/src/main/java/org/tikv/common/TiSession.java @@ -123,12 +123,7 @@ public static TiSession getInstance(TiConfiguration conf) { public RawKVClient createRawClient() { checkIsClosed(); - RawKVClient rawClient = new RawKVClient(this, this.getRegionStoreClientBuilder()); - - // Warm up raw client to avoid a slow first call. - rawClient.get(ByteString.EMPTY); - - return rawClient; + return new RawKVClient(this, this.getRegionStoreClientBuilder()); } public KVClient createKVClient() { diff --git a/src/main/java/org/tikv/common/util/ClientClassLoader.java b/src/main/java/org/tikv/common/util/ClientClassLoader.java new file mode 100644 index 00000000000..364e0543d25 --- /dev/null +++ b/src/main/java/org/tikv/common/util/ClientClassLoader.java @@ -0,0 +1,85 @@ +/* + * 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.util; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; +import java.util.Objects; +import javax.annotation.Nonnull; + +public class ClientClassLoader { + /** + * Get all classes under specific package root + * + * @param packageName package name + * @return classes + * @throws ClassNotFoundException class not found + * @throws IOException file read failure + */ + public static Class[] getClasses(@Nonnull String packageName) + throws ClassNotFoundException, IOException { + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + assert classLoader != null; + String path = packageName.replace('.', '/'); + Enumeration resources = classLoader.getResources(path); + List dirs = new ArrayList<>(); + while (resources.hasMoreElements()) { + URL resource = resources.nextElement(); + dirs.add(new File(resource.getFile())); + } + ArrayList> classes = new ArrayList<>(); + for (File directory : dirs) { + classes.addAll(findClasses(directory, packageName)); + } + return classes.toArray(new Class[0]); + } + + /** + * Recursive method used to find all classes in a given directory and sub-dirs. + * + * @param directory The base directory + * @param packageName The package name for classes found inside the base directory + * @return The classes + * @throws ClassNotFoundException class not found + */ + @Nonnull + private static List> findClasses(@Nonnull File directory, String packageName) + throws ClassNotFoundException { + List> classes = new ArrayList<>(); + if (!directory.exists()) { + return classes; + } + File[] files = directory.listFiles(); + assert files != null; + for (File file : files) { + if (file.isDirectory()) { + assert !file.getName().contains("."); + classes.addAll( + Objects.requireNonNull(findClasses(file, packageName + "." + file.getName()))); + } else if (file.getName().endsWith(".class")) { + // load class + classes.add( + Class.forName( + packageName + '.' + file.getName().substring(0, file.getName().length() - 6))); + } + } + return classes; + } +} diff --git a/src/main/java/org/tikv/raw/RawKVClient.java b/src/main/java/org/tikv/raw/RawKVClient.java index 7ffd1ed0514..5ae849b326f 100644 --- a/src/main/java/org/tikv/raw/RawKVClient.java +++ b/src/main/java/org/tikv/raw/RawKVClient.java @@ -20,6 +20,7 @@ import com.google.protobuf.ByteString; import io.prometheus.client.Counter; import io.prometheus.client.Histogram; +import java.io.IOException; import java.util.*; import java.util.concurrent.*; import java.util.stream.Collectors; @@ -89,6 +90,21 @@ public class RawKVClient implements AutoCloseable { private static final TiKVException ERR_MAX_SCAN_LIMIT_EXCEEDED = new TiKVException("limit should be less than MAX_RAW_SCAN_LIMIT"); + // warm up the client to load classes needed by protobuf + static { + logger.info("load start"); + try { + Class[] classes = ClientClassLoader.getClasses("org.tikv.kvproto"); + logger.info(classes.length + " classes"); + for (Class c : classes) { + logger.info("load: " + c.getName()); + } + } catch (ClassNotFoundException | IOException e) { + e.printStackTrace(); + } + logger.info("load complete"); + } + public RawKVClient(TiSession session, RegionStoreClientBuilder clientBuilder) { Objects.requireNonNull(session, "session is null"); Objects.requireNonNull(clientBuilder, "clientBuilder is null"); diff --git a/src/test/java/org/tikv/raw/RawKVClientTest.java b/src/test/java/org/tikv/raw/RawKVClientTest.java index 9e8884494c9..3c497e36193 100644 --- a/src/test/java/org/tikv/raw/RawKVClientTest.java +++ b/src/test/java/org/tikv/raw/RawKVClientTest.java @@ -98,6 +98,12 @@ public void tearDown() throws Exception { } } + @Test + public void testInitialize() { + // do nothing + logger.info("init done"); + } + @Test public void getKeyTTLTest() { long ttl = 10; From c40b61cbe5aa3c6f0c468ffc030cde5d944d3967 Mon Sep 17 00:00:00 2001 From: birdstorm Date: Thu, 9 Dec 2021 23:42:09 +0800 Subject: [PATCH 3/9] add load class from jar file Signed-off-by: birdstorm --- .../tikv/common/util/ClientClassLoader.java | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/tikv/common/util/ClientClassLoader.java b/src/main/java/org/tikv/common/util/ClientClassLoader.java index 364e0543d25..ac2281bfc7a 100644 --- a/src/main/java/org/tikv/common/util/ClientClassLoader.java +++ b/src/main/java/org/tikv/common/util/ClientClassLoader.java @@ -17,14 +17,20 @@ import java.io.File; import java.io.IOException; +import java.net.JarURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.Objects; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; import javax.annotation.Nonnull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ClientClassLoader { + private static final Logger logger = LoggerFactory.getLogger(ClientClassLoader.class); /** * Get all classes under specific package root * @@ -35,16 +41,24 @@ public class ClientClassLoader { */ public static Class[] getClasses(@Nonnull String packageName) throws ClassNotFoundException, IOException { + logger.info("package name: " + packageName); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); assert classLoader != null; String path = packageName.replace('.', '/'); + logger.info("package path: " + path); Enumeration resources = classLoader.getResources(path); List dirs = new ArrayList<>(); + ArrayList> classes = new ArrayList<>(); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); - dirs.add(new File(resource.getFile())); + String protocol = resource.getProtocol(); + if ("jar".equalsIgnoreCase(protocol)) { + JarURLConnection connection = (JarURLConnection) resource.openConnection(); + classes.addAll(findClasses(connection, packageName)); + } else if ("file".equalsIgnoreCase(protocol)) { + dirs.add(new File(resource.getFile())); + } } - ArrayList> classes = new ArrayList<>(); for (File directory : dirs) { classes.addAll(findClasses(directory, packageName)); } @@ -82,4 +96,26 @@ private static List> findClasses(@Nonnull File directory, String packag } return classes; } + + private static List> findClasses( + @Nonnull JarURLConnection connection, String packageName) + throws ClassNotFoundException, IOException { + List> classes = new ArrayList<>(); + JarFile jarFile = connection.getJarFile(); + if (jarFile != null) { + Enumeration jarEntryEnumeration = jarFile.entries(); + while (jarEntryEnumeration.hasMoreElements()) { + JarEntry entry = jarEntryEnumeration.nextElement(); + String jarEntryName = entry.getName(); + if (jarEntryName.contains(".class") + && jarEntryName.replaceAll("/", ".").startsWith(packageName)) { + String className = + jarEntryName.substring(0, jarEntryName.lastIndexOf(".")).replace("/", "."); + Class cls = Class.forName(className); + classes.add(cls); + } + } + } + return classes; + } } From e6dd4efe50a9c29e1ec5a6171873b4152d299669 Mon Sep 17 00:00:00 2001 From: birdstorm Date: Fri, 10 Dec 2021 02:41:05 +0800 Subject: [PATCH 4/9] warm up solution 2 Signed-off-by: birdstorm --- src/main/java/org/tikv/common/TiSession.java | 43 +++++-- .../org/tikv/common/region/RegionCache.java | 6 +- .../org/tikv/common/region/RegionManager.java | 2 + .../tikv/common/util/ClientClassLoader.java | 121 ------------------ src/main/java/org/tikv/raw/RawKVClient.java | 16 --- 5 files changed, 42 insertions(+), 146 deletions(-) delete mode 100644 src/main/java/org/tikv/common/util/ClientClassLoader.java diff --git a/src/main/java/org/tikv/common/TiSession.java b/src/main/java/org/tikv/common/TiSession.java index f3f10c9ccef..8e7c8b4b60c 100644 --- a/src/main/java/org/tikv/common/TiSession.java +++ b/src/main/java/org/tikv/common/TiSession.java @@ -20,10 +20,8 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.google.protobuf.ByteString; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; + +import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.stream.Collectors; @@ -35,10 +33,7 @@ import org.tikv.common.importer.SwitchTiKVModeClient; import org.tikv.common.key.Key; import org.tikv.common.meta.TiTimestamp; -import org.tikv.common.region.RegionManager; -import org.tikv.common.region.RegionStoreClient; -import org.tikv.common.region.TiRegion; -import org.tikv.common.region.TiStore; +import org.tikv.common.region.*; import org.tikv.common.util.*; import org.tikv.kvproto.ImportSstpb; import org.tikv.kvproto.Metapb; @@ -98,9 +93,41 @@ 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() { + this.client = getPDClient(); + this.regionManager = getRegionManager(); + List stores = this.client.getAllStores(ConcreteBackOffer.newGetBackOff()); + logger.info("number of stores=" + stores.size()); + // 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; + Optional prev = rawKVClient.get(exampleKey); + if (prev.isPresent()) { + rawKVClient.delete(exampleKey); + rawKVClient.putIfAbsent(exampleKey, prev.get()); + rawKVClient.put(exampleKey, prev.get()); + } else { + rawKVClient.putIfAbsent(exampleKey, ByteString.EMPTY); + rawKVClient.put(exampleKey, ByteString.EMPTY); + rawKVClient.delete(exampleKey); + } + } + @VisibleForTesting public static TiSession create(TiConfiguration conf) { return new TiSession(conf); diff --git a/src/main/java/org/tikv/common/region/RegionCache.java b/src/main/java/org/tikv/common/region/RegionCache.java index 1b8f457da05..f9d848ac9d3 100644 --- a/src/main/java/org/tikv/common/region/RegionCache.java +++ b/src/main/java/org/tikv/common/region/RegionCache.java @@ -141,8 +141,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; diff --git a/src/main/java/org/tikv/common/region/RegionManager.java b/src/main/java/org/tikv/common/region/RegionManager.java index bcce5f6e94b..b9fc08d73fd 100644 --- a/src/main/java/org/tikv/common/region/RegionManager.java +++ b/src/main/java/org/tikv/common/region/RegionManager.java @@ -206,6 +206,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) { diff --git a/src/main/java/org/tikv/common/util/ClientClassLoader.java b/src/main/java/org/tikv/common/util/ClientClassLoader.java deleted file mode 100644 index ac2281bfc7a..00000000000 --- a/src/main/java/org/tikv/common/util/ClientClassLoader.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * 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.util; - -import java.io.File; -import java.io.IOException; -import java.net.JarURLConnection; -import java.net.URL; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.List; -import java.util.Objects; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; -import javax.annotation.Nonnull; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class ClientClassLoader { - private static final Logger logger = LoggerFactory.getLogger(ClientClassLoader.class); - /** - * Get all classes under specific package root - * - * @param packageName package name - * @return classes - * @throws ClassNotFoundException class not found - * @throws IOException file read failure - */ - public static Class[] getClasses(@Nonnull String packageName) - throws ClassNotFoundException, IOException { - logger.info("package name: " + packageName); - ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - assert classLoader != null; - String path = packageName.replace('.', '/'); - logger.info("package path: " + path); - Enumeration resources = classLoader.getResources(path); - List dirs = new ArrayList<>(); - ArrayList> classes = new ArrayList<>(); - while (resources.hasMoreElements()) { - URL resource = resources.nextElement(); - String protocol = resource.getProtocol(); - if ("jar".equalsIgnoreCase(protocol)) { - JarURLConnection connection = (JarURLConnection) resource.openConnection(); - classes.addAll(findClasses(connection, packageName)); - } else if ("file".equalsIgnoreCase(protocol)) { - dirs.add(new File(resource.getFile())); - } - } - for (File directory : dirs) { - classes.addAll(findClasses(directory, packageName)); - } - return classes.toArray(new Class[0]); - } - - /** - * Recursive method used to find all classes in a given directory and sub-dirs. - * - * @param directory The base directory - * @param packageName The package name for classes found inside the base directory - * @return The classes - * @throws ClassNotFoundException class not found - */ - @Nonnull - private static List> findClasses(@Nonnull File directory, String packageName) - throws ClassNotFoundException { - List> classes = new ArrayList<>(); - if (!directory.exists()) { - return classes; - } - File[] files = directory.listFiles(); - assert files != null; - for (File file : files) { - if (file.isDirectory()) { - assert !file.getName().contains("."); - classes.addAll( - Objects.requireNonNull(findClasses(file, packageName + "." + file.getName()))); - } else if (file.getName().endsWith(".class")) { - // load class - classes.add( - Class.forName( - packageName + '.' + file.getName().substring(0, file.getName().length() - 6))); - } - } - return classes; - } - - private static List> findClasses( - @Nonnull JarURLConnection connection, String packageName) - throws ClassNotFoundException, IOException { - List> classes = new ArrayList<>(); - JarFile jarFile = connection.getJarFile(); - if (jarFile != null) { - Enumeration jarEntryEnumeration = jarFile.entries(); - while (jarEntryEnumeration.hasMoreElements()) { - JarEntry entry = jarEntryEnumeration.nextElement(); - String jarEntryName = entry.getName(); - if (jarEntryName.contains(".class") - && jarEntryName.replaceAll("/", ".").startsWith(packageName)) { - String className = - jarEntryName.substring(0, jarEntryName.lastIndexOf(".")).replace("/", "."); - Class cls = Class.forName(className); - classes.add(cls); - } - } - } - return classes; - } -} diff --git a/src/main/java/org/tikv/raw/RawKVClient.java b/src/main/java/org/tikv/raw/RawKVClient.java index 5ae849b326f..7ffd1ed0514 100644 --- a/src/main/java/org/tikv/raw/RawKVClient.java +++ b/src/main/java/org/tikv/raw/RawKVClient.java @@ -20,7 +20,6 @@ import com.google.protobuf.ByteString; import io.prometheus.client.Counter; import io.prometheus.client.Histogram; -import java.io.IOException; import java.util.*; import java.util.concurrent.*; import java.util.stream.Collectors; @@ -90,21 +89,6 @@ public class RawKVClient implements AutoCloseable { private static final TiKVException ERR_MAX_SCAN_LIMIT_EXCEEDED = new TiKVException("limit should be less than MAX_RAW_SCAN_LIMIT"); - // warm up the client to load classes needed by protobuf - static { - logger.info("load start"); - try { - Class[] classes = ClientClassLoader.getClasses("org.tikv.kvproto"); - logger.info(classes.length + " classes"); - for (Class c : classes) { - logger.info("load: " + c.getName()); - } - } catch (ClassNotFoundException | IOException e) { - e.printStackTrace(); - } - logger.info("load complete"); - } - public RawKVClient(TiSession session, RegionStoreClientBuilder clientBuilder) { Objects.requireNonNull(session, "session is null"); Objects.requireNonNull(clientBuilder, "clientBuilder is null"); From b8291687188c166cd5bab22701ca37cbd0539208 Mon Sep 17 00:00:00 2001 From: birdstorm Date: Fri, 10 Dec 2021 02:46:09 +0800 Subject: [PATCH 5/9] use smart raw kv client Signed-off-by: birdstorm --- src/main/java/org/tikv/common/TiSession.java | 16 ++++++++-------- src/main/java/org/tikv/raw/SmartRawKVClient.java | 10 ---------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/tikv/common/TiSession.java b/src/main/java/org/tikv/common/TiSession.java index e8ebef26d6e..a7e2fc81591 100644 --- a/src/main/java/org/tikv/common/TiSession.java +++ b/src/main/java/org/tikv/common/TiSession.java @@ -115,17 +115,17 @@ private synchronized void warmUp() { startKey = region.getEndKey(); } while (!startKey.isEmpty()); - RawKVClient rawKVClient = createRawClient(); + SmartRawKVClient smartRawClient = createSmartRawClient(); ByteString exampleKey = ByteString.EMPTY; - Optional prev = rawKVClient.get(exampleKey); + Optional prev = smartRawClient.get(exampleKey); if (prev.isPresent()) { - rawKVClient.delete(exampleKey); - rawKVClient.putIfAbsent(exampleKey, prev.get()); - rawKVClient.put(exampleKey, prev.get()); + smartRawClient.delete(exampleKey); + smartRawClient.putIfAbsent(exampleKey, prev.get()); + smartRawClient.put(exampleKey, prev.get()); } else { - rawKVClient.putIfAbsent(exampleKey, ByteString.EMPTY); - rawKVClient.put(exampleKey, ByteString.EMPTY); - rawKVClient.delete(exampleKey); + smartRawClient.putIfAbsent(exampleKey, ByteString.EMPTY); + smartRawClient.put(exampleKey, ByteString.EMPTY); + smartRawClient.delete(exampleKey); } } diff --git a/src/main/java/org/tikv/raw/SmartRawKVClient.java b/src/main/java/org/tikv/raw/SmartRawKVClient.java index a9fe1fa58a3..51cfa02225e 100644 --- a/src/main/java/org/tikv/raw/SmartRawKVClient.java +++ b/src/main/java/org/tikv/raw/SmartRawKVClient.java @@ -35,7 +35,6 @@ public class SmartRawKVClient implements RawKVClientBase { private static final Logger logger = LoggerFactory.getLogger(SmartRawKVClient.class); - private static final AtomicBoolean warmed = new AtomicBoolean(false); private static final Histogram REQUEST_LATENCY = Histogram.build() @@ -69,15 +68,6 @@ public class SmartRawKVClient implements RawKVClientBase { private final CircuitBreaker circuitBreaker; public SmartRawKVClient(RawKVClientBase client, TiConfiguration conf) { - // Warm up SmartRawKVClient to avoid the first slow call. - if (warmed.compareAndSet(false, true)) { - try { - logger.info("Warming up SmartRawKVClient"); - client.get(ByteString.EMPTY); - } catch (final TiKVException ignored) { - } - } - this.client = client; this.circuitBreaker = new CircuitBreakerImpl(conf); } From 74c9339d0888c70c4ebe3efbd157a87bbcaa5aec Mon Sep 17 00:00:00 2001 From: birdstorm Date: Fri, 10 Dec 2021 02:48:51 +0800 Subject: [PATCH 6/9] format Signed-off-by: birdstorm --- src/main/java/org/tikv/common/TiSession.java | 1 - src/main/java/org/tikv/raw/SmartRawKVClient.java | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/main/java/org/tikv/common/TiSession.java b/src/main/java/org/tikv/common/TiSession.java index a7e2fc81591..76267d4c2a0 100644 --- a/src/main/java/org/tikv/common/TiSession.java +++ b/src/main/java/org/tikv/common/TiSession.java @@ -20,7 +20,6 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.google.protobuf.ByteString; - import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; diff --git a/src/main/java/org/tikv/raw/SmartRawKVClient.java b/src/main/java/org/tikv/raw/SmartRawKVClient.java index 51cfa02225e..33a8981e81b 100644 --- a/src/main/java/org/tikv/raw/SmartRawKVClient.java +++ b/src/main/java/org/tikv/raw/SmartRawKVClient.java @@ -21,12 +21,10 @@ import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.concurrent.atomic.AtomicBoolean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.tikv.common.TiConfiguration; import org.tikv.common.exception.CircuitBreakerOpenException; -import org.tikv.common.exception.TiKVException; import org.tikv.common.util.Pair; import org.tikv.common.util.ScanOption; import org.tikv.kvproto.Kvrpcpb; From a2ac28caec5c23c29700911d7d8debc3c8ad16ba Mon Sep 17 00:00:00 2001 From: birdstorm Date: Fri, 10 Dec 2021 03:40:21 +0800 Subject: [PATCH 7/9] resolve comments Signed-off-by: birdstorm --- src/main/java/org/tikv/common/TiSession.java | 60 +++++++++++--------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/tikv/common/TiSession.java b/src/main/java/org/tikv/common/TiSession.java index 76267d4c2a0..0016e50d75d 100644 --- a/src/main/java/org/tikv/common/TiSession.java +++ b/src/main/java/org/tikv/common/TiSession.java @@ -98,33 +98,39 @@ public TiSession(TiConfiguration conf) { } private synchronized void warmUp() { - this.client = getPDClient(); - this.regionManager = getRegionManager(); - List stores = this.client.getAllStores(ConcreteBackOffer.newGetBackOff()); - logger.info("number of stores=" + stores.size()); - // 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()); - - SmartRawKVClient smartRawClient = createSmartRawClient(); - ByteString exampleKey = ByteString.EMPTY; - Optional prev = smartRawClient.get(exampleKey); - if (prev.isPresent()) { - smartRawClient.delete(exampleKey); - smartRawClient.putIfAbsent(exampleKey, prev.get()); - smartRawClient.put(exampleKey, prev.get()); - } else { - smartRawClient.putIfAbsent(exampleKey, ByteString.EMPTY); - smartRawClient.put(exampleKey, ByteString.EMPTY); - smartRawClient.delete(exampleKey); + try { + this.client = getPDClient(); + this.regionManager = getRegionManager(); + List stores = this.client.getAllStores(ConcreteBackOffer.newGetBackOff()); + logger.info("number of stores=" + stores.size()); + // 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; + Optional prev = rawKVClient.get(exampleKey); + if (prev.isPresent()) { + rawKVClient.delete(exampleKey); + rawKVClient.putIfAbsent(exampleKey, prev.get()); + rawKVClient.put(exampleKey, prev.get()); + } else { + rawKVClient.putIfAbsent(exampleKey, ByteString.EMPTY); + rawKVClient.put(exampleKey, ByteString.EMPTY); + rawKVClient.delete(exampleKey); + } + } catch (Exception e) { + // ignore error + logger.info("warm up fails, ignored", e); } } From c01265932c302d5580243f8661e8b2eb5ffcfec2 Mon Sep 17 00:00:00 2001 From: birdstorm Date: Fri, 10 Dec 2021 03:43:37 +0800 Subject: [PATCH 8/9] add logs Signed-off-by: birdstorm --- src/main/java/org/tikv/common/TiSession.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/tikv/common/TiSession.java b/src/main/java/org/tikv/common/TiSession.java index 0016e50d75d..6b3298ea40b 100644 --- a/src/main/java/org/tikv/common/TiSession.java +++ b/src/main/java/org/tikv/common/TiSession.java @@ -98,11 +98,11 @@ public TiSession(TiConfiguration conf) { } private synchronized void warmUp() { + long warmUpStartTime = System.currentTimeMillis(); try { this.client = getPDClient(); this.regionManager = getRegionManager(); List stores = this.client.getAllStores(ConcreteBackOffer.newGetBackOff()); - logger.info("number of stores=" + stores.size()); // warm up store cache for (Metapb.Store store : stores) { this.regionManager.updateStore( @@ -130,7 +130,10 @@ private synchronized void warmUp() { } } catch (Exception e) { // ignore error - logger.info("warm up fails, ignored", e); + logger.info("warm up fails, ignored ", e); + } finally { + logger.info( + String.format("warm up duration %d ms", System.currentTimeMillis() - warmUpStartTime)); } } From 2ed1fe80db3eb66ee117ab2614111cd2c60e5154 Mon Sep 17 00:00:00 2001 From: birdstorm Date: Fri, 10 Dec 2021 03:45:23 +0800 Subject: [PATCH 9/9] remove test Signed-off-by: birdstorm --- src/test/java/org/tikv/raw/RawKVClientTest.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/test/java/org/tikv/raw/RawKVClientTest.java b/src/test/java/org/tikv/raw/RawKVClientTest.java index 3c497e36193..9e8884494c9 100644 --- a/src/test/java/org/tikv/raw/RawKVClientTest.java +++ b/src/test/java/org/tikv/raw/RawKVClientTest.java @@ -98,12 +98,6 @@ public void tearDown() throws Exception { } } - @Test - public void testInitialize() { - // do nothing - logger.info("init done"); - } - @Test public void getKeyTTLTest() { long ttl = 10;