From e397f92dfc3944bbfb1d7fd783bdeffd6d7be3d5 Mon Sep 17 00:00:00 2001 From: songshuangyang Date: Fri, 14 Sep 2018 14:53:17 +0800 Subject: [PATCH] crush/CrushWrapper: fix crush tree json dumper The output json string is invalid for 'osd crush tree --format=json' command. It contains a array of 'nodes' and a array of 'stray', but not in a json object, and the stray array was not implemented. Applications which depends on the output of the above MonCommand will occur json parse error. Signed-off-by: Oshyn Song (cherry picked from commit 35c0d1f45cd676f201d4031cb8f447f7ea6aee0e) --- src/crush/CrushWrapper.cc | 12 ++++++++++++ src/mon/OSDMonitor.cc | 2 ++ 2 files changed, 14 insertions(+) diff --git a/src/crush/CrushWrapper.cc b/src/crush/CrushWrapper.cc index 349b7751be18d..b55fe32011fbf 100644 --- a/src/crush/CrushWrapper.cc +++ b/src/crush/CrushWrapper.cc @@ -3131,7 +3131,19 @@ public: f->open_array_section("nodes"); Parent::dump(f); f->close_section(); + + // There is no stray bucket whose id is a negative number, so just get + // the max_id and iterate from 0 to max_id to dump stray osds. f->open_array_section("stray"); + int32_t max_id = -1; + if (!crush->name_map.empty()) { + max_id = crush->name_map.rbegin()->first; + } + for (int32_t i = 0; i <= max_id; i++) { + if (crush->item_exists(i) && !is_touched(i) && should_dump(i)) { + dump_item(CrushTreeDumper::Item(i, 0, 0, 0), f); + } + } f->close_section(); } }; diff --git a/src/mon/OSDMonitor.cc b/src/mon/OSDMonitor.cc index da55af1519494..5a20d3a93f49b 100644 --- a/src/mon/OSDMonitor.cc +++ b/src/mon/OSDMonitor.cc @@ -5433,10 +5433,12 @@ bool OSDMonitor::preprocess_command(MonOpRequestRef op) bool show_shadow = shadow == "--show-shadow"; boost::scoped_ptr f(Formatter::create(format)); if (f) { + f->open_object_section("crush_tree"); osdmap.crush->dump_tree(nullptr, f.get(), osdmap.get_pool_names(), show_shadow); + f->close_section(); f->flush(rdata); } else { ostringstream ss; -- 2.39.5