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
30 changes: 17 additions & 13 deletions src/main/java/org/tikv/common/importer/SwitchTiKVModeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,36 @@ public class SwitchTiKVModeClient {
private final PDClient pdClient;
private final ImporterStoreClient.ImporterStoreClientBuilder builder;

private final ScheduledExecutorService ingestScheduledExecutorService;
private ScheduledExecutorService ingestScheduledExecutorService;

public SwitchTiKVModeClient(
PDClient pdClient, ImporterStoreClient.ImporterStoreClientBuilder builder) {
this.pdClient = pdClient;
this.builder = builder;

this.ingestScheduledExecutorService =
Executors.newSingleThreadScheduledExecutor(
new ThreadFactoryBuilder()
.setNameFormat("switch-tikv-mode-pool-%d")
.setDaemon(true)
.build());
}

public void switchTiKVToNormalMode() {
doSwitchTiKVMode(ImportSstpb.SwitchMode.Normal);
}

public void keepTiKVToImportMode() {
ingestScheduledExecutorService.scheduleAtFixedRate(
this::switchTiKVToImportMode, 0, KEEP_TIKV_TO_IMPORT_MODE_PERIOD, TimeUnit.SECONDS);
public synchronized void keepTiKVToImportMode() {
if (ingestScheduledExecutorService == null) {
ingestScheduledExecutorService =
Executors.newSingleThreadScheduledExecutor(
new ThreadFactoryBuilder()
.setNameFormat("switch-tikv-mode-pool-%d")
.setDaemon(true)
.build());
ingestScheduledExecutorService.scheduleAtFixedRate(
this::switchTiKVToImportMode, 0, KEEP_TIKV_TO_IMPORT_MODE_PERIOD, TimeUnit.SECONDS);
}
}

public void stopKeepTiKVToImportMode() {
ingestScheduledExecutorService.shutdown();
public synchronized void stopKeepTiKVToImportMode() {
if (ingestScheduledExecutorService != null) {
ingestScheduledExecutorService.shutdown();
ingestScheduledExecutorService = null;
}
}

private void switchTiKVToImportMode() {
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/org/tikv/common/importer/SwitchTiKVModeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ public void tearDown() throws Exception {
@Test
public void switchTiKVModeTest() throws InterruptedException {
SwitchTiKVModeClient switchTiKVModeClient = session.getSwitchTiKVModeClient();
switchTiKVModeClient.keepTiKVToImportMode();
Thread.sleep(6000);
switchTiKVModeClient.stopKeepTiKVToImportMode();
switchTiKVModeClient.switchTiKVToNormalMode();
for (int i = 0; i < 2; i++) {
switchTiKVModeClient.keepTiKVToImportMode();
Thread.sleep(6000);
switchTiKVModeClient.stopKeepTiKVToImportMode();
switchTiKVModeClient.switchTiKVToNormalMode();
}
}
}