Logo AND Algorithmique Numérique Distribuée

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