-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update file montior * update file montior pattern
- Loading branch information
Showing
9 changed files
with
53 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package com.xiaomi.mone.file.listener; | ||
|
||
import com.xiaomi.mone.file.LogFile2; | ||
import com.xiaomi.mone.file.ReadEvent; | ||
import com.xiaomi.mone.file.common.SafeRun; | ||
import com.xiaomi.mone.file.event.EventListener; | ||
import com.xiaomi.mone.file.event.EventType; | ||
|
@@ -10,6 +11,7 @@ | |
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* @author [email protected] | ||
|
@@ -20,10 +22,13 @@ public class DefaultMonitorListener implements EventListener { | |
|
||
private HeraFileMonitor monitor; | ||
|
||
private Consumer<ReadEvent> consumer; | ||
|
||
private ExecutorService pool = Executors.newVirtualThreadPerTaskExecutor(); | ||
|
||
public DefaultMonitorListener(HeraFileMonitor monitor) { | ||
public DefaultMonitorListener(HeraFileMonitor monitor, Consumer<ReadEvent> consumer) { | ||
this.monitor = monitor; | ||
this.consumer = consumer; | ||
} | ||
|
||
@Override | ||
|
@@ -32,7 +37,7 @@ public void onEvent(FileEvent event) { | |
log.info("log file:{}", event.getFileName()); | ||
LogFile2 logFile = new LogFile2(event.getFileName()); | ||
pool.submit(() -> { | ||
logFile.setListener(new OzHeraReadListener(monitor, logFile)); | ||
logFile.setListener(new OzHeraReadListener(monitor, logFile, consumer)); | ||
SafeRun.run(() -> logFile.readLine()); | ||
}); | ||
} | ||
|
@@ -55,7 +60,7 @@ public void onEvent(FileEvent event) { | |
log.info("create:{}", event.getFileName()); | ||
LogFile2 logFile = new LogFile2(event.getFileName()); | ||
pool.submit(() -> { | ||
logFile.setListener(new OzHeraReadListener(monitor, logFile)); | ||
logFile.setListener(new OzHeraReadListener(monitor, logFile, consumer)); | ||
SafeRun.run(() -> logFile.readLine()); | ||
}); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* @author [email protected] | ||
|
@@ -21,14 +22,17 @@ public class OzHeraReadListener implements ReadListener { | |
|
||
private LogFile2 logFile; | ||
|
||
public OzHeraReadListener(HeraFileMonitor monitor, LogFile2 logFile) { | ||
private Consumer<ReadEvent> consumer; | ||
|
||
public OzHeraReadListener(HeraFileMonitor monitor, LogFile2 logFile, Consumer<ReadEvent> consumer) { | ||
this.monitor = monitor; | ||
this.logFile = logFile; | ||
this.consumer = consumer; | ||
} | ||
|
||
@Override | ||
public void onEvent(ReadEvent event) { | ||
System.out.println(event.getReadResult().getLines()); | ||
consumer.accept(event); | ||
} | ||
|
||
@Override | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,12 @@ | |
import java.nio.file.*; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* @author [email protected] | ||
|
@@ -65,23 +67,23 @@ public HeraFileMonitor(EventListener listener) { | |
this.listener = listener; | ||
} | ||
|
||
public void reg(String path) throws IOException, InterruptedException { | ||
public void reg(String path, Predicate<String> predicate) throws IOException, InterruptedException { | ||
Path directory = Paths.get(path); | ||
File f = directory.toFile(); | ||
|
||
Arrays.stream(f.listFiles()).forEach(it -> initFile(it)); | ||
Arrays.stream(Objects.requireNonNull(f.listFiles())).filter(it -> predicate.test(it.getPath())).forEach(this::initFile); | ||
|
||
WatchService watchService = FileSystems.getDefault().newWatchService(); | ||
directory.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_CREATE); | ||
while (true) { | ||
WatchKey key = watchService.take(); | ||
for (WatchEvent<?> event : key.pollEvents()) { | ||
Path modifiedFile = (Path) event.context(); | ||
if (modifiedFile.getFileName().toString().startsWith(".")) { | ||
String filePath = String.format("%s%s", path, modifiedFile.getFileName().toString()); | ||
if (!predicate.test(filePath) || modifiedFile.getFileName().toString().startsWith(".")) { | ||
continue; | ||
} | ||
String filePath = path + "" + modifiedFile.getFileName(); | ||
log.info(event.kind() + " " + filePath); | ||
log.debug("epoll result,path:{}", event.kind() + filePath); | ||
HeraFile hfile = fileMap.get(filePath); | ||
|
||
if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
|
||
import java.io.IOException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* @Author [email protected] | ||
|
@@ -63,14 +64,23 @@ public boolean isContinue(String line) { | |
@SneakyThrows | ||
@Test | ||
public void testLogFileMonitor() { | ||
FileInfoCache.ins().load(); | ||
Runtime.getRuntime().addShutdownHook(new Thread(()->{ | ||
// FileInfoCache.ins().load(); | ||
FileInfoCache.ins().load("/home/work/log/log-agent/milog/memory/.ozhera_pointer"); | ||
Runtime.getRuntime().addShutdownHook(new Thread(() -> { | ||
log.info("shutdown"); | ||
FileInfoCache.ins().shutdown(); | ||
})); | ||
HeraFileMonitor monitor = new HeraFileMonitor(); | ||
monitor.setListener(new DefaultMonitorListener(monitor)); | ||
monitor.reg("/tmp/e/"); | ||
monitor.setListener(new DefaultMonitorListener(monitor, readEvent -> { | ||
System.out.println(readEvent.getReadResult().getLines()); | ||
})); | ||
String fileName = "/home/work/log/test/provider/server.log.*"; | ||
Pattern pattern = Pattern.compile(fileName); | ||
monitor.reg("/home/work/log/test/provider/", it -> { | ||
boolean matches = pattern.matcher(it).matches(); | ||
log.info("file:{},matches:{}", it, matches); | ||
return matches; | ||
}); | ||
log.info("reg finish"); | ||
System.in.read(); | ||
} | ||
|