You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by kl...@apache.org on 2017/01/18 02:41:17 UTC

geode git commit: Fix up intialization and remove deep dependency on isLoner

Repository: geode
Updated Branches:
  refs/heads/feature/GEODE-2297-2 a04a3eb1c -> 6e636f7b9


Fix up intialization and remove deep dependency on isLoner


Project: http://git-wip-us.apache.org/repos/asf/geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/6e636f7b
Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/6e636f7b
Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/6e636f7b

Branch: refs/heads/feature/GEODE-2297-2
Commit: 6e636f7b968d78eac7e02d2c54cc7f2485c73651
Parents: a04a3eb
Author: Kirk Lund <kl...@apache.org>
Authored: Tue Jan 17 18:40:35 2017 -0800
Committer: Kirk Lund <kl...@apache.org>
Committed: Tue Jan 17 18:40:35 2017 -0800

----------------------------------------------------------------------
 .../internal/InternalDistributedSystem.java     | 114 ++++++++++---------
 .../internal/logging/ManagerLogWriter.java      |  32 ++----
 .../logging/SecurityManagerLogWriter.java       |  13 +--
 .../internal/logging/log4j/AlertAppender.java   |  10 +-
 .../internal/logging/log4j/AlertService.java    |   4 +
 .../logging/log4j/LogWriterAppenders.java       |   8 +-
 .../internal/logging/TestLogWriterFactory.java  |   2 +-
 7 files changed, 92 insertions(+), 91 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/geode/blob/6e636f7b/geode-core/src/main/java/org/apache/geode/distributed/internal/InternalDistributedSystem.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/distributed/internal/InternalDistributedSystem.java b/geode-core/src/main/java/org/apache/geode/distributed/internal/InternalDistributedSystem.java
index f53e887..db2a5c6 100644
--- a/geode-core/src/main/java/org/apache/geode/distributed/internal/InternalDistributedSystem.java
+++ b/geode-core/src/main/java/org/apache/geode/distributed/internal/InternalDistributedSystem.java
@@ -570,50 +570,9 @@ public class InternalDistributedSystem extends DistributedSystem
     try {
       SocketCreatorFactory.setDistributionConfig(config);
 
-      // LOG: create LogWriterAppender(s) if log-file or security-log-file is specified
-      final boolean hasLogFile =
-          this.config.getLogFile() != null && !this.config.getLogFile().equals(new File(""));
-      final boolean hasSecurityLogFile = this.config.getSecurityLogFile() != null
-          && !this.config.getSecurityLogFile().equals(new File(""));
-      LogService.configureLoggers(hasLogFile, hasSecurityLogFile);
-      if (hasLogFile || hasSecurityLogFile) {
-
-        // main log file
-        if (hasLogFile) {
-          // if log-file then create logWriterAppender
-          this.logWriterAppender = LogWriterAppenders.getOrCreateAppender(
-              LogWriterAppenders.Identifier.MAIN, this.isLoner, this.config, true);
-        }
-
-        // security log file
-        if (hasSecurityLogFile) {
-          // if security-log-file then create securityLogWriterAppender
-          this.securityLogWriterAppender = LogWriterAppenders.getOrCreateAppender(
-              LogWriterAppenders.Identifier.SECURITY, this.isLoner, this.config, false);
-        } else {
-          // let security route to regular log-file or stdout
-        }
-      }
+      initializeAlerting();
 
-      // LOG: create LogWriterLogger(s) for backwards compatibility of getLogWriter and
-      // getSecurityLogWriter
-      if (this.logWriter == null) {
-        this.logWriter =
-            LogWriterFactory.createLogWriterLogger(this.isLoner, false, this.config, true);
-        this.logWriter.fine("LogWriter is created.");
-      }
-
-      // logWriter.info("Created log writer for IDS@"+System.identityHashCode(this));
-
-      if (this.securityLogWriter == null) {
-        // LOG: whole new LogWriterLogger instance for security
-        this.securityLogWriter =
-            LogWriterFactory.createLogWriterLogger(this.isLoner, true, this.config, false);
-        this.securityLogWriter.fine("SecurityLogWriter is created.");
-      }
-
-      Services.setLogWriter(this.logWriter);
-      Services.setSecurityLogWriter(this.securityLogWriter);
+      initializeLogging();
 
       this.clock = new DSClock(this.isLoner);
 
@@ -736,14 +695,7 @@ public class InternalDistributedSystem extends DistributedSystem
         this.sampler.start();
       }
 
-      if (this.logWriterAppender != null) {
-        LogWriterAppenders.startupComplete(LogWriterAppenders.Identifier.MAIN);
-      }
-      if (this.securityLogWriterAppender != null) {
-        LogWriterAppenders.startupComplete(LogWriterAppenders.Identifier.SECURITY);
-      }
-
-      // this.logger.info("ds created", new RuntimeException("DEBUG: STACK"));
+      postInitializeLogging();
 
       // Log any instantiators that were registered before the log writer
       // was created
@@ -758,6 +710,66 @@ public class InternalDistributedSystem extends DistributedSystem
     this.attemptingToReconnect = false;
   }
 
+  private void initializeAlerting() {
+    // alerting is disable if isLoner
+    AlertService.enableAlerting(!this.isLoner);
+  }
+
+  private void initializeLogging() {
+    // LOG: create LogWriterAppender(s) if log-file or security-log-file is specified
+    final boolean hasLogFile =
+        this.config.getLogFile() != null && !this.config.getLogFile().equals(new File(""));
+    final boolean hasSecurityLogFile = this.config.getSecurityLogFile() != null
+        && !this.config.getSecurityLogFile().equals(new File(""));
+    LogService.configureLoggers(hasLogFile, hasSecurityLogFile);
+    if (hasLogFile || hasSecurityLogFile) {
+
+      // main log file
+      if (hasLogFile) {
+        // if log-file then create logWriterAppender
+        this.logWriterAppender = LogWriterAppenders.getOrCreateAppender(
+            LogWriterAppenders.Identifier.MAIN, this.isLoner, this.config, true);
+      }
+
+      // security log file
+      if (hasSecurityLogFile) {
+        // if security-log-file then create securityLogWriterAppender
+        this.securityLogWriterAppender = LogWriterAppenders.getOrCreateAppender(
+            LogWriterAppenders.Identifier.SECURITY, this.isLoner, this.config, false);
+      } else {
+        // let security route to regular log-file or stdout
+      }
+    }
+
+    // LOG: create LogWriterLogger(s) for backwards compatibility of getLogWriter and
+    // getSecurityLogWriter
+    if (this.logWriter == null) {
+      this.logWriter =
+          LogWriterFactory.createLogWriterLogger(this.isLoner, false, this.config, true);
+      this.logWriter.fine("LogWriter is created.");
+    }
+
+    if (this.securityLogWriter == null) {
+      // LOG: whole new LogWriterLogger instance for security
+      this.securityLogWriter =
+          LogWriterFactory.createLogWriterLogger(this.isLoner, true, this.config, false);
+      this.securityLogWriter.fine("SecurityLogWriter is created.");
+    }
+
+    Services.setLogWriter(this.logWriter);
+    Services.setSecurityLogWriter(this.securityLogWriter);
+  }
+
+  private void postInitializeLogging() {
+    if (this.logWriterAppender != null) {
+      LogWriterAppenders.startupComplete(LogWriterAppenders.Identifier.MAIN);
+    }
+    if (this.securityLogWriterAppender != null) {
+      LogWriterAppenders.startupComplete(LogWriterAppenders.Identifier.SECURITY);
+    }
+
+  }
+
   /**
    * @since GemFire 5.7
    */

http://git-wip-us.apache.org/repos/asf/geode/blob/6e636f7b/geode-core/src/main/java/org/apache/geode/internal/logging/ManagerLogWriter.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/internal/logging/ManagerLogWriter.java b/geode-core/src/main/java/org/apache/geode/internal/logging/ManagerLogWriter.java
index 4b333ed..dcf76d4 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/logging/ManagerLogWriter.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/logging/ManagerLogWriter.java
@@ -20,7 +20,6 @@ import org.apache.geode.internal.OSProcess;
 import org.apache.geode.internal.i18n.LocalizedStrings;
 import org.apache.geode.internal.io.MainWithChildrenRollingFileHandler;
 import org.apache.geode.internal.io.RollingFileHandler;
-import org.apache.geode.internal.logging.log4j.AlertAppender;
 import org.apache.geode.internal.util.LogFileUtils;
 
 import java.io.File;
@@ -46,6 +45,9 @@ public class ManagerLogWriter extends LocalLogWriter {
 
   private final RollingFileHandler rollingFileHandler;
 
+  // Don't redirect sysouts/syserrs to client log file. See #49492.
+  private final boolean redirectStdOut;
+
   private LogConfig cfg = null;
 
   private LocalLogWriter mainLogger = this;
@@ -67,28 +69,22 @@ public class ManagerLogWriter extends LocalLogWriter {
 
   /**
    * Creates a writer that logs to <code>System.out</code>.
-   * 
-   * @param level only messages greater than or equal to this value will be logged.
-   * @throws IllegalArgumentException if level is not in legal range
-   */
-  public ManagerLogWriter(int level, PrintStream out) {
-    this(level, out, null);
-  }
-
-  /**
-   * Creates a writer that logs to <code>System.out</code>.
    *
-   * @param level only messages greater than or equal to this value will be logged.
-   * @param connectionName Name of the connection associated with this logger
+   * @param level only messages greater than or equal to this value will be logged
+   * @param out is the stream that message will be printed to
+   * @param connectionName name of the connection associated with this logger
+   * @param redirectStdOut true if this log writer should support redirecting stdout
    *
    * @throws IllegalArgumentException if level is not in legal range
    *
    * @since GemFire 3.5
    */
-  public ManagerLogWriter(int level, PrintStream out, String connectionName) {
+  public ManagerLogWriter(int level, PrintStream out, String connectionName,
+      final boolean redirectStdOut) {
     super(level, out, connectionName);
     this.fileSizeLimitInKB = Boolean.getBoolean(TEST_FILE_SIZE_LIMIT_IN_KB_PROPERTY);
     this.rollingFileHandler = new MainWithChildrenRollingFileHandler();
+    this.redirectStdOut = redirectStdOut;
   }
 
   /**
@@ -244,8 +240,7 @@ public class ManagerLogWriter extends LocalLogWriter {
               File tmpLogDir = rollingFileHandler.getParentFile(this.cfg.getLogFile());
               tmpFile = File.createTempFile("mlw", null, tmpLogDir);
               // close the old guy down before we do the rename
-              PrintStream tmpps = OSProcess.redirectOutput(tmpFile,
-                  AlertAppender.getInstance().isAlertingDisabled()/* See #49492 */);
+              PrintStream tmpps = OSProcess.redirectOutput(tmpFile, this.redirectStdOut);
               PrintWriter oldPW = this.setTarget(new PrintWriter(tmpps, true));
               if (oldPW != null) {
                 oldPW.close();
@@ -263,10 +258,7 @@ public class ManagerLogWriter extends LocalLogWriter {
           }
         }
         this.activeLogFile = new File(oldName);
-        // Don't redirect sysouts/syserrs to client log file. See #49492.
-        // IMPORTANT: This assumes that only a loner would have sendAlert set to false.
-        PrintStream ps = OSProcess.redirectOutput(activeLogFile,
-            AlertAppender.getInstance().isAlertingDisabled());
+        PrintStream ps = OSProcess.redirectOutput(this.activeLogFile, this.redirectStdOut);
         PrintWriter oldPW = this.setTarget(new PrintWriter(ps, true), this.activeLogFile.length());
         if (oldPW != null) {
           oldPW.close();

http://git-wip-us.apache.org/repos/asf/geode/blob/6e636f7b/geode-core/src/main/java/org/apache/geode/internal/logging/SecurityManagerLogWriter.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/internal/logging/SecurityManagerLogWriter.java b/geode-core/src/main/java/org/apache/geode/internal/logging/SecurityManagerLogWriter.java
index 3e62f7d..d26a239 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/logging/SecurityManagerLogWriter.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/logging/SecurityManagerLogWriter.java
@@ -31,20 +31,13 @@ import java.io.PrintStream;
  */
 public final class SecurityManagerLogWriter extends ManagerLogWriter {
 
-  public SecurityManagerLogWriter(int level, PrintStream stream) {
-
-    super(level, stream);
-  }
-
-  public SecurityManagerLogWriter(int level, PrintStream stream, String connectionName) {
-
-    super(level, stream, connectionName);
+  public SecurityManagerLogWriter(final int level, final PrintStream stream,
+      final String connectionName) {
+    super(level, stream, connectionName, false);
   }
 
-
   @Override
   public void setConfig(LogConfig config) {
-
     if (config instanceof DistributionConfig) {
       config = new SecurityLogConfig((DistributionConfig) config);
     }

http://git-wip-us.apache.org/repos/asf/geode/blob/6e636f7b/geode-core/src/main/java/org/apache/geode/internal/logging/log4j/AlertAppender.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/internal/logging/log4j/AlertAppender.java b/geode-core/src/main/java/org/apache/geode/internal/logging/log4j/AlertAppender.java
index 7d1a0c6..945ec47 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/logging/log4j/AlertAppender.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/logging/log4j/AlertAppender.java
@@ -69,14 +69,14 @@ public final class AlertAppender extends AbstractAppender implements PropertyCha
     return alertAppender;
   }
 
-  private AlertAppender() {
-    super(APPENDER_NAME, null, PatternLayout.createDefaultLayout());
-  }
-
   public static AlertAppender getInstance() {
     return instance;
   }
 
+  private AlertAppender() {
+    super(APPENDER_NAME, null, PatternLayout.createDefaultLayout());
+  }
+
   /**
    * Returns true if the current thread is in the process of delivering an alert message.
    */
@@ -92,7 +92,7 @@ public final class AlertAppender extends AbstractAppender implements PropertyCha
     return alertingDisabled;
   }
 
-  public void setAlertingDisabled(final boolean alertingDisabled) {
+  void setAlertingDisabled(final boolean alertingDisabled) {
     this.alertingDisabled = alertingDisabled;
   }
 

http://git-wip-us.apache.org/repos/asf/geode/blob/6e636f7b/geode-core/src/main/java/org/apache/geode/internal/logging/log4j/AlertService.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/internal/logging/log4j/AlertService.java b/geode-core/src/main/java/org/apache/geode/internal/logging/log4j/AlertService.java
index 0a86155..ce18abf 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/logging/log4j/AlertService.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/logging/log4j/AlertService.java
@@ -53,4 +53,8 @@ public class AlertService {
   public static void shuttingDown() {
     getAlertService().alertAppender.shuttingDown();
   }
+
+  public static void enableAlerting(final boolean enable) {
+    getAlertService().alertAppender.setAlertingDisabled(!enable);
+  }
 }

http://git-wip-us.apache.org/repos/asf/geode/blob/6e636f7b/geode-core/src/main/java/org/apache/geode/internal/logging/log4j/LogWriterAppenders.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/internal/logging/log4j/LogWriterAppenders.java b/geode-core/src/main/java/org/apache/geode/internal/logging/log4j/LogWriterAppenders.java
index 310deb2..315a732 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/logging/log4j/LogWriterAppenders.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/logging/log4j/LogWriterAppenders.java
@@ -91,7 +91,8 @@ public class LogWriterAppenders {
   /**
    * Creates the log writer appender for a distributed system based on the system's parsed
    * configuration. The initial banner and messages are also entered into the log by this method.
-   * 
+   *
+   * @param appendToFile true to append or false to rename old log file
    * @param isLoner Whether the distributed system is a loner or not
    * @param isSecurity Whether a log for security related messages has to be created
    * @param config The DistributionConfig for the target distributed system
@@ -102,14 +103,13 @@ public class LogWriterAppenders {
       final boolean isLoner, final boolean isSecurity, final LogConfig config,
       final boolean logConfig) {
 
+    final boolean redirectStdOut = !isLoner;
     final boolean isDistributionConfig = config instanceof DistributionConfig;
     final DistributionConfig dsConfig = isDistributionConfig ? (DistributionConfig) config : null;
     File logFile = config.getLogFile();
     String firstMsg = null;
     boolean firstMsgWarning = false;
 
-    AlertAppender.getInstance().setAlertingDisabled(isLoner);
-
     // security-log-file is specified in DistributionConfig
     if (isSecurity) {
       if (isDistributionConfig) {
@@ -174,7 +174,7 @@ public class LogWriterAppenders {
       mlw = new SecurityManagerLogWriter(dsConfig.getSecurityLogLevel(), out, config.getName());
       logWriterLoggerName = LogService.SECURITY_LOGGER_NAME;
     } else {
-      mlw = new ManagerLogWriter(config.getLogLevel(), out, config.getName());
+      mlw = new ManagerLogWriter(config.getLogLevel(), out, config.getName(), redirectStdOut);
       logWriterLoggerName = LogService.MAIN_LOGGER_NAME;
     }
 

http://git-wip-us.apache.org/repos/asf/geode/blob/6e636f7b/geode-core/src/test/java/org/apache/geode/internal/logging/TestLogWriterFactory.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/internal/logging/TestLogWriterFactory.java b/geode-core/src/test/java/org/apache/geode/internal/logging/TestLogWriterFactory.java
index 87f370c..c5683ac 100644
--- a/geode-core/src/test/java/org/apache/geode/internal/logging/TestLogWriterFactory.java
+++ b/geode-core/src/test/java/org/apache/geode/internal/logging/TestLogWriterFactory.java
@@ -92,7 +92,7 @@ public class TestLogWriterFactory extends Assert {
       if (isSecurityLog) {
         mlw = new SecurityManagerLogWriter(config.getSecurityLogLevel(), out, config.getName());
       } else {
-        mlw = new ManagerLogWriter(config.getLogLevel(), out, config.getName());
+        mlw = new ManagerLogWriter(config.getLogLevel(), out, config.getName(), false);
       }
       ((ManagerLogWriter) mlw).setConfig(config);
     }