mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
a little improvement for DatabaseTableMeta
This commit is contained in:
@@ -10,6 +10,8 @@ import java.util.concurrent.ScheduledExecutorService;
|
|||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.ThreadFactory;
|
import java.util.concurrent.ThreadFactory;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.locks.ReadWriteLock;
|
||||||
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.apache.commons.beanutils.BeanUtils;
|
import org.apache.commons.beanutils.BeanUtils;
|
||||||
@@ -57,6 +59,7 @@ public class DatabaseTableMeta implements TableMetaTSDB {
|
|||||||
return thread;
|
return thread;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
private ReadWriteLock lock = new ReentrantReadWriteLock();
|
||||||
private String destination;
|
private String destination;
|
||||||
private MemoryTableMeta memoryTableMeta;
|
private MemoryTableMeta memoryTableMeta;
|
||||||
private MysqlConnection connection; // 查询meta信息的链接
|
private MysqlConnection connection; // 查询meta信息的链接
|
||||||
@@ -129,15 +132,19 @@ public class DatabaseTableMeta implements TableMetaTSDB {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TableMeta find(String schema, String table) {
|
public TableMeta find(String schema, String table) {
|
||||||
synchronized (memoryTableMeta) {
|
lock.readLock().lock();
|
||||||
|
try{
|
||||||
return memoryTableMeta.find(schema, table);
|
return memoryTableMeta.find(schema, table);
|
||||||
|
}finally {
|
||||||
|
lock.readLock().unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean apply(EntryPosition position, String schema, String ddl, String extra) {
|
public boolean apply(EntryPosition position, String schema, String ddl, String extra) {
|
||||||
// 首先记录到内存结构
|
// 首先记录到内存结构
|
||||||
synchronized (memoryTableMeta) {
|
lock.writeLock().lock();
|
||||||
|
try{
|
||||||
if (memoryTableMeta.apply(position, schema, ddl, extra)) {
|
if (memoryTableMeta.apply(position, schema, ddl, extra)) {
|
||||||
this.lastPosition = position;
|
this.lastPosition = position;
|
||||||
this.hasNewDdl = true;
|
this.hasNewDdl = true;
|
||||||
@@ -146,6 +153,8 @@ public class DatabaseTableMeta implements TableMetaTSDB {
|
|||||||
} else {
|
} else {
|
||||||
throw new RuntimeException("apply to memory is failed");
|
throw new RuntimeException("apply to memory is failed");
|
||||||
}
|
}
|
||||||
|
}finally {
|
||||||
|
lock.writeLock().unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -269,18 +278,22 @@ public class DatabaseTableMeta implements TableMetaTSDB {
|
|||||||
*/
|
*/
|
||||||
private boolean applySnapshotToDB(EntryPosition position, boolean init) {
|
private boolean applySnapshotToDB(EntryPosition position, boolean init) {
|
||||||
// 获取一份快照
|
// 获取一份快照
|
||||||
MemoryTableMeta tmpMemoryTableMeta = new MemoryTableMeta();
|
|
||||||
Map<String, String> schemaDdls = null;
|
Map<String, String> schemaDdls = null;
|
||||||
synchronized (memoryTableMeta) {
|
lock.readLock().lock();
|
||||||
|
try{
|
||||||
if (!init && !hasNewDdl) {
|
if (!init && !hasNewDdl) {
|
||||||
// 如果是持续构建,则识别一下是否有DDL变更过,如果没有就忽略了
|
// 如果是持续构建,则识别一下是否有DDL变更过,如果没有就忽略了
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
this.hasNewDdl = false;
|
this.hasNewDdl = false;
|
||||||
schemaDdls = memoryTableMeta.snapshot();
|
schemaDdls = memoryTableMeta.snapshot();
|
||||||
for (Map.Entry<String, String> entry : schemaDdls.entrySet()) {
|
}finally{
|
||||||
tmpMemoryTableMeta.apply(position, entry.getKey(), entry.getValue(), null);
|
lock.readLock().unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MemoryTableMeta tmpMemoryTableMeta = new MemoryTableMeta();
|
||||||
|
for (Map.Entry<String, String> entry : schemaDdls.entrySet()) {
|
||||||
|
tmpMemoryTableMeta.apply(position, entry.getKey(), entry.getValue(), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 基于临时内存对象进行对比
|
// 基于临时内存对象进行对比
|
||||||
|
|||||||
Reference in New Issue
Block a user