Search in sources :

Example 1 with ExecutorTemplate

use of com.alibaba.otter.shared.common.utils.thread.ExecutorTemplate in project otter by alibaba.

the class FileBatchConflictDetectServiceImpl method onFileConflictDetect.

/**
 * 具体冲突检测的行为
 */
private FileBatch onFileConflictDetect(FileConflictDetectEvent event) {
    final FileBatch fileBatch = event.getFileBatch();
    if (CollectionUtils.isEmpty(fileBatch.getFiles())) {
        return fileBatch;
    }
    ExecutorTemplate executorTemplate = executorTemplateGetter.get();
    try {
        MDC.put(OtterConstants.splitPipelineLoadLogFileKey, String.valueOf(fileBatch.getIdentity().getPipelineId()));
        executorTemplate.start();
        // 重新设置下poolSize
        Pipeline pipeline = configClientService.findPipeline(fileBatch.getIdentity().getPipelineId());
        executorTemplate.adjustPoolSize(pipeline.getParameters().getFileLoadPoolSize());
        // 启动
        final List<FileData> result = Collections.synchronizedList(new ArrayList<FileData>());
        final List<FileData> filter = Collections.synchronizedList(new ArrayList<FileData>());
        for (final FileData source : fileBatch.getFiles()) {
            EventType type = source.getEventType();
            if (type.isDelete()) {
                result.add(source);
            } else {
                executorTemplate.submit(new Runnable() {

                    public void run() {
                        MDC.put(OtterConstants.splitPipelineLoadLogFileKey, String.valueOf(fileBatch.getIdentity().getPipelineId()));
                        // 处理更新类型
                        String namespace = source.getNameSpace();
                        String path = source.getPath();
                        FileData target = null;
                        int count = 0;
                        while (count++ < retry) {
                            // 进行重试处理
                            try {
                                if (true == StringUtils.isBlank(namespace)) {
                                    // local file
                                    java.io.File targetFile = new java.io.File(path);
                                    if (true == targetFile.exists()) {
                                        // modified time cost
                                        long lastModified = targetFile.lastModified();
                                        long size = targetFile.length();
                                        // 更新数据
                                        target = new FileData();
                                        target.setLastModifiedTime(lastModified);
                                        target.setSize(size);
                                    }
                                } else {
                                    // remote file
                                    throw new RuntimeException(source + " is not support!");
                                }
                                // 不出异常就跳出
                                break;
                            } catch (Exception ex) {
                                target = null;
                            }
                        }
                        boolean shouldSync = false;
                        if (target != null) {
                            if (true == accept(target, source)) {
                                shouldSync = true;
                            }
                        } else {
                            shouldSync = true;
                        }
                        if (true == shouldSync) {
                            result.add(source);
                        } else {
                            filter.add(source);
                        }
                    }
                });
            }
        }
        // 等待所有都处理完成
        executorTemplate.waitForResult();
        if (pipeline.getParameters().getDumpEvent() && logger.isInfoEnabled()) {
            logger.info(FileloadDumper.dumpFilterFileDatas(fileBatch.getIdentity(), fileBatch.getFiles().size(), result.size(), filter));
        }
        // 构造返回结果
        FileBatch target = new FileBatch();
        target.setIdentity(fileBatch.getIdentity());
        target.setFiles(result);
        return target;
    } finally {
        if (executorTemplate != null) {
            executorTemplateGetter.release(executorTemplate);
        }
        MDC.remove(OtterConstants.splitPipelineLoadLogFileKey);
    }
}
Also used : FileBatch(com.alibaba.otter.shared.etl.model.FileBatch) ExecutorTemplate(com.alibaba.otter.shared.common.utils.thread.ExecutorTemplate) EventType(com.alibaba.otter.shared.etl.model.EventType) ConflictEventType(com.alibaba.otter.node.etl.conflict.model.ConflictEventType) Pipeline(com.alibaba.otter.shared.common.model.config.pipeline.Pipeline) FileData(com.alibaba.otter.shared.etl.model.FileData)

Example 2 with ExecutorTemplate

use of com.alibaba.otter.shared.common.utils.thread.ExecutorTemplate 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)

Example 3 with ExecutorTemplate

use of com.alibaba.otter.shared.common.utils.thread.ExecutorTemplate in project otter by alibaba.

the class FileExtractor method doFileDetectCollector.

private void doFileDetectCollector(Pipeline pipeline, List<FileData> fileDatas) {
    ExecutorTemplate executorTemplate = executorTemplateGetter.get();
    try {
        executorTemplate.start();
        // 重新设置下poolSize
        executorTemplate.adjustPoolSize(pipeline.getParameters().getFileLoadPoolSize());
        for (final FileData fileData : fileDatas) {
            // 提交进行多线程处理
            executorTemplate.submit(new Runnable() {

                public void run() {
                    boolean isAranda = StringUtils.isNotEmpty(fileData.getNameSpace());
                    int count = 0;
                    Throwable exception = null;
                    while (count++ < retry) {
                        try {
                            if (isAranda) {
                                // remote file
                                throw new RuntimeException(fileData + " is not support!");
                            } else {
                                // 处理本地文件
                                File file = new File(fileData.getPath());
                                fileData.setLastModifiedTime(file.lastModified());
                                fileData.setSize(file.length());
                            }
                            // 没有异常就退出
                            return;
                        } catch (Exception e) {
                            fileData.setLastModifiedTime(Long.MIN_VALUE);
                            fileData.setSize(Long.MIN_VALUE);
                            exception = e;
                        }
                    }
                    if (count >= retry) {
                        logger.warn(String.format("FileDetectCollector is error! collect failed[%s]", fileData.getNameSpace() + "/" + fileData.getPath()), exception);
                    }
                }
            });
        }
        long start = System.currentTimeMillis();
        logger.info("start pipelinep[{}] waitFor FileData Size : {} ", pipeline.getId(), fileDatas.size());
        // 等待所有都处理完成
        executorTemplate.waitForResult();
        logger.info("end pipelinep[{}] waitFor FileData cost : {} ms ", pipeline.getId(), (System.currentTimeMillis() - start));
    } finally {
        if (executorTemplate != null) {
            executorTemplateGetter.release(executorTemplate);
        }
    }
}
Also used : ExecutorTemplate(com.alibaba.otter.shared.common.utils.thread.ExecutorTemplate) FileData(com.alibaba.otter.shared.etl.model.FileData) File(java.io.File) ExtractException(com.alibaba.otter.node.etl.extract.exceptions.ExtractException)

Aggregations

ExecutorTemplate (com.alibaba.otter.shared.common.utils.thread.ExecutorTemplate)3 ExtractException (com.alibaba.otter.node.etl.extract.exceptions.ExtractException)2 Pipeline (com.alibaba.otter.shared.common.model.config.pipeline.Pipeline)2 FileData (com.alibaba.otter.shared.etl.model.FileData)2 ConflictEventType (com.alibaba.otter.node.etl.conflict.model.ConflictEventType)1 DataMedia (com.alibaba.otter.shared.common.model.config.data.DataMedia)1 DataMediaPair (com.alibaba.otter.shared.common.model.config.data.DataMediaPair)1 EventProcessor (com.alibaba.otter.shared.etl.extend.processor.EventProcessor)1 DataSourceFetcher (com.alibaba.otter.shared.etl.extend.processor.support.DataSourceFetcher)1 DataSourceFetcherAware (com.alibaba.otter.shared.etl.extend.processor.support.DataSourceFetcherAware)1 EventData (com.alibaba.otter.shared.etl.model.EventData)1 EventType (com.alibaba.otter.shared.etl.model.EventType)1 FileBatch (com.alibaba.otter.shared.etl.model.FileBatch)1 RowBatch (com.alibaba.otter.shared.etl.model.RowBatch)1 File (java.io.File)1 DataSource (javax.sql.DataSource)1