Cloudslab / cloudsim

CloudSim: A Framework For Modeling And Simulation Of Cloud Computing Infrastructures And Services
http://www.cloudbus.org/cloudsim/
812 stars 491 forks source link

Structural desing patterns recommendation on CloudSimExample1 #176

Open jfcofer opened 1 month ago

jfcofer commented 1 month ago

The process of initializing and configuring the CloudSim simulator is complex and scattered throughout the main method, which makes the code difficult to maintain and extend. To address this issue, it is proposed to implement the Facade design pattern. This would involve creating a Facade class that hides the complex details of the simulator's initialization and configuration, thereby simplifying interaction with the CloudSim system.

Additionally, the Cloudlet class currently uses the UtilizationModelFull directly, which limits the flexibility to switch to other utilization models. To improve this, the Adapter design pattern can be applied, allowing Cloudlet to use different utilization models without modifying its code. This will enhance flexibility and make it easier to incorporate new models in the future.

Furthermore, the code for configuring datacenters and hosts is linear and could benefit from a more flexible and hierarchical structure. To improve this, the Composite design pattern can be implemented. This would enable handling the configuration of multiple datacenters, hosts, and PEs in a more structured manner, making management and maintenance easier.

public class CloudSimFacade {
    public void initializeCloudSim(int numUser, Calendar calendar, boolean traceFlag) {
        CloudSim.init(numUser, calendar, traceFlag);
    }

    public Datacenter createDatacenter(String name) {
        // Aquí están los pasos necesarios para crear un Datacenter:
        List<Host> hostList = new ArrayList<>();

        // Un Host contiene uno o más PEs o CPUs/Cores.
        List<Pe> peList = new ArrayList<>();
        int mips = 1000;
        peList.add(new Pe(0, new PeProvisionerSimple(mips)));

        int hostId = 0;
        int ram = 2048; // host memory (MB)
        long storage = 1000000; // host storage
        int bw = 10000;

        hostList.add(new Host(
                hostId,
                new RamProvisionerSimple(ram),
                new BwProvisionerSimple(bw),
                storage,
                peList,
                new VmSchedulerTimeShared(peList)
        ));

        String arch = "x86"; // system architecture
        String os = "Linux"; // operating system
        String vmm = "Xen";
        double time_zone = 10.0; // time zone this resource located
        double cost = 3.0; // the cost of using processing in this resource
        double costPerMem = 0.05; // the cost of using memory in this resource
        double costPerStorage = 0.001; // the cost of using storage in this resource
        double costPerBw = 0.0; // the cost of using bw in this resource
        LinkedList<Storage> storageList = new LinkedList<>();

        DatacenterCharacteristics characteristics = new DatacenterCharacteristics(
                arch, os, vmm, hostList, time_zone, cost, costPerMem,
                costPerStorage, costPerBw);

        Datacenter datacenter = null;
        try {
            datacenter = new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), storageList, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return datacenter;
    }

    public DatacenterBroker createBroker() {
        DatacenterBroker broker = null;
        try {
            broker = new DatacenterBroker("Broker");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return broker;
    }

    public void startSimulation() {
        CloudSim.startSimulation();
        CloudSim.stopSimulation();
    }

    public List<Cloudlet> getCloudletReceivedList(DatacenterBroker broker) {
        return broker.getCloudletReceivedList();
    }
}
public interface UtilizationModelAdapter {
    double getUtilization(double time);
}

public class FullUtilizationModelAdapter implements UtilizationModelAdapter {
    private final UtilizationModelFull model;

    public FullUtilizationModelAdapter(UtilizationModelFull model) {
        this.model = model;
    }

    @Override
    public double getUtilization(double time) {
        return model.getUtilization(time);
    }
}

UtilizationModelAdapter utilizationModel = new FullUtilizationModelAdapter(new UtilizationModelFull());

Cloudlet cloudlet = new Cloudlet(id, length, pesNumber, fileSize, outputSize, utilizationModel, utilizationModel, utilizationModel);
public interface DatacenterComponent {
    void addComponent(DatacenterComponent component);
    void removeComponent(DatacenterComponent component);
    List<DatacenterComponent> getComponents();
}

public class DatacenterHost implements DatacenterComponent {
    private final List<DatacenterComponent> components = new ArrayList<>();

    @Override
    public void addComponent(DatacenterComponent component) {
        components.add(component);
    }

    @Override
    public void removeComponent(DatacenterComponent component) {
        components.remove(component);
    }

    @Override
    public List<DatacenterComponent> getComponents() {
        return components;
    }
}

public class SimplePE implements DatacenterComponent {
    private final Pe pe;

    public SimplePE(Pe pe) {
        this.pe = pe;
    }

    @Override
    public void addComponent(DatacenterComponent component) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void removeComponent(DatacenterComponent component) {
        throw new UnsupportedOperationException();
    }

    @Override
    public List<DatacenterComponent> getComponents() {
        return Collections.emptyList();
    }

    public Pe getPe() {
        return pe;
    }
}