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

[#5460] feat(core): Add FlushIntervalSecs to audit log file writer #5458

Merged
merged 4 commits into from
Nov 5, 2024
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
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
Loading