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)

(cherry picked from commit 324d5db)
  • Loading branch information
mneethiraj committed Feb 22, 2025
1 parent c913aac commit aaaef8f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public abstract class AbstractRangerAuditWriter implements RangerAuditWriter {
public FileSystem fileSystem = null;
public Map<String, String> auditConfigs = null;
public Path auditPath = null;
public PrintWriter logWriter = null;
public volatile PrintWriter logWriter = null;
public RollingTimeUtil rollingTimeUtil = null;
public String auditProviderName = null;
public String fullPath = null;
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 @@ -90,15 +91,23 @@ synchronized public boolean logJSON(final Collection<String> events) throws Exce
out = MiscUtil.executePrivilegedAction(new PrivilegedExceptionAction<PrintWriter>() {
@Override
public PrintWriter run() throws Exception {
PrintWriter out = getLogFileStream();
for (String event : events) {
out.println(event);
PrintWriter out = null;

if (CollectionUtils.isEmpty(events)) {
closeFileIfNeeded();
} else {
out = getLogFileStream();

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

return out;
};
});
// 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 aaaef8f

Please sign in to comment.