From a7c815fb85576b5309222f03a4150d53310ed27e Mon Sep 17 00:00:00 2001 From: wtt <30461027+wtt40122@users.noreply.github.com> Date: Mon, 11 Dec 2023 11:21:29 +0800 Subject: [PATCH] feat: update file listening create file bug (#763) --- jcommon/file/pom.xml | 13 +++++-------- .../main/java/com/xiaomi/mone/file/LogFile2.java | 10 ++++++++++ .../file/listener/DefaultMonitorListener.java | 8 +++++--- .../mone/file/listener/OzHeraReadListener.java | 1 + .../xiaomi/mone/file/ozhera/HeraFileMonitor.java | 16 ++++++++++------ 5 files changed, 31 insertions(+), 17 deletions(-) diff --git a/jcommon/file/pom.xml b/jcommon/file/pom.xml index 3a9b97364..8f4a84f9a 100644 --- a/jcommon/file/pom.xml +++ b/jcommon/file/pom.xml @@ -9,7 +9,7 @@ 1.4-jdk20-SNAPSHOT file - + 1.4-jdk21-SNAPSHOT @@ -26,16 +26,13 @@ + org.apache.maven.plugins maven-compiler-plugin 3.11.0 - - --add-modules=jdk.incubator.concurrent - --enable-preview - - 20 - 20 - 20 + 21 + 21 + 21 diff --git a/jcommon/file/src/main/java/com/xiaomi/mone/file/LogFile2.java b/jcommon/file/src/main/java/com/xiaomi/mone/file/LogFile2.java index 83e12ca20..a242d580b 100644 --- a/jcommon/file/src/main/java/com/xiaomi/mone/file/LogFile2.java +++ b/jcommon/file/src/main/java/com/xiaomi/mone/file/LogFile2.java @@ -79,6 +79,15 @@ public LogFile2(String file) { this.pointer = readPointer(); } + public LogFile2(String file, long pointer, long lineNumber) { + this.file = file; + File f = new File(this.file); + this.fileKey = FileUtils.fileKey(f); + this.md5 = md5(file); + this.pointer = pointer; + this.lineNumber = lineNumber; + } + public LogFile2(String file, ReadListener listener, long pointer, long lineNumber) { this.file = file; @@ -114,6 +123,7 @@ public void readLine() throws IOException { } catch (Exception e) { log.error("file.length() IOException, file:{}", this.file, e); } + log.info("rel open file:{},pointer:{}", file, this.pointer); raf.seek(pointer); while (true) { diff --git a/jcommon/file/src/main/java/com/xiaomi/mone/file/listener/DefaultMonitorListener.java b/jcommon/file/src/main/java/com/xiaomi/mone/file/listener/DefaultMonitorListener.java index 5592c651f..7681e3fa9 100644 --- a/jcommon/file/src/main/java/com/xiaomi/mone/file/listener/DefaultMonitorListener.java +++ b/jcommon/file/src/main/java/com/xiaomi/mone/file/listener/DefaultMonitorListener.java @@ -38,7 +38,7 @@ public void onEvent(FileEvent event) { LogFile2 logFile = new LogFile2(event.getFileName()); pool.submit(() -> { logFile.setListener(new OzHeraReadListener(monitor, logFile, consumer)); - SafeRun.run(() -> logFile.readLine()); + SafeRun.run(logFile::readLine); }); } @@ -58,10 +58,12 @@ public void onEvent(FileEvent event) { if (event.getType().equals(EventType.create)) { log.info("create:{}", event.getFileName()); - LogFile2 logFile = new LogFile2(event.getFileName()); + +// LogFile2 logFile = new LogFile2(event.getFileName()); + LogFile2 logFile = new LogFile2(event.getFileName(), 0, 0); pool.submit(() -> { logFile.setListener(new OzHeraReadListener(monitor, logFile, consumer)); - SafeRun.run(() -> logFile.readLine()); + SafeRun.run(logFile::readLine); }); } } diff --git a/jcommon/file/src/main/java/com/xiaomi/mone/file/listener/OzHeraReadListener.java b/jcommon/file/src/main/java/com/xiaomi/mone/file/listener/OzHeraReadListener.java index 1e8eca36a..687db6f05 100644 --- a/jcommon/file/src/main/java/com/xiaomi/mone/file/listener/OzHeraReadListener.java +++ b/jcommon/file/src/main/java/com/xiaomi/mone/file/listener/OzHeraReadListener.java @@ -49,6 +49,7 @@ public boolean isBreak(String line) { if (null == line) { HeraFile f = monitor.getMap().get(logFile.getFileKey()); if (null == f || f.getState().get() == 1) { + log.info("file isBreak,file:{},f:{}", logFile.getFile(), f); return true; } } diff --git a/jcommon/file/src/main/java/com/xiaomi/mone/file/ozhera/HeraFileMonitor.java b/jcommon/file/src/main/java/com/xiaomi/mone/file/ozhera/HeraFileMonitor.java index 124a52293..b65243289 100644 --- a/jcommon/file/src/main/java/com/xiaomi/mone/file/ozhera/HeraFileMonitor.java +++ b/jcommon/file/src/main/java/com/xiaomi/mone/file/ozhera/HeraFileMonitor.java @@ -92,7 +92,6 @@ public void reg(String path, Predicate predicate) throws IOException, In if (!predicate.test(filePath) || modifiedFile.getFileName().toString().startsWith(".")) { continue; } - log.debug("epoll result,path:{}", event.kind() + filePath); HeraFile hfile = fileMap.get(filePath); if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) { @@ -111,17 +110,20 @@ public void reg(String path, Predicate predicate) throws IOException, In } if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) { - File file = new File(path + "" + modifiedFile.getFileName()); + File file = new File(filePath); Object k = FileUtils.fileKey(file); + + log.info("ENTRY_CREATE filePath:{},fileKey:{}", filePath, k); + HeraFile hf = HeraFile.builder().file(file).fileKey(k).fileName(filePath).build(); + map.putIfAbsent(k, hf); + fileMap.put(filePath, hf); + if (map.containsKey(k)) { log.info("change name " + map.get(k) + "--->" + file); listener.onEvent(FileEvent.builder().fileKey(k).type(EventType.rename).build()); } else { listener.onEvent(FileEvent.builder().type(EventType.create).fileName(file.getPath()).build()); } - HeraFile hf = HeraFile.builder().file(file).fileKey(k).fileName(filePath).build(); - map.putIfAbsent(k, hf); - fileMap.put(filePath, hf); } } key.reset(); @@ -136,9 +138,9 @@ private HeraFile initFile(File it) { if (name.startsWith(".")) { return null; } - Object fileKey = FileUtils.fileKey(it); lock.lock(); try { + Object fileKey = FileUtils.fileKey(it); if (map.containsKey(fileKey)) { return map.get(fileKey); } @@ -152,6 +154,8 @@ private HeraFile initFile(File it) { fileMap.put(hf.getFileName(), hf); this.listener.onEvent(FileEvent.builder().pointer(pointer).type(EventType.init).fileName(hf.getFileName()).build()); return hf; + } catch (Exception e) { + log.error("init file error,fileName:{}", name, e); } finally { lock.unlock(); }