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