From 49c7135434757526b9606ff5fe014c0a45c34d42 Mon Sep 17 00:00:00 2001 From: neo <1100909+neowu@users.noreply.github.com> Date: Mon, 30 Dec 2024 21:24:33 -0500 Subject: [PATCH] log-exporter: improve deleting trace log dir Signed-off-by: neo <1100909+neowu@users.noreply.github.com> --- .../java/core/framework/util/FilesTest.java | 1 + .../java/core/log/service/ArchiveService.java | 21 +++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/core-ng/src/test/java/core/framework/util/FilesTest.java b/core-ng/src/test/java/core/framework/util/FilesTest.java index 8b5b0cef7..455f6193f 100644 --- a/core-ng/src/test/java/core/framework/util/FilesTest.java +++ b/core-ng/src/test/java/core/framework/util/FilesTest.java @@ -32,5 +32,6 @@ void tempDir() { Files.createDir(tempDir.resolve("temp")); // createDir skips if dir already exists Files.deleteDir(tempDir); + assertThat(tempDir).doesNotExist(); } } diff --git a/ext/log-exporter/src/main/java/core/log/service/ArchiveService.java b/ext/log-exporter/src/main/java/core/log/service/ArchiveService.java index e7c370a03..bf696920c 100644 --- a/ext/log-exporter/src/main/java/core/log/service/ArchiveService.java +++ b/ext/log-exporter/src/main/java/core/log/service/ArchiveService.java @@ -2,6 +2,7 @@ import core.framework.crypto.Hash; import core.framework.inject.Inject; +import core.framework.util.Files; import core.framework.util.Network; import core.framework.util.Strings; import org.slf4j.Logger; @@ -9,11 +10,13 @@ import java.io.File; import java.io.IOException; -import java.nio.file.Files; import java.nio.file.Path; import java.time.LocalDate; import java.time.LocalDateTime; +import static java.nio.file.Files.createDirectories; +import static java.nio.file.Files.exists; + /** * @author neo */ @@ -32,18 +35,18 @@ public void uploadArchive(LocalDate date) { String actionLogPath = actionLogPath(date); Path actionLogFilePath = Path.of(logDir.toString(), actionLogPath); - if (Files.exists(actionLogFilePath)) { + if (exists(actionLogFilePath)) { uploadService.upload(actionLogFilePath, actionLogPath); } String eventPath = eventPath(date); Path eventFilePath = Path.of(logDir.toString(), eventPath); - if (Files.exists(eventFilePath)) { + if (exists(eventFilePath)) { uploadService.upload(eventFilePath, eventPath); } Path traceLogDirPath = Path.of(logDir.toString(), Strings.format("/trace/{}", date)); - if (Files.exists(traceLogDirPath)) { + if (exists(traceLogDirPath)) { File[] appDirs = traceLogDirPath.toFile().listFiles(File::isDirectory); if (appDirs != null) { for (File appDir : appDirs) { @@ -69,10 +72,10 @@ public void cleanupArchive(LocalDate date) { shell.execute("rm", "-f", eventFilePath.toString()); Path traceLogDirPath = Path.of(logDir.toString(), Strings.format("/trace/{}", date)); - if (Files.exists(traceLogDirPath)) { - String path = traceLogDirPath.toString(); - shell.execute("find", path, "-type", "f", "-delete"); - shell.execute("find", path, "-type", "d", "-delete"); + if (exists(traceLogDirPath)) { + logger.info("delete trace logs, path={}", traceLogDirPath); + // use shell (rm -rf or find) may cause pod terminate with error code 137 on mounted disk + Files.deleteDir(traceLogDirPath); } } @@ -91,7 +94,7 @@ public String traceLogPath(LocalDateTime now, String app, String id) { public Path initializeLogFilePath(String logPath) throws IOException { Path path = Path.of(logDir.toString(), logPath); Path parent = path.getParent(); - if (parent != null && !Files.exists(parent)) Files.createDirectories(parent); + if (parent != null && !exists(parent)) createDirectories(parent); return path; } }