This is an automated email from the ASF dual-hosted git repository.
fanng pushed a commit to branch branch-0.7
in repository https://gitbox.apache.org/repos/asf/gravitino.git
The following commit(s) were added to refs/heads/branch-0.7 by this push:
new ea5836831 [#5460] feat(core): Add FlushIntervalSecs to audit log file
writer (#5471)
ea5836831 is described below
commit ea5836831db31eeb5d56565ff2b44793885e4497
Author: github-actions[bot]
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Tue Nov 5 19:47:21 2024 +0800
[#5460] feat(core): Add FlushIntervalSecs to audit log file writer (#5471)
### What changes were proposed in this pull request?
1. keep `gravitino.audit.enabled` configuration consistent with document
2. add FlushIntervalSecs to flush log writer
### Why are the changes needed?
Fix: #5460
### Does this PR introduce _any_ user-facing change?
No
### How was this patch tested?
enable audit log and check audit is written after about 10s
Co-authored-by: FANNG <[email protected]>
---
.../main/java/org/apache/gravitino/Configs.java | 2 +-
.../apache/gravitino/audit/FileAuditWriter.java | 36 +++++++++++++++++-----
docs/gravitino-server-config.md | 9 +++---
3 files changed, 35 insertions(+), 12 deletions(-)
diff --git a/core/src/main/java/org/apache/gravitino/Configs.java
b/core/src/main/java/org/apache/gravitino/Configs.java
index 81388608d..fb30f57ab 100644
--- a/core/src/main/java/org/apache/gravitino/Configs.java
+++ b/core/src/main/java/org/apache/gravitino/Configs.java
@@ -348,7 +348,7 @@ public class Configs {
public static final String AUDIT_LOG_WRITER_CONFIG_PREFIX =
"gravitino.audit.writer.";
public static final ConfigEntry<Boolean> AUDIT_LOG_ENABLED_CONF =
- new ConfigBuilder("gravitino.audit.enable")
+ new ConfigBuilder("gravitino.audit.enabled")
.doc("Gravitino audit log enable flag")
.version(ConfigConstants.VERSION_0_7_0)
.booleanConf()
diff --git a/core/src/main/java/org/apache/gravitino/audit/FileAuditWriter.java
b/core/src/main/java/org/apache/gravitino/audit/FileAuditWriter.java
index cc3d93078..a8cefea80 100644
--- a/core/src/main/java/org/apache/gravitino/audit/FileAuditWriter.java
+++ b/core/src/main/java/org/apache/gravitino/audit/FileAuditWriter.java
@@ -25,6 +25,7 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
+import java.time.Instant;
import java.util.Map;
import org.apache.gravitino.exceptions.GravitinoRuntimeException;
import org.slf4j.Logger;
@@ -37,17 +38,18 @@ import org.slf4j.LoggerFactory;
public class FileAuditWriter implements AuditLogWriter {
private static final Logger Log =
LoggerFactory.getLogger(FileAuditWriter.class);
- public static final String AUDIT_LOG_FILE_NAME = "fileName";
+ private static final String AUDIT_LOG_FILE_NAME = "fileName";
+ private static final String APPEND = "append";
+ private static final String FLUSH_INTERVAL_SECS = "flushIntervalSecs";
+ private static final String LINE_SEPARATOR = System.lineSeparator();
- public static final String APPEND = "append";
-
- public static final String LINE_SEPARATOR = System.lineSeparator();
-
- Formatter formatter;
@VisibleForTesting Writer outWriter;
@VisibleForTesting String fileName;
- boolean append;
+ private Formatter formatter;
+ private boolean append;
+ private int flushIntervalSecs;
+ private Instant nextFlushTime = Instant.now();
@Override
public Formatter getFormatter() {
@@ -62,6 +64,7 @@ public class FileAuditWriter implements AuditLogWriter {
+ "/"
+ properties.getOrDefault(AUDIT_LOG_FILE_NAME,
"gravitino_audit.log");
this.append = Boolean.parseBoolean(properties.getOrDefault(APPEND,
"true"));
+ this.flushIntervalSecs =
Integer.parseInt(properties.getOrDefault(FLUSH_INTERVAL_SECS, "10"));
try {
OutputStream outputStream = new FileOutputStream(fileName, append);
this.outWriter = new OutputStreamWriter(outputStream,
StandardCharsets.UTF_8);
@@ -76,6 +79,7 @@ public class FileAuditWriter implements AuditLogWriter {
String log = auditLog.toString();
try {
outWriter.write(log + LINE_SEPARATOR);
+ tryFlush();
} catch (Exception e) {
Log.warn("Failed to write audit log: {}", log, e);
}
@@ -96,4 +100,22 @@ public class FileAuditWriter implements AuditLogWriter {
public String name() {
return "file";
}
+
+ private void tryFlush() {
+ Instant now = Instant.now();
+ if (now.isAfter(nextFlushTime)) {
+ nextFlushTime = now.plusSeconds(flushIntervalSecs);
+ doFlush();
+ }
+ }
+
+ private void doFlush() {
+ if (outWriter != null) {
+ try {
+ outWriter.flush();
+ } catch (Exception e) {
+ Log.warn("Flush audit log failed,", e);
+ }
+ }
+ }
}
diff --git a/docs/gravitino-server-config.md b/docs/gravitino-server-config.md
index 19481d7da..d9eb48977 100644
--- a/docs/gravitino-server-config.md
+++ b/docs/gravitino-server-config.md
@@ -171,10 +171,11 @@ The `AuditLogWriter` defines an interface that enables
the writing of metadata a
Writer configuration begins with `gravitino.audit.writer.${name}`, where
${name} is replaced with the actual writer name defined in method `name()`.
`FileAuditWriter` is a default implement to log audit information, whose name
is `file`.
-| Property name | Description
| Default value | Required |
Since Version |
-|----------------------------------------|-------------------------------------------------------------------------------|---------------------|----------|------------------|
-| `gravitino.audit.writer.file.fileName` | The audit log file name, the path
is `${sys:gravitino.log.path}/${fileName}`. | gravitino_audit.log | NO |
0.7.0-incubating |
-| `gravitino.audit.writer.file.append` | Whether the log will be written to
the end or the beginning of the file. | true | NO |
0.7.0-incubating |
+| Property name | Description
| Default value |
Required | Since Version |
+|-------------------------------------------------|-------------------------------------------------------------------------------|---------------------|----------|------------------|
+| `gravitino.audit.writer.file.fileName` | The audit log file name,
the path is `${sys:gravitino.log.path}/${fileName}`. | gravitino_audit.log | NO
| 0.7.0-incubating |
+| `gravitino.audit.writer.file.flushIntervalSecs` | The flush interval time of
the audit file in seconds. | 10 | NO
| 0.7.0-incubating |
+| `gravitino.audit.writer.file.append` | Whether the log will be
written to the end or the beginning of the file. | true |
NO | 0.7.0-incubating |
### Security configuration