mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
修改rdb批量提交分批的问题
This commit is contained in:
@@ -1,22 +0,0 @@
|
|||||||
package com.alibaba.otter.canal.adapter.launcher.config;
|
|
||||||
|
|
||||||
import org.springframework.cloud.context.refresh.ContextRefresher;
|
|
||||||
import org.springframework.cloud.context.scope.refresh.RefreshScope;
|
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public class RefresherConfig {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public RefreshScope refreshScope() {
|
|
||||||
return new RefreshScope();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public ContextRefresher contextRefresher(ConfigurableApplicationContext configurableApplicationContext,
|
|
||||||
RefreshScope refreshScope) {
|
|
||||||
return new ContextRefresher(configurableApplicationContext, refreshScope);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -5,6 +5,8 @@ import java.sql.SQLException;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.locks.Condition;
|
||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
@@ -20,6 +22,7 @@ import com.alibaba.fastjson.serializer.SerializerFeature;
|
|||||||
import com.alibaba.otter.canal.client.adapter.OuterAdapter;
|
import com.alibaba.otter.canal.client.adapter.OuterAdapter;
|
||||||
import com.alibaba.otter.canal.client.adapter.rdb.config.ConfigLoader;
|
import com.alibaba.otter.canal.client.adapter.rdb.config.ConfigLoader;
|
||||||
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.monitor.RdbConfigMonitor;
|
||||||
import com.alibaba.otter.canal.client.adapter.rdb.service.RdbEtlService;
|
import com.alibaba.otter.canal.client.adapter.rdb.service.RdbEtlService;
|
||||||
import com.alibaba.otter.canal.client.adapter.rdb.service.RdbSyncService;
|
import com.alibaba.otter.canal.client.adapter.rdb.service.RdbSyncService;
|
||||||
import com.alibaba.otter.canal.client.adapter.rdb.support.SimpleDml;
|
import com.alibaba.otter.canal.client.adapter.rdb.support.SimpleDml;
|
||||||
@@ -44,8 +47,11 @@ public class RdbAdapter implements OuterAdapter {
|
|||||||
private List<SimpleDml> dmlList = Collections
|
private List<SimpleDml> dmlList = Collections
|
||||||
.synchronizedList(new ArrayList<>());
|
.synchronizedList(new ArrayList<>());
|
||||||
private Lock syncLock = new ReentrantLock();
|
private Lock syncLock = new ReentrantLock();
|
||||||
|
private Condition condition = syncLock.newCondition();
|
||||||
private ExecutorService executor = Executors.newFixedThreadPool(1);
|
private ExecutorService executor = Executors.newFixedThreadPool(1);
|
||||||
|
|
||||||
|
private RdbConfigMonitor rdbConfigMonitor;
|
||||||
|
|
||||||
public Map<String, MappingConfig> getRdbMapping() {
|
public Map<String, MappingConfig> getRdbMapping() {
|
||||||
return rdbMapping;
|
return rdbMapping;
|
||||||
}
|
}
|
||||||
@@ -107,18 +113,21 @@ public class RdbAdapter implements OuterAdapter {
|
|||||||
executor.submit(() -> {
|
executor.submit(() -> {
|
||||||
while (running) {
|
while (running) {
|
||||||
try {
|
try {
|
||||||
int size1 = dmlList.size();
|
syncLock.lock();
|
||||||
Thread.sleep(3000);
|
if (!condition.await(3, TimeUnit.SECONDS)) {
|
||||||
int size2 = dmlList.size();
|
|
||||||
if (size1 == size2) {
|
|
||||||
// 超时提交
|
// 超时提交
|
||||||
sync();
|
sync();
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error(e.getMessage(), e);
|
logger.error(e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
syncLock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
rdbConfigMonitor = new RdbConfigMonitor();
|
||||||
|
rdbConfigMonitor.init(configuration.getKey(), this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -131,10 +140,9 @@ public class RdbAdapter implements OuterAdapter {
|
|||||||
if (configMap != null) {
|
if (configMap != null) {
|
||||||
configMap.values().forEach(config -> {
|
configMap.values().forEach(config -> {
|
||||||
List<SimpleDml> simpleDmlList = SimpleDml.dml2SimpleDml(dml, config);
|
List<SimpleDml> simpleDmlList = SimpleDml.dml2SimpleDml(dml, config);
|
||||||
|
|
||||||
dmlList.addAll(simpleDmlList);
|
dmlList.addAll(simpleDmlList);
|
||||||
|
|
||||||
if (dmlList.size() > commitSize) {
|
if (dmlList.size() >= commitSize) {
|
||||||
sync();
|
sync();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -148,6 +156,7 @@ public class RdbAdapter implements OuterAdapter {
|
|||||||
try {
|
try {
|
||||||
syncLock.lock();
|
syncLock.lock();
|
||||||
if (!dmlList.isEmpty()) {
|
if (!dmlList.isEmpty()) {
|
||||||
|
condition.signal();
|
||||||
rdbSyncService.sync(dmlList);
|
rdbSyncService.sync(dmlList);
|
||||||
dmlList.clear();
|
dmlList.clear();
|
||||||
}
|
}
|
||||||
@@ -251,6 +260,10 @@ public class RdbAdapter implements OuterAdapter {
|
|||||||
@Override
|
@Override
|
||||||
public void destroy() {
|
public void destroy() {
|
||||||
running = false;
|
running = false;
|
||||||
|
if (rdbConfigMonitor != null) {
|
||||||
|
rdbConfigMonitor.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
executor.shutdown();
|
executor.shutdown();
|
||||||
|
|
||||||
if (rdbSyncService != null) {
|
if (rdbSyncService != null) {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import java.util.LinkedHashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user