Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update file listening create file bug #763

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions jcommon/file/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<version>1.4-jdk20-SNAPSHOT</version>
</parent>
<artifactId>file</artifactId>

<version>1.4-jdk21-SNAPSHOT</version>

<dependencies>

Expand All @@ -26,16 +26,13 @@
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<compilerArgs>
<arg>--add-modules=jdk.incubator.concurrent</arg>
<arg>--enable-preview</arg>
</compilerArgs>
<compilerVersion>20</compilerVersion>
<source>20</source>
<target>20</target>
<compilerVersion>21</compilerVersion>
<source>21</source>
<target>21</target>
</configuration>
</plugin>
</plugins>
Expand Down
10 changes: 10 additions & 0 deletions jcommon/file/src/main/java/com/xiaomi/mone/file/LogFile2.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand All @@ -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);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ public void reg(String path, Predicate<String> 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) {
Expand All @@ -111,17 +110,20 @@ public void reg(String path, Predicate<String> 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();
Expand All @@ -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);
}
Expand All @@ -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();
}
Expand Down
Loading