Skip to content

Commit

Permalink
Monitor PkgManager operations
Browse files Browse the repository at this point in the history
Summary:
Adding monitoring on failures on
* processAll
* loadRequiredKmods
* unloadKmods
* processRpm

Some functions have a couple place where it exits or throws on failures.
So using SCOPE_* macro.

Reviewed By: rationalis

Differential Revision: D66711104

fbshipit-source-id: e0e71e7379f3c757d18cac8251e44217877a5ee0
  • Loading branch information
Justin Kim authored and facebook-github-bot committed Dec 13, 2024
1 parent 14fcc2b commit d2eb5a5
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
23 changes: 23 additions & 0 deletions fboss/platform/platform_manager/PkgManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ PkgManager::PkgManager(
platformFsUtils_(platformFsUtils) {}

void PkgManager::processAll() const {
SCOPE_SUCCESS {
fb303::fbData->setCounter(kProcessAllFailure, 0);
};
SCOPE_FAIL {
fb303::fbData->setCounter(kProcessAllFailure, 1);
};

if (FLAGS_local_rpm_path.size()) {
fb303::fbData->setExportedValue(
kBspKmodsRpmName, "local_rpm: " + FLAGS_local_rpm_path);
Expand Down Expand Up @@ -186,6 +193,13 @@ void PkgManager::processAll() const {
}

void PkgManager::processRpms() const {
SCOPE_SUCCESS {
fb303::fbData->setCounter(kProcessRpmFailure, 0);
};
SCOPE_FAIL {
fb303::fbData->setCounter(kProcessRpmFailure, 1);
};

int exitStatus{0};
if (auto installedRpms =
systemInterface_->getInstalledRpms(getKmodsRpmBaseWithKernelName());
Expand Down Expand Up @@ -242,6 +256,13 @@ void PkgManager::processLocalRpms() const {
}

void PkgManager::unloadBspKmods() const {
SCOPE_SUCCESS {
fb303::fbData->setCounter(kUnloadKmodsFailure, 0);
};
SCOPE_FAIL {
fb303::fbData->setCounter(kUnloadKmodsFailure, 1);
};

std::string keyword{};
re2::RE2::FullMatch(
*platformConfig_.bspKmodsRpmName(), kBspRpmNameRe, &keyword);
Expand Down Expand Up @@ -305,10 +326,12 @@ void PkgManager::loadRequiredKmods() const {
for (const auto& requiredKmod : *platformConfig_.requiredKmodsToLoad()) {
XLOG(INFO) << fmt::format("Loading {}", requiredKmod);
if (!systemInterface_->loadKmod(requiredKmod)) {
fb303::fbData->setCounter(kLoadKmodsFailure, 1);
throw std::runtime_error(
fmt::format("Failed to load ({})", requiredKmod));
}
}
fb303::fbData->setCounter(kLoadKmodsFailure, 0);
}

std::string PkgManager::getKmodsRpmName() const {
Expand Down
10 changes: 10 additions & 0 deletions fboss/platform/platform_manager/PkgManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ class SystemInterface {

class PkgManager {
public:
// ODS Counters
constexpr static auto kProcessAllFailure =
"package_manager.process_all_failure";
constexpr static auto kLoadKmodsFailure =
"package_manager.load_kmods_failure";
constexpr static auto kUnloadKmodsFailure =
"package_manager.unload_kmods_failure";
constexpr static auto kProcessRpmFailure =
"package_manager.process_rpm_failure";

explicit PkgManager(
const PlatformConfig& config,
const std::shared_ptr<package_manager::SystemInterface>& systemInterface =
Expand Down
1 change: 1 addition & 0 deletions fboss/platform/platform_manager/tests/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ cpp_unittest(
],
deps = [
"fbsource//third-party/googletest:gmock",
"//fb303:service_data",
"//fboss/platform/platform_manager:pkg_manager",
"//thrift/lib/cpp2/protocol:protocol",
],
Expand Down
25 changes: 25 additions & 0 deletions fboss/platform/platform_manager/tests/PkgManagerTest.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.

#include <fb303/ServiceData.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thrift/lib/cpp2/protocol/Serializer.h>
Expand Down Expand Up @@ -177,6 +178,8 @@ TEST_F(PkgManagerTest, processRpms) {
.WillOnce(Return(0));
EXPECT_CALL(*mockSystemInterface_, depmod()).WillOnce(Return(0));
EXPECT_NO_THROW(pkgManager_.processRpms());
EXPECT_EQ(
facebook::fb303::fbData->getCounter(PkgManager::kProcessRpmFailure), 0);
// Installed rpms
EXPECT_CALL(
*mockSystemInterface_,
Expand All @@ -199,6 +202,8 @@ TEST_F(PkgManagerTest, processRpms) {
.WillOnce(Return(0));
EXPECT_CALL(*mockSystemInterface_, depmod()).WillOnce(Return(0));
EXPECT_NO_THROW(pkgManager_.processRpms());
EXPECT_EQ(
facebook::fb303::fbData->getCounter(PkgManager::kProcessRpmFailure), 0);
// Remove installed rpms failed.
EXPECT_CALL(
*mockSystemInterface_,
Expand All @@ -213,6 +218,8 @@ TEST_F(PkgManagerTest, processRpms) {
"fboss_bsp_kmods-6.4.3-0_fbk1_755_ga25447393a1d-2.4.0-1"}))
.WillOnce(Return(1));
EXPECT_THROW(pkgManager_.processRpms(), std::runtime_error);
EXPECT_EQ(
facebook::fb303::fbData->getCounter(PkgManager::kProcessRpmFailure), 1);
// depmod failed
EXPECT_CALL(
*mockSystemInterface_,
Expand All @@ -234,6 +241,8 @@ TEST_F(PkgManagerTest, processRpms) {
.WillOnce(Return(0));
EXPECT_CALL(*mockSystemInterface_, depmod()).WillOnce(Return(1));
EXPECT_NO_THROW(pkgManager_.processRpms());
EXPECT_EQ(
facebook::fb303::fbData->getCounter(PkgManager::kProcessRpmFailure), 0);
// Rpm install failed
EXPECT_CALL(
*mockSystemInterface_,
Expand All @@ -255,6 +264,8 @@ TEST_F(PkgManagerTest, processRpms) {
.Times(3)
.WillRepeatedly(Return(1));
EXPECT_THROW(pkgManager_.processRpms(), std::runtime_error);
EXPECT_EQ(
facebook::fb303::fbData->getCounter(PkgManager::kProcessRpmFailure), 1);
}

TEST_F(PkgManagerTest, unloadBspKmods) {
Expand All @@ -271,6 +282,8 @@ TEST_F(PkgManagerTest, unloadBspKmods) {
.WillOnce(Return(std::vector<std::string>{}));
EXPECT_CALL(*mockSystemInterface_, lsmod()).Times(0);
EXPECT_NO_THROW(pkgManager_.unloadBspKmods());
EXPECT_EQ(
facebook::fb303::fbData->getCounter(PkgManager::kUnloadKmodsFailure), 0);
// No kmods.json when it should exist
EXPECT_CALL(*mockPlatformFsUtils_, getStringFileContent(_))
.WillOnce(Return(std::nullopt));
Expand All @@ -282,6 +295,8 @@ TEST_F(PkgManagerTest, unloadBspKmods) {
.WillOnce(Return(std::vector<std::string>{
"fboss_bsp_kmods-6.4.3-0_fbk1_755_ga25447393a1d-2.4.0-1"}));
EXPECT_THROW(pkgManager_.unloadBspKmods(), std::runtime_error);
EXPECT_EQ(
facebook::fb303::fbData->getCounter(PkgManager::kUnloadKmodsFailure), 1);
// kmods.json exist and all kmods are loaded
EXPECT_CALL(*mockPlatformFsUtils_, getStringFileContent(_))
.WillOnce(Return(jsonBspKmodsFile_));
Expand All @@ -294,19 +309,25 @@ TEST_F(PkgManagerTest, unloadBspKmods) {
bspKmodsFile_.bspKmods()->size())
.WillRepeatedly(Return(true));
EXPECT_NO_THROW(pkgManager_.unloadBspKmods());
EXPECT_EQ(
facebook::fb303::fbData->getCounter(PkgManager::kUnloadKmodsFailure), 0);
// kmods.json exists but unload fails
EXPECT_CALL(*mockPlatformFsUtils_, getStringFileContent(_))
.WillOnce(Return(jsonBspKmodsFile_));
EXPECT_CALL(*mockSystemInterface_, lsmod())
.WillOnce(Return(jsonBspKmodsFile_));
EXPECT_CALL(*mockSystemInterface_, unloadKmod(_)).WillOnce(Return(false));
EXPECT_THROW(pkgManager_.unloadBspKmods(), std::runtime_error);
EXPECT_EQ(
facebook::fb303::fbData->getCounter(PkgManager::kUnloadKmodsFailure), 1);
// kmods.json exist and all kmods aren't loaded
EXPECT_CALL(*mockPlatformFsUtils_, getStringFileContent(_))
.WillOnce(Return(jsonBspKmodsFile_));
EXPECT_CALL(*mockSystemInterface_, lsmod()).WillOnce(Return(""));
EXPECT_CALL(*mockSystemInterface_, unloadKmod(_)).Times(0);
EXPECT_NO_THROW(pkgManager_.unloadBspKmods());
EXPECT_EQ(
facebook::fb303::fbData->getCounter(PkgManager::kUnloadKmodsFailure), 0);
}

TEST_F(PkgManagerTest, loadRequiredKmods) {
Expand All @@ -315,8 +336,12 @@ TEST_F(PkgManagerTest, loadRequiredKmods) {
.Times(platformConfig_.requiredKmodsToLoad()->size())
.WillRepeatedly(Return(true));
EXPECT_NO_THROW(pkgManager_.loadRequiredKmods());
EXPECT_EQ(
facebook::fb303::fbData->getCounter(PkgManager::kLoadKmodsFailure), 0);
// Load kmods fail
EXPECT_CALL(*mockSystemInterface_, loadKmod(_)).WillOnce(Return(false));
EXPECT_THROW(pkgManager_.loadRequiredKmods(), std::runtime_error);
EXPECT_EQ(
facebook::fb303::fbData->getCounter(PkgManager::kLoadKmodsFailure), 1);
}
}; // namespace facebook::fboss::platform::platform_manager

0 comments on commit d2eb5a5

Please sign in to comment.