Package com.alibaba.otter.shared.common.utils.thread

Examples of com.alibaba.otter.shared.common.utils.thread.ExecutorTemplate


            return fileDatas;
        }
    }

    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);
View Full Code Here


        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));
            }
View Full Code Here

            return fileDatas;
        }
    }

    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);
View Full Code Here

    private ExtensionFactory       extensionFactory;
    private DataSourceService      dataSourceService;
    private ExecutorTemplateGetter executorTemplateGetter;

    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();
            final Set<EventData> removeDatas = Collections.synchronizedSet(new HashSet<EventData>());// 使用set,提升remove时的查找速度
            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 {
View Full Code Here

TOP

Related Classes of com.alibaba.otter.shared.common.utils.thread.ExecutorTemplate

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.