|
|
@@ -5,6 +5,7 @@ import com.usoftchina.saas.account.cache.CompanyLockCache;
|
|
|
import com.usoftchina.saas.context.SpringContextHolder;
|
|
|
import com.usoftchina.saas.dc.po.DataSourceInfo;
|
|
|
import com.usoftchina.saas.dc.po.Snapshot;
|
|
|
+import com.usoftchina.saas.dc.po.SnapshotUsage;
|
|
|
import com.usoftchina.saas.dc.repository.SnapshotDataRepository;
|
|
|
import com.usoftchina.saas.dc.repository.SnapshotRepository;
|
|
|
import com.usoftchina.saas.dc.repository.SnapshotUsageRepository;
|
|
|
@@ -92,7 +93,6 @@ public class SnapshotLifecycle {
|
|
|
@EventListener(CreateFailedEvent.class)
|
|
|
public void onCreateFailedEvent(CreateFailedEvent event) {
|
|
|
Snapshot snapshot = event.getSnapshot();
|
|
|
- event.getException().printStackTrace();
|
|
|
logger.error("failed to create snapshot {}", event.getException(), snapshot.get_id());
|
|
|
// 解锁
|
|
|
CompanyLockCache.unlock(snapshot.getCompanyId());
|
|
|
@@ -111,4 +111,63 @@ public class SnapshotLifecycle {
|
|
|
snapshotUsageRepository.deleteByUsedSnapshotId(snapshot.get_id());
|
|
|
snapshotUsageRepository.deleteByOriginSnapshotId(snapshot.get_id());
|
|
|
}
|
|
|
+
|
|
|
+ @Async
|
|
|
+ @EventListener(ReadyToRestoreEvent.class)
|
|
|
+ public void onReadyToRestoreEvent(ReadyToRestoreEvent event) {
|
|
|
+ Snapshot snapshot = event.getSnapshot();
|
|
|
+ SnapshotUsage usage = event.getUsage();
|
|
|
+ try {
|
|
|
+ usage.setStatus(SnapshotUsage.Status.RESTORE);
|
|
|
+ snapshotUsageRepository.save(usage);
|
|
|
+ // 为防止导入过程中有人操作,导致部分数据不一致,在导出前锁定该公司不允许操作
|
|
|
+ CompanyLockCache.lock(usage.getCompanyId());
|
|
|
+ restoreSnapshotData(snapshot);
|
|
|
+ SpringContextHolder.getContext().publishEvent(new RestoredEvent(this, snapshot, usage));
|
|
|
+ } catch (Exception e) {
|
|
|
+ SpringContextHolder.getContext().publishEvent(new RestoreFailedEvent(this, snapshot, usage, e));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 连接到不同数据中心,查找数据,生成快照数据,删除,使用快照数据写入
|
|
|
+ *
|
|
|
+ * @param snapshot
|
|
|
+ */
|
|
|
+ private void restoreSnapshotData(Snapshot snapshot) {
|
|
|
+ String dcName = StringUtils.nullIf(CompanyCache.current().getCompany().getDcName(), "default");
|
|
|
+ List<DataSourceInfo> dss = dataSourceInfoService.findByDcNameUseDefault(dcName);
|
|
|
+ if (!CollectionUtils.isEmpty(dss)) {
|
|
|
+ dss.parallelStream().forEach(ds -> {
|
|
|
+ if ("mysql".equals(ds.getDbType())) {
|
|
|
+ mysqlStrategy.imp(snapshot, ds);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Async
|
|
|
+ @EventListener(RestoredEvent.class)
|
|
|
+ public void onRestoredEvent(RestoredEvent event) {
|
|
|
+ SnapshotUsage usage = event.getUsage();
|
|
|
+ // 解锁
|
|
|
+ CompanyLockCache.unlock(usage.getCompanyId());
|
|
|
+ usage.setStatus(SnapshotUsage.Status.SUCCESS);
|
|
|
+ snapshotUsageRepository.save(usage);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Async
|
|
|
+ @EventListener(RestoreFailedEvent.class)
|
|
|
+ public void onRestoreFailedEvent(RestoreFailedEvent event) {
|
|
|
+ Snapshot snapshot = event.getSnapshot();
|
|
|
+ SnapshotUsage usage = event.getUsage();
|
|
|
+ logger.error("failed to restore by snapshot {}", event.getException(), snapshot.get_id());
|
|
|
+ // 解锁
|
|
|
+ CompanyLockCache.unlock(usage.getCompanyId());
|
|
|
+ // 回滚
|
|
|
+ usage.setStatus(SnapshotUsage.Status.FAILED);
|
|
|
+ usage.setMessage(event.getException().getMessage());
|
|
|
+ snapshotUsageRepository.save(usage);
|
|
|
+ // TODO
|
|
|
+ }
|
|
|
}
|