| /** @file | |
| ConfigContext - Context for configuration loading/reloading operations | |
| Provides: | |
| - Status tracking (in_progress, complete, fail, log) | |
| - Inline content support for YAML configs (via -d flag or RPC API) | |
| @par License | |
| Licensed to the Apache Software Foundation (ASF) under one | |
| or more contributor license agreements. See the NOTICE file | |
| distributed with this work for additional information | |
| regarding copyright ownership. The ASF licenses this file | |
| to you under the Apache License, Version 2.0 (the | |
| "License"); you may not use this file except in compliance | |
| with the License. You may obtain a copy of the License at | |
| http://www.apache.org/licenses/LICENSE-2.0 | |
| Unless required by applicable law or agreed to in writing, software | |
| distributed under the License is distributed on an "AS IS" BASIS, | |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| See the License for the specific language governing permissions and | |
| limitations under the License. | |
| */ | |
| #pragma once | |
| #include <memory> | |
| #include <string> | |
| #include <string_view> | |
| #include "swoc/Errata.h" | |
| #include "swoc/BufferWriter.h" | |
| #include "tsutil/ts_diag_levels.h" | |
| #include "yaml-cpp/node/node.h" | |
| // Forward declarations | |
| class ConfigReloadTask; | |
| class ReloadCoordinator; | |
| namespace config | |
| { | |
| class ConfigRegistry; | |
| } | |
| namespace detail | |
| { | |
| class RecordTriggeredReloadContinuation; | |
| } | |
| /// | |
| /// @brief Context passed to config handlers during load/reload operations. | |
| /// | |
| /// This object is passed to reconfigure() methods to: | |
| /// 1. Track progress/status of the operation (in_progress, complete, fail, log) | |
| /// 2. Provide RPC-supplied YAML content (for -d flag (traffic_ctl) or direct JSONRPC calls) | |
| /// | |
| /// For file-based reloads, handlers read from their own registered filename. | |
| /// For RPC reloads, handlers use supplied_yaml() to get the content. | |
| /// | |
| /// @note This context is also used during **startup** configuration loading. | |
| /// At startup there is no active reload task, so all status operations | |
| /// (in_progress, complete, fail, log) are safe **no-ops**. To keep the | |
| /// existing code logic for loading/reloading this design aims to avoid | |
| /// having two separate code paths for startup vs. reload - handlers | |
| /// can use the same API in both cases. | |
| /// | |
| /// Usage: | |
| /// @code | |
| /// void MyConfig::reconfigure(ConfigContext ctx) { | |
| /// ctx.in_progress(); | |
| /// | |
| /// YAML::Node root; | |
| /// if (auto yaml = ctx.supplied_yaml()) { | |
| /// // RPC mode: content provided via -d flag or RPC. | |
| /// // YAML::Node has explicit operator bool() → true when IsDefined(). | |
| /// // Copy is cheap (internally reference-counted). | |
| /// root = yaml; | |
| /// } else { | |
| /// // File mode: read from registered filename. | |
| /// root = YAML::LoadFile(my_config_filename); | |
| /// } | |
| /// | |
| /// // ... process config ... | |
| /// | |
| /// ctx.complete("Loaded successfully"); | |
| /// } | |
| /// @endcode | |
| /// | |
| class ConfigContext | |
| { | |
| public: | |
| ConfigContext() = default; | |
| explicit ConfigContext(std::shared_ptr<ConfigReloadTask> t, std::string_view description = "", std::string_view filename = ""); | |
| ~ConfigContext() = default; | |
| // Copy only - move is intentionally suppressed. | |
| // ConfigContext holds a weak_ptr to the task and a shared_ptr to the RPC-supplied | |
| // YAML and reload directives, so a copy is at most two reference-count increments. | |
| // Keep it that small: it is passed by value everywhere (see the size check in | |
| // ConfigContext.cc). | |
| // Suppressing move ensures that std::move(ctx) silently copies, keeping the | |
| // original valid. This is critical for execute_reload()'s post-handler check: | |
| // if a handler defers work (e.g. LogConfig), the original ctx must remain | |
| // valid so is_terminal() can detect the non-terminal state and emit a warning. | |
| ConfigContext(ConfigContext const &) = default; | |
| ConfigContext &operator=(ConfigContext const &) = default; | |
| /// Check if this context wraps a live task. | |
| /// Returns false for default-constructed or dedup-rejected contexts. | |
| explicit | |
| operator bool() const | |
| { | |
| return !_task.expired(); | |
| } | |
| void in_progress(std::string_view text = ""); | |
| template <typename... Args> | |
| void | |
| in_progress(swoc::TextView fmt, Args &&...args) | |
| { | |
| std::string buf; | |
| in_progress(swoc::bwprint(buf, fmt, std::forward<Args>(args)...)); | |
| } | |
| void log(std::string_view text); | |
| void log(DiagsLevel level, std::string_view text); | |
| void log(swoc::Errata const &errata); | |
| template <typename... Args> | |
| void | |
| log(swoc::TextView fmt, Args &&...args) | |
| { | |
| std::string buf; | |
| log(swoc::bwprint(buf, fmt, std::forward<Args>(args)...)); | |
| } | |
| /// Mark operation as successfully completed | |
| void complete(std::string_view text = ""); | |
| template <typename... Args> | |
| void | |
| complete(swoc::TextView fmt, Args &&...args) | |
| { | |
| std::string buf; | |
| complete(swoc::bwprint(buf, fmt, std::forward<Args>(args)...)); | |
| } | |
| /// Mark operation as failed. | |
| void fail(swoc::Errata const &errata, std::string_view summary = ""); | |
| void fail(std::string_view reason = ""); | |
| template <typename... Args> | |
| void | |
| fail(swoc::TextView fmt, Args &&...args) | |
| { | |
| std::string buf; | |
| fail(swoc::bwprint(buf, fmt, std::forward<Args>(args)...)); | |
| } | |
| /// Eg: fail(errata, "Failed to load config: {}", filename); | |
| template <typename... Args> | |
| void | |
| fail(swoc::Errata const &errata, swoc::TextView fmt, Args &&...args) | |
| { | |
| std::string buf; | |
| fail(errata, swoc::bwprint(buf, fmt, std::forward<Args>(args)...)); | |
| } | |
| /// Check if the task has reached a terminal state (SUCCESS, FAIL, TIMEOUT). | |
| [[nodiscard]] bool is_terminal() const; | |
| /// Get the description associated with this context's task. | |
| /// For registered configs this is the registration key (e.g., "sni", "ssl"). | |
| /// For dependent contexts it is the label passed to add_dependent_ctx(). | |
| [[nodiscard]] std::string get_description() const; | |
| /// Get the reload token identifying the current reload cycle. | |
| /// All tasks within the same reload share the same token. | |
| /// Returns empty string for default-constructed (no-op) contexts. | |
| [[nodiscard]] std::string get_reload_token() const; | |
| /// Create a dependent sub-task that tracks progress independently under this parent. | |
| /// Each dependent reports its own status (in_progress/complete/fail) and the parent | |
| /// task aggregates them. The dependent context also inherits the parent's supplied YAML node. | |
| /// | |
| [[nodiscard]] ConfigContext add_dependent_ctx(std::string_view description = "", std::string_view filename = ""); | |
| /// Get supplied YAML node (for RPC-based reloads). | |
| /// Returns Undefined when no content was provided (operator bool() == false). | |
| /// @code | |
| /// if (auto yaml = ctx.supplied_yaml()) { /* use yaml node */ } | |
| /// @endcode | |
| /// @return a handle on the supplied YAML node, or a new Undefined node on each call when | |
| /// none was supplied. YAML::Node is reference-counted: copies and dependents of | |
| /// this context share the node, so a write through it is seen by all of them. | |
| [[nodiscard]] YAML::Node supplied_yaml() const; | |
| /// Get reload directives extracted from the _reload key. | |
| /// Directives are operational parameters that modify how the handler performs | |
| /// the reload (e.g. scope to a single entry, dry-run) - distinct from config content. | |
| /// The framework extracts _reload from the supplied node before passing content | |
| /// to the handler, so supplied_yaml() never contains _reload. | |
| /// Returns Undefined when no directives were provided (operator bool() == false). | |
| /// @code | |
| /// if (auto directives = ctx.reload_directives()) { /* use directives */ } | |
| /// @endcode | |
| /// @return a handle on the directives YAML node, or a new Undefined node on each call when | |
| /// none were provided. YAML::Node is reference-counted: copies and dependents of | |
| /// this context share the node, so a write through it is seen by all of them. | |
| [[nodiscard]] YAML::Node reload_directives() const; | |
| private: | |
| /// Attach the registering plugin's name. A non-empty name marks the context's | |
| /// task as plugin-originated; an empty view marks it as core. Used for | |
| /// diagnostics and traffic_ctl status attribution. | |
| void set_plugin_name(std::string_view name); | |
| /// Set supplied YAML node. Only ConfigRegistry should call this during reload setup. | |
| void set_supplied_yaml(YAML::Node node); | |
| /// Set reload directives. Only ConfigRegistry should call this during reload setup. | |
| void set_reload_directives(YAML::Node node); | |
| /// Supplied YAML and reload directives. Copies and dependents of this context share | |
| /// it; the setters replace it instead of changing it. Defined in ConfigContext.cc. | |
| struct Payload; | |
| std::weak_ptr<ConfigReloadTask> _task; | |
| std::shared_ptr<Payload const> _payload; ///< null when nothing was supplied | |
| friend class ReloadCoordinator; | |
| friend class config::ConfigRegistry; | |
| friend class detail::RecordTriggeredReloadContinuation; | |
| }; | |
| namespace config | |
| { | |
| /// Create a ConfigContext for use in reconfigure handlers | |
| ConfigContext make_config_reload_context(std::string_view description, std::string_view filename = ""); | |
| } // namespace config |