-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathload_balancer.py
More file actions
73 lines (58 loc) · 2.31 KB
/
Copy pathload_balancer.py
File metadata and controls
73 lines (58 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
62
63
64
65
66
67
68
69
70
71
72
73
"""Definition of the node represented by the LB in the simulation"""
from collections.abc import Generator
from typing import TYPE_CHECKING
import simpy
from app.config.constants import LbAlgorithmsName, SystemNodes
from app.runtime.actors.edge import EdgeRuntime
from app.runtime.actors.helpers.lb_algorithms import (
least_connections,
round_robin,
)
from app.schemas.system_topology.full_system_topology import LoadBalancer
if TYPE_CHECKING:
from app.runtime.rqs_state import RequestState
class LoadBalancerRuntime:
"""class to define the behaviour of the LB in the simulation"""
def __init__(
self,
*,
env: simpy.Environment,
lb_config: LoadBalancer,
out_edges: list[EdgeRuntime] | None,
lb_box: simpy.Store,
) -> None:
"""
Descriprion of the instance attributes for the class
Args:
env (simpy.Environment): env of the simulation
lb_config (LoadBalancer): input to define the lb in the runtime
rqs_state (RequestState): state of the simulation
out_edges (list[EdgeRuntime]): list of edges that connects lb with servers
lb_box (simpy.Store): store to add the state
"""
self.env = env
self.lb_config = lb_config
self.out_edges = out_edges
self.lb_box = lb_box
self._round_robin_index: int = 0
def _forwarder(self) -> Generator[simpy.Event, None, None]:
"""Updtate the state before passing it to another node"""
assert self.out_edges is not None
while True:
state: RequestState = yield self.lb_box.get() # type: ignore[assignment]
state.record_hop(
SystemNodes.LOAD_BALANCER,
self.lb_config.id,
self.env.now,
)
if self.lb_config.algorithms == LbAlgorithmsName.ROUND_ROBIN:
out_edge, self._round_robin_index = round_robin(
self.out_edges,
self._round_robin_index,
)
else:
out_edge = least_connections(self.out_edges)
out_edge.transport(state)
def start(self) -> simpy.Process:
"""Initialization of the simpy process for the LB"""
return self.env.process(self._forwarder())