| #ifndef PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_PERSISTENT_STORAGE_IMPL_H_ |
| #define PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_PERSISTENT_STORAGE_IMPL_H_ |
| |
| #include <cstdint> |
| #include <string> |
| #include <vector> |
| |
| #include "persistent_storage.h" |
| #include "safepower_agent_config.pb.h" |
| #include "state_persistence.pb.h" |
| #include "absl/status/status.h" |
| #include "absl/status/statusor.h" |
| #include "absl/strings/string_view.h" |
| #include "absl/types/span.h" |
| |
| namespace persistent_storage { |
| constexpr std::uintmax_t kMaxFileSize = 16384; // 16 k |
| constexpr absl::string_view kDefaultPath = |
| "/var/google/gPowerD/SavedActions/"; |
| constexpr uint32_t kProtoKeepNumber = 10; |
| |
| constexpr absl::string_view kfileNamePrefix = "savedactions"; |
| |
| class PersistentStorageManagerImpl |
| : public safepower_agent::PersistentStorageManager { |
| public: |
| explicit PersistentStorageManagerImpl( |
| const safepower_agent_config::PersistentStorageConfig& config = |
| safepower_agent_config::PersistentStorageConfig::default_instance()); |
| |
| ~PersistentStorageManagerImpl() override = default; |
| |
| // Writes a delta to the saved actions to persistent storage. |
| absl::Status WriteSavedActionsChange( |
| const safepower_agent_persistence_proto::SavedActions& actions) override; |
| |
| absl::Status WriteSavedActionsChange( |
| const safepower_agent_persistence_proto::SavedActions& actions, |
| absl::string_view dir_path); |
| |
| // Reads all saved actions from persistent storage. |
| absl::StatusOr<safepower_agent_persistence_proto::SavedActions> |
| ReadSavedActions() override; |
| |
| absl::StatusOr<safepower_agent_persistence_proto::SavedActions> |
| ReadSavedActions(absl::string_view dir_path); |
| |
| private: |
| absl::Status ConvertAppendToMap( |
| safepower_agent_persistence_proto::SavedActions& map, |
| safepower_agent_persistence_proto::SavedActionAppendLog& log); |
| |
| // keep the most recent protos is called when the size of the saved files is |
| // larger then the kmaxFileSize. In the event of large protos we will always |
| // have at least kProtoKeepNumber of messages. That garuantees was more |
| // important then miniziains the file size. From a design perspective it would |
| // be ideal if kMaxFileSize was reached at 2*kProtoKeepNumber, but that is not |
| // necessary. |
| absl::Status keepMostRecentProtos( |
| safepower_agent_persistence_proto::SavedActions& map); |
| absl::StatusOr<uint64_t> findLargestFileId( |
| absl::Span<const std::string> files); |
| absl::StatusOr<std::vector<std::string> > listFiles( |
| absl::string_view file_path); |
| absl::Status deleteAllFilesExceptLargest(absl::string_view file_path); |
| std::string MakeFileName(absl::string_view file_path, uint64_t file_id); |
| |
| std::string dir_path_{kDefaultPath}; |
| std::uintmax_t max_file_size_ = kMaxFileSize; |
| uint32_t proto_keep_number_ = kProtoKeepNumber; |
| }; |
| |
| } // namespace persistent_storage |
| |
| #endif // PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_PERSISTENT_STORAGE_IMPL_H_ |