|
6 | 6 | from asyncflow.schemas.settings.simulation import SimulationSettings |
7 | 7 | from asyncflow.schemas.topology.graph import TopologyGraph |
8 | 8 | from asyncflow.schemas.workload.rqs_generator import RqsGenerator |
| 9 | +from asyncflow.config.constants import EventDescription |
9 | 10 |
|
10 | 11 |
|
11 | 12 | class SimulationPayload(BaseModel): |
@@ -45,29 +46,29 @@ def ensure_components_ids_is_compatible( |
45 | 46 | if model.events is None: |
46 | 47 | return model |
47 | 48 |
|
48 | | - server_list = model.topology_graph.nodes.servers |
| 49 | + servers_list = model.topology_graph.nodes.servers |
49 | 50 | edges_list = model.topology_graph.edges |
50 | 51 | valid_ids = ( |
51 | | - {server.id for server in server_list} |
| 52 | + {server.id for server in servers_list} |
52 | 53 | | {edge.id for edge in edges_list} |
53 | 54 | ) |
54 | 55 |
|
55 | 56 | for event in model.events: |
56 | 57 | if event.target_id not in valid_ids: |
57 | | - msg = (f"The target id {event.target_id} related to" |
| 58 | + msg = (f"The target id {event.target_id} related to " |
58 | 59 | f"the event {event.event_id} does not exist") |
59 | 60 | raise ValueError(msg) |
60 | 61 |
|
61 | 62 | return model |
62 | 63 |
|
63 | 64 | @model_validator(mode="after") # type: ignore[arg-type] |
64 | | - def ensure_event_time_inside_simulatioon_horizon( |
| 65 | + def ensure_event_time_inside_simulation_horizon( |
65 | 66 | cls, # noqa: N805 |
66 | 67 | model: "SimulationPayload", |
67 | 68 | ) -> "SimulationPayload": |
68 | 69 | """ |
69 | | - The interval of time associated to each events must be |
70 | | - included in the simulation horizon |
| 70 | + The time interval associated to each event must be in |
| 71 | + the simulation horizon |
71 | 72 | """ |
72 | 73 | if model.events is None: |
73 | 74 | return model |
@@ -109,11 +110,93 @@ def ensure_compatibility_event_kind_target_id( |
109 | 110 | ) -> "SimulationPayload": |
110 | 111 | """ |
111 | 112 | The kind of the event must be compatible with the target id |
112 | | - type |
| 113 | + type, for example we cannot have an event regarding a server |
| 114 | + with a target id associated to an edge |
113 | 115 | """ |
114 | 116 | if model.events is None: |
115 | 117 | return model |
116 | | - |
117 | 118 |
|
| 119 | + servers_list = model.topology_graph.nodes.servers |
| 120 | + edges_list = model.topology_graph.edges |
| 121 | + |
| 122 | + # We need just the Start or End kind because |
| 123 | + # we have a validation for the coherence between |
| 124 | + # the starting event kind and the finishing event kind |
| 125 | + server_kind = {EventDescription.SERVER_DOWN} |
| 126 | + edge_kind = {EventDescription.NETWORK_SPIKE_START} |
| 127 | + |
| 128 | + servers_ids = {server.id for server in servers_list} |
| 129 | + edges_ids = {edge.id for edge in edges_list} |
| 130 | + |
| 131 | + for event in model.events: |
| 132 | + if event.start.kind in server_kind and event.target_id not in servers_ids: |
| 133 | + msg = (f"The event {event.event_id} regarding a server does not have " |
| 134 | + "a compatible target id") |
| 135 | + raise ValueError(msg) |
| 136 | + elif event.start.kind in edge_kind and event.target_id not in edges_ids: |
| 137 | + msg = (f"The event {event.event_id} regarding an edge does not have " |
| 138 | + "a compatible target id") |
| 139 | + raise ValueError(msg) |
| 140 | + |
| 141 | + |
| 142 | + return model |
| 143 | + |
| 144 | + |
| 145 | + @model_validator(mode="after") # type: ignore[arg-type] |
| 146 | + def ensure_not_all_servers_are_down_simultaneously( |
| 147 | + cls, # noqa: N805 |
| 148 | + model: "SimulationPayload", |
| 149 | + ) -> "SimulationPayload": |
| 150 | + """ |
| 151 | + We will not accept the condition to have all server down |
| 152 | + at the same moment, always at least one server must be up |
| 153 | + and running |
| 154 | + """ |
| 155 | + |
| 156 | + if model.events is None: |
| 157 | + return model |
| 158 | + |
| 159 | + # First let us build a list of events related to the servers |
| 160 | + servers_list = model.topology_graph.nodes.servers |
| 161 | + servers_ids = {server.id for server in servers_list} |
| 162 | + server_events = [ |
| 163 | + event for event in model.events |
| 164 | + if event.target_id in servers_ids |
| 165 | + ] |
| 166 | + |
| 167 | + # Helpers needed in the algorithm to define a specific ordering |
| 168 | + # procedure |
| 169 | + START = "start" |
| 170 | + END = "end" |
| 171 | + |
| 172 | + # Let us define a list of tuple as a timeline, this approach ensure |
| 173 | + # the possibility to have different servers going up or down at the |
| 174 | + # same time, a more elegant approach through an hashmap has been |
| 175 | + # considered however it would require an extra assumption that all |
| 176 | + # the times had to be different, we thought that this would be too |
| 177 | + # strict |
| 178 | + timeline = [] |
| 179 | + for event in server_events: |
| 180 | + timeline.append((event.start.t_start, START, event.target_id)) |
| 181 | + timeline.append((event.end.t_end, END, event.target_id)) |
| 182 | + |
| 183 | + # Let us order the timeline by time if there are multiple events at the |
| 184 | + # same time process first the end type events |
| 185 | + timeline.sort(key=lambda x: (x[0], x[1] == START)) |
| 186 | + |
| 187 | + # Definition of a set to verify the condition that at least one server must |
| 188 | + # be up |
| 189 | + server_down = set() |
| 190 | + for time, kind, server_id in timeline: |
| 191 | + if kind == START: |
| 192 | + server_down.discard(server_id) |
| 193 | + else: # "start" |
| 194 | + server_down.add(server_id) |
| 195 | + if len(server_down) == len(servers_ids): |
| 196 | + raise ValueError( |
| 197 | + f"At time {time:.6f} all servers are down; keep at least one up." |
| 198 | + ) |
| 199 | + |
118 | 200 | return model |
| 201 | + |
119 | 202 |
|
0 commit comments