From 548869ea57bbddc97ea0c1780b1f058bb86acb28 Mon Sep 17 00:00:00 2001 From: dongdong tao Date: Wed, 3 Jan 2018 20:51:44 +0800 Subject: [PATCH] mds: add command "openfiles ls" list all the opening files with the corresponding clients and their caps. Signed-off-by: dongdong tao --- src/mds/MDCache.cc | 28 +++++++++++++++++++++++++++- src/mds/MDCache.h | 4 ++-- src/mds/MDLog.h | 4 ++++ src/mds/MDSDaemon.cc | 6 ++++++ src/mds/MDSRank.cc | 8 ++++++++ src/mds/MDSRank.h | 1 + 6 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/mds/MDCache.cc b/src/mds/MDCache.cc index 1f620e44e23..83e52b0421e 100644 --- a/src/mds/MDCache.cc +++ b/src/mds/MDCache.cc @@ -5631,7 +5631,33 @@ void MDCache::clean_open_file_lists() } } - +void MDCache::dump_openfiles(Formatter *f) +{ + f->open_array_section("openfiles"); + for (auto p = mds->mdlog->segments.begin(); + p != mds->mdlog->segments.end(); + ++p) { + LogSegment *ls = p->second; + + auto q = ls->open_files.begin(member_offset(CInode, item_open_file)); + while (!q.end()) { + CInode *in = *q; + ++q; + if ((in->last == CEPH_NOSNAP && !in->is_any_caps_wanted()) + || (in->last != CEPH_NOSNAP && in->client_snap_caps.empty())) + continue; + std::string path; + in->make_path_string(path, true); + if (path.empty()) + path = "/" + f->open_object_section("file"); + f->dump_string("path", path); + in->dump(f); + f->close_section(); + } + } + f->close_section(); +} Capability* MDCache::rejoin_import_cap(CInode *in, client_t client, const cap_reconnect_t& icr, mds_rank_t frommds) { diff --git a/src/mds/MDCache.h b/src/mds/MDCache.h index 64a9adf43ea..0914637330d 100644 --- a/src/mds/MDCache.h +++ b/src/mds/MDCache.h @@ -534,7 +534,7 @@ public: void clean_open_file_lists(); - + void dump_openfiles(Formatter *f); protected: // [rejoin] bool rejoins_pending; @@ -1178,7 +1178,7 @@ public: int dump_cache(const std::string &filename); int dump_cache(Formatter *f); int dump_cache(const std::string& dump_root, int depth, Formatter *f); - + int cache_status(Formatter *f); void dump_resolve_status(Formatter *f) const; diff --git a/src/mds/MDLog.h b/src/mds/MDLog.h index 5579e5abc00..02dc5803056 100644 --- a/src/mds/MDLog.h +++ b/src/mds/MDLog.h @@ -246,6 +246,10 @@ public: return segments[seq]; return NULL; } + + const std::map &get_segments() { + return segments; + } bool have_any_segments() const { return !segments.empty(); diff --git a/src/mds/MDSDaemon.cc b/src/mds/MDSDaemon.cc index 9eb133c03a6..04fcc3d7f0b 100644 --- a/src/mds/MDSDaemon.cc +++ b/src/mds/MDSDaemon.cc @@ -309,6 +309,11 @@ void MDSDaemon::set_up_admin_socket() asok_hook, "List fragments in directory"); assert(r == 0); + r = admin_socket->register_command("openfiles ls", + "openfiles ls", + asok_hook, + "List the opening files and their caps"); + assert(r == 0); } void MDSDaemon::clean_up_admin_socket() @@ -337,6 +342,7 @@ void MDSDaemon::clean_up_admin_socket() admin_socket->unregister_command("dirfrag split"); admin_socket->unregister_command("dirfrag merge"); admin_socket->unregister_command("dirfrag ls"); + admin_socket->unregister_command("openfiles ls"); delete asok_hook; asok_hook = NULL; } diff --git a/src/mds/MDSRank.cc b/src/mds/MDSRank.cc index 4b9732fd3d3..7b551489033 100644 --- a/src/mds/MDSRank.cc +++ b/src/mds/MDSRank.cc @@ -1986,6 +1986,8 @@ bool MDSRankDispatcher::handle_asok_command( command_dirfrag_merge(cmdmap, ss); } else if (command == "dirfrag ls") { command_dirfrag_ls(cmdmap, ss, f); + } else if (command == "openfiles ls") { + command_openfiles_ls(f); } else { return false; } @@ -2497,6 +2499,12 @@ bool MDSRank::command_dirfrag_ls( return true; } +void MDSRank::command_openfiles_ls(Formatter *f) +{ + Mutex::Locker l(mds_lock); + mdcache->dump_openfiles(f); +} + void MDSRank::dump_status(Formatter *f) const { if (state == MDSMap::STATE_REPLAY || diff --git a/src/mds/MDSRank.h b/src/mds/MDSRank.h index 170bb6ed5a5..26c76fef861 100644 --- a/src/mds/MDSRank.h +++ b/src/mds/MDSRank.h @@ -437,6 +437,7 @@ class MDSRank { CDir *_command_dirfrag_get( const cmdmap_t &cmdmap, std::ostream &ss); + void command_openfiles_ls(Formatter *f); protected: Messenger *messenger; -- 2.39.5