Search in sources :

Example 1 with Schema

use of com.alibaba.druid.sql.repository.Schema in project canal by alibaba.

the class DatabaseTableMeta method applySnapshotToDB.

/**
 * 发布数据到console上
 */
private boolean applySnapshotToDB(EntryPosition position, boolean init) {
    // 获取一份快照
    Map<String, String> schemaDdls = null;
    lock.readLock().lock();
    try {
        if (!init && !hasNewDdl) {
            // 如果是持续构建,则识别一下是否有DDL变更过,如果没有就忽略了
            return false;
        }
        this.hasNewDdl = false;
        schemaDdls = memoryTableMeta.snapshot();
    } finally {
        lock.readLock().unlock();
    }
    MemoryTableMeta tmpMemoryTableMeta = new MemoryTableMeta();
    for (Map.Entry<String, String> entry : schemaDdls.entrySet()) {
        tmpMemoryTableMeta.apply(position, entry.getKey(), entry.getValue(), null);
    }
    // 基于临时内存对象进行对比
    boolean compareAll = true;
    for (Schema schema : tmpMemoryTableMeta.getRepository().getSchemas()) {
        for (String table : schema.showTables()) {
            String fullName = schema.getName() + "." + table;
            if (blackFilter == null || !blackFilter.filter(fullName)) {
                if (filter == null || filter.filter(fullName)) {
                    // 在生成snapshot时重新过滤一遍
                    if (!compareTableMetaDbAndMemory(connection, tmpMemoryTableMeta, schema.getName(), table)) {
                        compareAll = false;
                    }
                }
            }
        }
    }
    if (compareAll) {
        Map<String, String> content = new HashMap<>();
        content.put("destination", destination);
        content.put("binlogFile", position.getJournalName());
        content.put("binlogOffest", String.valueOf(position.getPosition()));
        content.put("binlogMasterId", String.valueOf(position.getServerId()));
        content.put("binlogTimestamp", String.valueOf(position.getTimestamp()));
        content.put("data", JSON.toJSONString(schemaDdls));
        if (content.isEmpty()) {
            throw new RuntimeException("apply failed caused by content is empty in applySnapshotToDB");
        }
        MetaSnapshotDO snapshotDO = new MetaSnapshotDO();
        try {
            BeanUtils.populate(snapshotDO, content);
            metaSnapshotDAO.insert(snapshotDO);
        } catch (Throwable e) {
            if (isUkDuplicateException(e)) {
                // 忽略掉重复的位点
                logger.info("dup apply snapshot use position : " + position + " , just ignore");
            } else {
                throw new CanalParseException("apply failed caused by : " + e.getMessage(), e);
            }
        }
        return true;
    } else {
        logger.error("compare failed , check log");
    }
    return false;
}
Also used : HashMap(java.util.HashMap) Schema(com.alibaba.druid.sql.repository.Schema) MetaSnapshotDO(com.alibaba.otter.canal.parse.inbound.mysql.tsdb.dao.MetaSnapshotDO) HashMap(java.util.HashMap) Map(java.util.Map) CanalParseException(com.alibaba.otter.canal.parse.exception.CanalParseException)

Example 2 with Schema

use of com.alibaba.druid.sql.repository.Schema in project canal by alibaba.

the class MemoryTableMeta method snapshot.

public Map<String, String> snapshot() {
    Map<String, String> schemaDdls = new HashMap<>();
    for (Schema schema : repository.getSchemas()) {
        StringBuffer data = new StringBuffer(4 * 1024);
        for (String table : schema.showTables()) {
            SchemaObject schemaObject = schema.findTable(table);
            schemaObject.getStatement().output(data);
            data.append("; \n");
        }
        schemaDdls.put(schema.getName(), data.toString());
    }
    return schemaDdls;
}
Also used : SchemaObject(com.alibaba.druid.sql.repository.SchemaObject) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Schema(com.alibaba.druid.sql.repository.Schema)

Example 3 with Schema

use of com.alibaba.druid.sql.repository.Schema in project canal by alibaba.

the class MemoryTableMeta method find.

@Override
public TableMeta find(String schema, String table) {
    List<String> keys = Arrays.asList(schema, table);
    TableMeta tableMeta = tableMetas.get(keys);
    if (tableMeta == null) {
        synchronized (this) {
            tableMeta = tableMetas.get(keys);
            if (tableMeta == null) {
                Schema schemaRep = repository.findSchema(schema);
                if (schemaRep == null) {
                    return null;
                }
                SchemaObject data = schemaRep.findTable(table);
                if (data == null) {
                    return null;
                }
                SQLStatement statement = data.getStatement();
                if (statement == null) {
                    return null;
                }
                if (statement instanceof SQLCreateTableStatement) {
                    tableMeta = parse((SQLCreateTableStatement) statement);
                }
                if (tableMeta != null) {
                    if (table != null) {
                        tableMeta.setTable(table);
                    }
                    if (schema != null) {
                        tableMeta.setSchema(schema);
                    }
                    tableMetas.put(keys, tableMeta);
                }
            }
        }
    }
    return tableMeta;
}
Also used : SchemaObject(com.alibaba.druid.sql.repository.SchemaObject) SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) Schema(com.alibaba.druid.sql.repository.Schema) TableMeta(com.alibaba.otter.canal.parse.inbound.TableMeta) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 4 with Schema

use of com.alibaba.druid.sql.repository.Schema in project canal by alibaba.

the class MemoryTableMeta_DDL_Test method test_any.

@Test
public void test_any() throws Throwable {
    MemoryTableMeta memoryTableMeta = new MemoryTableMeta();
    URL url = Thread.currentThread().getContextClassLoader().getResource("dummy.txt");
    File dummyFile = new File(url.getFile());
    File create = new File(dummyFile.getParent() + "/ddl", "ddl_any.sql");
    String sql = StringUtils.join(IOUtils.readLines(new FileInputStream(create)), "\n");
    memoryTableMeta.apply(null, "test", sql, null);
    List<String> tableNames = new ArrayList<>();
    for (Schema schema : memoryTableMeta.getRepository().getSchemas()) {
        tableNames.addAll(schema.showTables());
    }
    for (String table : tableNames) {
        TableMeta sourceMeta = memoryTableMeta.find("test", table);
        System.out.println(sourceMeta.toString());
    }
}
Also used : Schema(com.alibaba.druid.sql.repository.Schema) ArrayList(java.util.ArrayList) TableMeta(com.alibaba.otter.canal.parse.inbound.TableMeta) File(java.io.File) URL(java.net.URL) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 5 with Schema

use of com.alibaba.druid.sql.repository.Schema in project canal by alibaba.

the class MemoryTableMeta_DDL_Test method test_create_if_not_exist.

@Test
public void test_create_if_not_exist() throws Throwable {
    MemoryTableMeta memoryTableMeta = new MemoryTableMeta();
    URL url = Thread.currentThread().getContextClassLoader().getResource("dummy.txt");
    File dummyFile = new File(url.getFile());
    File create = new File(dummyFile.getParent() + "/ddl", "ddl_create_if_not_exist.sql");
    String sql = StringUtils.join(IOUtils.readLines(new FileInputStream(create)), "\n");
    memoryTableMeta.apply(null, "test", sql, null);
    List<String> tableNames = new ArrayList<>();
    for (Schema schema : memoryTableMeta.getRepository().getSchemas()) {
        tableNames.addAll(schema.showTables());
    }
    for (String table : tableNames) {
        TableMeta sourceMeta = memoryTableMeta.find("test", table);
        System.out.println(sourceMeta.toString());
    }
}
Also used : Schema(com.alibaba.druid.sql.repository.Schema) ArrayList(java.util.ArrayList) TableMeta(com.alibaba.otter.canal.parse.inbound.TableMeta) File(java.io.File) URL(java.net.URL) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

Schema (com.alibaba.druid.sql.repository.Schema)6 TableMeta (com.alibaba.otter.canal.parse.inbound.TableMeta)4 ArrayList (java.util.ArrayList)3 SchemaObject (com.alibaba.druid.sql.repository.SchemaObject)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 URL (java.net.URL)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)1 CanalParseException (com.alibaba.otter.canal.parse.exception.CanalParseException)1 MetaSnapshotDO (com.alibaba.otter.canal.parse.inbound.mysql.tsdb.dao.MetaSnapshotDO)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1