1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
36
37 public class FixedCrashFailure
38 implements Runnable
39 {
40
41
42
43
44
45
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
59
60
61
62
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
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
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
105 Eventlogger.logEventFlush("BeginCrashRound Exp.no: " + ec.getIntProperty("repeat"));
106
107
108
109
110
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
136
137
138 public PropertyDefinition[] getProperties()
139 {
140 return properties;
141 }
142
143 }