Skip to content

Instantly share code, notes, and snippets.

@alessandroleite
Last active January 5, 2018 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save alessandroleite/4072321 to your computer and use it in GitHub Desktop.
Save alessandroleite/4072321 to your computer and use it in GitHub Desktop.
An example of DatacenterBroker that allocates the VMs following the Round-Robin algorithm.
package org.cloudbus.cloudsim.examples;
import java.util.List;
import org.cloudbus.cloudsim.DatacenterBroker;
import org.cloudbus.cloudsim.DatacenterCharacteristics;
import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.Vm;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.core.CloudSimTags;
import org.cloudbus.cloudsim.core.SimEvent;
import java.util.List;
import org.cloudbus.cloudsim.DatacenterBroker;
import org.cloudbus.cloudsim.DatacenterCharacteristics;
import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.Vm;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.core.CloudSimTags;
import org.cloudbus.cloudsim.core.SimEvent;
/**
* This broker allocates VMs to data centers following the
* <a href="http://en.wikipedia.org/wiki/Round-robin_scheduling">Round-robin</a> algorithm.
* @author alessandro
*/
public class RoundRobinDatacenterBroker extends DatacenterBroker
{
/**
* Creates an instance of this class associating to it a given name.
* @param name The name to be associated to this broker. It might not be <code>null</code> or empty.
* @throws Exception If the name contains spaces.
*/
public RoundRobinDatacenterBroker(String name) throws Exception
{
super(name);
}
@Override
protected void processResourceCharacteristics(SimEvent ev)
{
DatacenterCharacteristics characteristics = (DatacenterCharacteristics) ev.getData();
getDatacenterCharacteristicsList().put(characteristics.getId(), characteristics);
if (getDatacenterCharacteristicsList().size() == getDatacenterIdsList().size())
{
distributeRequestsForNewVmsAcrossDatacentersUsingTheRoundRobinApproach();
}
}
/**
* Distributes the VMs across the data centers using the round-robin approach. A VM is allocated to a data center only if there isn't
* a VM in the data center with the same id.
*/
protected void distributeRequestsForNewVmsAcrossDatacentersUsingTheRoundRobinApproach()
{
int numberOfVmsAllocated = 0;
int i = 0;
final List<Integer> availableDatacenters = getDatacenterIdsList();
for (Vm vm : getVmList())
{
int datacenterId = availableDatacenters.get(i++ % availableDatacenters.size());
String datacenterName = CloudSim.getEntityName(datacenterId);
if (!getVmsToDatacentersMap().containsKey(vm.getId()))
{
Log.printLine(CloudSim.clock() + ": " + getName() + ": Trying to Create VM #" + vm.getId() + " in " + datacenterName);
sendNow(datacenterId, CloudSimTags.VM_CREATE_ACK, vm);
numberOfVmsAllocated++;
}
}
setVmsRequested(numberOfVmsAllocated);
setVmsAcks(0);
}
}
@alna
Copy link

alna commented Nov 10, 2013

Hi,
i am pretty new to cloudsim and want to implement a RoundRobinDatacenterBroker in it ,but when i use the class introduced above an error occur in line 45 for characteristics.getId().how can it be solve?

@shreyag12
Copy link

where is the main function of this code?

@amarnathbh
Copy link

is this the example of Intercloud?

@ravikant987
Copy link

hello,
i am not embedding my encryption algorithm in cloudsim example than where i embed this implementation code. please help and suggest me.

@mukhtarEdris
Copy link

Dear alessandro,
I want to change this code to distributes the VMs across the data centers according to the available CPU capacity and energy on data centers
what I should do to achieve this?
Thanks in advance!

@Haripriyaboga
Copy link

Dear alessandro,
The code ended up having an error at Line no: 64(getVmList()) "Type mismatch: cannot convert from element type Object to Vm". How can i be able to resolve it.

Thanking you, waiting for your valuable reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment