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 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         vm0.destroy();
125         Process.sleep(20000);
126     }
127
128
129     public static void test_vm_shutdown_destroy () throws HostFailureException {
130
131         Msg.info("**** **** **** ***** ***** Test shutdown /destroy a VM ***** ***** **** **** ****");
132         Msg.info("Turn on host1, assign a VM on host1, launch a process inside the VM, and turn off the vm, " +
133                 "destroy it and check whether you can reallocate the same VM");
134
135
136         // Create VM0
137         int dpRate = 70;
138         XVM vm0 = null;
139         vm0 = new XVM(
140                 host1,
141                 "vm0",
142                 1, // Nb of vcpu
143                 2048, // Ramsize,
144                 125, // Net Bandwidth
145                 null, //VM disk image
146                 -1,   //size of disk image,
147                 125, // Net bandwidth,
148                 dpRate // Memory intensity
149         );
150         Msg.info("Start VM0");
151         vm0.start();
152         vm0.setLoad(90);
153
154         Process.sleep(5000);
155
156         Msg.info("Shutdown VM0");
157         vm0.shutdown();
158         Process.sleep(5000);
159         Msg.info("Destroy VM0");
160         vm0.destroy();
161
162         Process.sleep(5000);
163         Msg.info("Restart VM0");
164         vm0 = new XVM(
165                 host1,
166                 "vm0",
167                 1, // Nb of vcpu
168                 2048, // Ramsize,
169                 125, // Net Bandwidth
170                 null, //VM disk image
171                 -1,   //size of disk image,
172                 125, // Net bandwidth,
173                 dpRate // Memory intensity
174         );
175         vm0.start();
176         vm0.setLoad(90);
177     
178         Msg.info("You suceed to recreate and restart a VM without generating any exception ! Great the Test is ok");
179                 
180         Process.sleep(5000);
181         vm0.shutdown();
182         Process.sleep(5000);
183         vm0.destroy();
184     }
185
186 }
187
188
189
190