Commit ebb577a6 authored by Eduardo Falcão's avatar Eduardo Falcão
Browse files

refactor sorting of vms and hosts, and removed interface implementation in...

refactor sorting of vms and hosts, and removed interface implementation in credit-driven migration class
parent b6639852
......@@ -17,6 +17,7 @@ import org.cloudbus.cloudsim.provisioners.ResourceProvisionerSimple;
import org.cloudbus.cloudsim.selectionpolicies.power.PowerVmSelectionPolicy;
import org.cloudbus.cloudsim.vms.Vm;
import org.cloudsimplus.slametrics.SLAMonitor;
import org.cloudbus.cloudsim.core.Machine;
import org.cloudbus.cloudsim.core.Simulation;
import org.cloudbus.cloudsim.datacenters.Datacenter;
......@@ -40,15 +41,15 @@ import static java.util.stream.Collectors.toSet;
* @author Kaio Kassiano Moura Oliveira (@kaiokmo)
* @author Eduardo de Lucena Falcao (@eduardolfalcao)
*/
public class VmAllocationPolicyMigrationSla extends
VmAllocationPolicyAbstract implements VmAllocationPolicyMigration {
public class VmAllocationPolicyMigrationSla extends VmAllocationPolicyAbstract {
private static final String LOOSE = "LOOSE";
private static final String TIGHT = "TIGHT";
private static final Double IMPRECISION = 0.001;
//FIXME to be initialized by the constructor
private final int MIGRATION_TIME;
private VmAllocationPolicy allocationPolicy;
/**
* A map between a Host and its previous fragmentation.
......@@ -60,14 +61,13 @@ public class VmAllocationPolicyMigrationSla extends
*/
private Map<Host, Double> currentFragmentation;
public VmAllocationPolicyMigrationSla(int migrationTime) {
public VmAllocationPolicyMigrationSla(VmAllocationPolicy allocationPolicy, int migrationTime) {
this.allocationPolicy = allocationPolicy;
this.MIGRATION_TIME = migrationTime;
previousFragmentation = new HashMap<Host, Double>();
currentFragmentation = new HashMap<Host, Double>();
this.previousFragmentation = new HashMap<Host, Double>();
this.currentFragmentation = new HashMap<Host, Double>();
}
@Override
public Map<Vm, Host> getOptimizedAllocationMap(
final List<? extends Vm> vmList) {
......@@ -84,44 +84,14 @@ public class VmAllocationPolicyMigrationSla extends
//filter heaven hosts
List<Host> looseSlaHosts = getHostsWithLooseSla(getDatacenter());
//get all vms
//get all vms from heaven hosts and sort vms from higher to lower fragmentation
List<Vm> vmsDescendingFragmentation = new ArrayList<Vm>();
vmsDescendingFragmentation.addAll(getVmsWithEnoughCredit(looseSlaHosts));
sort(vmsDescendingFragmentation);
//sort vms from higher to lower fragmentation
Comparator<Vm> vmsComparator = new Comparator<Vm>() {
@Override
public int compare(Vm vm1, Vm vm2) {
double fragmentationVm1 = getVmFragmentation(vm1);
double fragmentationVm2 = getVmFragmentation(vm2);
if(fragmentationVm1 == fragmentationVm2)
return 0;
else if(fragmentationVm1 < fragmentationVm2)
return 1;
else
return -1;
}
};
Collections.sort(vmsDescendingFragmentation, vmsComparator);
//filter hell hosts
//filter hell hosts and sort hell hosts ascending by fragmentation
List<Host> tightSlaHosts = getHostsWithTightSla(getDatacenter());
//sort hell hosts ascending by fragmentation
Comparator<Host> hostsComparator = new Comparator<Host>() {
@Override
public int compare(Host h1, Host h2) {
double fragmentationH1 = getHostFragmentation(h1);
double fragmentationH2 = getHostFragmentation(h2);
if(fragmentationH1 == fragmentationH2)
return 0;
else if(fragmentationH1 < fragmentationH2)
return -1;
else
return 1;
}
};
Collections.sort(tightSlaHosts, hostsComparator);
sort(tightSlaHosts);
//update current fragmentation
for(Host h : getDatacenter().getHostList()){
......@@ -150,11 +120,60 @@ public class VmAllocationPolicyMigrationSla extends
}
}
//TODO and from hell to heaven??
previousFragmentation.putAll(currentFragmentation);
return migrationMap;
}
/**
* Sorts a list of VMs from high to low fragmentation,
* or a list of hosts from low to high fragmentation
* @param list a list of Machines (vms or hosts)
*/
private void sort(List<? extends Machine> list){
if(list.isEmpty())
return;
Comparator<Machine> comparator = null;
if(list.get(0) instanceof Vm){
comparator = new Comparator<Machine>() {
@Override
public int compare(Machine m1, Machine m2) {
Vm vm1 = (Vm) m1;
Vm vm2 = (Vm) m2;
double fragmentationVm1 = getVmFragmentation(vm1);
double fragmentationVm2 = getVmFragmentation(vm2);
if(fragmentationVm1 == fragmentationVm2)
return 0;
else if(fragmentationVm1 < fragmentationVm2)
return 1;
else
return -1;
}
};
}
else{
comparator = new Comparator<Machine>() {
@Override
public int compare(Machine m1, Machine m2) {
Host h1 = (Host) m1;
Host h2 = (Host) m2;
double fragmentationH1 = getHostFragmentation(h1);
double fragmentationH2 = getHostFragmentation(h2);
if(fragmentationH1 == fragmentationH2)
return 0;
else if(fragmentationH1 < fragmentationH2)
return -1;
else
return 1;
}
};
}
Collections.sort(list,comparator);
}
/**
* Get candidate Vms with enough credit to go to a best effort host.
* @param h the host
......@@ -248,6 +267,11 @@ public class VmAllocationPolicyMigrationSla extends
}
}
return exploitationHosts;
}
@Override
public Optional<Host> findHostForVm(Vm vm) {
return allocationPolicy.findHostForVm(vm);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment