Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
small changes
[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       Msg.error("You are trying to use a non existing node:" + e.getMessage());
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 (HostNotFoundException e) {
86           Msg.error("You are trying to migrate from/to a non existing node: " + e.getMessage());
87           e.printStackTrace();
88         }
89         if (destHost != null && sourceHost.isOn() && destHost.isOn()) {
90           try {
91             Msg.info("Migrate vm "+args[0]+" to node "+destHost.getName());
92             VM.getVMByName(args[0]).migrate(destHost);
93           } catch (HostFailureException e) {
94             Msg.error("Something occurs during the migration that cannot validate the operation: " + e.getMessage());
95             e.printStackTrace();
96           }
97         }
98       }
99     }.start();
100
101     // Wait killAt ms before killing thehost
102     Process.sleep(killAt);
103     Msg.info("The migration process should be stopped and we should catch an exception");
104     hostToKill.off();
105
106     Process.sleep(50000);
107     Msg.info("Destroy VMs");
108     vm0.shutdown();
109     Process.sleep(20000);
110   }
111
112   public void testVMShutdownDestroy () throws HostFailureException {
113     Msg.info("**** **** **** ***** ***** Test shutdown a VM ***** ***** **** **** ****");
114     Msg.info("Turn on host1, assign a VM on host1, launch a process inside the VM, and turn off the vm, " +
115         "and check whether you can reallocate the same VM");
116
117     // Create VM0
118     int dpRate = 70;
119     XVM vm0 = new XVM(host1, "vm0",
120         1, // Nb of vcpu
121         2048, // Ramsize,
122         125, // Net Bandwidth
123         null, //VM disk image
124         -1,   //size of disk image,
125         125, // Net bandwidth,
126         dpRate // Memory intensity
127         );
128     Msg.info("Start VM0");
129     vm0.start();
130     vm0.setLoad(90);
131
132     Process.sleep(5000);
133
134     Msg.info("Shutdown VM0");
135     vm0.shutdown();
136     Process.sleep(5000);
137
138     Msg.info("Restart VM0");
139     vm0 = new XVM(host1, "vm0",
140         1, // Nb of vcpu
141         2048, // Ramsize,
142         125, // Net Bandwidth
143         null, //VM disk image
144         -1,   //size of disk image,
145         125, // Net bandwidth,
146         dpRate // Memory intensity
147         );
148     vm0.start();
149     vm0.setLoad(90);
150
151     Msg.info("You suceed to recreate and restart a VM without generating any exception ! Great the Test is ok");
152
153     Process.sleep(5000);
154     vm0.shutdown();
155   }
156 }