From aafd0ba0382492bfb5fc5869db46d8bfe87b9449 Mon Sep 17 00:00:00 2001 From: avalon566 Date: Thu, 14 Nov 2019 00:50:19 +0800 Subject: [PATCH] Refactor SyncTaskController --- .../core/config/SyncConfiguration.java | 4 + .../core/job/ReportCallback.java | 35 ++++++++ .../core/job/SyncTaskController.java | 85 ++++++++++--------- .../core/job/sync/HistoryDataSyncTask.java | 4 +- .../core/job/sync/RealtimeDataSyncTask.java | 6 +- .../core/job/sync/executor/Event.java | 2 + .../job/sync/executor/SyncJobExecutor.java | 4 +- .../executor/local/LocalSyncJobExecutor.java | 26 +++++- 8 files changed, 119 insertions(+), 47 deletions(-) create mode 100644 sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/ReportCallback.java diff --git a/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/config/SyncConfiguration.java b/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/config/SyncConfiguration.java index 4e40667bed275..abde35b9175d6 100755 --- a/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/config/SyncConfiguration.java +++ b/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/config/SyncConfiguration.java @@ -21,6 +21,8 @@ import lombok.Data; import lombok.RequiredArgsConstructor; +import java.util.UUID; + /** * Sync configuration. * @@ -30,6 +32,8 @@ @RequiredArgsConstructor public class SyncConfiguration { + private final String taskId = UUID.randomUUID().toString(); + private final SyncType syncType; /** diff --git a/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/ReportCallback.java b/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/ReportCallback.java new file mode 100644 index 0000000000000..8b186da4a51bd --- /dev/null +++ b/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/ReportCallback.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package info.avalon566.shardingscaling.core.job; + +import info.avalon566.shardingscaling.core.job.sync.executor.Event; + +/** + * Report callback. + * + * @author avalon566 + */ +public interface ReportCallback { + + /** + * process report event. + * + * @param event report event + */ + void onProcess(Event event); +} diff --git a/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/SyncTaskController.java b/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/SyncTaskController.java index 85f95b59eb844..30d5cc4e02b60 100644 --- a/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/SyncTaskController.java +++ b/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/SyncTaskController.java @@ -20,12 +20,11 @@ import info.avalon566.shardingscaling.core.config.RdbmsConfiguration; import info.avalon566.shardingscaling.core.config.SyncConfiguration; import info.avalon566.shardingscaling.core.config.SyncType; +import info.avalon566.shardingscaling.core.job.sync.RealtimeDataSyncTask; import info.avalon566.shardingscaling.core.job.sync.executor.Event; import info.avalon566.shardingscaling.core.job.sync.executor.EventType; -import info.avalon566.shardingscaling.core.job.sync.executor.Reporter; import info.avalon566.shardingscaling.core.job.sync.executor.SyncJobExecutor; import info.avalon566.shardingscaling.core.job.sync.executor.local.LocalSyncJobExecutor; -import info.avalon566.shardingscaling.core.job.sync.RealtimeDataSyncTask; import info.avalon566.shardingscaling.core.sync.reader.LogPosition; import info.avalon566.shardingscaling.core.sync.reader.ReaderFactory; import info.avalon566.shardingscaling.core.sync.util.DataSourceFactory; @@ -35,7 +34,9 @@ import javax.sql.DataSource; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * Sync task controller, synchronize history data and realtime data. @@ -43,12 +44,22 @@ * @author avalon566 */ @Slf4j -public final class SyncTaskController implements Runnable { +public final class SyncTaskController implements ReportCallback, Runnable { + + private static final String STAGE_SYNC_HISTORY_DATA = "SYNC_HISTORY_DATA"; + + private static final String STAGE_SYNC_REALTIME_DATA = "SYNC_REALTIME_DATA"; private final SyncJobExecutor syncJobExecutor = new LocalSyncJobExecutor(); private final SyncConfiguration syncConfiguration; + private final Map migrateProgresses = new HashMap<>(); + + private LogPosition startLogPosition; + + private String stage = STAGE_SYNC_HISTORY_DATA; + public SyncTaskController(final SyncConfiguration syncConfiguration) { this.syncConfiguration = syncConfiguration; } @@ -82,15 +93,40 @@ public SyncTaskProgress getProgress() { @Override public void run() { - LogPosition position = new RealtimeDataSyncTask(syncConfiguration, null).preRun(); + startLogPosition = new RealtimeDataSyncTask(syncConfiguration, null).preRun(); syncHistoryData(); - syncRealtimeData(position); + } + + @Override + public void onProcess(final Event event) { + migrateProgresses.put(event.getTaskId(), event); + if (EventType.FINISHED == event.getEventType()) { + boolean finished = true; + for (Object each : migrateProgresses.values()) { + if (null == each || EventType.FINISHED != ((Event) each).getEventType()) { + finished = false; + } + } + if (finished) { + log.info("data sync finish"); + if (STAGE_SYNC_HISTORY_DATA.equals(stage)) { + stage = STAGE_SYNC_REALTIME_DATA; + syncRealtimeData(startLogPosition); + } + } + } + if (EventType.EXCEPTION_EXIT == event.getEventType()) { + System.exit(1); + } } private void syncHistoryData() { List configs = split(syncConfiguration); - Reporter reporter = syncJobExecutor.start(configs); - waitSlicesFinished(configs, reporter); + migrateProgresses.clear(); + for (SyncConfiguration each : configs) { + migrateProgresses.put(each.getTaskId(), null); + } + syncJobExecutor.start(configs, this); } private List split(final SyncConfiguration syncConfiguration) { @@ -109,25 +145,6 @@ private List split(final SyncConfiguration syncConfiguration) return syncConfigurations; } - private void waitSlicesFinished(final List syncConfigurations, final Reporter reporter) { - int counter = 0; - boolean hasException = false; - while (true) { - Event event = reporter.consumeEvent(); - if (EventType.FINISHED == event.getEventType()) { - counter++; - } - if (EventType.EXCEPTION_EXIT == event.getEventType()) { - hasException = true; - System.exit(1); - } - if (syncConfigurations.size() == counter) { - log.info("history data sync finish"); - break; - } - } - } - private void syncRealtimeData(final LogPosition position) { syncConfiguration.setPosition(position); SyncConfiguration realConfiguration = new SyncConfiguration( @@ -135,18 +152,8 @@ private void syncRealtimeData(final LogPosition position) { syncConfiguration.getReaderConfiguration(), syncConfiguration.getWriterConfiguration()); realConfiguration.setPosition(position); - Reporter realtimeReporter = syncJobExecutor.start(Collections.singletonList(realConfiguration)); - while (true) { - Event event = realtimeReporter.consumeEvent(); - if (EventType.FINISHED == event.getEventType()) { - return; - } - if (EventType.EXCEPTION_EXIT == event.getEventType()) { - System.exit(1); - } - if (EventType.REALTIME_SYNC_POSITION == event.getEventType()) { - - } - } + migrateProgresses.clear(); + migrateProgresses.put(syncConfiguration.getTaskId(), null); + syncJobExecutor.start(Collections.singletonList(realConfiguration), this); } } diff --git a/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/sync/HistoryDataSyncTask.java b/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/sync/HistoryDataSyncTask.java index 44600a2bd81c5..567adeeb14910 100644 --- a/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/sync/HistoryDataSyncTask.java +++ b/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/sync/HistoryDataSyncTask.java @@ -77,11 +77,11 @@ public void run() { try { new SyncExecutor(new MemoryChannel(), reader, Collections.singletonList(writer)).execute(); log.info("{} table slice sync finish", syncConfiguration.getReaderConfiguration().getTableName()); - reporter.report(new Event(EventType.FINISHED)); + reporter.report(new Event(syncConfiguration.getTaskId(), EventType.FINISHED)); } catch (SyncExecuteException ex) { log.error("{} table slice sync exception exit", syncConfiguration.getReaderConfiguration().getTableName()); ex.logExceptions(); - reporter.report(new Event(EventType.EXCEPTION_EXIT)); + reporter.report(new Event(syncConfiguration.getTaskId(), EventType.EXCEPTION_EXIT)); } } } diff --git a/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/sync/RealtimeDataSyncTask.java b/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/sync/RealtimeDataSyncTask.java index 5c9029370a07a..1f1d9e7f79f7f 100644 --- a/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/sync/RealtimeDataSyncTask.java +++ b/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/sync/RealtimeDataSyncTask.java @@ -106,11 +106,11 @@ public void onAck(final List records) { startReportRealtimeSyncPosition(); new SyncExecutor(channel, logReader, writers).execute(); log.info("realtime data sync finish"); - reporter.report(new Event(EventType.FINISHED)); + reporter.report(new Event(syncConfiguration.getTaskId(), EventType.FINISHED)); } catch (SyncExecuteException ex) { log.error("realtime data sync exception exit"); ex.logExceptions(); - reporter.report(new Event(EventType.EXCEPTION_EXIT)); + reporter.report(new Event(syncConfiguration.getTaskId(), EventType.EXCEPTION_EXIT)); } } @@ -128,7 +128,7 @@ public void run() { } if (null == lastLogPosition || -1 == lastLogPosition.compareTo(currentLogPosition)) { lastLogPosition = currentLogPosition; - Event event = new Event(EventType.REALTIME_SYNC_POSITION); + Event event = new Event(syncConfiguration.getTaskId(), EventType.REALTIME_SYNC_POSITION); event.setPayload(lastLogPosition); reporter.report(event); } diff --git a/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/sync/executor/Event.java b/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/sync/executor/Event.java index 50d0777f80e29..93837c98744ce 100644 --- a/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/sync/executor/Event.java +++ b/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/sync/executor/Event.java @@ -28,6 +28,8 @@ @RequiredArgsConstructor public class Event { + private final String taskId; + private final EventType eventType; private Object payload; diff --git a/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/sync/executor/SyncJobExecutor.java b/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/sync/executor/SyncJobExecutor.java index 40e78bc261da5..8e31fb4590d72 100644 --- a/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/sync/executor/SyncJobExecutor.java +++ b/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/sync/executor/SyncJobExecutor.java @@ -18,6 +18,7 @@ package info.avalon566.shardingscaling.core.job.sync.executor; import info.avalon566.shardingscaling.core.config.SyncConfiguration; +import info.avalon566.shardingscaling.core.job.ReportCallback; import info.avalon566.shardingscaling.core.job.SyncTaskProgress; import java.util.List; @@ -32,9 +33,10 @@ public interface SyncJobExecutor { * start sync jobs. * * @param configs job configs + * @param reportCallback report callback * @return reporter */ - Reporter start(List configs); + Reporter start(List configs, ReportCallback reportCallback); /** * stop all sync jobs. diff --git a/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/sync/executor/local/LocalSyncJobExecutor.java b/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/sync/executor/local/LocalSyncJobExecutor.java index 475d906175470..7a02da59df52b 100644 --- a/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/sync/executor/local/LocalSyncJobExecutor.java +++ b/sharding-scaling-core/src/main/java/info/avalon566/shardingscaling/core/job/sync/executor/local/LocalSyncJobExecutor.java @@ -18,14 +18,18 @@ package info.avalon566.shardingscaling.core.job.sync.executor.local; import info.avalon566.shardingscaling.core.config.SyncConfiguration; +import info.avalon566.shardingscaling.core.job.ReportCallback; import info.avalon566.shardingscaling.core.job.SyncTaskProgress; import info.avalon566.shardingscaling.core.job.sync.SyncTask; +import info.avalon566.shardingscaling.core.job.sync.executor.Event; import info.avalon566.shardingscaling.core.job.sync.executor.Reporter; import info.avalon566.shardingscaling.core.job.sync.executor.SyncJobExecutor; import info.avalon566.shardingscaling.core.job.sync.SyncTaskFactory; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; /** * Local sync job executor. @@ -36,11 +40,29 @@ public class LocalSyncJobExecutor implements SyncJobExecutor { private List syncTasks; + private final LocalReporter reporter = new LocalReporter(); + + private final Map reportCallbackMap = new ConcurrentHashMap<>(); + + public LocalSyncJobExecutor() { + new Thread(new Runnable() { + @Override + public void run() { + while (true) { + Event event = reporter.consumeEvent(); + if (null != event) { + reportCallbackMap.get(event.getTaskId()).onProcess(event); + } + } + } + }).start(); + } + @Override - public final Reporter start(final List syncConfigurations) { - LocalReporter reporter = new LocalReporter(); + public final Reporter start(final List syncConfigurations, final ReportCallback reportCallback) { syncTasks = new ArrayList<>(syncConfigurations.size()); for (SyncConfiguration syncConfiguration : syncConfigurations) { + reportCallbackMap.put(syncConfiguration.getTaskId(), reportCallback); SyncTask syncTask = SyncTaskFactory.createSyncJobInstance(syncConfiguration, reporter); syncTask.start(); syncTasks.add(syncTask);