Skip to content

Container Scheduler

The "ContainerScheduler" class is responsible for allocating suitable "vHost" instances to "vContainer" instances based on the available CPU and RAM resources. The scheduling process is implemented as an event that is triggered whenever a new "vContainer" is created or a "vContainer" is terminated. Only one scheduling process can exist at any time during the simulation.

The "ContainerScheduler" class includes an abstract member function called "findHost", which allows developers to customize the conditions for determining which "vHost" instances are eligible for hosting a specific "vContainer". By implementing the "findHost" function, different scheduling strategies can be employed based on specific requirements and constraints.

PyCloudSim provides several default schedulers that can be used with the "ContainerScheduler" class:

  1. "Bestfit" scheduler: This scheduler finds the most utilized "vHost" instance that still has available resources to host the "vContainer" being scheduled.
  2. "Worstfit" scheduler: This scheduler finds the most underutilized "vHost" instance that still has available resources to host the "vContainer" being scheduled.
  3. "Random" scheduler: This scheduler allocates the "vContainer" to a random "vHost" instance that has sufficient resources.

Only one "ContainerScheduler" can be defined and used in the simulation. If multiple "ContainerScheduler" instances are initialized, the last one defined will be used during the simulation.

The general procedure followed by the "ContainerScheduler" involves evaluating the available resources of each "vHost" instance and selecting the most suitable "vHost" to host the "vContainer" based on the defined scheduling strategy. This process ensures efficient resource allocation and utilization in the simulated cloud environment.

BestfitContainerScheduler

Bases: ContainerScheduler

Bestfit container scheduler. It will return the host with the least amount of resources.

Source code in PyCloudSim\scheduler\container_scheduler.py
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
class BestfitContainerScheduler(ContainerScheduler):
    """Bestfit container scheduler. It will return the host with the least amount of resources."""

    def __init__(self) -> None:
        super().__init__()

    def find_host(self, container: vContainer) -> vHost | None:
        simulation.hosts.sort(key=lambda host: host.rom_reservoir.amount)
        simulation.hosts.sort(key=lambda host: host.ram_reservoir.amount)
        simulation.hosts.sort(key=lambda host: host.cpu_reservoir.amount)
        for host in simulation.hosts:
            if (
                host.powered_on
                and host.ram_reservoir.amount >= container.ram
                and host.cpu_reservoir.amount >= container.cpu
                and host.rom_reservoir.amount >= container.image_size
            ):
                return host

ContainerScheduler

Bases: ABC, Entity

Base for all container schedulers.

Source code in PyCloudSim\scheduler\container_scheduler.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class ContainerScheduler(ABC, Entity):
    """Base for all container schedulers."""

    def __init__(self) -> None:
        super().__init__(label="Container Scheduler", create_at=0)
        simulation._container_scheduler = self

    def on_creation(self):
        super().on_creation()

        @self.continuous_event(
            at=simulation.now,
            interval=simulation.min_time_unit,
            duration=inf,
            label="Scheduling Containers",
            priority=inf,
        )
        def _scheduling():
            simulation.containers.sort(key=lambda container: container.priority)
            for container in simulation.containers:
                # skip if container is already scheduled
                if container.scheduled:
                    continue

                # skip if not all volumes are scheduled
                if not all([volume.scheduled for volume in container.volumes]):
                    continue

                # find a host for the container
                host = self.find_host(container)
                # if a host is found, allocate the container to the host
                if host:
                    host.allocate_container(container)
                else:
                    logger.debug(
                        f"{simulation.now}:\tContainer {container.label} cannot be scheduled."
                    )

    def on_termination(self):
        return super().on_termination()

    def on_destruction(self):
        return super().on_destruction()

    @abstractmethod
    def find_host(self, container: vContainer) -> vHost | None:
        pass

DefaultContainerScheduler

Bases: ContainerScheduler

Default container scheduler. It will return the first available host.

Source code in PyCloudSim\scheduler\container_scheduler.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class DefaultContainerScheduler(ContainerScheduler):
    """Default container scheduler. It will return the first available host."""

    def __init__(self) -> None:
        super().__init__()

    def find_host(self, container: vContainer) -> vHost | None:
        for host in simulation.hosts:
            if (
                host.powered_on
                and host.ram_reservoir.amount >= container.ram
                and host.cpu_reservoir.amount >= container.cpu
                and host.rom_reservoir.amount >= container.image_size
            ):
                return host

WorstfitContainerScheduler

Bases: ContainerScheduler

Worstfit container scheduler. It will return the host with the most amount of resources.

Source code in PyCloudSim\scheduler\container_scheduler.py
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
class WorstfitContainerScheduler(ContainerScheduler):
    """Worstfit container scheduler. It will return the host with the most amount of resources."""

    def __init__(self) -> None:
        super().__init__()

    def find_host(self, container: vContainer) -> vHost | None:
        simulation.hosts.sort(key=lambda host: host.rom_reservoir.amount, reverse=True)
        simulation.hosts.sort(key=lambda host: host.ram_reservoir.amount, reverse=True)
        simulation.hosts.sort(key=lambda host: host.cpu_reservoir.amount, reverse=True)
        for host in simulation.hosts:
            if (
                host.powered_on
                and host.ram_reservoir.amount >= container.ram
                and host.cpu_reservoir.amount >= container.cpu
                and host.rom_reservoir.amount >= container.image_size
            ):
                return host