forked from cloudsimplus/cloudsimplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatacenterSelectionByTimeZoneExample.java
executable file
·235 lines (203 loc) · 8.79 KB
/
DatacenterSelectionByTimeZoneExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* CloudSim Plus: A modern, highly-extensible and easier-to-use Framework for
* Modeling and Simulation of Cloud Computing Infrastructures and Services.
* http://cloudsimplus.org
*
* Copyright (C) 2015-2018 Universidade da Beira Interior (UBI, Portugal) and
* the Instituto Federal de Educação Ciência e Tecnologia do Tocantins (IFTO, Brazil).
*
* This file is part of CloudSim Plus.
*
* CloudSim Plus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CloudSim Plus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CloudSim Plus. If not, see <http://www.gnu.org/licenses/>.
*/
package org.cloudsimplus.examples.brokers;
import ch.qos.logback.classic.Level;
import org.cloudbus.cloudsim.brokers.DatacenterBroker;
import org.cloudbus.cloudsim.brokers.DatacenterBrokerSimple;
import org.cloudbus.cloudsim.cloudlets.Cloudlet;
import org.cloudbus.cloudsim.cloudlets.CloudletSimple;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.datacenters.Datacenter;
import org.cloudbus.cloudsim.datacenters.DatacenterSimple;
import org.cloudbus.cloudsim.datacenters.TimeZoned;
import org.cloudbus.cloudsim.hosts.Host;
import org.cloudbus.cloudsim.hosts.HostSimple;
import org.cloudbus.cloudsim.resources.Pe;
import org.cloudbus.cloudsim.resources.PeSimple;
import org.cloudbus.cloudsim.utilizationmodels.UtilizationModelDynamic;
import org.cloudbus.cloudsim.vms.Vm;
import org.cloudbus.cloudsim.vms.VmSimple;
import org.cloudsimplus.builders.tables.CloudletsTableBuilder;
import org.cloudsimplus.builders.tables.TextTableColumn;
import org.cloudsimplus.util.Log;
import java.util.*;
/**
* An example showing how to use the {@link DatacenterBrokerSimple}
* to select the {@link Datacenter} closest to every submitted {@link Vm},
* according to their {@link TimeZoned#getTimeZone() TimeZone offset}.
*
* <p>Realize that for this feature to work, the {@link DatacenterBroker#setSelectClosestDatacenter(boolean)}
* must be called to enable it.</p>
*
* @author Manoel Campos da Silva Filho
* @since CloudSim Plus 4.8.0
*/
public class DatacenterSelectionByTimeZoneExample {
/**
* Amazon Web Services Regions (just some of them).
* @see <a href="https://aws.amazon.com/about-aws/global-infrastructure/">AWS Global Infrastructure</a>
*/
private static final Map<String, Double> DATACENTERS_TIMEZONES = new TreeMap<String, Double>(){{
put("Oregon", -7.0);
put("California", -7.0);
put("Canada", -4.0);
put("São Paulo", -3.0);
put("Ireland", 1.0);
put("London", 1.0);
put("Paris", 2.0);
put("Frankfurt", 2.0);
put("Cape Town", 2.0);
put("Bahrain", 3.0);
put("Mumbai", 5.5);
put("Singapore", 8.0);
put("Tokyo", 9.0);
put("Seoul", 9.0);
put("Sydney", 10.0);
}};
private static final int HOSTS = 5;
private static final int HOST_PES = 8;
/**
* The list of timezones where each VM is expected to be placed.
* This way, the broker will try to place each VM at the closest
* Datacenter as possible.
*/
private static final double[] VMS_TIMEZONES = {1, 5, -3, -5, 0, 5.5, 2, 4, 9, 11, -9};
private static final int VM_PES = 4;
private static final int CLOUDLETS = VMS_TIMEZONES.length;
private static final int CLOUDLET_PES = 2;
private static final int CLOUDLET_LENGTH = 10000;
private final CloudSim simulation;
private DatacenterBroker broker0;
private List<Vm> vmList;
private List<Cloudlet> cloudletList;
private List<Datacenter> datacenterList;
private long lastHostId;
public static void main(String[] args) {
new DatacenterSelectionByTimeZoneExample();
}
private DatacenterSelectionByTimeZoneExample() {
Log.setLevel(Level.WARN);
simulation = new CloudSim();
datacenterList = createDatacenters();
//Creates a broker that is a software acting on behalf a cloud customer to manage his/her VMs and Cloudlets
broker0 = new DatacenterBrokerSimple(simulation);
vmList = createVms();
cloudletList = createCloudlets();
/*Enables the selection of the closest datacenter for every VM,
then submits Vms and Cloudlets.*/
broker0.setSelectClosestDatacenter(true)
.submitVmList(vmList)
.submitCloudletList(cloudletList);
simulation.start();
final List<Cloudlet> finishedCloudlets = broker0.getCloudletFinishedList();
finishedCloudlets.sort(Comparator.comparingDouble(cl -> cl.getVm().getTimeZone()));
new CloudletsTableBuilder(finishedCloudlets)
.addColumn(3, new TextTableColumn(" DC ", "TimeZone"), this::getDatacenterTimeZone)
.addColumn(8, new TextTableColumn("VM Expected", " TimeZone "), this::getVmTimeZone)
.build();
}
public String getDatacenterTimeZone(final Cloudlet cloudlet) {
return TimeZoned.format(cloudlet.getVm().getHost().getDatacenter().getTimeZone());
}
public String getVmTimeZone(final Cloudlet cloudlet) {
return TimeZoned.format(cloudlet.getVm().getTimeZone());
}
/**
* Creates a List of Datacenters, each Datacenter having
* Hosts with a number of PEs higher than the previous Datacenter.
* @return
*/
private List<Datacenter> createDatacenters(){
final List<Datacenter> list = new ArrayList<>(DATACENTERS_TIMEZONES.size());
for (Map.Entry<String, Double> entry : DATACENTERS_TIMEZONES.entrySet()) {
final Datacenter dc = createDatacenter(entry.getValue());
list.add(dc);
System.out.printf("Created Datacenter %2d in %15s | %s%n", dc.getId(), entry.getKey(), TimeZoned.format(entry.getValue()));
}
System.out.println();
return list;
}
/**
* Creates a Datacenter in a given timezone.
* @param timeZone the time zone offset
*/
private Datacenter createDatacenter(final double timeZone) {
final List<Host> hostList = new ArrayList<>(HOSTS);
for(int i = 0; i < HOSTS; i++) {
Host host = createHost();
hostList.add(host);
}
//Uses a VmAllocationPolicySimple by default to allocate VMs
final Datacenter dc = new DatacenterSimple(simulation, hostList);
dc.setTimeZone(timeZone);
return dc;
}
private Host createHost() {
final List<Pe> peList = new ArrayList<>(HOST_PES);
//List of Host's CPUs (Processing Elements, PEs)
for (int i = 0; i < HOST_PES; i++) {
//Uses a PeProvisionerSimple by default to provision PEs for VMs
peList.add(new PeSimple(1000));
}
final long ram = 2048; //in Megabytes
final long bw = 10000; //in Megabits/s
final long storage = 1000000; //in Megabytes
/*
Uses ResourceProvisionerSimple by default for RAM and BW provisioning
and VmSchedulerSpaceShared for VM scheduling.
*/
final Host host = new HostSimple(ram, bw, storage, peList);
host.setId(++lastHostId);
return host;
}
/**
* Creates a list of VMs, setting the timezone they are expected to be placed.
* This way, the broker will try to place each VM at the closest
* Datacenter as possible.
*/
private List<Vm> createVms() {
final List<Vm> list = new ArrayList<>(VMS_TIMEZONES.length);
for (final double timezone : VMS_TIMEZONES) {
//Uses a CloudletSchedulerTimeShared by default to schedule Cloudlets
final Vm vm = new VmSimple(1000, VM_PES);
vm.setRam(512).setBw(1000).setSize(10000).setTimeZone(timezone);
list.add(vm);
}
return list;
}
/**
* Creates a list of Cloudlets.
*/
private List<Cloudlet> createCloudlets() {
final List<Cloudlet> list = new ArrayList<>(CLOUDLETS);
//UtilizationModel defining the Cloudlets use only 50% of any resource all the time
final UtilizationModelDynamic utilizationModel = new UtilizationModelDynamic(0.5);
for (int i = 0; i < CLOUDLETS; i++) {
final Cloudlet cloudlet = new CloudletSimple(CLOUDLET_LENGTH, CLOUDLET_PES, utilizationModel);
cloudlet.setSizes(1024);
list.add(cloudlet);
}
return list;
}
}