Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor addMigratingInVm Method Using Strategy Pattern on CloudSimExample1 #177

Open
dquishpe opened this issue Jul 28, 2024 · 0 comments

Comments

@dquishpe
Copy link

  • This code refactors the addMigratingInVm method in the Host class by applying the Strategy design pattern.
  • The primary motivation for using the Strategy pattern is to improve code modularity and maintainability.
  • Initially, the method contained multiple resource checks (storage, RAM, bandwidth, MIPS) using conditional statements,
    making the code difficult to read and maintain. By encapsulating each resource check into separate strategy classes,
    we achieve a cleaner, more extensible design. This allows us to easily modify or add new resource checks without changing
    the main method's logic, thus adhering to the Single Responsibility Principle and enhancing the code's flexibility and cohesion.
public interface ResourceCheckStrategy {
    boolean check(Vm vm, Host host);
public class StorageCheckStrategy implements ResourceCheckStrategy {
    @Override
    public boolean check(Vm vm, Host host) {
        if (host.getStorage() < vm.getSize()) {
            Log.printConcatLine("[VmScheduler.addMigratingInVm] Allocation of VM #", vm.getId(), " to Host #",
                    host.getId(), " failed by storage");
            return false;
        }
        return true;
    }
}

public class RamCheckStrategy implements ResourceCheckStrategy {
    @Override
    public boolean check(Vm vm, Host host) {
        if (!host.getRamProvisioner().allocateRamForVm(vm, vm.getCurrentRequestedRam())) {
            Log.printConcatLine("[VmScheduler.addMigratingInVm] Allocation of VM #", vm.getId(), " to Host #",
                    host.getId(), " failed by RAM");
            return false;
        }
        return true;
    }
}

public class BwCheckStrategy implements ResourceCheckStrategy {
    @Override
    public boolean check(Vm vm, Host host) {
        if (!host.getBwProvisioner().allocateBwForVm(vm, vm.getCurrentRequestedBw())) {
            Log.printLine("[VmScheduler.addMigratingInVm] Allocation of VM #" + vm.getId() + " to Host #"
                    + host.getId() + " failed by BW");
            return false;
        }
        return true;
    }
}

public class MipsCheckStrategy implements ResourceCheckStrategy {
    @Override
    public boolean check(Vm vm, Host host) {
        if (!host.getVmScheduler().allocatePesForVm(vm, vm.getCurrentRequestedMips())) {
            Log.printLine("[VmScheduler.addMigratingInVm] Allocation of VM #" + vm.getId() + " to Host #"
                    + host.getId() + " failed by MIPS");
            return false;
        }
        return true;
    }
}

public class Host {
    private List<ResourceCheckStrategy> strategies;

    public Host() {
        strategies = new ArrayList<>();
        strategies.add(new StorageCheckStrategy());
        strategies.add(new RamCheckStrategy());
        strategies.add(new BwCheckStrategy());
        strategies.add(new MipsCheckStrategy());
    }

    public void addMigratingInVm(Vm vm) {
        vm.setInMigration(true);

        if (!getVmsMigratingIn().contains(vm)) {
            for (ResourceCheckStrategy strategy : strategies) {
                if (!strategy.check(vm, this)) {
                    System.exit(0);
                }
            }

            getVmScheduler().getVmsMigratingIn().add(vm.getUid());
            setStorage(getStorage() - vm.getSize());

            getVmsMigratingIn().add(vm);
            getVmList().add(vm);
            updateVmsProcessing(CloudSim.clock());
            vm.getHost().updateVmsProcessing(CloudSim.clock());
        }
    }

    // Other methods and attributes of the Host class
}

public class Vm {
    private boolean inMigration;
    private double size;
    private double currentRequestedRam;
    private double currentRequestedBw;
    private double currentRequestedMips;
    private Host host;
    private String uid;
    private int id;

    // Getters and setters

    public void setInMigration(boolean inMigration) {
        this.inMigration = inMigration;
    }

    public boolean isInMigration() {
        return inMigration;
    }

    public double getSize() {
        return size;
    }

    public double getCurrentRequestedRam() {
        return currentRequestedRam;
    }

    public double getCurrentRequestedBw() {
        return currentRequestedBw;
    }

    public double getCurrentRequestedMips() {
        return currentRequestedMips;
    }

    public Host getHost() {
        return host;
    }

    public String getUid() {
        return uid;
    }

    public int getId() {
        return id;
    }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant