/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.master.audit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.LinkedBlockingQueue;
import javax.annotation.concurrent.ThreadSafe;

/**
 * {@link AsyncUserAccessAuditLogWriter} writes user access audit log entries asynchronously.
 */
@ThreadSafe
public final class AsyncUserAccessAuditLogWriter {
  private static final int QUEUE_SIZE = 10000;
  private static final Logger AUDIT_LOG =
      LoggerFactory.getLogger(AsyncUserAccessAuditLogWriter.class);
  private volatile boolean mStopped;
  /**
   * A thread-safe linked-list-based queue with an optional capacity limit.
   */
  private LinkedBlockingQueue<AuditContext> mAuditLogEntries;

  /**
   * Background thread that performs actual log writing.
   */
  private Thread mLoggingWorkerThread;

  /**
   * Constructs an {@link AsyncUserAccessAuditLogWriter} instance.
   */
  public AsyncUserAccessAuditLogWriter() {
    mAuditLogEntries = new LinkedBlockingQueue<>(QUEUE_SIZE);
    mStopped = true;
  }

  /**
   * Starts {@link AsyncUserAccessAuditLogWriter}.
   */
  public void start() {
    if (mStopped) {
      mStopped = false;
      mLoggingWorkerThread = new Thread(new AuditLoggingWorker());
      mLoggingWorkerThread.start();
    }
  }

  /**
   * Stops {@link AsyncUserAccessAuditLogWriter}.
   */
  public void stop() {
    if (!mStopped) {
      mLoggingWorkerThread.interrupt();
      try {
        mLoggingWorkerThread.join();
      } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
      } finally {
        mStopped = true;
      }
    }
  }

  /**
   * Appends an audit context.
   *
   * @param context the audit context to append
   * @return true if append operation succeeds, false otherwise
   */
  public boolean append(AuditContext context) {
    try {
      mAuditLogEntries.put(context);
    } catch (InterruptedException e) {
      // Reset the interrupted flag and return because some other thread has
      // told us not to wait any more.
      Thread.currentThread().interrupt();
      return false;
    }
    return true;
  }

  /**
   * Consumer thread of the queue to perform actual logging of audit info.
   */
  private class AuditLoggingWorker implements Runnable {
    public AuditLoggingWorker() {}

    @Override
    public void run() {
      while (!mStopped) {
        try {
          AuditContext headContext = mAuditLogEntries.take();
          AUDIT_LOG.info(headContext.toString());
        } catch (InterruptedException e) {
          Thread.currentThread().interrupt();
          break;
        }
      }
    }
  }
}

@@ -1605,24 +1605,52 @@
 d start() %7B%0A
+    synchronized (this) %7B%0A  
     if (mSto
@@ -1653,24 +1653,26 @@
 mStopped) %7B%0A
+  
       mStopp
@@ -1681,32 +1681,34 @@
  = false;%0A      
+  
 mLoggingWorkerTh
@@ -1754,24 +1754,26 @@
 r());%0A      
+  
 mLoggingWork
@@ -1790,22 +1790,30 @@
 tart();%0A
+  
     %7D%0A
+    %7D%0A
   %7D%0A%0A  /
@@ -1890,24 +1890,52 @@
 id stop() %7B%0A
+    synchronized (this) %7B%0A  
     if (!mSt
@@ -1945,24 +1945,26 @@
 ed) %7B%0A      
+ 
+ 
 mLoggingWork
@@ -1981,24 +1981,26 @@
 nterrupt();%0A
+  
       try %7B%0A
@@ -1999,32 +1999,34 @@
   try %7B%0A        
+  
 mLoggingWorkerTh
@@ -2040,24 +2040,26 @@
 in();%0A      
+ 
+ 
 %7D catch (Int
@@ -2073,32 +2073,34 @@
 dException e) %7B%0A
+  
         Thread.c
@@ -2133,16 +2133,18 @@
 ;%0A      
+  
 %7D finall
@@ -2147,32 +2147,34 @@
 nally %7B%0A        
+ 
+ 
 mStopped = true;
@@ -2174,16 +2174,26 @@
 = true;%0A
+        %7D%0A
       %7D%0A

Make AsyncUserAccessAuditLogWriter truly thread-safe.