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] Spdlog FD sink #50144

Merged
merged 8 commits into from
Feb 1, 2025
Merged

Conversation

dentiny
Copy link
Contributor

@dentiny dentiny commented Jan 30, 2025

Followup PR for #50044

As proposed by @rynewang , wrap file descriptor (stdout/stderr in the context) into a spdlog sink, so we don't need to have extra flush and close function for StreamRedirectionHandle, and everything should be handled by spdlog logger.

@dentiny dentiny added the go add ONLY when ready to merge, run all tests label Jan 30, 2025
@dentiny dentiny requested review from jjyao and rynewang January 30, 2025 22:42
Signed-off-by: dentiny <[email protected]>
@dentiny dentiny force-pushed the hjiang/spdlog-fd-sink branch from 739bf5f to f6cbd36 Compare January 30, 2025 22:42
src/ray/util/BUILD Outdated Show resolved Hide resolved
template <typename Mutex>
class fd_sink final : public base_sink<Mutex> {
public:
// [fd] is not owned by [FdSink], which means the file descriptor should be closed by
Copy link
Contributor

Choose a reason for hiding this comment

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

Q: how do we plan to manage lifetime of fd, if the sink does not own it? can we design it to let sink hold lifetime of fd and close it on sink close?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As I documented, fd not owned by logger, and should be closed by callers.

Copy link
Contributor Author

@dentiny dentiny Jan 30, 2025

Choose a reason for hiding this comment

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

For example, the ideal use case for now is for stdout and stderr, which we don't need to manage inside of the sink; if we do need to transfer ownership in the future, we could add a boolean flag to enable/disable.

But I don't think it's a strong need, because general-purpose files could use RotatingFileSink; FdSink should only handle special-purpose file types.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually have the same question: do we have a use case where we want the fd to outlive the sink? Seems it's easier to move the fd to the sink and let the sink be the owner of the fd.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

stdout and stderr doesn't need to be closed

#include <windows.h>
#endif

namespace spdlog::sinks {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure it's best practice to put our classes in spdlog namespace. maybe just namespace ray?

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 prefer to place it under spdlog namespace, so we could use it as other sinks.

For example,

spdlog::sinks::rotating_sink file_sink{...};
spdlog::sinks::fd_sink stdout_sink{duped_stdout};
spdlog::sinks::fd_sink stderr_sink{duped_stderr};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The coding practice, including naming (use snake_case instead of CamelCase for class name) and type alias both follows spdlog style.

};

using fd_sink_mt = fd_sink<std::mutex>;
using fd_sink_st = fd_sink<details::null_mutex>;
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need _st variant?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dentiny dentiny requested a review from rynewang January 30, 2025 23:11
@rynewang
Copy link
Contributor

merge conflict

template <typename Mutex>
class fd_sink final : public base_sink<Mutex> {
public:
// [fd] is not owned by [FdSink], which means the file descriptor should be closed by
Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually have the same question: do we have a use case where we want the fd to outlive the sink? Seems it's easier to move the fd to the sink and let the sink be the owner of the fd.

Signed-off-by: dentiny <[email protected]>
Signed-off-by: dentiny <[email protected]>
Comment on lines +67 to +68
using non_owned_fd_sink_mt = non_owned_fd_sink<std::mutex>;
using non_owned_fd_sink_st = non_owned_fd_sink<spdlog::details::null_mutex>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

what's the difference between these two?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

multithreaded version and single threaded version, it's compatible to spdlog's implementation, example https://github.com/gabime/spdlog/blob/ae1de0dc8cf480f54eaa425c4a9dc4fef29b28ba/include/spdlog/sinks/rotating_file_sink.h#L56-L57

Comment on lines +23 to +27
#if defined(__APPLE__) || defined(__linux__)
int GetStdoutHandle() { return STDOUT_FILENO; }
#elif defined(_WIN32)
HANDLE GetStdoutHandle() { return GetStdHandle(STD_OUTPUT_HANDLE); }
#endif
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we already define this somewhere else?

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'm working on a PR right now to have compatible function for file descriptor / handle on windows and unix platform, including: (1) file descriptor; (2) write/flush/read/close syscalls. We could migrate afterwards

@jjyao jjyao changed the title [core] FD sink [core] Spdlog FD sink Feb 1, 2025
@jjyao jjyao enabled auto-merge (squash) February 1, 2025 04:55
@jjyao jjyao merged commit 3a8dd4d into ray-project:master Feb 1, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
go add ONLY when ready to merge, run all tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants