sentry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sra...@apache.org
Subject incubator-sentry git commit: SENTRY-752: Sentry service audit log file name format should be consistent (Prasad Mujumdar via Sravya Tirukkovalur)
Date Thu, 04 Jun 2015 19:14:35 GMT
Repository: incubator-sentry
Updated Branches:
  refs/heads/master 4d0e2e7c3 -> 42dfe9c8e


SENTRY-752: Sentry service audit log file name format should be consistent (Prasad Mujumdar
via Sravya Tirukkovalur)


Project: http://git-wip-us.apache.org/repos/asf/incubator-sentry/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-sentry/commit/42dfe9c8
Tree: http://git-wip-us.apache.org/repos/asf/incubator-sentry/tree/42dfe9c8
Diff: http://git-wip-us.apache.org/repos/asf/incubator-sentry/diff/42dfe9c8

Branch: refs/heads/master
Commit: 42dfe9c8e3422a68bea6b49c40be8f349760c6ec
Parents: 4d0e2e7
Author: Sravya Tirukkovalur <sravya@clouera.com>
Authored: Thu Jun 4 12:13:48 2015 -0700
Committer: Sravya Tirukkovalur <sravya@clouera.com>
Committed: Thu Jun 4 12:13:48 2015 -0700

----------------------------------------------------------------------
 .../RollingFileWithoutDeleteAppender.java       | 57 +++++++-------------
 .../TestRollingFileWithoutDeleteAppender.java   | 25 +++++++++
 2 files changed, 44 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-sentry/blob/42dfe9c8/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/provider/db/log/appender/RollingFileWithoutDeleteAppender.java
----------------------------------------------------------------------
diff --git a/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/provider/db/log/appender/RollingFileWithoutDeleteAppender.java
b/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/provider/db/log/appender/RollingFileWithoutDeleteAppender.java
index edbd160..7ca5813 100644
--- a/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/provider/db/log/appender/RollingFileWithoutDeleteAppender.java
+++ b/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/provider/db/log/appender/RollingFileWithoutDeleteAppender.java
@@ -22,6 +22,7 @@ import java.io.File;
 import java.io.IOException;
 import java.io.InterruptedIOException;
 import java.io.Writer;
+import java.nio.file.Files;
 
 import org.apache.log4j.FileAppender;
 import org.apache.log4j.Layout;
@@ -57,7 +58,7 @@ public class RollingFileWithoutDeleteAppender extends FileAppender {
    */
   public RollingFileWithoutDeleteAppender(Layout layout, String filename,
       boolean append) throws IOException {
-    super(layout, filename, append);
+    super(layout, getLogFileName(filename), append);
   }
 
   /**
@@ -69,7 +70,7 @@ public class RollingFileWithoutDeleteAppender extends FileAppender {
    */
   public RollingFileWithoutDeleteAppender(Layout layout, String filename)
       throws IOException {
-    super(layout, filename);
+    super(layout, getLogFileName(filename));
   }
 
   /**
@@ -88,10 +89,6 @@ public class RollingFileWithoutDeleteAppender extends FileAppender {
    */
   // synchronization not necessary since doAppend is alreasy synched
   public void rollOver() {
-    File target;
-    File file;
-    String suffix = Long.toString(System.currentTimeMillis());
-
     if (qw != null) {
       long size = ((CountingQuietWriter) qw).getCount();
       LogLog.debug("rolling over count=" + size);
@@ -100,40 +97,19 @@ public class RollingFileWithoutDeleteAppender extends FileAppender {
       nextRollover = size + maxFileSize;
     }
 
-    boolean renameSucceeded = true;
-
-    // Rename fileName to fileName.yyyyMMddHHmmss
-    target = new File(fileName + "." + suffix);
-
     this.closeFile(); // keep windows happy.
 
-    file = new File(fileName);
-    LogLog.debug("Renaming file " + file + " to " + target);
-    renameSucceeded = file.renameTo(target);
-    //
-    // if file rename failed, reopen file with append = true
-    //
-    if (!renameSucceeded) {
-      try {
-        this.setFile(fileName, true, bufferedIO, bufferSize);
-      } catch (IOException e) {
-        if (e instanceof InterruptedIOException) {
-          Thread.currentThread().interrupt();
-        }
-        LogLog.error("setFile(" + fileName + ", true) call failed.", e);
-      }
-    } else {
-      try {
-        // This will also close the file. This is OK since multiple
-        // close operations are safe.
-        this.setFile(fileName, false, bufferedIO, bufferSize);
-        nextRollover = 0;
-      } catch (IOException e) {
-        if (e instanceof InterruptedIOException) {
-          Thread.currentThread().interrupt();
-        }
-        LogLog.error("setFile(" + fileName + ", false) call failed.", e);
+    String newFileName = getLogFileName(fileName);
+    try {
+      // This will also close the file. This is OK since multiple
+      // close operations are safe.
+      this.setFile(newFileName, false, bufferedIO, bufferSize);
+      nextRollover = 0;
+    } catch (IOException e) {
+      if (e instanceof InterruptedIOException) {
+        Thread.currentThread().interrupt();
       }
+      LogLog.error("setFile(" + newFileName + ", false) call failed.", e);
     }
   }
 
@@ -154,7 +130,7 @@ public class RollingFileWithoutDeleteAppender extends FileAppender {
    * required for differentiating the setter taking a <code>long</code> argument
    * from the setter taking a <code>String</code> argument by the JavaBeans
    * {@link java.beans.Introspector Introspector}.
-   * 
+   *
    * @see #setMaxFileSize(String)
    */
   public void setMaximumFileSize(long maxFileSize) {
@@ -192,4 +168,9 @@ public class RollingFileWithoutDeleteAppender extends FileAppender {
       }
     }
   }
+
+  // Mangled file name. Append the current timestamp
+  private static String getLogFileName(String oldFileName) {
+    return oldFileName + "." + Long.toString(System.currentTimeMillis());
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-sentry/blob/42dfe9c8/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/log/appender/TestRollingFileWithoutDeleteAppender.java
----------------------------------------------------------------------
diff --git a/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/log/appender/TestRollingFileWithoutDeleteAppender.java
b/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/log/appender/TestRollingFileWithoutDeleteAppender.java
index 15393da..e1ebce6 100644
--- a/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/log/appender/TestRollingFileWithoutDeleteAppender.java
+++ b/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/log/appender/TestRollingFileWithoutDeleteAppender.java
@@ -20,6 +20,7 @@ package org.apache.sentry.provider.db.log.appender;
 
 import static junit.framework.Assert.assertEquals;
 import static junit.framework.Assert.fail;
+import static org.junit.Assert.assertTrue;
 
 import java.io.File;
 
@@ -74,6 +75,30 @@ public class TestRollingFileWithoutDeleteAppender {
 
   }
 
+  /***
+   * Generate log enough to cause a single rollover. Verify the file name format
+   * @throws Throwable
+   */
+  @Test
+  public void testFileNamePattern() throws Throwable {
+    if (dataDir == null) {
+      fail("Excepted temp folder for audit log is created.");
+    }
+    RollingFileWithoutDeleteAppender appender = new RollingFileWithoutDeleteAppender(
+        new PatternLayout("%m%n"), dataDir.getPath() + "/auditLog.log");
+    appender.setMaximumFileSize(10);
+    sentryLogger.addAppender(appender);
+    sentryLogger.debug("123456789012345");
+    File[] files = dataDir.listFiles();
+    if (files != null) {
+      assertEquals(files.length, 2);
+      assertTrue(files[0].getName().contains("auditLog.log."));
+      assertTrue(files[1].getName().contains("auditLog.log."));
+    } else {
+      fail("Excepted 2 log files.");
+    }
+  }
+
   @After
   public void destroy() {
     if (dataDir != null) {


Mime
View raw message