Logo AND Algorithmique Numérique Distribuée

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