Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a few more fixes
[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   public void main(String[] strings) throws MsgException {
27     /* get hosts 1 and 2*/
28     try {
29       host0 = Host.getByName("host0");
30       host1 = Host.getByName("host1");
31       host1 = Host.getByName("host2");
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 =0 ; i < 55000 ; i++)
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 static 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] = "host1";
78     args[2] = "host2";
79     new Process(host1, "Migrate-" + new Random().nextDouble(), 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               e.printStackTrace();
98               Msg.info("Something occurs during the migration that cannot validate the operation");
99             }
100           }
101         }
102       }
103     }.start();
104
105     // Wait killAt ms before killing thehost
106     Process.sleep(killAt);
107     hostToKill.off();
108     Process.sleep(5);
109     Msg.info("The migration process should be stopped and we should catch an exception\n");
110     Process.sleep(5);
111
112     Process.sleep(50000);
113     Msg.info("Destroy VMs");
114     vm0.shutdown();
115     Process.sleep(20000);
116   }
117
118   public static void testVMShutdownDestroy () throws HostFailureException {
119     Msg.info("**** **** **** ***** ***** Test shutdown a VM ***** ***** **** **** ****");
120     Msg.info("Turn on host1, assign a VM on host1, launch a process inside the VM, and turn off the vm, " +
121         "and check whether you can reallocate the same VM");
122
123     // Create VM0
124     int dpRate = 70;
125     XVM vm0 = new XVM(host1, "vm0",
126         1, // Nb of vcpu
127         2048, // Ramsize,
128         125, // Net Bandwidth
129         null, //VM disk image
130         -1,   //size of disk image,
131         125, // Net bandwidth,
132         dpRate // Memory intensity
133         );
134     Msg.info("Start VM0");
135     vm0.start();
136     vm0.setLoad(90);
137
138     Process.sleep(5000);
139
140     Msg.info("Shutdown VM0");
141     vm0.shutdown();
142     Process.sleep(5000);
143
144     Msg.info("Restart VM0");
145     vm0 = new XVM(host1, "vm0",
146         1, // Nb of vcpu
147         2048, // Ramsize,
148         125, // Net Bandwidth
149         null, //VM disk image
150         -1,   //size of disk image,
151         125, // Net bandwidth,
152         dpRate // Memory intensity
153         );
154     vm0.start();
155     vm0.setLoad(90);
156
157     Msg.info("You suceed to recreate and restart a VM without generating any exception ! Great the Test is ok");
158
159     Process.sleep(5000);
160     vm0.shutdown();
161   }
162 }