diff --git a/src/main/java/org/tikv/common/TiSession.java b/src/main/java/org/tikv/common/TiSession.java index 87410eab637..f8ad6eba08e 100644 --- a/src/main/java/org/tikv/common/TiSession.java +++ b/src/main/java/org/tikv/common/TiSession.java @@ -71,6 +71,7 @@ public class TiSession implements AutoCloseable { private volatile RegionStoreClient.RegionStoreClientBuilder clientBuilder; private volatile ImporterStoreClient.ImporterStoreClientBuilder importerClientBuilder; private volatile boolean isClosed = false; + private volatile SwitchTiKVModeClient switchTiKVModeClient; private MetricsServer metricsServer; private static final int MAX_SPLIT_REGION_STACK_DEPTH = 6; @@ -387,7 +388,16 @@ public ChannelFactory getChannelFactory() { public SwitchTiKVModeClient getSwitchTiKVModeClient() { checkIsClosed(); - return new SwitchTiKVModeClient(getPDClient(), getImporterRegionStoreClientBuilder()); + SwitchTiKVModeClient res = switchTiKVModeClient; + if (res == null) { + synchronized (this) { + if (switchTiKVModeClient == null) { + switchTiKVModeClient = new SwitchTiKVModeClient(getPDClient(), getImporterRegionStoreClientBuilder()); + } + res = switchTiKVModeClient; + } + } + return res; } /** @@ -578,6 +588,10 @@ private synchronized void cleanAfterTerminated() throws InterruptedException { if (catalog != null) { catalog.close(); } + + if(switchTiKVModeClient != null) { + switchTiKVModeClient.stopKeepTiKVToImportMode(); + } } private List getExecutorServices() {