Logo AND Algorithmique Numérique Distribuée

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