|
| 1 | +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
| 2 | +// vim: ts=8 sw=2 smarttab |
| 3 | + |
| 4 | +#include "BlueAdmin.h" |
| 5 | +#include "Compression.h" |
| 6 | +#include "common/pretty_binary.h" |
| 7 | +#include "common/debug.h" |
| 8 | +#include <asm-generic/errno-base.h> |
| 9 | +#include <vector> |
| 10 | +#include <limits> |
| 11 | + |
| 12 | +#define dout_subsys ceph_subsys_bluestore |
| 13 | +#define dout_context store.cct |
| 14 | + |
| 15 | +using ceph::bufferlist; |
| 16 | +using ceph::Formatter; |
| 17 | +using ceph::common::cmd_getval; |
| 18 | + |
| 19 | +BlueStore::SocketHook::SocketHook(BlueStore& store) |
| 20 | + : store(store) |
| 21 | +{ |
| 22 | + AdminSocket *admin_socket = store.cct->get_admin_socket(); |
| 23 | + if (admin_socket) { |
| 24 | + int r = admin_socket->register_command( |
| 25 | + "bluestore collections", |
| 26 | + this, |
| 27 | + "list all collections"); |
| 28 | + if (r != 0) { |
| 29 | + dout(1) << __func__ << " cannot register SocketHook" << dendl; |
| 30 | + return; |
| 31 | + } |
| 32 | + r = admin_socket->register_command( |
| 33 | + "bluestore list " |
| 34 | + "name=collection,type=CephString,req=true " |
| 35 | + "name=start,type=CephString,req=false " |
| 36 | + "name=max,type=CephInt,req=false", |
| 37 | + this, |
| 38 | + "list objects in specific collection"); |
| 39 | + ceph_assert(r == 0); |
| 40 | + r = admin_socket->register_command( |
| 41 | + "bluestore onode metadata " |
| 42 | + "name=object_name,type=CephString,req=true", |
| 43 | + this, |
| 44 | + "print object internals"); |
| 45 | + ceph_assert(r == 0); |
| 46 | + r = admin_socket->register_command( |
| 47 | + "bluestore compression stats " |
| 48 | + "name=collection,type=CephString,req=false", |
| 49 | + this, |
| 50 | + "print compression stats, per collection"); |
| 51 | + ceph_assert(r == 0); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +BlueStore::SocketHook::~SocketHook() |
| 56 | +{ |
| 57 | + AdminSocket *admin_socket = store.cct->get_admin_socket(); |
| 58 | + if (admin_socket) { |
| 59 | + admin_socket->unregister_commands(this); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +int BlueStore::SocketHook::call( |
| 64 | + std::string_view command, |
| 65 | + const cmdmap_t& cmdmap, |
| 66 | + const bufferlist& inbl, |
| 67 | + Formatter *f, |
| 68 | + std::ostream& ss, |
| 69 | + bufferlist& out) |
| 70 | +{ |
| 71 | + int r = 0; |
| 72 | + if (command == "bluestore collections") { |
| 73 | + std::vector<coll_t> collections; |
| 74 | + store.list_collections(collections); |
| 75 | + std::stringstream result; |
| 76 | + for (const auto& c : collections) { |
| 77 | + result << c << std::endl; |
| 78 | + } |
| 79 | + out.append(result.str()); |
| 80 | + return 0; |
| 81 | + } else if (command == "bluestore list") { |
| 82 | + std::string coll; |
| 83 | + std::string start; |
| 84 | + int64_t max; |
| 85 | + cmd_getval(cmdmap, "collection", coll); |
| 86 | + cmd_getval(cmdmap, "start", start); |
| 87 | + if (!cmd_getval(cmdmap, "max", max)) { |
| 88 | + max = 100; |
| 89 | + } |
| 90 | + if (max == 0) { |
| 91 | + max = std::numeric_limits<int>::max(); |
| 92 | + } |
| 93 | + coll_t c; |
| 94 | + if (c.parse(coll) == false) { |
| 95 | + ss << "Cannot parse collection" << std::endl; |
| 96 | + return -EINVAL; |
| 97 | + } |
| 98 | + BlueStore::CollectionRef col = store._get_collection(c); |
| 99 | + if (!col) { |
| 100 | + ss << "No such collection" << std::endl; |
| 101 | + return -ENOENT; |
| 102 | + } |
| 103 | + ghobject_t start_object; |
| 104 | + if (start.length() > 0) { |
| 105 | + if (start_object.parse(start) == false) { |
| 106 | + ss << "Cannot parse start object"; |
| 107 | + return -EINVAL; |
| 108 | + } |
| 109 | + } |
| 110 | + std::vector<ghobject_t> list; |
| 111 | + { |
| 112 | + std::shared_lock l(col->lock); |
| 113 | + r = store._collection_list(col.get(), start_object, ghobject_t::get_max(), |
| 114 | + max, false, &list, nullptr); |
| 115 | + } |
| 116 | + if (r != 0) { |
| 117 | + return 0; |
| 118 | + } |
| 119 | + std::stringstream result; |
| 120 | + for (auto& obj : list) { |
| 121 | + result << obj << std::endl; |
| 122 | + } |
| 123 | + out.append(result.str()); |
| 124 | + return 0; |
| 125 | + } else if (command == "bluestore onode metadata") { |
| 126 | + std::string object_name; |
| 127 | + cmd_getval(cmdmap, "object_name", object_name); |
| 128 | + ghobject_t object; |
| 129 | + if (!object.parse(object_name)) { |
| 130 | + ss << "Cannot parse object" << std::endl; |
| 131 | + return -EINVAL; |
| 132 | + } |
| 133 | + std::shared_lock l(store.coll_lock); |
| 134 | + for (const auto& cp : store.coll_map) { |
| 135 | + if (cp.second->contains(object)) { |
| 136 | + std::shared_lock l(cp.second->lock); |
| 137 | + OnodeRef o = cp.second->get_onode(object, false); |
| 138 | + if (!o || !o->exists) { |
| 139 | + ss << "Object not found" << std::endl; |
| 140 | + return -ENOENT; |
| 141 | + } |
| 142 | + o->extent_map.fault_range(store.db, 0, 0xffffffff); |
| 143 | + using P = BlueStore::printer; |
| 144 | + std::stringstream result; |
| 145 | + result << o->print(P::PTR + P::DISK + P::USE + P::BUF + P::CHK + P::ATTRS) << std::endl; |
| 146 | + out.append(result.str()); |
| 147 | + return 0; |
| 148 | + } |
| 149 | + } |
| 150 | + r = -ENOENT; |
| 151 | + ss << "No collection that can hold such object" << std::endl; |
| 152 | + } else if (command == "bluestore compression stats") { |
| 153 | + std::vector<CollectionRef> copied; |
| 154 | + { |
| 155 | + std::shared_lock l(store.coll_lock); |
| 156 | + copied.reserve(store.coll_map.size()); |
| 157 | + for (const auto& c : store.coll_map) { |
| 158 | + copied.push_back(c.second); |
| 159 | + } |
| 160 | + } |
| 161 | + std::string coll; |
| 162 | + cmd_getval(cmdmap, "collection", coll); |
| 163 | + f->open_array_section("compression"); |
| 164 | + for (const auto& c : copied) { |
| 165 | + std::shared_lock l(c->lock); |
| 166 | + if ((coll.empty() && bool(c->estimator)) |
| 167 | + || coll == c->get_cid().c_str()) { |
| 168 | + f->open_object_section("collection"); |
| 169 | + f->dump_string("cid", c->get_cid().c_str()); |
| 170 | + f->open_object_section("estimator"); |
| 171 | + if (c->estimator) { |
| 172 | + c->estimator->dump(f); |
| 173 | + } |
| 174 | + f->close_section(); |
| 175 | + f->close_section(); |
| 176 | + } |
| 177 | + } |
| 178 | + f->close_section(); |
| 179 | + return 0; |
| 180 | + } else { |
| 181 | + ss << "Invalid command" << std::endl; |
| 182 | + r = -ENOSYS; |
| 183 | + } |
| 184 | + return r; |
| 185 | +} |
0 commit comments