fix sync order in RdbMirrorDbSyncService (#3928)

This commit is contained in:
He Wang
2022-05-13 14:19:40 +08:00
committed by GitHub
parent 67e85d201c
commit bd1f91cd9c
2 changed files with 55 additions and 48 deletions

View File

@@ -17,7 +17,6 @@ import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter.Feature; import com.alibaba.fastjson2.JSONWriter.Feature;
import com.alibaba.otter.canal.client.adapter.rdb.config.MappingConfig; import com.alibaba.otter.canal.client.adapter.rdb.config.MappingConfig;
import com.alibaba.otter.canal.client.adapter.rdb.config.MirrorDbConfig; import com.alibaba.otter.canal.client.adapter.rdb.config.MirrorDbConfig;
import com.alibaba.otter.canal.client.adapter.rdb.support.SingleDml;
import com.alibaba.otter.canal.client.adapter.support.Dml; import com.alibaba.otter.canal.client.adapter.support.Dml;
/** /**
@@ -45,7 +44,7 @@ public class RdbMirrorDbSyncService {
/** /**
* 批量同步方法 * 批量同步方法
* *
* @param dmls 批量 DML * @param dmls 批量 DML,包含DDL
*/ */
public void sync(List<Dml> dmls) { public void sync(List<Dml> dmls) {
List<Dml> dmlList = new ArrayList<>(); List<Dml> dmlList = new ArrayList<>();
@@ -66,6 +65,10 @@ public class RdbMirrorDbSyncService {
} }
if (dml.getIsDdl() != null && dml.getIsDdl() && StringUtils.isNotEmpty(dml.getSql())) { if (dml.getIsDdl() != null && dml.getIsDdl() && StringUtils.isNotEmpty(dml.getSql())) {
// 确保执行DDL前DML已执行完
syncDml(dmlList);
dmlList.clear();
// DDL // DDL
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("DDL: {}", JSON.toJSONString(dml, Feature.WriteNulls)); logger.debug("DDL: {}", JSON.toJSONString(dml, Feature.WriteNulls));
@@ -79,7 +82,18 @@ public class RdbMirrorDbSyncService {
dmlList.add(dml); dmlList.add(dml);
} }
} }
if (!dmlList.isEmpty()) { syncDml(dmlList);
}
/**
* 批量同步Dml
*
* @param dmlList Dml列表,不包含DDL
*/
private void syncDml(List<Dml> dmlList) {
if (dmlList == null || dmlList.isEmpty()) {
return;
}
rdbSyncService.sync(dmlList, dml -> { rdbSyncService.sync(dmlList, dml -> {
MirrorDbConfig mirrorDbConfig = mirrorDbConfigCache.get(dml.getDestination() + "." + dml.getDatabase()); MirrorDbConfig mirrorDbConfig = mirrorDbConfigCache.get(dml.getDestination() + "." + dml.getDatabase());
if (mirrorDbConfig == null) { if (mirrorDbConfig == null) {
@@ -91,27 +105,10 @@ public class RdbMirrorDbSyncService {
if (config == null) { if (config == null) {
return false; return false;
} }
// 是否区分大小写 rdbSyncService.appendDmlPartition(config, dml);
boolean caseInsensitive = config.getDbMapping().isCaseInsensitive();
if (config.getConcurrent()) {
List<SingleDml> singleDmls = SingleDml.dml2SingleDmls(dml, caseInsensitive);
singleDmls.forEach(singleDml -> {
int hash = rdbSyncService.pkHash(config.getDbMapping(), singleDml.getData());
RdbSyncService.SyncItem syncItem = new RdbSyncService.SyncItem(config, singleDml);
rdbSyncService.getDmlsPartition()[hash].add(syncItem);
});
} else {
int hash = 0;
List<SingleDml> singleDmls = SingleDml.dml2SingleDmls(dml, caseInsensitive);
singleDmls.forEach(singleDml -> {
RdbSyncService.SyncItem syncItem = new RdbSyncService.SyncItem(config, singleDml);
rdbSyncService.getDmlsPartition()[hash].add(syncItem);
});
}
return true; return true;
}); });
} }
}
/** /**
* 初始化表配置 * 初始化表配置

View File

@@ -177,6 +177,20 @@ public class RdbSyncService {
} }
for (MappingConfig config : configMap.values()) { for (MappingConfig config : configMap.values()) {
appendDmlPartition(config, dml);
}
return true;
}
} );
}
/**
* 将Dml加入 {@link #dmlsPartition}
*
* @param config 表映射配置
* @param dml Dml对象
*/
public void appendDmlPartition(MappingConfig config, Dml dml) {
boolean caseInsensitive = config.getDbMapping().isCaseInsensitive(); boolean caseInsensitive = config.getDbMapping().isCaseInsensitive();
if (config.getConcurrent()) { if (config.getConcurrent()) {
List<SingleDml> singleDmls = SingleDml.dml2SingleDmls(dml, caseInsensitive); List<SingleDml> singleDmls = SingleDml.dml2SingleDmls(dml, caseInsensitive);
@@ -194,10 +208,6 @@ public class RdbSyncService {
}); });
} }
} }
return true;
}
} );
}
/** /**
* 单条 dml 同步 * 单条 dml 同步