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  
23  import jgroup.experiment.PropertyDefinition;
24  import jgroup.experiment.Runnable;
25  import jgroup.experiment.ShellCommand;
26  import jgroup.relacs.config.ExperimentConfig;
27  import jgroup.relacs.config.Host;
28  import jgroup.relacs.config.HostSet;
29  
30  /**
31   * @author Bjarte Svaeren
32   * @author Hein Meling
33   */
34  public class KillAllHosts
35    implements Runnable 
36  {
37  
38    ////////////////////////////////////////////////////////////////////////////////////////////
39    // Methods from Runnable
40    ////////////////////////////////////////////////////////////////////////////////////////////
41  
42    /* (non-Javadoc)
43     * @see jgroup.experiment.Runnable#run(jgroup.relacs.config.ExperimentConfig)
44     */
45    public void run(ExperimentConfig ec) {
46      // get the server hosts
47      HostSet hosts = (HostSet) ec.getServerConfig().getAllHosts().clone();
48  
49      // Try a benign shutdown sequence first (only servers)
50      for (Iterator iter = hosts.iterator(); iter.hasNext();) {
51        Host host = (Host) iter.next();
52        host.shutdown(-1);
53      }
54      // get the client hosts
55      HostSet clients = ec.getClientConfig().getAllHosts();
56      hosts.addHosts(clients);
57  
58      try { Thread.sleep(3000); } catch (InterruptedException e1) { }
59      killAllHosts(hosts, ec, "");
60      try { Thread.sleep(5000); } catch (InterruptedException e1) { }
61      killAllHosts(hosts, ec, "-9");
62      checkHosts(hosts, ec);
63    }
64  
65    private void checkHosts(HostSet theHosts, ExperimentConfig ec)
66    {
67      // Need to clone this host set to permit removal in the iteration loop.
68      HostSet hosts = (HostSet) theHosts.clone();
69      String userName = ec.getProperty("user.name");
70  
71      ThreadGroup threadGroup = new ThreadGroup("CheckHosts");
72      for (Iterator iter = hosts.iterator(); iter.hasNext();) {
73        Host host = (Host) iter.next();
74        String hostName = host.getCanonicalHostName();
75        StringBuilder b = new StringBuilder(ec.ssh(hostName));
76        b.append("pgrep -fu ");
77        b.append(userName);
78        b.append(" java");
79        ShellCommand sc = new ShellCommand(b.toString(), threadGroup);
80        host.put("ShellCommand", sc);
81      }
82      for (Iterator iter = hosts.iterator(); iter.hasNext();) {
83        Host host = (Host) iter.next();
84        ShellCommand sc = (ShellCommand) host.get("ShellCommand");
85        if (sc.waitForProcess()) {
86          iter.remove();
87          if (sc.stdoutModified()) {
88            // FIXME Kill the host associated with this shell command
89          }
90        }
91      }
92      ShellCommand.waitFor(threadGroup);      
93    }
94  
95    private void killAllHosts(HostSet hosts, ExperimentConfig ec, String killSig)
96    {
97      String userName = ec.getProperty("user.name");
98  
99      ThreadGroup threadGroup = new ThreadGroup("KillAllHosts");
100     for (Iterator iter = hosts.iterator(); iter.hasNext();) {
101       Host host = (Host) iter.next();
102       String hostName = host.getCanonicalHostName();
103       StringBuilder b = new StringBuilder(30);
104       b.append(ec.ssh(hostName));
105       b.append("pkill ");
106       b.append(killSig);
107       b.append(" -u ");
108       b.append(userName);
109       String killCmd = b.toString();
110       try {
111         /*
112          * Don't kill java processes at the localhost, as this could
113          * potentially kill your IDE as well.
114          */
115         if (!host.isLocal()) {
116           ShellCommand.exec(killCmd + " java", threadGroup);
117           ShellCommand.exec(killCmd + " ant", threadGroup);
118         }
119       } catch (Exception e) {
120         e.printStackTrace();
121         System.out.println("Could not kill java processes at " + hostName);
122       }
123     }
124     ShellCommand.waitFor(threadGroup);  
125   }
126 
127 
128   /* (non-Javadoc)
129    * @see jgroup.experiment.Runnable#getProperties()
130    */
131   public PropertyDefinition[] getProperties()
132   {
133     return null;
134   }
135 
136 } // END KillAllHosts