Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] [3/N] Check cgroupv2 mount status #48833

Merged
merged 9 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,30 @@ ray_cc_test(
],
)

ray_cc_library(
name = "cgroup_v2_utils",
srcs = ["src/ray/raylet/cgroup_v2_utils.cc"],
hdrs = ["src/ray/raylet/cgroup_v2_utils.h"],
deps = [
"@com_google_absl//absl/strings",
"//src/ray/util",
],
)

ray_cc_test(
name = "cgroup_v2_utils_test",
size = "small",
srcs = ["src/ray/raylet/test/cgroup_v2_utils_test.cc"],
tags = [
"team:core",
"manual", # Expect cgroupv2 mount, not intended for unit test and CI.
],
deps = [
":cgroup_v2_utils",
"@com_google_googletest//:gtest_main",
],
)

ray_cc_test(
name = "pull_manager_test",
size = "small",
Expand Down
59 changes: 59 additions & 0 deletions src/ray/raylet/cgroup_v2_utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2024 The Ray Authors.
//
// Licensed 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.

#include "ray/raylet/cgroup_v2_utils.h"

#include <fstream>
#include <string>

#include "absl/strings/str_split.h"
#include "absl/strings/strip.h"
#include "ray/util/logging.h"

namespace ray {

bool IsCgroupV2MountedAsRw() {
Copy link

@lanbochen-anyscale lanbochen-anyscale Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nits: I think you also need to check that the normal user in the container can modify the cgroup path, usually /sys/fs/cgroup is owned by root user even though it's writable, it is only writable by the root.

See https://github.com/anyscale/product/pull/32052/files#diff-005124dd6eac41dac5da8cac101b497921e0ee33ca99ec034fbb9fd82ddba508

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a good point, to update cgroupv2, mounted as rw is not enough, current user should also have the permission to write. Add a test to check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thought, another way we could do is we don't do any precheck before actually setting cgroup, which fails directly due to lacking permission.

// Checking all mountpoints directly and parse textually is the easiest way, compared
// with mounted filesystem attributes.
std::ifstream mounts("/proc/mounts");
if (!mounts.is_open()) {
return false;
}

// Mount information is formatted as:
// <fs_spec> <fs_file> <fs_vfstype> <fs_mntopts> <dump-field> <fsck-field>
std::string line;
while (std::getline(mounts, line)) {
std::vector<std::string_view> mount_info_tokens = absl::StrSplit(line, ' ');
RAY_CHECK_EQ(mount_info_tokens.size(), 6UL);
// For cgroupv2, `fs_spec` should be `cgroupv2` and there should be only one mount
// information item.
if (mount_info_tokens[0] != "cgroup2") {
continue;
}
const auto &fs_mntopts = mount_info_tokens[3];

// Mount options are formatted as: <opt1,opt2,...>.
std::vector<std::string_view> mount_opts = absl::StrSplit(fs_mntopts, ',');

// CgroupV2 has only one mount item, directly returns.
return std::any_of(mount_opts.begin(),
mount_opts.end(),
[](const std::string_view cur_opt) { return cur_opt == "rw"; });
}

return false;
}

} // namespace ray
24 changes: 24 additions & 0 deletions src/ray/raylet/cgroup_v2_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2024 The Ray Authors.
//
// Licensed 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.

// Utils for cgroup v2 related.

#pragma once

namespace ray {

// Return whether cgroup V2 is mounted in read and write mode.
bool IsCgroupV2MountedAsRw();

} // namespace ray
32 changes: 32 additions & 0 deletions src/ray/raylet/test/cgroup_v2_utils_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 The Ray Authors.
//
// Licensed 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.

#include "src/ray/raylet/cgroup_v2_utils.h"

#include <gtest/gtest.h>

namespace ray {

namespace {

// Precondition: cgroup V2 has already been mounted as rw.
//
// Setup command:
// sudo umount /sys/fs/cgroup/unified
// sudo mount -t cgroup2 cgroup2 /sys/fs/cgroup/unified -o rw

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qq, I am not familiar but what requires to put these step in ray's cicd?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think it should be placed under CI, which is a group of unit test, but for cgroup related feature they should be tested as integration tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why you see me mark test as "manual".

TEST(CgroupV2UtilsTest, CheckCgroupV2Mount) { EXPECT_TRUE(IsCgroupV2MountedAsRw()); }

} // namespace

} // namespace ray