Search in sources :

Example 1 with WriterResult

use of com.alicloud.openservices.tablestore.writer.WriterResult in project canal by alibaba.

the class TablestoreAdapter method sync.

@Override
public void sync(List<Dml> dmls) {
    if (dmls == null || dmls.isEmpty()) {
        return;
    }
    try {
        Set<TableStoreWriter> writerSet = new HashSet<>();
        List<Future<WriterResult>> futureList = new ArrayList<>();
        for (Dml dml : dmls) {
            String destination = StringUtils.trimToEmpty(dml.getDestination());
            String groupId = StringUtils.trimToEmpty(dml.getGroupId());
            String database = dml.getDatabase();
            String table = dml.getTable();
            String key;
            if (envProperties != null && !"tcp".equalsIgnoreCase(envProperties.getProperty("canal.conf.mode"))) {
                key = destination + "-" + groupId + "_" + database + "-" + table;
            } else {
                key = destination + "_" + database + "-" + table;
            }
            Map<String, MappingConfig> configMap = mappingConfigCache.get(key);
            if (configMap == null) {
                // 可能有dml中涉及到的表并没有出现在配置中,说明此类dml并不需要同步
                continue;
            }
            Map<String, TableStoreWriter> writerMap = writerCache.get(key);
            for (Map.Entry<String, MappingConfig> entry : configMap.entrySet()) {
                TableStoreWriter w = writerMap.get(entry.getKey());
                // 拿到所有future用于判定失败的记录
                Future<WriterResult> futureTemp = tablestoreSyncService.sync(entry.getValue(), dml, w);
                if (futureTemp != null) {
                    writerSet.add(w);
                    futureList.add(futureTemp);
                }
            }
        }
        if (writerSet.isEmpty()) {
            return;
        }
        writerSet.forEach(e -> e.flush());
        List<WriterResult.RowChangeStatus> totalFailedRows = new ArrayList<>();
        for (Future<WriterResult> future : futureList) {
            try {
                WriterResult result = future.get();
                List<WriterResult.RowChangeStatus> failedRows = result.getFailedRows();
                if (!CollectionUtils.isEmpty(failedRows)) {
                    totalFailedRows.addAll(failedRows);
                }
            } catch (InterruptedException e) {
                logger.info("InterruptedException", e);
                Thread.currentThread().interrupt();
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            }
        }
        if (!CollectionUtils.isEmpty(totalFailedRows)) {
            // 认为有失败的请求
            List<String> msgs = totalFailedRows.stream().map(e -> buildErrorMsgForFailedRowChange(e)).collect(Collectors.toList());
            throw new RuntimeException("Failed rows:" + org.springframework.util.StringUtils.collectionToDelimitedString(msgs, ",", "[", "]"));
        }
    } catch (Exception e) {
        throw e;
    }
}
Also used : DefaultTableStoreWriter(com.alicloud.openservices.tablestore.DefaultTableStoreWriter) TableStoreWriter(com.alicloud.openservices.tablestore.TableStoreWriter) java.util(java.util) StringUtils(org.apache.commons.lang.StringUtils) OuterAdapter(com.alibaba.otter.canal.client.adapter.OuterAdapter) WriteMode(com.alicloud.openservices.tablestore.writer.enums.WriteMode) Dml(com.alibaba.otter.canal.client.adapter.support.Dml) MappingConfig(com.alibaba.otter.canal.client.adapter.tablestore.config.MappingConfig) LoggerFactory(org.slf4j.LoggerFactory) EtlResult(com.alibaba.otter.canal.client.adapter.support.EtlResult) DefaultCredentials(com.alicloud.openservices.tablestore.core.auth.DefaultCredentials) ServiceCredentials(com.alicloud.openservices.tablestore.core.auth.ServiceCredentials) PropertyConstants(com.alibaba.otter.canal.client.adapter.tablestore.common.PropertyConstants) DispatchMode(com.alicloud.openservices.tablestore.writer.enums.DispatchMode) WriterRetryStrategy(com.alicloud.openservices.tablestore.writer.enums.WriterRetryStrategy) WriterConfig(com.alicloud.openservices.tablestore.writer.WriterConfig) SPI(com.alibaba.otter.canal.client.adapter.support.SPI) BatchRequestType(com.alicloud.openservices.tablestore.writer.enums.BatchRequestType) DefaultTableStoreWriter(com.alicloud.openservices.tablestore.DefaultTableStoreWriter) Logger(org.slf4j.Logger) java.util.concurrent(java.util.concurrent) TablestoreSyncService(com.alibaba.otter.canal.client.adapter.tablestore.service.TablestoreSyncService) OuterAdapterConfig(com.alibaba.otter.canal.client.adapter.support.OuterAdapterConfig) Collectors(java.util.stream.Collectors) TableStoreWriter(com.alicloud.openservices.tablestore.TableStoreWriter) WriterResult(com.alicloud.openservices.tablestore.writer.WriterResult) TablestoreEtlService(com.alibaba.otter.canal.client.adapter.tablestore.service.TablestoreEtlService) CollectionUtils(org.springframework.util.CollectionUtils) ConfigLoader(com.alibaba.otter.canal.client.adapter.tablestore.config.ConfigLoader) Dml(com.alibaba.otter.canal.client.adapter.support.Dml) MappingConfig(com.alibaba.otter.canal.client.adapter.tablestore.config.MappingConfig) WriterResult(com.alicloud.openservices.tablestore.writer.WriterResult)

Example 2 with WriterResult

use of com.alicloud.openservices.tablestore.writer.WriterResult in project canal by alibaba.

the class TablestoreEtlService method executeSqlImport.

@Override
protected boolean executeSqlImport(DataSource srcDS, String sql, List<Object> values, AdapterConfig.AdapterMapping mapping, AtomicLong impCount, List<String> errMsg) {
    try {
        MappingConfig.DbMapping dbMapping = (MappingConfig.DbMapping) mapping;
        Map<String, String> columnsMap = dbMapping.getTargetColumnsParsed();
        Util.sqlRS(srcDS, sql, values, rs -> {
            int idx = 0;
            List<Future<WriterResult>> futureList = new ArrayList<>();
            while (true) {
                try {
                    if (!rs.next())
                        break;
                } catch (SQLException throwables) {
                    logger.error("Error while get data from srcDs", throwables);
                    break;
                }
                Dml dml = getDMLByRs(columnsMap, rs);
                List<RowChange> rowChanges = syncService.getRowChanges(dml, config);
                if (CollectionUtils.isEmpty(rowChanges)) {
                    return null;
                }
                Future<WriterResult> future = writer.addRowChangeWithFuture(rowChanges);
                if (future != null) {
                    futureList.add(future);
                }
            }
            writer.flush();
            for (Future<WriterResult> future : futureList) {
                try {
                    WriterResult result = future.get();
                    if (result != null && result.isAllSucceed()) {
                        impCount.incrementAndGet();
                        idx++;
                    } else if (result != null && !result.isAllSucceed()) {
                        List<WriterResult.RowChangeStatus> totalFailedRows = result.getFailedRows();
                        List<String> msgs = totalFailedRows.stream().map(e -> TablestoreAdapter.buildErrorMsgForFailedRowChange(e)).collect(Collectors.toList());
                        logger.error("Failed rows when ETL:" + org.springframework.util.StringUtils.collectionToDelimitedString(msgs, ",", "[", "]"));
                    }
                } catch (InterruptedException e) {
                    logger.info("InterruptedException", e);
                    errMsg.add(e.getMessage());
                    Thread.currentThread().interrupt();
                } catch (ExecutionException e) {
                    errMsg.add(e.getMessage());
                    throw new RuntimeException(e);
                }
            }
            return idx;
        });
        return true;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        return false;
    }
}
Also used : SQLException(java.sql.SQLException) RowChange(com.alicloud.openservices.tablestore.model.RowChange) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) SQLException(java.sql.SQLException) MappingConfig(com.alibaba.otter.canal.client.adapter.tablestore.config.MappingConfig) WriterResult(com.alicloud.openservices.tablestore.writer.WriterResult) Future(java.util.concurrent.Future) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

MappingConfig (com.alibaba.otter.canal.client.adapter.tablestore.config.MappingConfig)2 WriterResult (com.alicloud.openservices.tablestore.writer.WriterResult)2 OuterAdapter (com.alibaba.otter.canal.client.adapter.OuterAdapter)1 Dml (com.alibaba.otter.canal.client.adapter.support.Dml)1 EtlResult (com.alibaba.otter.canal.client.adapter.support.EtlResult)1 OuterAdapterConfig (com.alibaba.otter.canal.client.adapter.support.OuterAdapterConfig)1 SPI (com.alibaba.otter.canal.client.adapter.support.SPI)1 PropertyConstants (com.alibaba.otter.canal.client.adapter.tablestore.common.PropertyConstants)1 ConfigLoader (com.alibaba.otter.canal.client.adapter.tablestore.config.ConfigLoader)1 TablestoreEtlService (com.alibaba.otter.canal.client.adapter.tablestore.service.TablestoreEtlService)1 TablestoreSyncService (com.alibaba.otter.canal.client.adapter.tablestore.service.TablestoreSyncService)1 DefaultTableStoreWriter (com.alicloud.openservices.tablestore.DefaultTableStoreWriter)1 TableStoreWriter (com.alicloud.openservices.tablestore.TableStoreWriter)1 DefaultCredentials (com.alicloud.openservices.tablestore.core.auth.DefaultCredentials)1 ServiceCredentials (com.alicloud.openservices.tablestore.core.auth.ServiceCredentials)1 RowChange (com.alicloud.openservices.tablestore.model.RowChange)1 WriterConfig (com.alicloud.openservices.tablestore.writer.WriterConfig)1 BatchRequestType (com.alicloud.openservices.tablestore.writer.enums.BatchRequestType)1 DispatchMode (com.alicloud.openservices.tablestore.writer.enums.DispatchMode)1 WriteMode (com.alicloud.openservices.tablestore.writer.enums.WriteMode)1