Skip to content

Commit

Permalink
[#5460] feat(core): Add FlushIntervalSecs to audit log file writer (#…
Browse files Browse the repository at this point in the history
…5458)

### 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
  • Loading branch information
FANNG1 authored Nov 5, 2024
1 parent dd9d9cc commit 00a25b2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/org/apache/gravitino/Configs.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ private 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()
Expand Down
36 changes: 29 additions & 7 deletions core/src/main/java/org/apache/gravitino/audit/FileAuditWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;
Expand All @@ -37,17 +38,18 @@
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() {
Expand All @@ -62,6 +64,7 @@ public void init(Formatter formatter, Map<String, String> properties) {
+ "/"
+ 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);
Expand All @@ -76,6 +79,7 @@ public void doWrite(AuditLog auditLog) {
String log = auditLog.toString();
try {
outWriter.write(log + LINE_SEPARATOR);
tryFlush();
} catch (Exception e) {
Log.warn("Failed to write audit log: {}", log, e);
}
Expand All @@ -96,4 +100,22 @@ public void close() {
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);
}
}
}
}
9 changes: 5 additions & 4 deletions docs/gravitino-server-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 00a25b2

Please sign in to comment.