Fix: dynamically fetch the session daemon port
authorDavid Goulet <dgoulet@efficios.com>
Thu, 27 Feb 2014 16:53:22 +0000 (11:53 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 27 Feb 2014 17:29:53 +0000 (12:29 -0500)
When connecting to the session daemon, fetch the port at each try. This
moves the read port file functions to the TCP session daemon client
where the connect happens.

If the port file is not found, simply retry 3 seconds later. Also, this
adds a timeout to the acquire semaphore of 3 seconds.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Signed-off-by: David Goulet <dgoulet@efficios.com>
liblttng-ust-jul/org/lttng/ust/jul/LTTngAgent.java
liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java
liblttng-ust-jul/org/lttng/ust/jul/LTTngThread.java

index 72e60d235574fa1d3fa188ff405adbfaafcfbaee..2485be2deb1781c05451eafaddd0a284ad89ec3b 100644 (file)
 package org.lttng.ust.jul;
 
 import java.io.IOException;
-import java.io.FileNotFoundException;
 import java.io.InputStream;
 import java.io.BufferedReader;
 import java.io.FileReader;
 import java.util.concurrent.Semaphore;
+import java.util.concurrent.TimeUnit;
 import java.util.logging.FileHandler;
 import java.util.logging.Handler;
 import java.util.logging.Level;
@@ -48,12 +48,13 @@ public class LTTngAgent {
        private static boolean initialized = false;
 
        private static Semaphore registerSem;
+       private final static int semTimeout = 3; /* Seconds */
 
+       /*
+        * Default value to connect to session daemon. Port number is dynamically
+        * fetched from the port file that is created by a running session daemon.
+        */
        private static final String sessiondAddr = "127.0.0.1";
-       private static final int sessiondPort = 5345;
-
-       private static final String rootPortFile = "/var/run/lttng/jul.port";
-       private static final String userPortFile = "/.lttng/jul.port";
 
        /*
         * Constructor is private. This is a singleton and a reference should be
@@ -100,29 +101,6 @@ public class LTTngAgent {
                return uid;
        }
 
-       private String getHomePath() {
-               return System.getProperty("user.home");
-       }
-
-       private int getPortFromFile(String path) throws IOException {
-               int port;
-               BufferedReader br;
-
-               try {
-                       br = new BufferedReader(new FileReader(path));
-                       String line = br.readLine();
-                       port = Integer.parseInt(line, 10);
-                       if (port < 0 || port > 65535) {
-                               port = sessiondPort;
-                       }
-                       br.close();
-               } catch (FileNotFoundException e) {
-                       port = sessiondPort;
-               }
-
-               return port;
-       }
-
        /*
         * Public getter to acquire a reference to this singleton object.
         */
@@ -140,42 +118,32 @@ public class LTTngAgent {
         * returned by the logManager.
         */
        private synchronized void init() throws SecurityException, IOException {
-               int user_port, root_port;
                int nr_acquires = 0;
 
                if (this.initialized) {
                        return;
                }
 
-               root_port = getPortFromFile(rootPortFile);
-               if (getUID() == 0) {
-                       user_port = root_port;
-               } else {
-                       user_port = getPortFromFile(getHomePath() + userPortFile);
-               }
-
                /* Handle user session daemon if any. */
-               this.lttngThreadUser = new LTTngThread(this.sessiondAddr, user_port,
+               this.lttngThreadUser = new LTTngThread(this.sessiondAddr,
                                this.lttngHandlerUser, this.registerSem);
                this.sessiondThUser = new Thread(lttngThreadUser);
                this.sessiondThUser.start();
                /* Wait for registration done of per-user sessiond */
                nr_acquires++;
 
-               /* Having two different ports, we have to try both. */
-               if (root_port != user_port) {
-                       /* Handle root session daemon. */
-                       this.lttngThreadRoot = new LTTngThread(this.sessiondAddr,
-                                       root_port, this.lttngHandlerRoot, this.registerSem);
-                       this.sessiondThRoot = new Thread(lttngThreadRoot);
-                       this.sessiondThRoot.start();
-                       /* Wait for registration done of system-wide sessiond */
-                       nr_acquires++;
-               }
+               /* Handle root session daemon. */
+               this.lttngThreadRoot = new LTTngThread(this.sessiondAddr,
+                               this.lttngHandlerRoot, this.registerSem);
+               this.sessiondThRoot = new Thread(lttngThreadRoot);
+               this.sessiondThRoot.start();
+               /* Wait for registration done of system-wide sessiond */
+               nr_acquires++;
 
                /* Wait for each registration to end. */
                try {
-                       this.registerSem.acquire(nr_acquires);
+                       this.registerSem.tryAcquire(nr_acquires, semTimeout,
+                                       TimeUnit.SECONDS);
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }
index 21c228f00bdde7a2428f4090213a28bfee6381bd..594e735a25309dea2ee21348e85204f71285561c 100644 (file)
@@ -23,9 +23,12 @@ import java.nio.ByteOrder;
 import java.lang.Integer;
 import java.io.IOException;
 import java.io.BufferedOutputStream;
+import java.io.BufferedReader;
 import java.io.ByteArrayOutputStream;
 import java.io.DataOutputStream;
 import java.io.DataInputStream;
+import java.io.FileReader;
+import java.io.FileNotFoundException;
 import java.net.*;
 import java.lang.management.ManagementFactory;
 import java.util.ArrayList;
@@ -49,7 +52,6 @@ public class LTTngTCPSessiondClient {
                new LTTngSessiondCmd2_4.sessiond_hdr();
 
        private final String sessiondHost;
-       private final int sessiondPort;
        private Socket sessiondSock;
        private boolean quit = false;
 
@@ -71,9 +73,14 @@ public class LTTngTCPSessiondClient {
        private final static long timerDelay = 5 * 1000;
        private static boolean timerInitialized;
 
-       public LTTngTCPSessiondClient(String host, int port, Semaphore sem) {
+       private static final String rootPortFile = "/var/run/lttng/jul.port";
+       private static final String userPortFile = "/.lttng/jul.port";
+
+       /* Indicate if we've already release the semaphore. */
+       private boolean sem_posted = false;
+
+       public LTTngTCPSessiondClient(String host, Semaphore sem) {
                this.sessiondHost = host;
-               this.sessiondPort = port;
                this.registerSem = sem;
                this.eventTimer = new Timer();
                this.timerInitialized = false;
@@ -168,6 +175,18 @@ public class LTTngTCPSessiondClient {
                this.timerInitialized = true;
        }
 
+       /*
+        * Try to release the registerSem if it's not already done.
+        */
+       private void tryReleaseSem()
+       {
+               /* Release semaphore so we unblock the agent. */
+               if (!this.sem_posted) {
+                       this.registerSem.release();
+                       this.sem_posted = true;
+               }
+       }
+
        public void init(LTTngLogHandler handler) throws InterruptedException {
                this.handler = handler;
 
@@ -198,13 +217,13 @@ public class LTTngTCPSessiondClient {
                                 */
                                handleSessiondCmd();
                        } catch (UnknownHostException uhe) {
-                               this.registerSem.release();
+                               tryReleaseSem();
                                System.out.println(uhe);
                        } catch (IOException ioe) {
-                               this.registerSem.release();
+                               tryReleaseSem();
                                Thread.sleep(3000);
                        } catch (Exception e) {
-                               this.registerSem.release();
+                               tryReleaseSem();
                                e.printStackTrace();
                        }
                }
@@ -279,7 +298,7 @@ public class LTTngTCPSessiondClient {
                                         * Release semaphore so meaning registration is done and we
                                         * can proceed to continue tracing.
                                         */
-                                       this.registerSem.release();
+                                       tryReleaseSem();
                                        /*
                                         * We don't send any reply to the registration done command.
                                         * This just marks the end of the initial session setup.
@@ -346,8 +365,54 @@ public class LTTngTCPSessiondClient {
                }
        }
 
+       private String getHomePath() {
+               return System.getProperty("user.home");
+       }
+
+       /**
+        * Read port number from file created by the session daemon.
+        *
+        * @return port value if found else 0.
+        */
+       private int getPortFromFile(String path) throws IOException {
+               int port;
+               BufferedReader br;
+
+               try {
+                       br = new BufferedReader(new FileReader(path));
+                       String line = br.readLine();
+                       port = Integer.parseInt(line, 10);
+                       if (port < 0 || port > 65535) {
+                               /* Invalid value. Ignore. */
+                               port = 0;
+                       }
+                       br.close();
+               } catch (FileNotFoundException e) {
+                       /* No port available. */
+                       port = 0;
+               }
+
+               return port;
+       }
+
        private void connectToSessiond() throws Exception {
-               this.sessiondSock = new Socket(this.sessiondHost, this.sessiondPort);
+               int port;
+
+               if (this.handler.is_root == 1) {
+                       port = getPortFromFile(rootPortFile);
+                       if (port == 0) {
+                               /* No session daemon available. Stop and retry later. */
+                               throw new IOException();
+                       }
+               } else {
+                       port = getPortFromFile(getHomePath() + userPortFile);
+                       if (port == 0) {
+                               /* No session daemon available. Stop and retry later. */
+                               throw new IOException();
+                       }
+               }
+
+               this.sessiondSock = new Socket(this.sessiondHost, port);
                this.inFromSessiond = new DataInputStream(
                                sessiondSock.getInputStream());
                this.outToSessiond = new DataOutputStream(
index a64c6a9b36c06888b0b9f04961b3b4724127b41e..b3aa0e6ea0a9000b5279501aa01aee09488158c6 100644 (file)
@@ -23,11 +23,10 @@ public class LTTngThread implements Runnable {
        private LTTngLogHandler handler;
        private LTTngTCPSessiondClient sessiondClient;
 
-       public LTTngThread(String host, int port, LTTngLogHandler handler,
+       public LTTngThread(String host, LTTngLogHandler handler,
                        Semaphore registerSem) {
                this.handler = handler;
-               this.sessiondClient = new LTTngTCPSessiondClient(host, port,
-                               registerSem);
+               this.sessiondClient = new LTTngTCPSessiondClient(host, registerSem);
        }
 
        @Override
This page took 0.031358 seconds and 4 git commands to generate.