让Flume的TaildirSource支持Windows操作系统

最近准备重写以前的监控日志文件变动的项目,以前监控日志文件变动使用的技术基础是jdk1.7新出的WatchService,但是使用了接近一年以后发现了几个问题:

1.无法指定专门文件监听,只能对整个目录的所有文件进行监听,然后自己过滤
2.当修改了目录或者文件的任何metadata后,就不会再传入任何变动事件给监听者了
3.无法很好的控制监控频率

对于以上的三个问题,使用WatchService技术无法很好的解决他们,所以到处找别的解决方案。
因为我使用的是flume, 版本是1.6,当我看到flume1.7使用的TaildirSource的时候,忍不住眼前一亮,这就是我需要的解决方案。但是使用之前我要判断下是否有坑,于是去读了下他的源代码。

TaildirSource的代码很简单,主要就是五个类:
ReliableTaildirEventReader类
TaildirMatcher类
TaildirSource类
TaildirSourceConfigurationConstants类
TailFile类

这五个类之间的关系描述如下:
TailSource不用说,Flume的启动点
在其process方法中
TailSource调用ReliableTaildirEventReader的updateTailFiles方法去获取所有需要关注的文件,并读取解析
而为了只关注需要关注的文件,在ReliableTaildirEventReader的updateTailFiles方法中使用了TaildirMatcher去过滤出需要的文件
而TaildirMatcher和文件系统的目录是一一对应的,一个目录可以抽象为一个TaildirMatcher,而被关注的文件被抽象为TailFile对象
TaildirSourceConfigurationConstants类不用多说,一些配置常量而已

理解了类和代码,我们就可以明白整套TaildirSource动态监听文件变化的技术基础就是获取文件的inode,建立inode和文件之间的一一对应关系,利用RandomAccessFile去读取文件,并将inode和读取的位置以及文件位置保存成json文件进行持久化,以便后续的继续跟踪。
但是,注意,inode是linux文件的概念,而获取inode是在ReliableTaildirEventReader的getInode方法里,在这个方法里,我们将受到一万点暴击:(long) Files.getAttribute(file.toPath(), "unix:ino");
这段代码无耻的排除了windows操作系统的存在。

看明白了TaildirSource的代码,发现TaildirSource是不支持Windows的。怎么办呢?首先想想解决思路:
我们知道整体TaildirSource的思想是获取一个文件的标识(linux里inode可以作为文件的标识使用,当系统读取文件时,其实就是根据文件路径转换成对应的inode值来做的操作)并记录对应的文件路径,那么我们首先要确认在Windows中是否是有类似于inode这种东西的存在。这个回答解答了这个问题,windows中是有file id这种类似于inode的存在的。

那么继续,这个file id是否是有什么限制或者有什么特性呢?
参考这个回答 我们知道file id是跟文件系统有关的, 在FAT系统中,如果修改的名字长于旧名字,file id可能会发生改变,但是在NTFS系统中,在删除之前file id都是稳定的。

ok,方案可以确定了,如果是windows系统 并且文件系统是ntfs,那么我们就使用file id去获取文件,剩下的逻辑几乎和linux是一模一样。
如果是fat系统(在我们的工作环境中不可能出现),我们暂时不做支持。
(另外提一句,在windows 2012中新增加了一个Refs 这个由于我们基本不用,所以没有做考虑,参考这个)
方案落地如下:
使用java的jna
Kernel32的代码:

package com.creditease.ns.jna;/* Copyright (c) 2007, 2013 Timothy Wall, Markus Karg, All Rights Reserved
 *
 * The contents of this file is dual-licensed under 2 
 * alternative Open Source/Free licenses: LGPL 2.1 or later and 
 * Apache License 2.0. (starting with JNA version 4.0.0).
 * 
 * You can freely decide which license you want to apply to 
 * the project.
 * 
 * You may obtain a copy of the LGPL License at:
 * 
 * http://www.gnu.org/licenses/licenses.html
 * 
 * A copy is also included in the downloadable source code package
 * containing JNA, in file "LGPL2.1".
 * 
 * You may obtain a copy of the Apache License at:
 * 
 * http://www.apache.org/licenses/
 * 
 * A copy is also included in the downloadable source code package
 * containing JNA, in file "AL2.0".
 */

import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.WinBase;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.platform.win32.Wincon;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Interface definitions for kernel32.dll. Includes additional
 * alternate mappings from {@link WinNT} which make use of NIO buffers,
 * {@link Wincon} for console API.
 */
public interface Kernel32 extends StdCallLibrary, WinNT, Wincon {

    /**
     * The instance.
     */
    Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class, W32APIOptions.DEFAULT_OPTIONS);

    /**
     * The GetLastError function retrieves the calling thread's last-error code
     * value. The last-error code is maintained on a per-thread basis. Multiple
     * threads do not overwrite each other's last-error code.
     *
     * @return The return value is the calling thread's last-error code value.
     */
    int GetLastError();

    /**
     * The CreateFile function creates or opens a file, file stream, directory,
     * physical disk, volume, console buffer, tape drive, communications
     * resource, mailslot, or named pipe. The function returns a handle that can
     * be used to access an object.
     *
     * @param lpFileName            A pointer to a null-terminated string that specifies the name
     *                              of an object to create or open.
     * @param dwDesiredAccess       The access to the object, which can be read, write, or both.
     * @param dwShareMode           The sharing mode of an object, which can be read, write, both,
     *                              or none.
     * @param lpSecurityAttributes  A pointer to a SECURITY_ATTRIBUTES structure that determines
     *                              whether or not the returned handle can be inherited by child
     *                              processes. If lpSecurityAttributes is NULL, the handle cannot
     *                              be inherited.
     * @param dwCreationDisposition An action to take on files that exist and do not exist.
     * @param dwFlagsAndAttributes  The file attributes and flags.
     * @param hTemplateFile         Handle to a template file with the GENERIC_READ access right.
     *                              The template file supplies file attributes and extended
     *                              attributes for the file that is being created. This parameter
     *                              can be NULL.
     * @return If the function succeeds, the return value is an open handle to a
     * specified file. If a specified file exists before the function
     * call and dwCreationDisposition is CREATE_ALWAYS or OPEN_ALWAYS, a
     * call to GetLastError returns ERROR_ALREADY_EXISTS, even when the
     * function succeeds. If a file does not exist before the call,
     * GetLastError returns 0 (zero). If the function fails, the return
     * value is INVALID_HANDLE_VALUE. To get extended error information,
     * call GetLastError.
     */
    HANDLE CreateFile(String lpFileName, int dwDesiredAccess, int dwShareMode,
        WinBase.SECURITY_ATTRIBUTES lpSecurityAttributes,
        int dwCreationDisposition, int dwFlagsAndAttributes,
        HANDLE hTemplateFile);


    /**
     * Closes an open object handle.
     *
     * @param hObject Handle to an open object. This parameter can be a pseudo
     *                handle or INVALID_HANDLE_VALUE.
     * @return If the function succeeds, the return value is nonzero. If the
     * function fails, the return value is zero. To get extended error
     * information, call {@code GetLastError}.
     * @see CloseHandle
     */
    boolean CloseHandle(HANDLE hObject);


    class BY_HANDLE_FILE_INFORMATION extends Structure {
        public DWORD dwFileAttributes;
        public FILETIME ftCreationTime;
        public FILETIME ftLastAccessTime;
        public FILETIME ftLastWriteTime;
        public DWORD dwVolumeSerialNumber;
        public DWORD nFileSizeHigh;
        public DWORD nFileSizeLow;
        public DWORD nNumberOfLinks;
        public DWORD nFileIndexHigh;
        public DWORD nFileIndexLow;

        public static class ByReference extends BY_HANDLE_FILE_INFORMATION
            implements Structure.ByReference {

        }


        public static class ByValue extends BY_HANDLE_FILE_INFORMATION
            implements Structure.ByValue {

        }

        @Override
        protected List getFieldOrder() {
            List fields = new ArrayList();
            fields.addAll(Arrays.asList(new String[]{"dwFileAttributes",
                "ftCreationTime", "ftLastAccessTime", "ftLastWriteTime",
                "dwVolumeSerialNumber", "nFileSizeHigh", "nFileSizeLow",
                "nNumberOfLinks", "nFileIndexHigh", "nFileIndexLow"}));
            return fields;

        }
    }

    boolean GetFileInformationByHandle(HANDLE hFile,
        BY_HANDLE_FILE_INFORMATION lpFileInformation);
}
package com.creditease.ns.jna;

import com.sun.jna.platform.win32.WinBase;
import com.sun.jna.platform.win32.WinNT;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.Path;

public class WinFileUtils {
    private static Logger logger = LoggerFactory.getLogger(WinFileUtils.class);

    private final static int GENERIC_READ = 0x80000000;
    private final static int FILE_SHARE_READ = 0x00000001;
    private static final WinBase.SECURITY_ATTRIBUTES SECURITY_ATTRIBUTES = null;
    private static final int OPEN_EXISTING = 3;
    private static final int FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;
    public static final String IO_ERROR = "_ERROR_";

    public static String getUniqueFileId(Path file) {
        Kernel32.BY_HANDLE_FILE_INFORMATION nfo = new Kernel32.BY_HANDLE_FILE_INFORMATION();
        WinNT.HANDLE handle = Kernel32.INSTANCE.CreateFile(file.toString(), GENERIC_READ, FILE_SHARE_READ,
            SECURITY_ATTRIBUTES, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, null);
        int errorRet = Kernel32.INSTANCE.GetLastError();
        String identifier;
        if (errorRet != 0) {
            logger.error("创建/打开文件时发生错误 错误级别:{} 文件路径:{}", errorRet, file);
            identifier = IO_ERROR;
        } else {
            Kernel32.INSTANCE.GetFileInformationByHandle(handle, nfo);
            errorRet = Kernel32.INSTANCE.GetLastError();
            if (errorRet != 0) {
                logger.error("获取文件信息时发生错误 错误级别:{} 文件路径:{}", errorRet, file);
                identifier = IO_ERROR;
            } else {
                identifier = nfo.nFileIndexHigh + nfo.nFileIndexLow.toString() + Integer.toHexString(nfo
                    .dwVolumeSerialNumber.intValue());
            }
        }
        Kernel32.INSTANCE.CloseHandle(handle);
        return identifier;
    }
}

以上代码综合参考了这里和这里
再次强调下,以上获取FileId的代码不适用于Refs系统,如果想支持的更完善,请使用JNA 4.4.0版本 这个版本里的kernel32有一个方法叫做GetFileInformationByHandleEx 它是支持最新的Refs系统的。

你可能感兴趣的:(让Flume的TaildirSource支持Windows操作系统)