Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sonar: reduce verbosity now that it works
[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.ArrayList;
10 import java.util.List;
11 import java.util.Random; 
12
13 import org.simgrid.msg.*;
14 import org.simgrid.msg.Process;
15
16 // This test aims at validating that the migration process is robust in face of host turning off either on the SRC 
17 // node or on the DST node. 
18 public class TestHostOnOff extends Process{    
19
20   public static Host host0 = null;
21   public static Host host1 = null;
22   public static Host host2 = null;
23
24
25   TestHostOnOff(Host host, String name, String[] args) throws HostNotFoundException, NativeException {
26     super(host, name, args);
27   }
28
29   public void main(String[] strings) throws MsgException {
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 "
58              + "turn off either the SRC or DST");
59
60     host1.off();
61     host2.off();
62     host1.on();
63     host2.on();
64
65     // Create VM0
66     int dpRate = 70;
67     XVM vm0 = null;
68     vm0 = new XVM(host1, "vm0",
69         1, // Nb of vcpu
70         2048, // Ramsize,
71         125, // Net Bandwidth
72         null, //VM disk image
73         -1,   //size of disk image,
74         125, // Net bandwidth,
75         dpRate // Memory intensity
76         );
77     vm0.start();
78     vm0.setLoad(90);
79
80     String[] args = new String[3];
81
82     args[0] = "vm0";
83     args[1] = "host1";
84     args[2] = "host2";
85     new Process(host1, "Migrate-" + new Random().nextDouble(), args) {
86       public void main(String[] args) {
87         Host destHost = null;
88         Host sourceHost = null;
89
90         try {
91           sourceHost = Host.getByName(args[1]);
92           destHost = Host.getByName(args[2]);
93         } catch (Exception e) {
94           e.printStackTrace();
95           System.err.println("You are trying to migrate from/to a non existing node");
96         }
97         if (destHost != null) {
98           if (sourceHost.isOn() && destHost.isOn()) {
99             try {
100               Msg.info("Migrate vm "+args[0]+" to node "+destHost.getName());
101               VM.getVMByName(args[0]).migrate(destHost);
102             } catch (HostFailureException e) {
103               e.printStackTrace();
104               Msg.info("Something occurs during the migration that cannot validate the operation");
105             }
106           }
107         }
108       }
109     }.start();
110
111     // Wait killAt ms before killing thehost
112     Process.sleep(killAt);
113     hostToKill.off();
114     Process.sleep(5);
115     Msg.info("The migration process should be stopped and we should catch an exception\n");
116     Process.sleep(5);
117
118     Process.sleep(50000);
119     Msg.info("Destroy VMs");
120     vm0.shutdown();
121     Process.sleep(20000);
122   }
123
124   public static void test_vm_shutdown_destroy () throws HostFailureException {
125     Msg.info("**** **** **** ***** ***** Test shutdown a VM ***** ***** **** **** ****");
126     Msg.info("Turn on host1, assign a VM on host1, launch a process inside the VM, and turn off the vm, " +
127         "and check whether you can reallocate the same VM");
128
129     // Create VM0
130     int dpRate = 70;
131     XVM vm0 = null;
132     vm0 = new XVM(host1, "vm0",
133         1, // Nb of vcpu
134         2048, // Ramsize,
135         125, // Net Bandwidth
136         null, //VM disk image
137         -1,   //size of disk image,
138         125, // Net bandwidth,
139         dpRate // Memory intensity
140         );
141     Msg.info("Start VM0");
142     vm0.start();
143     vm0.setLoad(90);
144
145     Process.sleep(5000);
146
147     Msg.info("Shutdown VM0");
148     vm0.shutdown();
149     Process.sleep(5000);
150
151     Msg.info("Restart VM0");
152     vm0 = new XVM(host1, "vm0",
153         1, // Nb of vcpu
154         2048, // Ramsize,
155         125, // Net Bandwidth
156         null, //VM disk image
157         -1,   //size of disk image,
158         125, // Net bandwidth,
159         dpRate // Memory intensity
160         );
161     vm0.start();
162     vm0.setLoad(90);
163
164     Msg.info("You suceed to recreate and restart a VM without generating any exception ! Great the Test is ok");
165
166     Process.sleep(5000);
167     vm0.shutdown();
168   }
169 }