You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by se...@apache.org on 2018/05/21 20:17:54 UTC

[directory-studio] branch master updated: DIRSTUDIO-1148: Fix OOM caused by large log file

This is an automated email from the ASF dual-hosted git repository.

seelmann pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/directory-studio.git


The following commit(s) were added to refs/heads/master by this push:
     new e2df091  DIRSTUDIO-1148: Fix OOM caused by large log file
e2df091 is described below

commit e2df0914247eb9537263d90a9bcf986b5cee5e45
Author: Stefan Seelmann <ma...@stefan-seelmann.de>
AuthorDate: Mon May 21 22:15:21 2018 +0200

    DIRSTUDIO-1148: Fix OOM caused by large log file
    
    * Limit the max amount of characters shown in the console
    * Don't read the full log file on server start
    * Use commons-io Tailer class to follow log file
---
 eclipse-trgt-platform/pom-first.xml                |   6 +
 ...directory.studio.eclipse-trgt-platform.template |   3 +-
 features/rcp.feature/feature.xml                   |   7 ++
 plugins/ldapservers/pom-first.xml                  |   1 +
 .../studio/ldapservers/ConsolePrinterThread.java   | 124 ---------------------
 .../studio/ldapservers/ConsolesManager.java        |   2 +
 .../studio/ldapservers/LdapServersUtils.java       |  33 ++++--
 pom.xml                                            |   2 +
 8 files changed, 44 insertions(+), 134 deletions(-)

diff --git a/eclipse-trgt-platform/pom-first.xml b/eclipse-trgt-platform/pom-first.xml
index 521cbad..7496532 100644
--- a/eclipse-trgt-platform/pom-first.xml
+++ b/eclipse-trgt-platform/pom-first.xml
@@ -57,6 +57,12 @@
     </dependency>
 
     <dependency>
+      <groupId>commons-io</groupId>
+      <artifactId>commons-io</artifactId>
+      <version>${org.apache.commons.io.version}</version>
+    </dependency>
+
+    <dependency>
       <groupId>commons-lang</groupId>
       <artifactId>commons-lang</artifactId>
       <version>${org.apache.commons.lang.version}</version>
diff --git a/eclipse-trgt-platform/template/org.apache.directory.studio.eclipse-trgt-platform.template b/eclipse-trgt-platform/template/org.apache.directory.studio.eclipse-trgt-platform.template
index 7e86d18..bc897ab 100644
--- a/eclipse-trgt-platform/template/org.apache.directory.studio.eclipse-trgt-platform.template
+++ b/eclipse-trgt-platform/template/org.apache.directory.studio.eclipse-trgt-platform.template
@@ -19,7 +19,7 @@
   @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
 -->
 <?pde version="3.8"?>
-<target name="Apache Directory Studio Platform" sequenceNumber="323">
+<target name="Apache Directory Studio Platform" sequenceNumber="325">
   <locations>
 
     <location includeAllPlatforms="false" includeConfigurePhase="true" includeMode="planner" includeSource="true" type="InstallableUnit">
@@ -32,6 +32,7 @@
       <!-- Commons jars -->
       <unit id="org.apache.commons.codec" version="${org.apache.commons.codec.bundleversion}"/>
       <unit id="org.apache.commons.collections" version="${org.apache.commons.collections.bundleversion}"/>
+      <unit id="org.apache.commons.io" version="${org.apache.commons.io.bundleversion}"/>
       <unit id="org.apache.commons.lang" version="${org.apache.commons.lang.bundleversion}"/>
       <unit id="org.apache.commons.pool2" version="${org.apache.commons.pool.bundleversion}"/>
 
diff --git a/features/rcp.feature/feature.xml b/features/rcp.feature/feature.xml
index 6447fa0..8ec5c85 100644
--- a/features/rcp.feature/feature.xml
+++ b/features/rcp.feature/feature.xml
@@ -225,6 +225,13 @@ http://www.apache.org/licenses/
          unpack="false"/>
 
    <plugin
+         id="org.apache.commons.io"
+         download-size="0"
+         install-size="0"
+         version="0.0.0"
+         unpack="false"/>
+
+   <plugin
          id="org.apache.commons.lang"
          download-size="0"
          install-size="0"
diff --git a/plugins/ldapservers/pom-first.xml b/plugins/ldapservers/pom-first.xml
index e7c9141..50a6d62 100644
--- a/plugins/ldapservers/pom-first.xml
+++ b/plugins/ldapservers/pom-first.xml
@@ -55,6 +55,7 @@
  
             <Import-Package>
  org.apache.commons.collections.map,
+ org.apache.commons.io.input,
  org.apache.mina.util,
  org.dom4j,
  org.dom4j.io,
diff --git a/plugins/ldapservers/src/main/java/org/apache/directory/studio/ldapservers/ConsolePrinterThread.java b/plugins/ldapservers/src/main/java/org/apache/directory/studio/ldapservers/ConsolePrinterThread.java
deleted file mode 100644
index eba9883..0000000
--- a/plugins/ldapservers/src/main/java/org/apache/directory/studio/ldapservers/ConsolePrinterThread.java
+++ /dev/null
@@ -1,124 +0,0 @@
-/*
- *  Licensed to the Apache Software Foundation (ASF) under one
- *  or more contributor license agreements.  See the NOTICE file
- *  distributed with this work for additional information
- *  regarding copyright ownership.  The ASF licenses this file
- *  to you under the Apache License, Version 2.0 (the
- *  "License"); you may not use this file except in compliance
- *  with the License.  You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing,
- *  software distributed under the License is distributed on an
- *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- *  KIND, either express or implied.  See the License for the
- *  specific language governing permissions and limitations
- *  under the License.
- *
- */
-package org.apache.directory.studio.ldapservers;
-
-
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.IOException;
-
-import org.eclipse.ui.console.MessageConsoleStream;
-
-
-/**
- * This class implements a thread used to print in the console the contents of a file.
- *
- * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
- */
-public class ConsolePrinterThread extends Thread
-{
-    /** The flag to stop the console printer */
-    private boolean stop = false;
-
-    /** The file to read */
-    private File file;
-
-    /** The console stream */
-    private MessageConsoleStream consoleStream;
-
-
-    /**
-     * Creates a new instance of ConsolePrinterThread.
-     *
-     * @param file the file to read
-     * @param consoleStream the console stream
-     */
-    public ConsolePrinterThread( File file, MessageConsoleStream consoleStream )
-    {
-        this.file = file;
-        this.consoleStream = consoleStream;
-    }
-
-
-    /**
-     * {@inheritDoc}
-     */
-    public void run()
-    {
-        if ( file.exists() && file.isFile() && file.canRead() )
-        {
-            try
-            {
-                // Opening the file reader
-                BufferedReader reader = new BufferedReader( new FileReader( file ) );
-
-                while ( !stop )
-                {
-                    // Checking if the console stream is closed
-                    if ( consoleStream.isClosed() )
-                    {
-                        // We need to exit
-                        break;
-                    }
-                    
-                    // Getting the next line to print
-                    String line = reader.readLine();
-
-                    // Checking the line
-                    if ( line != null )
-                    {
-                        // Writing the line to the console and moving the next
-                        consoleStream.println( line );
-                        continue;
-                    }
-
-                    // Waiting
-                    sleep( 1000 );
-                }
-
-                // Closing the file reader
-                reader.close();
-            }
-            catch ( FileNotFoundException e )
-            {
-                // Will never get thrown
-            }
-            catch ( InterruptedException e )
-            {
-                // Nothing to do
-            }
-            catch ( IOException e )
-            {
-                // Nothing to do
-            }
-        }
-    }
-
-
-    /**
-     * Closes the console printer (makes the thread stop).
-     */
-    public void close()
-    {
-        stop = true;
-    }
-}
diff --git a/plugins/ldapservers/src/main/java/org/apache/directory/studio/ldapservers/ConsolesManager.java b/plugins/ldapservers/src/main/java/org/apache/directory/studio/ldapservers/ConsolesManager.java
index 0040cec..34ea5f7 100644
--- a/plugins/ldapservers/src/main/java/org/apache/directory/studio/ldapservers/ConsolesManager.java
+++ b/plugins/ldapservers/src/main/java/org/apache/directory/studio/ldapservers/ConsolesManager.java
@@ -91,6 +91,8 @@ public class ConsolesManager
         {
             MessageConsole messageConsole = new MessageConsole( server.getName()
                 + " " + Messages.getString( "ConsolesManager.LdapServer" ), null ); //$NON-NLS-1$ //$NON-NLS-2$
+            // DIRSTUDIO-1148: limit the amount of characters shown in the console
+            messageConsole.setWaterMarks( 70000, 80000 );
             consolesMap.put( server, messageConsole );
 
             ConsolePlugin.getDefault().getConsoleManager().addConsoles( new IConsole[]
diff --git a/plugins/ldapservers/src/main/java/org/apache/directory/studio/ldapservers/LdapServersUtils.java b/plugins/ldapservers/src/main/java/org/apache/directory/studio/ldapservers/LdapServersUtils.java
index 28486f7..4365533 100644
--- a/plugins/ldapservers/src/main/java/org/apache/directory/studio/ldapservers/LdapServersUtils.java
+++ b/plugins/ldapservers/src/main/java/org/apache/directory/studio/ldapservers/LdapServersUtils.java
@@ -28,6 +28,9 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URL;
 
+import org.apache.commons.io.input.Tailer;
+import org.apache.commons.io.input.TailerListener;
+import org.apache.commons.io.input.TailerListenerAdapter;
 import org.apache.directory.studio.common.core.jobs.StudioProgressMonitor;
 import org.apache.directory.studio.common.ui.CommonUIUtils;
 import org.apache.directory.studio.ldapservers.model.LdapServer;
@@ -42,6 +45,7 @@ import org.eclipse.debug.core.ILaunch;
 import org.eclipse.debug.core.model.RuntimeProcess;
 import org.eclipse.osgi.util.NLS;
 import org.eclipse.ui.console.MessageConsole;
+import org.eclipse.ui.console.MessageConsoleStream;
 import org.osgi.framework.Bundle;
 
 
@@ -189,17 +193,28 @@ public class LdapServersUtils
     public static void startConsolePrinterThread( LdapServer server, File serverLogsFile )
     {
         MessageConsole messageConsole = ConsolesManager.getDefault().getMessageConsole( server );
-        ConsolePrinterThread consolePrinter = new ConsolePrinterThread( serverLogsFile,
-            messageConsole.newMessageStream() );
-        consolePrinter.start();
+        MessageConsoleStream messageStream = messageConsole.newMessageStream();
 
-        // Storing the console printer as a custom object in the LDAP Server for later use
-        server.putCustomObject( CONSOLE_PRINTER_CUSTOM_OBJECT, consolePrinter );
+        /*
+         * DIRSTUDIO-1148: Tail the log file and update the console.
+         * Tail from end only to avoid overwhelming the system in case the log file is large.
+         */
+        TailerListener l = new TailerListenerAdapter()
+        {
+            public void handle( String line )
+            {
+                messageStream.println( line );
+            };
+        };
+        Tailer tailer = Tailer.create( serverLogsFile, l, 1000L, true );
+
+        // Storing the tailer as a custom object in the LDAP Server for later use
+        server.putCustomObject( CONSOLE_PRINTER_CUSTOM_OBJECT, tailer );
     }
 
 
     /**
-     * Stops the console printer thread.
+     * Stops the tailer thread.
      *
      * @param server
      *      the server
@@ -207,12 +222,12 @@ public class LdapServersUtils
     public static void stopConsolePrinterThread( LdapServer server )
     {
         // Getting the console printer
-        ConsolePrinterThread consolePrinter = ( ConsolePrinterThread ) server
+        Tailer tailer = ( Tailer ) server
             .removeCustomObject( CONSOLE_PRINTER_CUSTOM_OBJECT );
-        if ( ( consolePrinter != null ) && ( consolePrinter.isAlive() ) )
+        if ( tailer != null )
         {
             // Closing the console printer
-            consolePrinter.close();
+            tailer.stop();
         }
     }
 
diff --git a/pom.xml b/pom.xml
index 50795fe..4445687 100644
--- a/pom.xml
+++ b/pom.xml
@@ -82,6 +82,8 @@
     <org.apache.commons.codec.bundleversion>1.11.0</org.apache.commons.codec.bundleversion>
     <org.apache.commons.collections.version>3.2.2</org.apache.commons.collections.version>
     <org.apache.commons.collections.bundleversion>3.2.2</org.apache.commons.collections.bundleversion>
+    <org.apache.commons.io.version>2.6</org.apache.commons.io.version>
+    <org.apache.commons.io.bundleversion>2.6.0</org.apache.commons.io.bundleversion>
     <org.apache.commons.lang.version>2.6</org.apache.commons.lang.version>
     <org.apache.commons.lang.bundleversion>2.6.0</org.apache.commons.lang.bundleversion>
     <org.apache.commons.pool.version>2.5.0</org.apache.commons.pool.version>

-- 
To stop receiving notification emails like this one, please contact
seelmann@apache.org.