From 7e618c937bba233b5d3091644e42a05aa4554f29 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Mon, 16 Dec 2013 16:09:44 -0800 Subject: [PATCH] mon: move supported_commands fields, methods into Monitor, and fix leak We were leaking the static leader_supported_mon_commands. Move this into the class so that we can clean up in the destructor. Rename get_command_descriptions -> format_command_descriptions. Fixes: #7009 Signed-off-by: Sage Weil --- src/mon/Elector.cc | 10 ++-- src/mon/Monitor.cc | 51 ++++++++++++--------- src/mon/Monitor.h | 24 +++++----- src/test/common/get_command_descriptions.cc | 2 +- 4 files changed, 48 insertions(+), 39 deletions(-) diff --git a/src/mon/Elector.cc b/src/mon/Elector.cc index f31ef54c499b3..44c9485730ec8 100644 --- a/src/mon/Elector.cc +++ b/src/mon/Elector.cc @@ -180,10 +180,10 @@ void Elector::victory() const MonCommand *cmds; int cmdsize; if (use_classic_commands) { - get_classic_monitor_commands(&cmds, &cmdsize); + mon->get_classic_monitor_commands(&cmds, &cmdsize); cmds_bl = &mon->get_classic_commands_bl(); } else { - get_locally_supported_monitor_commands(&cmds, &cmdsize); + mon->get_locally_supported_monitor_commands(&cmds, &cmdsize); cmds_bl = &mon->get_supported_commands_bl(); } @@ -324,12 +324,12 @@ void Elector::handle_victory(MMonElection *m) int cmdsize; bufferlist::iterator bi = m->commands.begin(); MonCommand::decode_array(&new_cmds, &cmdsize, bi); - set_leader_supported_commands(new_cmds, cmdsize); + mon->set_leader_supported_commands(new_cmds, cmdsize); } else { // they are a legacy monitor; use known legacy command set const MonCommand *new_cmds; int cmdsize; - get_classic_monitor_commands(&new_cmds, &cmdsize); - set_leader_supported_commands(new_cmds, cmdsize); + mon->get_classic_monitor_commands(&new_cmds, &cmdsize); + mon->set_leader_supported_commands(new_cmds, cmdsize); } m->put(); diff --git a/src/mon/Monitor.cc b/src/mon/Monitor.cc index 4ac90a629b196..c0d7b6f349d85 100644 --- a/src/mon/Monitor.cc +++ b/src/mon/Monitor.cc @@ -94,6 +94,18 @@ static ostream& _prefix(std::ostream *_dout, const Monitor *mon) { const string Monitor::MONITOR_NAME = "monitor"; const string Monitor::MONITOR_STORE_PREFIX = "monitor_store"; + +#undef COMMAND +MonCommand mon_commands[] = { +#define COMMAND(parsesig, helptext, modulename, req_perms, avail) \ + {parsesig, helptext, modulename, req_perms, avail}, +#include +}; +MonCommand classic_mon_commands[] = { +#include +}; + + long parse_pos_long(const char *s, ostream *pss) { if (*s == '-' || *s == '+') { @@ -137,6 +149,8 @@ Monitor::Monitor(CephContext* cct_, string nm, MonitorDBStore *s, auth_service_required(cct, cct->_conf->auth_supported.length() ? cct->_conf->auth_supported : cct->_conf->auth_service_required), + leader_supported_mon_commands(NULL), + leader_supported_mon_commands_size(0), store(s), state(STATE_PROBING), @@ -213,6 +227,9 @@ Monitor::~Monitor() delete paxos; assert(session_map.sessions.empty()); delete mon_caps; + if (leader_supported_mon_commands != mon_commands && + leader_supported_mon_commands != classic_mon_commands) + delete[] leader_supported_mon_commands; } @@ -1892,18 +1909,6 @@ void Monitor::get_status(stringstream &ss, Formatter *f) } } -#undef COMMAND -MonCommand mon_commands[] = { -#define COMMAND(parsesig, helptext, modulename, req_perms, avail) \ - {parsesig, helptext, modulename, req_perms, avail}, -#include -}; -MonCommand classic_mon_commands[] = { -#include -}; -const MonCommand *leader_supported_mon_commands = NULL; -int leader_supported_mon_commands_size = 0; - void Monitor::_generate_command_map(map& cmdmap, map ¶m_str_map) { @@ -1957,10 +1962,11 @@ bool Monitor::_allowed_command(MonSession *s, string &module, string &prefix, return capable; } -void get_command_descriptions(const MonCommand *commands, - unsigned commands_size, - Formatter *f, - bufferlist *rdata) { +void Monitor::format_command_descriptions(const MonCommand *commands, + unsigned commands_size, + Formatter *f, + bufferlist *rdata) +{ int cmdnum = 0; f->open_object_section("command_descriptions"); for (const MonCommand *cp = commands; @@ -1978,22 +1984,23 @@ void get_command_descriptions(const MonCommand *commands, f->flush(*rdata); } -void get_locally_supported_monitor_commands(const MonCommand **cmds, int *count) +void Monitor::get_locally_supported_monitor_commands(const MonCommand **cmds, + int *count) { *cmds = mon_commands; *count = ARRAY_SIZE(mon_commands); } -void get_leader_supported_commands(const MonCommand **cmds, int *count) +void Monitor::get_leader_supported_commands(const MonCommand **cmds, int *count) { *cmds = leader_supported_mon_commands; *count = leader_supported_mon_commands_size; } -void get_classic_monitor_commands(const MonCommand **cmds, int *count) +void Monitor::get_classic_monitor_commands(const MonCommand **cmds, int *count) { *cmds = classic_mon_commands; *count = ARRAY_SIZE(classic_mon_commands); } -void set_leader_supported_commands(const MonCommand *cmds, int size) +void Monitor::set_leader_supported_commands(const MonCommand *cmds, int size) { if (leader_supported_mon_commands != mon_commands && leader_supported_mon_commands != classic_mon_commands) @@ -2047,8 +2054,8 @@ void Monitor::handle_command(MMonCommand *m) if (prefix == "get_command_descriptions") { bufferlist rdata; Formatter *f = new_formatter("json"); - get_command_descriptions(leader_supported_mon_commands, - leader_supported_mon_commands_size, f, &rdata); + format_command_descriptions(leader_supported_mon_commands, + leader_supported_mon_commands_size, f, &rdata); delete f; reply_command(m, 0, "", rdata, 0); return; diff --git a/src/mon/Monitor.h b/src/mon/Monitor.h index 4f142b37525c5..eb6776bd75787 100644 --- a/src/mon/Monitor.h +++ b/src/mon/Monitor.h @@ -140,6 +140,9 @@ public: CompatSet features; + const MonCommand *leader_supported_mon_commands; + int leader_supported_mon_commands_size; + private: void new_tick(); friend class C_Mon_Tick; @@ -866,6 +869,16 @@ public: void _convert_machines(); void _convert_paxos(); }; + + static void format_command_descriptions(const MonCommand *commands, + unsigned commands_size, + Formatter *f, + bufferlist *rdata); + void get_locally_supported_monitor_commands(const MonCommand **cmds, int *count); + void get_classic_monitor_commands(const MonCommand **cmds, int *count); + void get_leader_supported_commands(const MonCommand **cmds, int *count); + /// the Monitor owns this pointer once you pass it in + void set_leader_supported_commands(const MonCommand *cmds, int size); }; #define CEPH_MON_FEATURE_INCOMPAT_BASE CompatSet::Feature (1, "initial feature set (~v.18)") @@ -929,15 +942,4 @@ struct MonCommand { }; WRITE_CLASS_ENCODER(MonCommand); -void get_command_descriptions(const MonCommand *commands, - unsigned commands_size, - Formatter *f, - bufferlist *rdata); -void get_locally_supported_monitor_commands(const MonCommand **cmds, int *count); -void get_classic_monitor_commands(const MonCommand **cmds, int *count); -void get_leader_supported_commands(const MonCommand **cmds, int *count); -/// the Monitor owns this pointer once you pass it in -void set_leader_supported_commands(const MonCommand *cmds, int size); - - #endif diff --git a/src/test/common/get_command_descriptions.cc b/src/test/common/get_command_descriptions.cc index 9f20102f705e5..c35e47ba6bdb7 100644 --- a/src/test/common/get_command_descriptions.cc +++ b/src/test/common/get_command_descriptions.cc @@ -45,7 +45,7 @@ static void json_print(const MonCommand *mon_commands, int size) { bufferlist rdata; Formatter *f = new_formatter("json"); - get_command_descriptions(mon_commands, size, f, &rdata); + Monitor::format_command_descriptions(mon_commands, size, f, &rdata); delete f; string data(rdata.c_str(), rdata.length()); cout << data << std::endl; -- 2.39.5