blob: 30f53aa1f8f0d68eb3cfae8a8664e255d9178a22 [file]
#ifndef THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_COLLECTOR_RESOURCE_STATE_MANAGER_H_
#define THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_COLLECTOR_RESOURCE_STATE_MANAGER_H_
#include <memory>
#include <string>
#include <thread> // NOLINT
#include <vector>
#include "absl/base/thread_annotations.h"
#include "absl/container/flat_hash_map.h"
#include "absl/status/statusor.h"
#include "absl/synchronization/mutex.h"
#include "absl/time/time.h"
#include "boost/asio/executor_work_guard.hpp" // NOLINT
#include "boost/asio/io_context.hpp" // NOLINT
#include "boost/asio/steady_timer.hpp" // NOLINT
#include "resource.pb.h"
namespace milotic_tlbmc {
struct ResourceStateManagerOptions {
milotic_tlbmc::RelatedState target_state;
std::vector<milotic_tlbmc::RelatedStateConfig> related_state_configs;
};
// ResourceStateManager is used to track the state of resources that are
// related to a particular state. Each ResourceStateManager instance is supposed
// to track the state of resources related to a particular state, i.e.
// Host Power State, OS POST state, etc.
// Current usage:
// - Track FRU resources are missing_ok or not during state transitions.
// This will be refactored/expanded to track sensor resources in the future.
// This class is thread-safe.
class ResourceStateManager {
public:
static absl::StatusOr<std::unique_ptr<ResourceStateManager>> Create(
ResourceStateManagerOptions options);
virtual ~ResourceStateManager();
// ResourceStateManager is not copyable.
ResourceStateManager(const ResourceStateManager&) = delete;
ResourceStateManager& operator=(const ResourceStateManager&) = delete;
// Handles state on to off event for a particular host.
// Sets missing_ok to true for all resources and cancels any pending
// timers.
virtual void HandleStateOnToOff(std::string host_id)
ABSL_LOCKS_EXCLUDED(mutex_);
// Handles state off to on event for a particular host.
// Starts a timer for each resource to set
// missing_ok to false after its configured delay.
virtual void HandleStateOffToOn(std::string host_id)
ABSL_LOCKS_EXCLUDED(mutex_);
// Returns true if it is OK for state related resource to be missing.
// Returns false if the resource is not found or if it is not OK for it to be
// missing.
bool IsStateRelatedResourceMissingOk(const std::string& resource_name) const
ABSL_LOCKS_EXCLUDED(mutex_);
protected:
ResourceStateManager() : work_guard_(io_context_.get_executor()) {}
struct ResourceState {
// True if it is OK for the resource to be missing (e.g. during host power
// down).
bool missing_ok = false;
// The host id this resource is associated with.
std::string associated_host_id;
// The time delay after which missing_ok should transition to false when the
// host powers on.
absl::Duration expected_uptime;
// Timer used to trigger the missing_ok state transition.
std::unique_ptr<boost::asio::steady_timer> timer;
};
explicit ResourceStateManager(
milotic_tlbmc::RelatedState target_state,
const std::vector<milotic_tlbmc::RelatedStateConfig>&
related_state_configs);
milotic_tlbmc::RelatedState target_state_;
std::vector<milotic_tlbmc::RelatedStateConfig> related_state_configs_;
mutable absl::Mutex mutex_;
ResourceStateManagerOptions options_;
boost::asio::io_context io_context_;
boost::asio::executor_work_guard<boost::asio::io_context::executor_type>
work_guard_;
absl::flat_hash_map<std::string, ResourceState> resource_states_
ABSL_GUARDED_BY(mutex_);
std::thread io_thread_;
};
} // namespace milotic_tlbmc
#endif // THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_COLLECTOR_RESOURCE_STATE_MANAGER_H_