Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 1 | #include "aos/starter/starter_rpc_lib.h" |
| 2 | |
Stephan Pleines | f581a07 | 2024-05-23 20:59:27 -0700 | [diff] [blame] | 3 | #include <algorithm> |
| 4 | #include <ostream> |
| 5 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 6 | #include "absl/log/check.h" |
| 7 | #include "absl/log/log.h" |
Stephan Pleines | f581a07 | 2024-05-23 20:59:27 -0700 | [diff] [blame] | 8 | #include "flatbuffers/buffer.h" |
| 9 | #include "flatbuffers/flatbuffer_builder.h" |
| 10 | #include "flatbuffers/string.h" |
| 11 | #include "flatbuffers/vector.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 12 | |
Stephan Pleines | f581a07 | 2024-05-23 20:59:27 -0700 | [diff] [blame] | 13 | #include "aos/events/context.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 14 | #include "aos/events/shm_event_loop.h" |
| 15 | #include "aos/flatbuffer_merge.h" |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 16 | #include "aos/starter/starterd_lib.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 17 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 18 | namespace aos::starter { |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 19 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 20 | namespace { |
| 21 | State ExpectedStateForCommand(Command command) { |
| 22 | switch (command) { |
| 23 | case Command::START: |
| 24 | case Command::RESTART: |
| 25 | return State::RUNNING; |
| 26 | case Command::STOP: |
| 27 | return State::STOPPED; |
| 28 | } |
| 29 | return State::STOPPED; |
| 30 | } |
| 31 | } // namespace |
| 32 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 33 | const aos::starter::ApplicationStatus *FindApplicationStatus( |
| 34 | const aos::starter::Status &status, std::string_view name) { |
| 35 | if (!status.has_statuses()) { |
| 36 | return nullptr; |
| 37 | } |
| 38 | |
| 39 | auto statuses = status.statuses(); |
| 40 | |
| 41 | auto search = |
| 42 | std::find_if(statuses->begin(), statuses->end(), |
| 43 | [name](const aos::starter::ApplicationStatus *app_status) { |
| 44 | return app_status->has_name() && |
| 45 | app_status->name()->string_view() == name; |
| 46 | }); |
| 47 | if (search == statuses->end()) { |
| 48 | return nullptr; |
| 49 | } |
| 50 | return *search; |
| 51 | } |
| 52 | |
Austin Schuh | dae1e8d | 2022-03-26 15:09:31 -0700 | [diff] [blame] | 53 | std::string_view FindApplication(const std::string_view name, |
milind upadhyay | 4272f38 | 2021-04-07 18:03:08 -0700 | [diff] [blame] | 54 | const aos::Configuration *config) { |
| 55 | std::string_view app_name = name; |
| 56 | for (const auto app : *config->applications()) { |
Austin Schuh | dae1e8d | 2022-03-26 15:09:31 -0700 | [diff] [blame] | 57 | if (app->name()->string_view() == name) { |
| 58 | return name; |
| 59 | } |
| 60 | } |
| 61 | for (const auto app : *config->applications()) { |
Milind Upadhyay | 7641ce8 | 2021-04-10 14:15:28 -0700 | [diff] [blame] | 62 | if (app->has_executable_name() && |
milind upadhyay | 4272f38 | 2021-04-07 18:03:08 -0700 | [diff] [blame] | 63 | app->executable_name()->string_view() == name) { |
| 64 | app_name = app->name()->string_view(); |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | return app_name; |
| 69 | } |
| 70 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 71 | StarterClient::StarterClient(EventLoop *event_loop) |
| 72 | : event_loop_(event_loop), |
| 73 | timeout_timer_(event_loop_->AddTimer([this]() { Timeout(); })), |
| 74 | cmd_sender_(event_loop_->MakeSender<StarterRpc>("/aos")) { |
James Kuszmaul | 06a8f35 | 2024-03-15 14:15:57 -0700 | [diff] [blame] | 75 | timeout_timer_->set_name("rpc_timeout"); |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 76 | if (configuration::MultiNode(event_loop_->configuration())) { |
| 77 | for (const aos::Node *node : |
| 78 | configuration::GetNodes(event_loop_->configuration())) { |
| 79 | const Channel *channel = |
| 80 | StatusChannelForNode(event_loop_->configuration(), node); |
| 81 | CHECK(channel != nullptr) << ": Failed to find channel /aos for " |
| 82 | << Status::GetFullyQualifiedName() << " on " |
| 83 | << node->name()->string_view(); |
| 84 | if (!configuration::ChannelIsReadableOnNode(channel, |
| 85 | event_loop_->node())) { |
James Kuszmaul | 9d36a62 | 2021-11-10 16:48:00 -0800 | [diff] [blame] | 86 | VLOG(1) << "Status channel " |
| 87 | << configuration::StrippedChannelToString(channel) |
| 88 | << " is not readable on " |
| 89 | << event_loop_->node()->name()->string_view(); |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 90 | } else if (!configuration::ChannelIsReadableOnNode( |
| 91 | StarterRpcChannelForNode(event_loop_->configuration(), |
| 92 | event_loop_->node()), |
| 93 | node)) { |
| 94 | // Don't attempt to construct a status fetcher if the other node won't |
| 95 | // even be able to receive our commands. |
James Kuszmaul | 9d36a62 | 2021-11-10 16:48:00 -0800 | [diff] [blame] | 96 | VLOG(1) << "StarterRpc channel for " |
| 97 | << event_loop_->node()->name()->string_view() |
| 98 | << " is not readable on " << node->name()->string_view(); |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 99 | } else { |
| 100 | status_fetchers_[node->name()->str()] = |
| 101 | event_loop_->MakeFetcher<Status>(channel->name()->string_view()); |
| 102 | event_loop_->MakeNoArgWatcher<Status>(channel->name()->string_view(), |
| 103 | [this]() { |
| 104 | if (CheckCommandsSucceeded()) { |
| 105 | Succeed(); |
| 106 | } |
| 107 | }); |
| 108 | } |
| 109 | } |
| 110 | } else { |
| 111 | status_fetchers_[""] = event_loop_->MakeFetcher<Status>("/aos"); |
| 112 | event_loop_->MakeNoArgWatcher<Status>("/aos", [this]() { |
| 113 | if (CheckCommandsSucceeded()) { |
| 114 | Succeed(); |
| 115 | } |
| 116 | }); |
| 117 | } |
Austin Schuh | e4b748a | 2021-10-16 14:19:58 -0700 | [diff] [blame] | 118 | } |
| 119 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 120 | void StarterClient::SendCommands( |
| 121 | const std::vector<ApplicationCommand> &commands, |
| 122 | monotonic_clock::duration timeout) { |
| 123 | CHECK(current_commands_.empty()); |
| 124 | for (auto &pair : status_fetchers_) { |
| 125 | pair.second.Fetch(); |
| 126 | } |
| 127 | const bool is_multi_node = |
| 128 | aos::configuration::MultiNode(event_loop_->configuration()); |
| 129 | for (const auto &command : commands) { |
| 130 | auto builder = cmd_sender_.MakeBuilder(); |
| 131 | const auto application_offset = |
| 132 | builder.fbb()->CreateString(command.application); |
| 133 | std::vector<flatbuffers::Offset<flatbuffers::String>> node_offsets; |
| 134 | CHECK(!command.nodes.empty()) |
| 135 | << "At least one node must be specified for application " |
| 136 | << command.application; |
| 137 | for (const aos::Node *node : command.nodes) { |
| 138 | const std::string node_name((node == nullptr) ? "" : node->name()->str()); |
| 139 | if (status_fetchers_.count(node_name) == 0) { |
| 140 | if (is_multi_node) { |
| 141 | LOG(FATAL) << "Node \"" << node_name |
| 142 | << "\" must be configured to both receive StarterRpc " |
| 143 | "messages from \"" |
| 144 | << event_loop_->node()->name()->string_view() |
| 145 | << "\" as well as to send starter Status messages back."; |
| 146 | } else { |
| 147 | LOG(FATAL) << "On single-node configs, use an empty string for the " |
| 148 | "node name."; |
| 149 | } |
| 150 | } |
James Kuszmaul | 43d7016 | 2023-03-04 18:25:14 -0800 | [diff] [blame] | 151 | if (status_fetchers_[node_name].get() == nullptr) { |
| 152 | LOG(WARNING) << ": No status available for node " << node_name |
| 153 | << "; not executing commands for that node."; |
| 154 | continue; |
| 155 | } |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 156 | if (is_multi_node) { |
| 157 | node_offsets.push_back(builder.fbb()->CreateString(node_name)); |
| 158 | } |
Austin Schuh | 6bdcc37 | 2024-06-27 14:49:11 -0700 | [diff] [blame] | 159 | const ApplicationStatus *last_status = FindApplicationStatus( |
| 160 | *status_fetchers_[node_name], command.application); |
| 161 | CHECK(last_status != nullptr); |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 162 | current_commands_[node_name].push_back(CommandStatus{ |
| 163 | .expected_state = ExpectedStateForCommand(command.command), |
| 164 | .application = std::string(command.application), |
| 165 | .old_id = std::nullopt}); |
| 166 | // If we are restarting, then we need to track what the current ID of the |
| 167 | // process is to ensure that it actually got restarted. For just starting, |
| 168 | // we leave the application running and so don't care. |
| 169 | if (command.command == Command::RESTART && last_status->has_id()) { |
| 170 | current_commands_[node_name].back().old_id = last_status->id(); |
| 171 | } |
| 172 | } |
| 173 | flatbuffers::Offset< |
| 174 | flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>> |
| 175 | nodes_offset; |
| 176 | if (is_multi_node) { |
| 177 | nodes_offset = builder.fbb()->CreateVector(node_offsets); |
| 178 | } |
| 179 | auto command_builder = builder.MakeBuilder<StarterRpc>(); |
| 180 | command_builder.add_command(command.command); |
| 181 | command_builder.add_name(application_offset); |
| 182 | if (is_multi_node) { |
| 183 | command_builder.add_nodes(nodes_offset); |
| 184 | } |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 185 | builder.CheckOk(builder.Send(command_builder.Finish())); |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 188 | timeout_timer_->Schedule(event_loop_->monotonic_now() + timeout); |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | bool StarterClient::CheckCommandsSucceeded() { |
| 192 | if (current_commands_.empty()) { |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | for (auto &pair : status_fetchers_) { |
| 197 | pair.second.Fetch(); |
| 198 | } |
| 199 | |
| 200 | bool succeeded = true; |
| 201 | |
| 202 | for (const auto &pair : current_commands_) { |
| 203 | if (pair.second.empty()) { |
| 204 | continue; |
| 205 | } |
| 206 | CHECK(status_fetchers_[pair.first].get() != nullptr) |
| 207 | << ": No status available for node " << pair.first; |
| 208 | const Status &status = *status_fetchers_[pair.first]; |
| 209 | for (const auto &command : pair.second) { |
| 210 | const ApplicationStatus *application_status = |
Austin Schuh | 6bdcc37 | 2024-06-27 14:49:11 -0700 | [diff] [blame] | 211 | FindApplicationStatus(status, command.application); |
| 212 | CHECK(application_status != nullptr); |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 213 | if (application_status->state() == command.expected_state) { |
| 214 | if (command.expected_state == State::RUNNING && |
| 215 | application_status->id() == command.old_id) { |
| 216 | succeeded = false; |
| 217 | } |
| 218 | } else { |
| 219 | succeeded = false; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | return succeeded; |
| 224 | } |
| 225 | |
| 226 | void StarterClient::Timeout() { |
| 227 | // Clear commands prior to calling handlers to allow the handler to call |
| 228 | // SendCommands() again if desired. |
| 229 | current_commands_.clear(); |
| 230 | if (timeout_handler_) { |
| 231 | timeout_handler_(); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void StarterClient::Succeed() { |
| 236 | // Clear commands prior to calling handlers to allow the handler to call |
| 237 | // SendCommands() again if desired. |
| 238 | current_commands_.clear(); |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 239 | // Clear the timer before calling success handler, in case the success |
| 240 | // handler needs to modify timeout handler. |
| 241 | timeout_timer_->Disable(); |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 242 | if (success_handler_) { |
| 243 | success_handler_(); |
| 244 | } |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | bool SendCommandBlocking(aos::starter::Command command, std::string_view name, |
| 248 | const aos::Configuration *config, |
| 249 | std::chrono::milliseconds timeout, |
| 250 | std::vector<const aos::Node *> nodes) { |
| 251 | return SendCommandBlocking({{command, name, nodes}}, config, timeout); |
| 252 | } |
| 253 | |
| 254 | bool SendCommandBlocking(const std::vector<ApplicationCommand> &commands, |
| 255 | const aos::Configuration *config, |
| 256 | std::chrono::milliseconds timeout) { |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 257 | aos::ShmEventLoop event_loop(config); |
| 258 | event_loop.SkipAosLog(); |
| 259 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 260 | StarterClient client(&event_loop); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 261 | |
Austin Schuh | e4b748a | 2021-10-16 14:19:58 -0700 | [diff] [blame] | 262 | // Wait until event loop starts to send all commands so the watcher is ready |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 263 | event_loop.OnRun([&commands, &client, timeout]() { |
| 264 | client.SendCommands(commands, timeout); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 265 | }); |
| 266 | |
| 267 | // If still waiting after timeout milliseconds, exit the loop |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 268 | client.SetTimeoutHandler([&event_loop]() { event_loop.Exit(); }); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 269 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 270 | bool success = false; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 271 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 272 | client.SetSuccessHandler([&event_loop, &success]() { |
| 273 | success = true; |
| 274 | event_loop.Exit(); |
Austin Schuh | e4b748a | 2021-10-16 14:19:58 -0700 | [diff] [blame] | 275 | }); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 276 | |
| 277 | event_loop.Run(); |
| 278 | |
| 279 | return success; |
| 280 | } |
| 281 | |
James Kuszmaul | 2ca441b | 2022-01-07 18:16:23 -0800 | [diff] [blame] | 282 | const std::optional< |
| 283 | std::pair<aos::monotonic_clock::time_point, |
| 284 | FlatbufferDetachedBuffer<aos::starter::ApplicationStatus>>> |
James Kuszmaul | e4bb0a2 | 2022-01-07 18:14:43 -0800 | [diff] [blame] | 285 | GetStatus(std::string_view name, const Configuration *config, |
| 286 | const aos::Node *node) { |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 287 | ShmEventLoop event_loop(config); |
| 288 | event_loop.SkipAosLog(); |
| 289 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 290 | auto status_fetcher = event_loop.MakeFetcher<aos::starter::Status>( |
| 291 | StatusChannelForNode(config, node)->name()->string_view()); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 292 | status_fetcher.Fetch(); |
James Kuszmaul | e4bb0a2 | 2022-01-07 18:14:43 -0800 | [diff] [blame] | 293 | if (status_fetcher.get() != nullptr) { |
| 294 | const aos::starter::ApplicationStatus *status = |
| 295 | FindApplicationStatus(*status_fetcher, name); |
| 296 | if (status != nullptr) { |
James Kuszmaul | 2ca441b | 2022-01-07 18:16:23 -0800 | [diff] [blame] | 297 | return std::make_pair(status_fetcher.context().monotonic_remote_time, |
| 298 | aos::CopyFlatBuffer(status)); |
James Kuszmaul | e4bb0a2 | 2022-01-07 18:14:43 -0800 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | return std::nullopt; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 302 | } |
| 303 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 304 | std::optional<std::pair<aos::monotonic_clock::time_point, |
| 305 | const aos::FlatbufferVector<aos::starter::Status>>> |
| 306 | GetStarterStatus(const aos::Configuration *config, const aos::Node *node) { |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 307 | ShmEventLoop event_loop(config); |
| 308 | event_loop.SkipAosLog(); |
| 309 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 310 | auto status_fetcher = event_loop.MakeFetcher<aos::starter::Status>( |
| 311 | StatusChannelForNode(config, node)->name()->string_view()); |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 312 | status_fetcher.Fetch(); |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 313 | return (status_fetcher.get() == nullptr) |
| 314 | ? std::nullopt |
| 315 | : std::make_optional(std::make_pair( |
| 316 | status_fetcher.context().monotonic_remote_time, |
| 317 | status_fetcher.CopyFlatBuffer())); |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 320 | } // namespace aos::starter |