Skip to content

Commit

Permalink
refactor: update file coll (XiaoMi#889)
Browse files Browse the repository at this point in the history
* refactor: update es and upgrade file

* fix: Fixed the issue of reading a large number of files OOM and upgraded the ES version

* refactor: update compile jdk version

* refactor: update file coll
  • Loading branch information
wtt40122 authored Aug 29, 2024
1 parent 766ce45 commit bd03aa7
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ public void readLine() throws IOException {
readResult.setFilePathName(file);
readResult.setLineNumber(++lineNumber);
ReadEvent event = new ReadEvent(readResult);
listener.setReadTime();

listener.onEvent(event);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ default void setPointer(Object obj) {
default void saveProgress() {
}

default void setReadTime() {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,12 @@ public void setPointer(Object obj) {
}
}
}

@Override
public void setReadTime() {
HeraFile f = monitor.getFileMap().get(logFile.getFileKey());
if (null != f) {
f.getReadTime().set(System.currentTimeMillis());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ public class HeraFile {

@Builder.Default
private AtomicLong utime = new AtomicLong(System.currentTimeMillis());

@Builder.Default
private AtomicLong readTime = new AtomicLong(System.currentTimeMillis());
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class HeraFileMonitor {
@Getter
private ConcurrentHashMap<Object, HeraFile> map = new ConcurrentHashMap<>();

@Getter
private ConcurrentHashMap<String, HeraFile> fileMap = new ConcurrentHashMap<>();

@Setter
Expand All @@ -52,7 +53,7 @@ public HeraFileMonitor(long removeTime) {
List<Pair<String, Object>> remList = Lists.newArrayList();
long now = System.currentTimeMillis();
fileMap.values().forEach(it -> {
if (now - it.getUtime().get() >= removeTime) {
if (now - it.getUtime().get() >= removeTime && now - it.getReadTime().get() >= removeTime) {
remList.add(Pair.of(it.getFileName(), it.getFileKey()));
}
});
Expand Down Expand Up @@ -129,7 +130,7 @@ public void reg(String path, Predicate<String> predicate) throws IOException, In
map.putIfAbsent(k, hf);
fileMap.put(filePath, hf);

listener.onEvent(FileEvent.builder().type(EventType.create).fileName(file.getPath()).build());
listener.onEvent(FileEvent.builder().type(EventType.create).fileKey(k).fileName(file.getPath()).build());
}
}
}
Expand Down Expand Up @@ -167,7 +168,12 @@ private HeraFile initFile(File it) {
log.info("initFile fileName:{},fileKey:{}", name, fileKey);
map.put(hf.getFileKey(), hf);
fileMap.put(hf.getFileName(), hf);
this.listener.onEvent(FileEvent.builder().pointer(pointer).type(EventType.init).fileName(hf.getFileName()).build());
this.listener.onEvent(FileEvent.builder()
.pointer(pointer)
.type(EventType.init)
.fileName(hf.getFileName())
.fileKey(hf.getFileKey())
.build());
return hf;
} catch (Exception e) {
log.error("init file error,fileName:{}", name, e);
Expand Down
16 changes: 14 additions & 2 deletions jcommon/file/src/test/java/com/xiaomi/mone/file/LogFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -74,9 +76,14 @@ public void testLogFileMonitor() {
monitor.setListener(new DefaultMonitorListener(monitor, readEvent -> {
System.out.println(readEvent.getReadResult().getLines());
}));
String fileName = "/home/work/log/file.log.*";
String fileName = "/home/work/log/test/file*.txt";
Pattern pattern = Pattern.compile(fileName);
monitor.reg("/home/work/log", it -> {

ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

scheduler.scheduleAtFixedRate(this::test, 1, 2, TimeUnit.SECONDS);

monitor.reg("/home/work/log/test", it -> {
boolean matches = pattern.matcher(it).matches();
log.info("file:{},matches:{}", it, true);
return true;
Expand All @@ -85,6 +92,11 @@ public void testLogFileMonitor() {
System.in.read();
}

private void test() {
log.info("test save progress");
FileInfoCache.ins().shutdown();
}

@Test
public void testLogWS() throws IOException {
LogFile log = new LogFile("D:\\test.log", new ReadListener() {
Expand Down

0 comments on commit bd03aa7

Please sign in to comment.