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.io.File;
22  import java.net.InetAddress;
23  import java.net.UnknownHostException;
24  
25  import jgroup.core.ConfigurationException;
26  import jgroup.experiment.Experiment;
27  import jgroup.experiment.PropertyDefinition;
28  import jgroup.experiment.Runnable;
29  import jgroup.relacs.config.ExperimentConfig;
30  import jgroup.relacs.config.Host;
31  import jgroup.relacs.config.HostSet;
32  import jgroup.util.log.Eventlogger;
33  
34  /**
35   * @author Hein Meling
36   */
37  public class FixedCrashFailure
38    implements Runnable
39  {
40  
41    ////////////////////////////////////////////////////////////////////////////////////////////
42    // Static fields
43    ////////////////////////////////////////////////////////////////////////////////////////////
44  
45    /** Property strings */
46    private static final String FAILURE_TIMES  = "failure.times";
47    private static final String HOSTS_TO_CRASH = "hosts.to.crash";
48    private static final String RECOVERY_TIME  = "recovery.time";
49  
50    private static final PropertyDefinition[] properties = {
51        new PropertyDefinition(FAILURE_TIMES, PropertyDefinition.INT_TYPE),
52        new PropertyDefinition(HOSTS_TO_CRASH, PropertyDefinition.INT_TYPE),
53        new PropertyDefinition(RECOVERY_TIME, PropertyDefinition.INT_TYPE),
54    };
55  
56  
57    ////////////////////////////////////////////////////////////////////////////////////////////
58    // Methods from Runnable
59    ////////////////////////////////////////////////////////////////////////////////////////////
60  
61    /* (non-Javadoc)
62     * @see jgroup.experiment.Runnable#run(jgroup.relacs.config.ExperimentConfig)
63     */
64    public void run(ExperimentConfig ec)
65      throws ConfigurationException
66    {
67      String failureTimeStr = ec.getProperty(this, FAILURE_TIMES);
68      String hostsToCrashStr = ec.getProperty(this, HOSTS_TO_CRASH);
69      String[] failureT = failureTimeStr.split(", ");
70      String[] hostsToCrash = hostsToCrashStr.split(", ");
71      if (failureT.length != hostsToCrash.length)
72        throw new ConfigurationException("Mismatch between failure times and hosts to crash");
73      int recoveryTime = ec.getIntProperty(this, RECOVERY_TIME, 10000);
74  
75      // All available hosts may be selected to be crashed
76      HostSet availableHosts = ec.getServerConfig().getAllHosts().getAvailHosts();
77      int crashes = failureT.length;
78      Host[] aHosts = new Host[crashes];
79      int[] failureTime = new int[crashes];
80      for (int i = 0; i < failureTime.length; i++) {
81        failureTime[i] = Integer.parseInt(failureT[i]);
82        try {
83          aHosts[i] = availableHosts.getHost(InetAddress.getByName(hostsToCrash[i]));
84          if (aHosts[i] == null)
85            throw new ConfigurationException("Host not available: " + hostsToCrash[i]);
86        } catch (UnknownHostException e) {
87          throw new ConfigurationException("Unknown host: " + hostsToCrash[i], e);
88        }
89      }
90  
91      if (Eventlogger.ENABLED) {
92        String logDir = ec.getProperty("local.save.dir");
93        logDir = logDir + File.separator + ec.getExperimentName();
94        File ldir = new File(logDir);
95        // If the directory does not exist, create it.
96        if (!ldir.exists()) {
97          if (!ldir.mkdirs()) {
98            throw new ConfigurationException("Could not create experiment directory: " + ldir);
99          }
100       }
101     }
102 
103     if (Eventlogger.ENABLED)
104       //Debug.logEvent("BeginCrashRound", "Exp.no: " + ec.getIntProperty("repeat"));
105       Eventlogger.logEventFlush("BeginCrashRound Exp.no: " + ec.getIntProperty("repeat"));
106 
107     /*
108      * If we get here, at least one of the monitored service replicas
109      * are supposed to be crashed, and in this part we perform the actual
110      * failure injections.
111      */
112     int lastFailureTime = 0;
113     for (int i = 0; i < failureTime.length; i++) {
114       Host host = aHosts[i];
115       System.out.println("Shutdown: " + host.getCanonicalHostName()
116           + " @ " + failureTime[i]);
117       if (Eventlogger.ENABLED) {
118         Eventlogger.logEvent("Crashing " + host.getCanonicalHostName());
119       }
120       if (failureTime[i] > lastFailureTime)
121         lastFailureTime = failureTime[i];
122       host.shutdown(failureTime[i]);
123     }
124     recoveryTime += lastFailureTime;
125     Experiment.log.info("Waiting for recovery for: " + (recoveryTime/1000) + " seconds.");
126     try {
127       Thread.sleep(recoveryTime);
128     } catch (InterruptedException e) { }
129     Experiment.log.info("Waiting completed");
130     if (Eventlogger.ENABLED)
131       Eventlogger.logEvent("EndCrashRound MonitoredServiceAffected");
132   }
133 
134 
135   /* (non-Javadoc)
136    * @see jgroup.experiment.Runnable#getProperties()
137    */
138   public PropertyDefinition[] getProperties()
139   {
140     return properties;
141   }
142 
143 } // END FixedCrashFailure