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

Datacenter, Host and VMs iterator #178

Open randyRivera0 opened 1 month ago

randyRivera0 commented 1 month ago

Issue: Extend HostVMIterator for Datacenter-Level Iteration

Description:

Currently, the HostVMIterator provides functionality to iterate over VMs across multiple hosts. However, there's a need to extend this capability to handle datacenter-level iteration. This will enable efficient management and processing of VMs across entire datacenters.

Expected Behavior:

Introduce a Datacenter class to encapsulate a list of Host objects.
Create a DatacenterIterator class extending HostVMIterator to iterate over datacenters, hosts, and VMs sequentially.
Implement proper error handling for null or empty datacenters/hosts.
Consider performance optimizations for large-scale environments.

Additional Considerations:

Explore parallel iteration for performance gains.
Provide customizable filtering options for VMs.
Write comprehensive unit tests.

By implementing this feature, we will enhance the flexibility and usability of the HostVMIterator for managing VMs across datacenters. `public class HostVMIterator implements Iterator {

private List<Host> hosts;
private int currentHostIndex;
private Iterator<VM> currentHostVMIterator;

public HostVMIterator(List<Host> hosts) {
    this.hosts = hosts;
    this.currentHostIndex = 0;
    this.currentHostVMIterator = null;
}

@Override
public boolean hasNext() {
    if (currentHostVMIterator != null && currentHostVMIterator.hasNext()) {
        return true;
    }
    return nextHostVMIterator() != null;
}

@Override
public VM next() {
    if (currentHostVMIterator == null || !currentHostVMIterator.hasNext()) {
        nextHostVMIterator();
    }
    return currentHostVMIterator.next();
}

private Iterator<VM> nextHostVMIterator() {
    if (currentHostIndex >= hosts.size()) {
        return null;
    }

    Host host = hosts.get(currentHostIndex);
    currentHostVMIterator = host.getVMs().iterator();
    currentHostIndex++;
    return currentHostVMIterator;
}

} `

`public class DatacenterVMIterator implements Iterator {

private Datacenter datacenter;
private Iterator<Host> hostIterator;
private Iterator<VM> vmIterator;

public DatacenterVMIterator(Datacenter datacenter) {
    this.datacenter = datacenter;
    this.hostIterator = datacenter.getHosts().iterator();
}

@Override
public boolean hasNext() {
    if (vmIterator != null && vmIterator.hasNext()) {
        return true;
    }
    while (hostIterator.hasNext()) {
        vmIterator = hostIterator.next().getVMs().iterator();
        if (vmIterator.hasNext()) {
            return true;
        }
    }
    return false;
}

@Override
public VM next() {
    if (hasNext()) {
        return vmIterator.next();
    }
    throw new NoSuchElementException();
}

} `