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] Use statusor for filesystem and resolve dependency #50029

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
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
7 changes: 6 additions & 1 deletion src/ray/common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,12 @@ ray_cc_library(
hdrs = ["status.h"],
deps = [
":source_location",
"//src/ray/util",
"//src/ray/util:logging",
"//src/ray/util:macros",
"//src/ray/util:visibility",
"@boost//:system",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/strings",
],
)

Expand Down
1 change: 1 addition & 0 deletions src/ray/common/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "ray/common/status.h"
#include "ray/thirdparty/aligned_alloc.h"
#include "ray/util/logging.h"

#define BUFFER_ALIGNMENT 64

Expand Down
17 changes: 9 additions & 8 deletions src/ray/common/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,15 @@
#include <iosfwd>
#include <string>

#include "absl/strings/str_cat.h"
#include "ray/common/source_location.h"
#include "ray/util/logging.h"
#include "ray/util/macros.h"
#include "ray/util/visibility.h"

namespace boost {

namespace system {

namespace boost::system {
class error_code;

} // namespace system

} // namespace boost
} // namespace boost::system

// Return the given status if it is not OK.
#define RAY_RETURN_NOT_OK(s) \
Expand Down Expand Up @@ -328,6 +323,12 @@ class RAY_EXPORT Status {

std::string message() const { return ok() ? "" : state_->msg; }

template <typename... T>
Status &operator<<(T &&...msg) {
absl::StrAppend(&state_->msg, std::forward<T>(msg)...);
return *this;
}

private:
struct State {
StatusCode code;
Expand Down
1 change: 1 addition & 0 deletions src/ray/common/test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ ray_cc_test(
ray_cc_library(
name = "testing",
hdrs = ["testing.h"],
deps = ["//src/ray/util:macros"],
testonly = True,
)

Expand Down
23 changes: 14 additions & 9 deletions src/ray/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,30 @@ ray_cc_library(
# TODO(hjiang): filesystem and logging has interdependency, we should split them into three targets: filesystem, logging, ray_check_macros.
ray_cc_library(
name = "logging",
hdrs = [
"filesystem.h",
"logging.h",
],
srcs = [
"filesystem.cc",
"logging.cc",
],
hdrs = ["logging.h"],
srcs = ["logging.cc"],
deps = [
":event_label",
":macros",
":string_utils",
":thread_utils",
"@com_github_spdlog//:spdlog",
"@com_google_absl//absl/debugging:failure_signal_handler",
"@com_google_absl//absl/strings:str_format",
"@com_google_googletest//:gtest_main",
"@com_google_googletest//:gtest_prod",
"@nlohmann_json",
],
)

ray_cc_library(
name = "filesystem",
hdrs = ["filesystem.h"],
srcs = ["filesystem.cc"],
deps = [
"//src/ray/common:status_or",
],
)

ray_cc_library(
name = "container_util",
hdrs = ["container_util.h"],
Expand All @@ -82,6 +86,7 @@ ray_cc_library(
],
deps = [
":cmd_line_utils",
":filesystem",
":logging",
":macros",
"@boost//:asio",
Expand Down
13 changes: 7 additions & 6 deletions src/ray/util/filesystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
#include <cstdlib>
#include <fstream>

#include "ray/util/logging.h"

#ifdef _WIN32
#include <Windows.h>
#endif
Expand Down Expand Up @@ -57,17 +55,20 @@ std::string GetUserTempDir() {
while (!result.empty() && IsDirSep(result.back())) {
result.pop_back();
}
RAY_CHECK(!result.empty());
return result;
}

std::string CompleteReadFile(const std::string &fname) {
StatusOr<std::string> CompleteReadFile(const std::string &fname) {
dentiny marked this conversation as resolved.
Show resolved Hide resolved
std::ifstream file(fname);
RAY_CHECK(file.good()) << "Fails to open file " << fname;
if (!file.good()) {
return Status::IOError("") << "Fails to open file " << fname;
}

std::ostringstream buffer;
buffer << file.rdbuf();
RAY_CHECK(file.good()) << "Fails to read from file " << fname;
if (!file.good()) {
return Status::IOError("") << "Fails to read from file " << fname;
}

std::string content = buffer.str();
file.close();
Expand Down
24 changes: 3 additions & 21 deletions src/ray/util/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <string>
#include <utility>

#include "ray/common/status_or.h"

// Filesystem and path manipulation APIs.
// (NTFS stream & attribute paths are not supported.)

Expand All @@ -42,29 +44,9 @@ static inline bool IsDirSep(char ch) {
return result;
}

/// \return The result of joining multiple path components.
template <class... Paths>
std::string JoinPaths(std::string base, const Paths &...components) {
auto join = [](auto &joined_path, const auto &component) {
// if the components begin with "/" or "////", just get the path name.
if (!component.empty() &&
component.front() == std::filesystem::path::preferred_separator) {
joined_path = std::filesystem::path(joined_path)
.append(std::filesystem::path(component).filename().string())
.string();
} else {
joined_path = std::filesystem::path(joined_path).append(component).string();
}
};
(join(base, std::string_view(components)), ...);
return base;
}

// Read the whole content for the given [fname], and return as string.
// If any error happens, error message will be logged and the program will exit
// immediately.
//
// TODO(hjiang): Use StatusOr as return type in the followup PR.
std::string CompleteReadFile(const std::string &fname);
StatusOr<std::string> CompleteReadFile(const std::string &fname);

} // namespace ray
4 changes: 2 additions & 2 deletions src/ray/util/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#include "absl/strings/str_format.h"
#include "nlohmann/json.hpp"
#include "ray/util/event_label.h"
#include "ray/util/filesystem.h"
#include "ray/util/string_utils.h"
#include "ray/util/thread_utils.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"
Expand Down Expand Up @@ -367,7 +367,7 @@ void RayLog::InitLogFormat() {
app_name_without_path = "DefaultApp";
} else {
// Find the app name without the path.
std::string app_file_name = ray::GetFileName(app_name);
std::string app_file_name = std::filesystem::path(app_name).filename().string();
Copy link
Contributor Author

@dentiny dentiny Jan 23, 2025

Choose a reason for hiding this comment

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

To reduce circular dependency between logging and filesystem.

if (!app_file_name.empty()) {
app_name_without_path = app_file_name;
}
Expand Down
1 change: 1 addition & 0 deletions src/ray/util/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#include <vector>

#include "ray/util/macros.h"
#include "ray/util/string_utils.h"

#if defined(_WIN32)
#ifndef _WINDOWS_
Expand Down
19 changes: 19 additions & 0 deletions src/ray/util/string_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#pragma once

#include <filesystem>
#include <string>

namespace ray {
Expand All @@ -27,4 +28,22 @@ std::string StringToHex(const std::string &str);
/// \return The scanned prefix of the string, if any.
std::string ScanToken(std::string::const_iterator &c_str, std::string format);

/// \return The result of joining multiple path components.
template <class... Paths>
std::string JoinPaths(std::string base, const Paths &...components) {
auto join = [](auto &joined_path, const auto &component) {
// if the components begin with "/" or "////", just get the path name.
if (!component.empty() &&
component.front() == std::filesystem::path::preferred_separator) {
joined_path = std::filesystem::path(joined_path)
.append(std::filesystem::path(component).filename().string())
.string();
} else {
joined_path = std::filesystem::path(joined_path).append(component).string();
}
};
(join(base, std::string_view(components)), ...);
return base;
}

} // namespace ray
2 changes: 2 additions & 0 deletions src/ray/util/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ ray_cc_test(
name = "pipe_logger_test",
srcs = ["pipe_logger_test.cc"],
deps = [
"//src/ray/common/test:testing",
"//src/ray/util",
"//src/ray/util:pipe_logger",
"@com_google_googletest//:gtest_main",
Expand All @@ -230,6 +231,7 @@ ray_cc_test(
name = "stream_redirection_utils_test",
srcs = ["stream_redirection_utils_test.cc"],
deps = [
"//src/ray/common/test:testing",
"//src/ray/util",
"//src/ray/util:stream_redirection_utils",
"@com_google_googletest//:gtest_main",
Expand Down
27 changes: 20 additions & 7 deletions src/ray/util/tests/pipe_logger_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <string_view>

#include "absl/cleanup/cleanup.h"
#include "ray/common/test/testing.h"
#include "ray/util/filesystem.h"
#include "ray/util/util.h"

Expand Down Expand Up @@ -62,8 +63,9 @@ TEST_P(PipeLoggerTest, NoPipeWrite) {

// Check log content after completion.
const auto actual_content = CompleteReadFile(test_file_path);
RAY_ASSERT_OK(actual_content);
const std::string expected_content = absl::StrFormat("%s%s", kLogLine1, kLogLine2);
EXPECT_EQ(actual_content, expected_content);
EXPECT_EQ(*actual_content, expected_content);
}

INSTANTIATE_TEST_SUITE_P(PipeLoggerTest, PipeLoggerTest, testing::Values(1024, 3));
Expand Down Expand Up @@ -114,8 +116,13 @@ TEST_P(PipeLoggerTest, PipeWrite) {
stream_redirection_handle.Close();

// Check log content after completion.
EXPECT_EQ(CompleteReadFile(log_file_path1), kLogLine2);
EXPECT_EQ(CompleteReadFile(log_file_path2), kLogLine1);
const auto actual_content1 = CompleteReadFile(log_file_path1);
RAY_ASSERT_OK(actual_content1);
EXPECT_EQ(*actual_content1, kLogLine2);

const auto actual_content2 = CompleteReadFile(log_file_path2);
RAY_ASSERT_OK(actual_content1);
EXPECT_EQ(*actual_content2, kLogLine1);
}

TEST(PipeLoggerTestWithTee, RedirectionWithTee) {
Expand Down Expand Up @@ -145,8 +152,9 @@ TEST(PipeLoggerTestWithTee, RedirectionWithTee) {
EXPECT_EQ(stdout_content, absl::StrFormat("%s%s", kLogLine1, kLogLine2));

// Check log content after completion.
EXPECT_EQ(CompleteReadFile(test_file_path),
absl::StrFormat("%s%s", kLogLine1, kLogLine2));
const auto actual_content = CompleteReadFile(test_file_path);
RAY_ASSERT_OK(actual_content);
EXPECT_EQ(*actual_content, absl::StrFormat("%s%s", kLogLine1, kLogLine2));
}

TEST(PipeLoggerTestWithTee, RotatedRedirectionWithTee) {
Expand Down Expand Up @@ -181,8 +189,13 @@ TEST(PipeLoggerTestWithTee, RotatedRedirectionWithTee) {
EXPECT_EQ(stderr_content, absl::StrFormat("%s%s", kLogLine1, kLogLine2));

// Check log content after completion.
EXPECT_EQ(CompleteReadFile(test_file_path), kLogLine2);
EXPECT_EQ(CompleteReadFile(log_file_path2), kLogLine1);
const auto actual_content1 = CompleteReadFile(log_file_path1);
RAY_ASSERT_OK(actual_content1);
EXPECT_EQ(*actual_content1, kLogLine2);

const auto actual_content2 = CompleteReadFile(log_file_path2);
RAY_ASSERT_OK(actual_content2);
EXPECT_EQ(*actual_content2, kLogLine1);
}

} // namespace
Expand Down
9 changes: 7 additions & 2 deletions src/ray/util/tests/stream_redirection_utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <iostream>
#include <thread>

#include "ray/common/test/testing.h"
#include "ray/util/filesystem.h"
#include "ray/util/util.h"

Expand Down Expand Up @@ -56,10 +57,14 @@ TEST(LoggingUtilTest, RedirectStderr) {

// Check log content after completion.
const std::string log_file_path1 = test_file_path;
EXPECT_EQ(CompleteReadFile(test_file_path), kLogLine2);
const auto actual_content1 = CompleteReadFile(log_file_path1);
RAY_ASSERT_OK(actual_content1);
EXPECT_EQ(*actual_content1, kLogLine2);

const std::string log_file_path2 = absl::StrFormat("%s.1", test_file_path);
EXPECT_EQ(CompleteReadFile(log_file_path2), kLogLine1);
const auto actual_content2 = CompleteReadFile(log_file_path2);
RAY_ASSERT_OK(actual_content2);
EXPECT_EQ(*actual_content2, kLogLine1);

// Check tee-ed to stderr content.
std::string stderr_content = testing::internal::GetCapturedStderr();
Expand Down
Loading