Skip to content

Commit

Permalink
RANGER-5153: fix for intermittent unit test failure in RangerJSONAudi…
Browse files Browse the repository at this point in the history
…tWriterTest (#536)
  • Loading branch information
mneethiraj authored Feb 22, 2025
1 parent 7d553f1 commit 324d5db
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public abstract class AbstractRangerAuditWriter implements RangerAuditWriter {
public FileSystem fileSystem;
public Map<String, String> auditConfigs;
public Path auditPath;
public PrintWriter logWriter;
public RollingTimeUtil rollingTimeUtil;
public String auditProviderName;
public String fullPath;
Expand All @@ -71,6 +70,7 @@ public abstract class AbstractRangerAuditWriter implements RangerAuditWriter {
public int fileRolloverSec = 24 * 60 * 60; // In seconds
public boolean rollOverByDuration;

public volatile PrintWriter logWriter;
public volatile FSDataOutputStream ostream; // output stream wrapped in logWriter

protected boolean reUseLastLogFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* under the License.
*/

import org.apache.commons.collections.CollectionUtils;
import org.apache.ranger.audit.provider.MiscUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -94,17 +95,23 @@ public synchronized boolean logJSON(final Collection<String> events) throws Exce
logger.debug("UGI = {}, will write to HDFS file = {}", MiscUtil.getUGILoginUser(), currentFileName);

out = MiscUtil.executePrivilegedAction((PrivilegedExceptionAction<PrintWriter>) () -> {
PrintWriter out1 = getLogFileStream();
PrintWriter out1 = null;

for (String event : events) {
out1.println(event);
if (CollectionUtils.isEmpty(events)) {
closeFileIfNeeded();
} else {
out1 = getLogFileStream();

for (String event : events) {
out1.println(event);
}
}

return out1;
});

// flush and check the stream for errors
if (out.checkError()) {
if (out != null && out.checkError()) {
// In theory, this count may NOT be accurate as part of the messages may have been successfully written.
// However, in practice, since client does buffering, either all or none would succeed.
logger.error("Stream encountered errors while writing audits to HDFS!");
Expand Down

0 comments on commit 324d5db

Please sign in to comment.