HDFS audit advanced features

This topic explains how the Apache Ranger audit writer handles file system detection, stream synchronization across local and cloud storage, dynamic file naming, directory creation, rollover strategies, and error-handling mechanisms.

File system scheme detection

The audit writer automatically detects the file system scheme from the dir property:

// Extracted from fullPath like "hdfs://namenode:8020/path"
// Returns: "HDFS", "WASB", "S3A", etc.
String scheme = logFolder.substring(0, logFolder.indexOf(":")).toUpperCase();

This scheme is used in log messages to help identify which file system is being used.

HFlush capability detection

The writer checks if the underlying stream supports HFlush (HDFS sync capability):

boolean isHFlushCapable = ostream.hasCapability(StreamCapabilities.HFLUSH);

Behavior:

  • HDFS: Uses hflush() for durability guarantees

  • S3/Azure Blob: Uses flush() as hflush() throws UnsupportedOperationException

This ensures compatibility across different file systems while maximizing durability when possible.

Automatic file uniqueness

If a file already exists (common with multiple plugin instances), the writer automatically appends a numeric suffix:

Original: hive_ranger_audit_node1.log
Exists? → hive_ranger_audit_node1.log.1
Exists? → hive_ranger_audit_node1.log.2
...

This prevents:

  • File overwrites

  • Write conflicts

  • Data loss from simultaneous writes

Parent directory creation

The writer automatically creates parent directories if they don't exist:

fileSystem.mkdirs(parentPath);

You don't need to pre-create directory structures; they will be created automatically with appropriate permissions.

Rollover strategies

Two rollover strategies are supported:
  • Duration-based rollover

    Uses file.rollover.sec - simple duration from last rollover time.

    Pros: Simple, predictable intervals

    Cons: Not aligned to calendar boundaries

  • Period-based rollover

    Uses file.rollover.period - calendar-aware rollover.

    Pros: Aligned to calendar boundaries (midnight, start of week, month, etc.)

    Cons: Slightly more complex

    Recommendation: Use file.rollover.period for better alignment with batch processing schedules and compliance requirements.

Error handling and resilience

Append failure fallback

If append fails, automatically creates a new file:

try {
    ostream = fileSystem.append(auditPath);
} catch (Exception e) {
    logger.error("Failed to append, creating new file");
    createFileSystemFolders();
    ostream = fileSystem.create(auditPath);
}
Stream error detection

Checks for stream errors during write:

if (out.checkError()) {
    logger.error("Stream encountered errors!");
    closeWriter();
    resetWriter();
    return false;
}
Graceful degradation
  • Write failures return false without throwing exceptions

  • Allows audit framework to handle failures (retry, spool, etc.)

  • Prevents service disruption due to audit failures