Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
545b4f548b46c6727ddb6fd346aeec0565719f84
[simgrid.git] / examples / cpp / exec-failure / s4u-exec-failure.cpp
1 /* Copyright (c) 2021-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /* This examples shows how to survive to host failure exceptions that occur when an host is turned off.
7  *
8  * The actors do not get notified when the host on which they run is turned off: they are just terminated
9  * in this case, and their ``on_exit()`` callback gets executed.
10  *
11  * For remote executions on failing hosts however, any blocking operation such as ``exec`` or ``wait`` will
12  * raise an exception that you can catch and react to, as illustrated in this example.
13  */
14
15 #include <simgrid/s4u.hpp>
16 #include "simgrid/kernel/ProfileBuilder.hpp"
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_exec_failure, "Messages specific for this s4u example");
19 namespace sg4 = simgrid::s4u;
20
21 static void dispatcher(std::vector<sg4::Host*> const& hosts)
22 {
23   std::vector<sg4::ExecPtr> pending_execs;
24   for (auto* host: hosts) {
25     XBT_INFO("Initiating asynchronous exec on %s", host->get_cname());
26     // Computing 20 flops on an host which speed is 1f takes 20 seconds (when it does not fail)
27     auto exec = sg4::this_actor::exec_init(20)->set_host(host);
28     pending_execs.push_back(exec);
29     exec->start();
30   }
31
32   XBT_INFO("---------------------------------");
33   XBT_INFO("Wait on the first exec, which host is turned off at t=10 by the another actor.");
34   try {
35     pending_execs[0]->wait();
36     xbt_assert("This wait was not supposed to succeed.");
37   } catch (const simgrid::HostFailureException&) {
38     XBT_INFO("Dispatcher has experienced a host failure exception, so it knows that something went wrong.");
39   }
40
41   XBT_INFO("State of each exec:");
42   for (auto const& exec : pending_execs) 
43     XBT_INFO("  Exec on %s has state: %s", exec->get_host()->get_cname(), exec->get_state_str());
44
45   XBT_INFO("---------------------------------");
46   XBT_INFO("Wait on the second exec, which host is turned off at t=12 by the state profile.");
47   try {
48     pending_execs[1]->wait();
49     xbt_assert("This wait was not supposed to succeed.");
50   } catch (const simgrid::HostFailureException&) {
51     XBT_INFO("Dispatcher has experienced a host failure exception, so it knows that something went wrong.");
52   }
53   XBT_INFO("State of each exec:");
54   for (auto const& exec : pending_execs) 
55     XBT_INFO("  Exec on %s has state: %s", exec->get_host()->get_cname(), exec->get_state_str());
56
57   XBT_INFO("---------------------------------");
58   XBT_INFO("Wait on the third exec, which should succeed.");
59   try {
60     pending_execs[2]->wait();
61     XBT_INFO("No exception occured.");
62   } catch (const simgrid::HostFailureException&) {
63     xbt_assert("This wait was not supposed to fail.");
64   }
65   XBT_INFO("State of each exec:");
66   for (auto const& exec : pending_execs) 
67     XBT_INFO("  Exec on %s has state: %s", exec->get_host()->get_cname(), exec->get_state_str());
68 }
69
70 static void host_killer(sg4::Host* to_kill)
71 {
72   sg4::this_actor::sleep_for(10.0);
73   XBT_INFO("HostKiller turns off the host '%s'.", to_kill->get_cname());
74   to_kill->turn_off();
75 }
76
77 int main(int argc, char** argv)
78 {
79   sg4::Engine engine(&argc, argv);
80
81   auto* zone  = sg4::create_full_zone("world");
82   std::vector<sg4::Host*> hosts;
83   for (auto name : {"Host1", "Host2", "Host3"}) {
84     auto* host = zone->create_host(name, "1f");
85     hosts.push_back(host);
86   }
87   /* Attaching a state profile (ie a list of events changing the on/off state of the resource) to host3.
88    * The syntax of the profile (second parameter) is a list of: "date state\n"
89    *   The R"(   )" thing is the C++ way of writing multiline strings, including literals \n.
90    *     You'd have the same behavior by using "12 0\n20 1\n" instead.
91    *   So here, the link is turned off at t=12 and back on at t=20.
92    * The last parameter is the period of that profile, meaning that it loops after 30 seconds.
93    */
94   hosts[1]->set_state_profile(simgrid::kernel::profile::ProfileBuilder::from_string("profile name", R"(
95 12 0
96 20 1
97 )",                                                                               30));
98
99   zone->seal();
100
101   sg4::Actor::create("Dispatcher", hosts[2], dispatcher, hosts);
102   sg4::Actor::create("HostKiller", hosts[2], host_killer, hosts[0]);
103
104   engine.run();
105
106   return 0;
107 }