From 7ef4eb8da813628c9db821faae4be1b152b43b98 Mon Sep 17 00:00:00 2001 From: zhanghongqing Date: Thu, 24 Dec 2020 16:17:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4flume=20=E6=96=87=E4=BB=B6sin?= =?UTF-8?q?k=E6=94=AF=E6=8C=81=E7=89=B9=E6=80=A7=EF=BC=9A=20+=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E9=92=88=E5=AF=B9=E5=B7=B2=E5=A4=84=E7=90=86=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E6=96=87=E4=BB=B6=E6=B7=BB=E5=8A=A0=E5=90=8E=E7=BC=80?= =?UTF-8?q?=20+=20=E6=94=AF=E6=8C=81=E5=B7=B2=E5=A4=84=E7=90=86=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E6=96=87=E4=BB=B6=E7=A7=BB=E5=8A=A8=20+=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E7=A9=BA=E6=96=87=E4=BB=B6=E5=88=A0=E9=99=A4=EF=BC=88?= =?UTF-8?q?=E5=BD=93=E8=BF=9B=E8=A1=8C=E6=96=87=E4=BB=B6=E7=A7=BB=E5=8A=A8?= =?UTF-8?q?=E6=88=96=E6=96=87=E4=BB=B6=E9=87=8D=E5=91=BD=E5=90=8D=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E5=A6=82=E6=96=87=E4=BB=B6=E4=B8=BA=E7=A9=BA=EF=BC=8C?= =?UTF-8?q?=E5=88=99=E6=89=A7=E8=A1=8Cdelete()=E6=93=8D=E4=BD=9C=EF=BC=89?= =?UTF-8?q?=20+=20=E6=94=AF=E6=8C=81=E6=96=87=E4=BB=B6=E5=A4=9A=E8=B7=AF?= =?UTF-8?q?=E5=A4=8D=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- safe-file-roll-sink/.gitignore | 3 + safe-file-roll-sink/LICENSE | 201 ++++++++ safe-file-roll-sink/README.MD | 58 +++ safe-file-roll-sink/pom.xml | 33 ++ .../java/cn/flume/sinks/SafePathManager.java | 67 +++ .../cn/flume/sinks/SafeRollingFileSink.java | 439 ++++++++++++++++++ 6 files changed, 801 insertions(+) create mode 100644 safe-file-roll-sink/.gitignore create mode 100644 safe-file-roll-sink/LICENSE create mode 100644 safe-file-roll-sink/README.MD create mode 100644 safe-file-roll-sink/pom.xml create mode 100644 safe-file-roll-sink/src/main/java/cn/flume/sinks/SafePathManager.java create mode 100644 safe-file-roll-sink/src/main/java/cn/flume/sinks/SafeRollingFileSink.java diff --git a/safe-file-roll-sink/.gitignore b/safe-file-roll-sink/.gitignore new file mode 100644 index 0000000..c2e74c4 --- /dev/null +++ b/safe-file-roll-sink/.gitignore @@ -0,0 +1,3 @@ +*.iml +.idea +target \ No newline at end of file diff --git a/safe-file-roll-sink/LICENSE b/safe-file-roll-sink/LICENSE new file mode 100644 index 0000000..5c304d1 --- /dev/null +++ b/safe-file-roll-sink/LICENSE @@ -0,0 +1,201 @@ +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/safe-file-roll-sink/README.MD b/safe-file-roll-sink/README.MD new file mode 100644 index 0000000..79bcb8c --- /dev/null +++ b/safe-file-roll-sink/README.MD @@ -0,0 +1,58 @@ +flume-safe-roll-file-sink +========================= +支持以下特性: +------------------------------- ++ 支持针对已处理完成文件添加后缀 ++ 支持已处理完成文件移动 ++ 支持空文件删除(当进行文件移动或文件重命名时,如文件为空,则执行delete()操作) ++ 支持文件多路复制 + + +配置参数: +---------------------- +- sink.moveFile default:false 含义:是否移动文件 +- sink.targetDirectory default:'' 含义:移动目标目录 +- sink.useFileSuffix default:false 含义:是否添加文件后缀(移动和复制过程均会使用后缀名) +- sink.rollInterval default:30 含义:生成文件间隔 单位秒 +- sink.fileSuffix default:'' 含义:文件后缀名 +- sink.filePrefix default:'' 含义:文件前缀名 +- sink.useCopy default:false 含义:是否复制文件 +- sink.copyDirectory default:'' 含义:复制目标目录(多个目录请使用英文逗号分隔) + +配置示例: +-------------------- +```shell +# 负载机flume client配置 + +# 组件名称配置 +client.sources = source_client +client.sinks = sink_client +client.channels = channel_client + +# 日志源组件配置(监听目录下新增文件) +client.sources.source_client.type = spooldir +client.sources.source_client.channels = channel_client +client.sources.source_client.spoolDir = /some_logs_dir/ +client.sources.source_client.fileHeader = true + +# sink组件配置 +client.sinks.sink_client.type = SafeRollingFileSink +client.sinks.sink_client.channel = channel_client +client.sinks.sink_client.sink.directory = /data/source +client.sinks.sink_client.sink.rollInterval = 1 + +client.sinks.sink_client.sink.moveFile = true +client.sinks.sink_client.sink.targetDirectory = /data/target + +client.sinks.sink_client.sink.useFileSuffix = true +client.sinks.sink_client.sink.fileSuffix = .COMPLETED +client.sinks.sink_client.sink.filePrefix = CONNECTION + +client.sinks.sink_client.sink.useCopy = true +client.sinks.sink_client.sink.copyDirectory = /data/copy1,/data/copy2 + +# 文件管道设置 +client.channels.channel_client.type = file +client.channels.channel_client.checkpointDir = /data/tmp/checkpoint +client.channels.channel_client.dataDirs = /data/tmp +``` diff --git a/safe-file-roll-sink/pom.xml b/safe-file-roll-sink/pom.xml new file mode 100644 index 0000000..b8cd634 --- /dev/null +++ b/safe-file-roll-sink/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + cn.huyanping.flume + safe-roll-file-sink + 0.2 + + + + org.apache.flume + flume-ng-core + 1.6.0 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.7 + 1.7 + + + + + + + \ No newline at end of file diff --git a/safe-file-roll-sink/src/main/java/cn/flume/sinks/SafePathManager.java b/safe-file-roll-sink/src/main/java/cn/flume/sinks/SafePathManager.java new file mode 100644 index 0000000..cc26ff0 --- /dev/null +++ b/safe-file-roll-sink/src/main/java/cn/flume/sinks/SafePathManager.java @@ -0,0 +1,67 @@ +package cn.flume.sinks; + +import java.io.File; +import java.util.concurrent.atomic.AtomicInteger; + + +/** + * Created by Jenner on 2015/4/9. + * + * 修正org.apache.flume.formatter.output.PathManager在移动文件时可能出现文件名重复的问题 + */ +public class SafePathManager { + private long seriesTimestamp; + private File baseDirectory; + private AtomicInteger fileIndex; + private String filePrefix = ""; + + private File currentFile; + + public SafePathManager() { + } + + public File nextFile() { + //重新生成微秒级文件名 + seriesTimestamp = System.currentTimeMillis(); + fileIndex = new AtomicInteger(); + currentFile = new File(baseDirectory, filePrefix + fileIndex.incrementAndGet() + "-" + seriesTimestamp ); + + return currentFile; + } + + public File getCurrentFile() { + if (currentFile == null) { + return nextFile(); + } + + return currentFile; + } + + public void rotate() { + currentFile = null; + } + + public File getBaseDirectory() { + return baseDirectory; + } + + public void setBaseDirectory(File baseDirectory) { + this.baseDirectory = baseDirectory; + } + + public long getSeriesTimestamp() { + return seriesTimestamp; + } + + public AtomicInteger getFileIndex() { + return fileIndex; + } + + public String getFilePrefix() { + return filePrefix; + } + + public void setFilePrefix(String filePrefix) { + this.filePrefix = filePrefix; + } +} \ No newline at end of file diff --git a/safe-file-roll-sink/src/main/java/cn/flume/sinks/SafeRollingFileSink.java b/safe-file-roll-sink/src/main/java/cn/flume/sinks/SafeRollingFileSink.java new file mode 100644 index 0000000..84145d6 --- /dev/null +++ b/safe-file-roll-sink/src/main/java/cn/flume/sinks/SafeRollingFileSink.java @@ -0,0 +1,439 @@ +package cn.flume.sinks; + +import com.google.common.base.Preconditions; +import com.google.common.util.concurrent.ThreadFactoryBuilder; +import org.apache.flume.*; +import org.apache.flume.conf.Configurable; +import org.apache.flume.instrumentation.SinkCounter; +import org.apache.flume.serialization.EventSerializer; +import org.apache.flume.serialization.EventSerializerFactory; +import org.apache.flume.sink.AbstractSink; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.*; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +public class SafeRollingFileSink extends AbstractSink implements Configurable { + + private static final Logger logger = LoggerFactory + .getLogger(SafeRollingFileSink.class); + private static final long defaultRollInterval = 30; + private static final int defaultBatchSize = 100; + + private int batchSize = defaultBatchSize; + + private File directory; + private long rollInterval; + //已完成写入的文件,是否使用后缀 + private boolean useFileSuffix; + //后缀名 + private String fileSuffix; + //前缀名 + private String filePrefix; + //是否移动文件 + private boolean moveFile; + //移动文件目录 + private File targetDirectory; + //是否复制文件 + private boolean useCopy; + //目标目录 + private File[] copyDirectory; + + private OutputStream outputStream; + private ScheduledExecutorService rollService; + + private String serializerType; + private Context serializerContext; + private EventSerializer serializer; + + private SinkCounter sinkCounter; + + private SafePathManager pathController; + private volatile boolean shouldRotate; + + public SafeRollingFileSink() { + //原pathController轮转文件名可能会出现重复情况,修正 + pathController = new SafePathManager(); + shouldRotate = false; + } + + public void configure(Context context) { + + String directory = context.getString("sink.directory"); + String rollInterval = context.getString("sink.rollInterval"); + useFileSuffix = context.getBoolean("sink.useFileSuffix", false); + fileSuffix = context.getString("sink.fileSuffix", ""); + filePrefix = context.getString("sink.filePrefix", ""); + moveFile = context.getBoolean("sink.moveFile", false); + String targetDirectory = context.getString("sink.targetDirectory", directory); + useCopy = context.getBoolean("sink.useCopy", false); + String copyDirectory = context.getString("sink.copyDirectory", ""); + + serializerType = context.getString("sink.serializer", "TEXT"); + serializerContext = + new Context(context.getSubProperties("sink." + + EventSerializer.CTX_PREFIX)); + + Preconditions.checkArgument(directory != null, "Directory may not be null"); + Preconditions.checkNotNull(serializerType, "Serializer type is undefined"); + + if (rollInterval == null) { + this.rollInterval = defaultRollInterval; + } else { + this.rollInterval = Long.parseLong(rollInterval); + } + + batchSize = context.getInteger("sink.batchSize", defaultBatchSize); + + this.directory = new File(directory); + this.targetDirectory = new File(targetDirectory); + + //检查目录权限 + if(!this.directory.exists()){ + if(!this.directory.mkdirs()){ + throw new IllegalArgumentException("sink.directory is not a directory"); + } + }else if(!this.directory.canWrite()){ + throw new IllegalArgumentException("sink.directory can not write"); + } + + //检查目标目录权限 + if(!this.targetDirectory.exists()){ + if(!this.targetDirectory.mkdirs()){ + throw new IllegalArgumentException("sink.directory is not a directory"); + } + }else if(!this.targetDirectory.canWrite()){ + throw new IllegalArgumentException("sink.directory can not write"); + } + + //配置文件复制 + if(copyDirectory.length()>0 && useCopy){ + String[] copyDirectories = copyDirectory.split(","); + this.copyDirectory = new File[copyDirectories.length]; + for(int i=0; i 0){ + + rollService = Executors.newScheduledThreadPool( + 1, + new ThreadFactoryBuilder().setNameFormat( + "rollingFileSink-roller-" + + Thread.currentThread().getId() + "-%d").build()); + + /* + * Every N seconds, mark that it's time to rotate. We purposefully do NOT + * touch anything other than the indicator flag to avoid error handling + * issues (e.g. IO exceptions occuring in two different threads. + * Resist the urge to actually perform rotation in a separate thread! + */ + rollService.scheduleAtFixedRate(new Runnable() { + + public void run() { + logger.debug("Marking time to rotate file {}", + pathController.getCurrentFile()); + shouldRotate = true; + } + + }, rollInterval, rollInterval, TimeUnit.SECONDS); + } else{ + logger.info("RollInterval is not valid, file rolling will not happen."); + } + logger.info("RollingFileSink {} started.", getName()); + } + + public Status process() throws EventDeliveryException { + if (shouldRotate) { + logger.debug("Time to rotate {}", pathController.getCurrentFile()); + + if (outputStream != null) { + logger.debug("Closing file {}", pathController.getCurrentFile()); + + try { + serializer.flush(); + serializer.beforeClose(); + outputStream.close(); + sinkCounter.incrementConnectionClosedCount(); + shouldRotate = false; + if(useCopy){ + if(!copyLogFile(pathController.getCurrentFile())){ + logger.error("Copy completed file failed"); + throw new IOException("Copy completed file failed"); + } + } + //文件名加后缀、移动文件 + if(!rename(pathController.getCurrentFile())){ + logger.error("Rename completed file failed"); + throw new IOException("Rname completed file failed"); + } + } catch (IOException e) { + sinkCounter.incrementConnectionFailedCount(); + throw new EventDeliveryException("Unable to rotate file " + + pathController.getCurrentFile() + " while delivering event", e); + } finally { + serializer = null; + outputStream = null; + } + + pathController.rotate(); + } + } + + if (outputStream == null) { + File currentFile = pathController.getCurrentFile(); + logger.debug("Opening output stream for file {}", currentFile); + try { + outputStream = new BufferedOutputStream( + new FileOutputStream(currentFile)); + serializer = EventSerializerFactory.getInstance( + serializerType, serializerContext, outputStream); + serializer.afterCreate(); + sinkCounter.incrementConnectionCreatedCount(); + } catch (IOException e) { + sinkCounter.incrementConnectionFailedCount(); + throw new EventDeliveryException("Failed to open file " + + pathController.getCurrentFile() + " while delivering event", e); + } + } + + Channel channel = getChannel(); + Transaction transaction = channel.getTransaction(); + Event event = null; + Status result = Status.READY; + + try { + transaction.begin(); + int eventAttemptCounter = 0; + for (int i = 0; i < batchSize; i++) { + event = channel.take(); + if (event != null) { + sinkCounter.incrementEventDrainAttemptCount(); + eventAttemptCounter++; + serializer.write(event); + + /* + * FIXME: Feature: Rotate on size and time by checking bytes written and + * setting shouldRotate = true if we're past a threshold. + */ + + /* + * FIXME: Feature: Control flush interval based on time or number of + * events. For now, we're super-conservative and flush on each write. + */ + } else { + // No events found, request back-off semantics from runner + result = Status.BACKOFF; + break; + } + } + serializer.flush(); + outputStream.flush(); + transaction.commit(); + sinkCounter.addToEventDrainSuccessCount(eventAttemptCounter); + } catch (Exception ex) { + transaction.rollback(); + throw new EventDeliveryException("Failed to process transaction", ex); + } finally { + transaction.close(); + } + + return result; + } + + @Override + public void stop() { + logger.info("RollingFile sink {} stopping...", getName()); + sinkCounter.stop(); + super.stop(); + + if (outputStream != null) { + logger.debug("Closing file {}", pathController.getCurrentFile()); + + try { + serializer.flush(); + serializer.beforeClose(); + outputStream.close(); + sinkCounter.incrementConnectionClosedCount(); + if(useCopy){ + if(!copyLogFile(pathController.getCurrentFile())){ + logger.error("Copy completed file failed"); + throw new IOException("Copy completed file failed"); + } + } + //文件名加后缀、移动文件 + if(!rename(pathController.getCurrentFile())){ + logger.error("Rename completed file failed"); + throw new IOException("Ranme completed file failed"); + } + } catch (IOException e) { + sinkCounter.incrementConnectionFailedCount(); + logger.error("Unable to close output stream. Exception follows.", e); + } finally { + outputStream = null; + serializer = null; + } + } + if(rollInterval > 0){ + rollService.shutdown(); + + while (!rollService.isTerminated()) { + try { + rollService.awaitTermination(1, TimeUnit.SECONDS); + } catch (InterruptedException e) { + logger + .debug( + "Interrupted while waiting for roll service to stop. " + + "Please report this.", e); + } + } + } + logger.info("RollingFile sink {} stopped. Event metrics: {}", + getName(), sinkCounter); + } + + public File getDirectory() { + return directory; + } + + public void setDirectory(File directory) { + this.directory = directory; + } + + public long getRollInterval() { + return rollInterval; + } + + public void setRollInterval(long rollInterval) { + this.rollInterval = rollInterval; + } + + private boolean rename(File current) { + if (current.length() == 0L) { + logger.info("Delete empty file{}", current.getName()); + return current.delete(); + } + if(useFileSuffix && moveFile){ + return current.renameTo(new File(this.targetDirectory, current.getName() + fileSuffix)); + }else if(useFileSuffix){ + return current.renameTo(new File(this.directory, current.getName() + fileSuffix)); + }else if(moveFile){ + return current.renameTo(new File(this.targetDirectory, current.getName())); + }else{ + return true; + } + } + + private boolean copyLogFile(File current) throws IOException { + if (current.length() == 0L) { + logger.info("Delete empty file{}", current.getName()); + return current.delete(); + } + for(File targetDir : this.copyDirectory){ + File targetFile = new File(targetDir.getAbsolutePath(), current.getName() + fileSuffix); + boolean copyResult = this.copyFile(current, targetFile, false); + if(!copyResult) return false; + } + + return true; + } + + /** + * 复制单个文件 + * + * @param srcFile + * 待复制的文件名 + * @param destFile + * 目标文件名 + * @param overlay + * 如果目标文件存在,是否覆盖 + * @return 如果复制成功返回true,否则返回false + */ + public boolean copyFile(File srcFile, File destFile, + boolean overlay) throws IOException { + + + // 判断源文件是否存在 + if (!srcFile.exists()) { + throw new IOException("Copy file failed, source file does not exists"); + } else if (!srcFile.isFile()) { + String MESSAGE = "复制文件失败,源文件:" + srcFile.getName() + "不是一个文件!"; + throw new IOException("Copy file failed, source file is not a file"); + } + + // 判断目标文件是否存在 + if (destFile.exists()) { + // 如果目标文件存在并允许覆盖 + if (overlay) { + // 删除已经存在的目标文件,无论目标文件是目录还是单个文件 + destFile.delete(); + } + } else { + // 如果目标文件所在目录不存在,则创建目录 + if (!destFile.getParentFile().exists()) { + // 目标文件所在目录不存在 + if (!destFile.getParentFile().mkdirs()) { + // 复制文件失败:创建目标文件所在目录失败 + return false; + } + } + } + + // 复制文件 + int byteread = 0; // 读取的字节数 + InputStream in = null; + OutputStream out = null; + + try { + in = new FileInputStream(srcFile); + out = new FileOutputStream(destFile); + byte[] buffer = new byte[1024]; + + while ((byteread = in.read(buffer)) != -1) { + out.write(buffer, 0, byteread); + } + return true; + } catch (FileNotFoundException e) { + return false; + } catch (IOException e) { + return false; + } finally { + try { + if (out != null) + out.close(); + if (in != null) + in.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + +} + +