Search in sources :

Example 1 with BatchExecutor

use of com.alibaba.otter.canal.client.adapter.rdb.support.BatchExecutor in project canal by alibaba.

the class RdbSyncService method sync.

/**
 * 批量同步回调
 *
 * @param dmls 批量 DML
 * @param function 回调方法
 */
public void sync(List<Dml> dmls, Function<Dml, Boolean> function) {
    try {
        boolean toExecute = false;
        for (Dml dml : dmls) {
            if (!toExecute) {
                toExecute = function.apply(dml);
            } else {
                function.apply(dml);
            }
        }
        if (toExecute) {
            List<Future<Boolean>> futures = new ArrayList<>();
            for (int i = 0; i < threads; i++) {
                int j = i;
                if (dmlsPartition[j].isEmpty()) {
                    // bypass
                    continue;
                }
                futures.add(executorThreads[i].submit(() -> {
                    try {
                        dmlsPartition[j].forEach(syncItem -> sync(batchExecutors[j], syncItem.config, syncItem.singleDml));
                        dmlsPartition[j].clear();
                        batchExecutors[j].commit();
                        return true;
                    } catch (Throwable e) {
                        dmlsPartition[j].clear();
                        batchExecutors[j].rollback();
                        throw new RuntimeException(e);
                    }
                }));
            }
            futures.forEach(future -> {
                try {
                    future.get();
                } catch (ExecutionException | InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
        }
    } finally {
        for (BatchExecutor batchExecutor : batchExecutors) {
            if (batchExecutor != null) {
                batchExecutor.close();
            }
        }
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Connection(java.sql.Connection) SerializerFeature(com.alibaba.fastjson.serializer.SerializerFeature) Util(com.alibaba.otter.canal.client.adapter.support.Util) LoggerFactory(org.slf4j.LoggerFactory) Dml(com.alibaba.otter.canal.client.adapter.support.Dml) Function(java.util.function.Function) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) SQLException(java.sql.SQLException) Future(java.util.concurrent.Future) Map(java.util.Map) DataSource(javax.sql.DataSource) SingleDml(com.alibaba.otter.canal.client.adapter.rdb.support.SingleDml) ExecutorService(java.util.concurrent.ExecutorService) MappingConfig(com.alibaba.otter.canal.client.adapter.rdb.config.MappingConfig) Properties(java.util.Properties) Logger(org.slf4j.Logger) BatchExecutor(com.alibaba.otter.canal.client.adapter.rdb.support.BatchExecutor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) JSON(com.alibaba.fastjson.JSON) DbMapping(com.alibaba.otter.canal.client.adapter.rdb.config.MappingConfig.DbMapping) SyncUtil(com.alibaba.otter.canal.client.adapter.rdb.support.SyncUtil) ResultSetMetaData(java.sql.ResultSetMetaData) BatchExecutor(com.alibaba.otter.canal.client.adapter.rdb.support.BatchExecutor) Dml(com.alibaba.otter.canal.client.adapter.support.Dml) SingleDml(com.alibaba.otter.canal.client.adapter.rdb.support.SingleDml) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

JSON (com.alibaba.fastjson.JSON)1 SerializerFeature (com.alibaba.fastjson.serializer.SerializerFeature)1 MappingConfig (com.alibaba.otter.canal.client.adapter.rdb.config.MappingConfig)1 DbMapping (com.alibaba.otter.canal.client.adapter.rdb.config.MappingConfig.DbMapping)1 BatchExecutor (com.alibaba.otter.canal.client.adapter.rdb.support.BatchExecutor)1 SingleDml (com.alibaba.otter.canal.client.adapter.rdb.support.SingleDml)1 SyncUtil (com.alibaba.otter.canal.client.adapter.rdb.support.SyncUtil)1 Dml (com.alibaba.otter.canal.client.adapter.support.Dml)1 Util (com.alibaba.otter.canal.client.adapter.support.Util)1 Connection (java.sql.Connection)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1