View Javadoc

1   /*
2    * Copyright (c) 1998-2004 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  
19  package jgroup.experiment.runnables;
20  
21  import java.util.Iterator;
22  import java.util.Timer;
23  import java.util.TimerTask;
24  
25  import jgroup.core.ConfigurationException;
26  import jgroup.experiment.PropertyDefinition;
27  import jgroup.experiment.Runnable;
28  import jgroup.experiment.ShellCommand;
29  import jgroup.relacs.config.ExperimentConfig;
30  import jgroup.relacs.config.Host;
31  import jgroup.relacs.config.HostSet;
32  
33  import org.apache.log4j.Logger;
34  
35  /**
36   * @author Bjarte Svaeren
37   * @author Hein Meling
38   */
39  public class DeployExecDaemons
40    implements Runnable 
41  {
42  
43    ////////////////////////////////////////////////////////////////////////////////////////////
44    // Logger
45    ////////////////////////////////////////////////////////////////////////////////////////////
46  
47    /** Obtain logger for this class */
48    public static final Logger log = Logger.getLogger(DeployExecDaemons.class);
49  
50  
51    ////////////////////////////////////////////////////////////////////////////////////////////
52    // Constants
53    ////////////////////////////////////////////////////////////////////////////////////////////
54  
55    /** The number of milliseconds to wait (max) per host being deployed */
56    private static final int WAIT_TIME_PER_HOST = 3000;
57  
58  
59    ////////////////////////////////////////////////////////////////////////////////////////////
60    // Fields
61    ////////////////////////////////////////////////////////////////////////////////////////////
62  
63    /** Indicate if the timeout has occurred */
64    private volatile boolean timeout;
65  
66  
67    ////////////////////////////////////////////////////////////////////////////////////////////
68    // Methods from Runnable
69    ////////////////////////////////////////////////////////////////////////////////////////////
70  
71    /* (non-Javadoc)
72     * @see jgroup.experiment.Runnable#run(jgroup.relacs.config.ExperimentConfig)
73     */
74    public void run(ExperimentConfig ec)
75      throws ConfigurationException
76    {
77      HostSet hosts = (HostSet) ec.getServerConfig().getAllHosts().clone();
78      boolean useWindows = ec.getBooleanProperty(this, "use.separate.windows");
79      int maxWait = hosts.size()*WAIT_TIME_PER_HOST;
80      maxWait = ec.getIntProperty(this, "max.waiting.time", maxWait);
81  
82      ThreadGroup threadGroup = new ThreadGroup("DeployExecDaemons");
83      for (Iterator iter = hosts.iterator(); iter.hasNext(); ) {
84        Host host = (Host) iter.next();
85        String hostName = host.getCanonicalHostName();
86        StringBuilder sshCmd = new StringBuilder(50);
87        if (useWindows) {
88          sshCmd.append(ec.getProperty(this, "terminal.cmd", "xterm"));
89          sshCmd.append(" -e ");
90        }
91        sshCmd.append(ec.ssh(hostName));
92        sshCmd.append(ec.ant("execdaemon"));
93        try {
94          ShellCommand.exec(sshCmd.toString(), threadGroup);
95        } catch (Exception e) {
96          e.printStackTrace();
97          System.out.println("Could not start ExecDaemon at " + hostName);
98        }
99      }
100     timeout = false;
101     TimerTask awaitHosts = new TimerTask() {
102       public void run() {
103         log.error("Some hosts seems to be unavailable (check log file)");
104         timeout = true;
105       }
106     };
107     Timer timer = new Timer("AwaitExecDaemons", true);
108     timer.schedule(awaitHosts, maxWait);
109     log.info("Waiting for execdaemons to start: " + hosts.size());
110     while (hosts.size() > 0  && !timeout) {
111       log.debug("Unavailable hosts remaining: " + hosts.size());
112       try {
113         Thread.sleep(2000);
114       } catch (InterruptedException e) { /* Ignored */ }
115       for (Iterator iter = hosts.iterator(); iter.hasNext();) {
116         Host host = (Host) iter.next();
117         log.debug("Checking: " + host.getHostName());
118         if (host.ping()) {
119           log.debug(host.getHostName() + " is available");
120           iter.remove();
121         } else {
122           log.debug(host.getHostName() + " is not available");
123         }
124       }
125     }
126     log.info("Exiting DeplyExecDaemons");
127     timer.cancel();
128     if (!hosts.isEmpty()) {
129       for (Iterator iter = hosts.iterator(); iter.hasNext();) {
130         log.debug("Remaining host: " + iter.next());
131       }
132     }
133   }
134 
135   /* (non-Javadoc)
136    * @see jgroup.experiment.Runnable#getProperties()
137    */
138   public PropertyDefinition[] getProperties()
139   {
140     return null;
141   }
142 
143 } // END DeplyExecDaemons