Search in sources :

Example 21 with DataMedia

use of com.alibaba.otter.shared.common.model.config.data.DataMedia in project otter by alibaba.

the class DataSourceList method execute.

public void execute(@Param("pageIndex") int pageIndex, @Param("searchKey") String searchKey, Context context) throws Exception {
    @SuppressWarnings("unchecked") Map<String, Object> condition = new HashMap<String, Object>();
    if ("请输入关键字(目前支持DataSource的ID、名字搜索)".equals(searchKey)) {
        searchKey = "";
    }
    condition.put("searchKey", searchKey);
    int count = dataMediaSourceService.getCount(condition);
    Paginator paginator = new Paginator();
    paginator.setItems(count);
    paginator.setPage(pageIndex);
    condition.put("offset", paginator.getOffset());
    condition.put("length", paginator.getLength());
    List<DataMediaSource> dataMediaSources = dataMediaSourceService.listByCondition(condition);
    List<SeniorDataMediaSource> seniorDataMediaSources = new ArrayList<SeniorDataMediaSource>();
    for (DataMediaSource dataMediaSource : dataMediaSources) {
        SeniorDataMediaSource seniorDataMediaSource = new SeniorDataMediaSource();
        seniorDataMediaSource.setEncode(dataMediaSource.getEncode());
        seniorDataMediaSource.setGmtCreate(dataMediaSource.getGmtCreate());
        seniorDataMediaSource.setGmtModified(dataMediaSource.getGmtModified());
        seniorDataMediaSource.setId(dataMediaSource.getId());
        seniorDataMediaSource.setName(dataMediaSource.getName());
        seniorDataMediaSource.setType(dataMediaSource.getType());
        if (dataMediaSource instanceof DbMediaSource) {
            seniorDataMediaSource.setDriver(((DbMediaSource) dataMediaSource).getDriver());
            seniorDataMediaSource.setUrl(((DbMediaSource) dataMediaSource).getUrl());
            seniorDataMediaSource.setUsername(((DbMediaSource) dataMediaSource).getUsername());
        } else if (dataMediaSource instanceof MqMediaSource) {
            seniorDataMediaSource.setUrl(((MqMediaSource) dataMediaSource).getUrl());
            seniorDataMediaSource.setStorePath(((MqMediaSource) dataMediaSource).getStorePath());
        }
        List<DataMedia> dataMedia = dataMediaService.listByDataMediaSourceId(dataMediaSource.getId());
        seniorDataMediaSource.setDataMedias(dataMedia);
        if (dataMedia.size() < 1) {
            seniorDataMediaSource.setUsed(false);
        } else {
            seniorDataMediaSource.setUsed(true);
        }
        seniorDataMediaSources.add(seniorDataMediaSource);
    }
    context.put("sources", seniorDataMediaSources);
    context.put("paginator", paginator);
    context.put("searchKey", searchKey);
}
Also used : SeniorDataMediaSource(com.alibaba.otter.manager.web.common.model.SeniorDataMediaSource) HashMap(java.util.HashMap) MqMediaSource(com.alibaba.otter.shared.common.model.config.data.mq.MqMediaSource) ArrayList(java.util.ArrayList) Paginator(com.alibaba.citrus.util.Paginator) DataMediaSource(com.alibaba.otter.shared.common.model.config.data.DataMediaSource) SeniorDataMediaSource(com.alibaba.otter.manager.web.common.model.SeniorDataMediaSource) DbMediaSource(com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource) DataMedia(com.alibaba.otter.shared.common.model.config.data.DataMedia)

Example 22 with DataMedia

use of com.alibaba.otter.shared.common.model.config.data.DataMedia in project otter by alibaba.

the class EditDataMedia method execute.

public void execute(@Param("dataMediaId") Long dataMediaId, @Param("pageIndex") int pageIndex, @Param("searchKey") String searchKey, Context context) throws Exception {
    DataMedia dataMedia = dataMediaService.findById(dataMediaId);
    context.put("dataMedia", dataMedia);
    context.put("pageIndex", pageIndex);
    context.put("searchKey", searchKey);
}
Also used : DataMedia(com.alibaba.otter.shared.common.model.config.data.DataMedia)

Example 23 with DataMedia

use of com.alibaba.otter.shared.common.model.config.data.DataMedia in project otter by alibaba.

the class DatabaseExtractor method checkNeedDbForRowMode.

private boolean checkNeedDbForRowMode(Pipeline pipeline, EventData eventData) {
    // 获取数据表信息
    DataMedia dataMedia = ConfigHelper.findDataMedia(pipeline, eventData.getTableId());
    DbDialect dbDialect = dbDialectFactory.getDbDialect(pipeline.getId(), (DbMediaSource) dataMedia.getSource());
    Table table = dbDialect.findTable(eventData.getSchemaName(), eventData.getTableName());
    if (table.getColumnCount() == eventData.getColumns().size() + eventData.getKeys().size()) {
        return false;
    } else {
        return true;
    }
}
Also used : Table(org.apache.ddlutils.model.Table) DbDialect(com.alibaba.otter.node.etl.common.db.dialect.DbDialect) DataMedia(com.alibaba.otter.shared.common.model.config.data.DataMedia)

Example 24 with DataMedia

use of com.alibaba.otter.shared.common.model.config.data.DataMedia in project otter by alibaba.

the class FreedomExtractor method extract.

public void extract(DbBatch dbBatch) throws ExtractException {
    Assert.notNull(dbBatch);
    // 读取配置
    Pipeline pipeline = getPipeline(dbBatch.getRowBatch().getIdentity().getPipelineId());
    boolean skipFreedom = pipeline.getParameters().getSkipFreedom();
    String bufferSchema = pipeline.getParameters().getSystemSchema();
    String bufferTable = pipeline.getParameters().getSystemBufferTable();
    List<EventData> eventDatas = dbBatch.getRowBatch().getDatas();
    // 使用set,提升remove时的查找速度
    Set<EventData> removeDatas = new HashSet<EventData>();
    for (EventData eventData : eventDatas) {
        if (StringUtils.equalsIgnoreCase(bufferSchema, eventData.getSchemaName()) && StringUtils.equalsIgnoreCase(bufferTable, eventData.getTableName())) {
            if (eventData.getEventType().isDdl()) {
                continue;
            }
            if (skipFreedom) {
                // 判断是否需要忽略
                removeDatas.add(eventData);
                continue;
            }
            // 只处理insert / update记录
            if (eventData.getEventType().isInsert() || eventData.getEventType().isUpdate()) {
                // 重新改写一下EventData的数据,根据系统表的定义
                EventColumn tableIdColumn = getMatchColumn(eventData.getColumns(), TABLE_ID);
                // 获取到对应tableId的media信息
                try {
                    DataMedia dataMedia = null;
                    Long tableId = Long.valueOf(tableIdColumn.getColumnValue());
                    eventData.setTableId(tableId);
                    if (tableId <= 0) {
                        // 直接按照full_name进行查找
                        // 尝试直接根据schema+table name进行查找
                        EventColumn fullNameColumn = getMatchColumn(eventData.getColumns(), FULL_NAME);
                        if (fullNameColumn != null) {
                            String[] names = StringUtils.split(fullNameColumn.getColumnValue(), ".");
                            if (names.length >= 2) {
                                dataMedia = ConfigHelper.findSourceDataMedia(pipeline, names[0], names[1]);
                                eventData.setTableId(dataMedia.getId());
                            } else {
                                throw new ConfigException("no such DataMedia " + names);
                            }
                        }
                    } else {
                        // 如果指定了tableId,需要按照tableId进行严格查找,如果没找到,那说明不需要进行同步
                        dataMedia = ConfigHelper.findDataMedia(pipeline, Long.valueOf(tableIdColumn.getColumnValue()));
                    }
                    DbDialect dbDialect = dbDialectFactory.getDbDialect(pipeline.getId(), (DbMediaSource) dataMedia.getSource());
                    // 考虑offer[1-128]的配置模式
                    if (!dataMedia.getNameMode().getMode().isSingle() || !dataMedia.getNamespaceMode().getMode().isSingle()) {
                        boolean hasError = true;
                        EventColumn fullNameColumn = getMatchColumn(eventData.getColumns(), FULL_NAME);
                        if (fullNameColumn != null) {
                            String[] names = StringUtils.split(fullNameColumn.getColumnValue(), ".");
                            if (names.length >= 2) {
                                eventData.setSchemaName(names[0]);
                                eventData.setTableName(names[1]);
                                hasError = false;
                            }
                        }
                        if (hasError) {
                            // 出现异常,需要记录一下
                            logger.warn("dataMedia mode:{} , fullname:{} ", dataMedia.getMode(), fullNameColumn == null ? null : fullNameColumn.getColumnValue());
                            removeDatas.add(eventData);
                            // 跳过这条记录
                            continue;
                        }
                    } else {
                        eventData.setSchemaName(dataMedia.getNamespace());
                        eventData.setTableName(dataMedia.getName());
                    }
                    // 更新业务类型
                    EventColumn typeColumn = getMatchColumn(eventData.getColumns(), TYPE);
                    EventType eventType = EventType.valuesOf(typeColumn.getColumnValue());
                    eventData.setEventType(eventType);
                    if (eventType.isUpdate()) {
                        // 如果是update强制修改为insert,这样可以在目标端执行merge
                        // sql
                        eventData.setEventType(EventType.INSERT);
                    } else if (eventType.isDdl()) {
                        dbDialect.reloadTable(eventData.getSchemaName(), eventData.getTableName());
                        // 删除当前记录
                        removeDatas.add(eventData);
                        continue;
                    }
                    // 重新构建新的业务主键字段
                    EventColumn pkDataColumn = getMatchColumn(eventData.getColumns(), PK_DATA);
                    String pkData = pkDataColumn.getColumnValue();
                    String[] pks = StringUtils.split(pkData, PK_SPLIT);
                    Table table = dbDialect.findTable(eventData.getSchemaName(), eventData.getTableName());
                    List<EventColumn> newColumns = new ArrayList<EventColumn>();
                    Column[] primaryKeyColumns = table.getPrimaryKeyColumns();
                    if (primaryKeyColumns.length > pks.length) {
                        throw new ExtractException("data pk column size not match , data:" + eventData.toString());
                    }
                    // 构建字段
                    Column[] allColumns = table.getColumns();
                    int pkIndex = 0;
                    for (int i = 0; i < allColumns.length; i++) {
                        Column column = allColumns[i];
                        if (column.isPrimaryKey()) {
                            EventColumn newColumn = new EventColumn();
                            // 设置下标
                            newColumn.setIndex(i);
                            newColumn.setColumnName(column.getName());
                            newColumn.setColumnType(column.getTypeCode());
                            newColumn.setColumnValue(pks[pkIndex]);
                            newColumn.setKey(true);
                            newColumn.setNull(pks[pkIndex] == null);
                            newColumn.setUpdate(true);
                            // 添加到记录
                            newColumns.add(newColumn);
                            pkIndex++;
                        }
                    }
                    // 设置数据
                    eventData.setKeys(newColumns);
                    eventData.setOldKeys(new ArrayList<EventColumn>());
                    eventData.setColumns(new ArrayList<EventColumn>());
                    // 设置为行记录+反查
                    eventData.setSyncMode(SyncMode.ROW);
                    eventData.setSyncConsistency(SyncConsistency.MEDIA);
                    eventData.setRemedy(true);
                    // 默认为1kb,如果还是按照binlog大小计算的话,可能会采用rpc传输,导致内存不够用
                    eventData.setSize(1024);
                } catch (ConfigException e) {
                    // 忽略掉,因为系统表会被共享,所以这条记录会被不是该同步通道给获取到
                    logger.info("find DataMedia error " + eventData.toString(), e);
                    removeDatas.add(eventData);
                    continue;
                } catch (Throwable e) {
                    // 出现异常时忽略掉
                    logger.warn("process freedom data error " + eventData.toString(), e);
                    removeDatas.add(eventData);
                    continue;
                }
            } else {
                // 删除该记录
                removeDatas.add(eventData);
            }
        }
    }
    if (!CollectionUtils.isEmpty(removeDatas)) {
        eventDatas.removeAll(removeDatas);
    }
}
Also used : ExtractException(com.alibaba.otter.node.etl.extract.exceptions.ExtractException) Table(org.apache.ddlutils.model.Table) EventColumn(com.alibaba.otter.shared.etl.model.EventColumn) EventType(com.alibaba.otter.shared.etl.model.EventType) ArrayList(java.util.ArrayList) ConfigException(com.alibaba.otter.shared.common.model.config.ConfigException) EventData(com.alibaba.otter.shared.etl.model.EventData) Pipeline(com.alibaba.otter.shared.common.model.config.pipeline.Pipeline) EventColumn(com.alibaba.otter.shared.etl.model.EventColumn) Column(org.apache.ddlutils.model.Column) DbDialect(com.alibaba.otter.node.etl.common.db.dialect.DbDialect) DataMedia(com.alibaba.otter.shared.common.model.config.data.DataMedia) HashSet(java.util.HashSet)

Example 25 with DataMedia

use of com.alibaba.otter.shared.common.model.config.data.DataMedia in project otter by alibaba.

the class ProcessorExtractor method extract.

public void extract(DbBatch param) throws ExtractException {
    ExecutorTemplate executorTemplate = null;
    try {
        RowBatch rowBatch = param.getRowBatch();
        final Pipeline pipeline = getPipeline(rowBatch.getIdentity().getPipelineId());
        List<EventData> eventDatas = rowBatch.getDatas();
        // 使用set,提升remove时的查找速度
        final Set<EventData> removeDatas = Collections.synchronizedSet(new HashSet<EventData>());
        executorTemplate = executorTemplateGetter.get();
        executorTemplate.start();
        // 重新设置下poolSize
        executorTemplate.adjustPoolSize(pipeline.getParameters().getExtractPoolSize());
        for (final EventData eventData : eventDatas) {
            List<DataMediaPair> dataMediaPairs = ConfigHelper.findDataMediaPairByMediaId(pipeline, eventData.getTableId());
            if (dataMediaPairs == null) {
                throw new ExtractException("ERROR ## the dataMediaId = " + eventData.getTableId() + " dataMediaPair is null,please check");
            }
            for (DataMediaPair dataMediaPair : dataMediaPairs) {
                if (!dataMediaPair.isExistFilter()) {
                    continue;
                }
                final EventProcessor eventProcessor = extensionFactory.getExtension(EventProcessor.class, dataMediaPair.getFilterData());
                if (eventProcessor instanceof DataSourceFetcherAware) {
                    ((DataSourceFetcherAware) eventProcessor).setDataSourceFetcher(new DataSourceFetcher() {

                        @Override
                        public DataSource fetch(Long tableId) {
                            DataMedia dataMedia = ConfigHelper.findDataMedia(pipeline, tableId);
                            return dataSourceService.getDataSource(pipeline.getId(), dataMedia.getSource());
                        }
                    });
                    executorTemplate.submit(new Runnable() {

                        @Override
                        public void run() {
                            MDC.put(OtterConstants.splitPipelineLogFileKey, String.valueOf(pipeline.getId()));
                            boolean process = eventProcessor.process(eventData);
                            if (!process) {
                                // 添加到删除记录中
                                removeDatas.add(eventData);
                            }
                        }
                    });
                } else {
                    boolean process = eventProcessor.process(eventData);
                    if (!process) {
                        // 添加到删除记录中
                        removeDatas.add(eventData);
                        break;
                    }
                }
            }
        }
        // 等待所有都处理完成
        executorTemplate.waitForResult();
        if (!CollectionUtils.isEmpty(removeDatas)) {
            eventDatas.removeAll(removeDatas);
        }
    } finally {
        if (executorTemplate != null) {
            executorTemplateGetter.release(executorTemplate);
        }
    }
}
Also used : ExtractException(com.alibaba.otter.node.etl.extract.exceptions.ExtractException) ExecutorTemplate(com.alibaba.otter.shared.common.utils.thread.ExecutorTemplate) DataMediaPair(com.alibaba.otter.shared.common.model.config.data.DataMediaPair) DataSourceFetcher(com.alibaba.otter.shared.etl.extend.processor.support.DataSourceFetcher) DataSourceFetcherAware(com.alibaba.otter.shared.etl.extend.processor.support.DataSourceFetcherAware) EventData(com.alibaba.otter.shared.etl.model.EventData) Pipeline(com.alibaba.otter.shared.common.model.config.pipeline.Pipeline) DataSource(javax.sql.DataSource) RowBatch(com.alibaba.otter.shared.etl.model.RowBatch) EventProcessor(com.alibaba.otter.shared.etl.extend.processor.EventProcessor) DataMedia(com.alibaba.otter.shared.common.model.config.data.DataMedia)

Aggregations

DataMedia (com.alibaba.otter.shared.common.model.config.data.DataMedia)29 ArrayList (java.util.ArrayList)12 RepeatConfigureException (com.alibaba.otter.manager.biz.common.exceptions.RepeatConfigureException)10 DataMediaPair (com.alibaba.otter.shared.common.model.config.data.DataMediaPair)10 EventData (com.alibaba.otter.shared.etl.model.EventData)8 DbDialect (com.alibaba.otter.node.etl.common.db.dialect.DbDialect)7 DataMediaSource (com.alibaba.otter.shared.common.model.config.data.DataMediaSource)7 ManagerException (com.alibaba.otter.manager.biz.common.exceptions.ManagerException)5 Table (org.apache.ddlutils.model.Table)4 Paginator (com.alibaba.citrus.util.Paginator)3 ExtractException (com.alibaba.otter.node.etl.extract.exceptions.ExtractException)3 ColumnPair (com.alibaba.otter.shared.common.model.config.data.ColumnPair)3 ExtensionData (com.alibaba.otter.shared.common.model.config.data.ExtensionData)3 DbDataMedia (com.alibaba.otter.shared.common.model.config.data.db.DbDataMedia)3 MqDataMedia (com.alibaba.otter.shared.common.model.config.data.mq.MqDataMedia)3 MqMediaSource (com.alibaba.otter.shared.common.model.config.data.mq.MqMediaSource)3 Pipeline (com.alibaba.otter.shared.common.model.config.pipeline.Pipeline)3 EventColumn (com.alibaba.otter.shared.etl.model.EventColumn)3 EventType (com.alibaba.otter.shared.etl.model.EventType)3 HashMap (java.util.HashMap)3