Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
modifiers in right order
[simgrid.git] / examples / java / cloud / migration / TestHostOnOff.java
1 /* Copyright (c) 2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 package cloud.migration;
8
9 import org.simgrid.msg.*;
10 import org.simgrid.msg.Process;
11
12 // This test aims at validating that the migration process is robust in face of host turning off either on the SRC 
13 // node or on the DST node. 
14 public class TestHostOnOff extends Process{
15
16   protected Host host0 = null;
17   protected Host host1 = null;
18   protected Host host2 = null;
19
20   TestHostOnOff(String hostname, String name) throws  HostNotFoundException, NativeException {
21     super(hostname, name);
22   }
23
24   public void main(String[] strings) throws MsgException {
25     /* get hosts 1 and 2*/
26     try {
27       host0 = Host.getByName("PM0");
28       host1 = Host.getByName("PM1");
29       host2 = Host.getByName("PM2");
30     }catch (HostNotFoundException e) {
31       e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
32     }
33
34     // Robustness on the SRC node
35     for (int i =100 ; i < 55000 ; i+=100)
36      testVMMigrate(host1, i);
37
38     // Robustness on the DST node
39     for (int i =0 ; i < 55000 ; i++)
40       testVMMigrate(host2, i);
41
42     /* End of Tests */
43     Msg.info("Nor more tests, Bye Bye !");
44     Main.setEndOfTest();
45   }
46
47   public void testVMMigrate (Host hostToKill, long killAt) throws MsgException {
48     Msg.info("**** **** **** ***** ***** Test Migrate with host shutdown ***** ***** **** **** ****");
49     Msg.info("Turn on one host, assign a VM on this host, launch a process inside the VM, migrate the VM and "
50              + "turn off either the SRC or DST");
51
52     host1.off();
53     host2.off();
54     host1.on();
55     host2.on();
56
57     // Create VM0
58     int dpRate = 70;
59     XVM vm0 = null;
60     vm0 = new XVM(host1, "vm0",
61         1, // Nb of vcpu
62         2048, // Ramsize,
63         125, // Net Bandwidth
64         null, //VM disk image
65         -1,   //size of disk image,
66         125, // Net bandwidth,
67         dpRate // Memory intensity
68         );
69     vm0.start();
70     vm0.setLoad(90);
71
72     String[] args = new String[3];
73
74     args[0] = "vm0";
75     args[1] = "PM1";
76     args[2] = "PM2";
77     new Process(host1, "Migrate-" + killAt, args) {
78       public void main(String[] args) {
79         Host destHost = null;
80         Host sourceHost = null;
81
82         try {
83           sourceHost = Host.getByName(args[1]);
84           destHost = Host.getByName(args[2]);
85         } catch (Exception e) {
86           e.printStackTrace();
87           System.err.println("You are trying to migrate from/to a non existing node");
88         }
89         if (destHost != null) {
90           if (sourceHost.isOn() && destHost.isOn()) {
91             try {
92               Msg.info("Migrate vm "+args[0]+" to node "+destHost.getName());
93               VM.getVMByName(args[0]).migrate(destHost);
94             } catch (HostFailureException e) {
95               Msg.info("Something occurs during the migration that cannot validate the operation");
96               e.printStackTrace();
97             }
98           }
99         }
100       }
101     }.start();
102
103     // Wait killAt ms before killing thehost
104     Process.sleep(killAt);
105     Msg.info("The migration process should be stopped and we should catch an exception");
106     hostToKill.off();
107
108     Process.sleep(50000);
109     Msg.info("Destroy VMs");
110     vm0.shutdown();
111     Process.sleep(20000);
112   }
113
114   public void testVMShutdownDestroy () throws HostFailureException {
115     Msg.info("**** **** **** ***** ***** Test shutdown a VM ***** ***** **** **** ****");
116     Msg.info("Turn on host1, assign a VM on host1, launch a process inside the VM, and turn off the vm, " +
117         "and check whether you can reallocate the same VM");
118
119     // Create VM0
120     int dpRate = 70;
121     XVM vm0 = new XVM(host1, "vm0",
122         1, // Nb of vcpu
123         2048, // Ramsize,
124         125, // Net Bandwidth
125         null, //VM disk image
126         -1,   //size of disk image,
127         125, // Net bandwidth,
128         dpRate // Memory intensity
129         );
130     Msg.info("Start VM0");
131     vm0.start();
132     vm0.setLoad(90);
133
134     Process.sleep(5000);
135
136     Msg.info("Shutdown VM0");
137     vm0.shutdown();
138     Process.sleep(5000);
139
140     Msg.info("Restart VM0");
141     vm0 = new XVM(host1, "vm0",
142         1, // Nb of vcpu
143         2048, // Ramsize,
144         125, // Net Bandwidth
145         null, //VM disk image
146         -1,   //size of disk image,
147         125, // Net bandwidth,
148         dpRate // Memory intensity
149         );
150     vm0.start();
151     vm0.setLoad(90);
152
153     Msg.info("You suceed to recreate and restart a VM without generating any exception ! Great the Test is ok");
154
155     Process.sleep(5000);
156     vm0.shutdown();
157   }
158 }