#include #include "dgrid_logger.h" #include "dgridd_datastore.h" #include "dgridd_datastore_simple.h" #include "dgridd_datastore_table.h" namespace DGrid { #define TABLEID_HASH_SEED 0x136C936C uint64_t TableIDhasher::operator()(const TableID& k) const { uint64_t hash(TABLEID_HASH_SEED); for(const uint8_t c: k) { hash = ((hash << 1) | (hash >> 63)) ^ c; } return hash; } DGriddDataStore* DGriddDataStore::_instance = nullptr; DGriddDataStore::DGriddDataStore() { _simple_default = std::make_shared(); _table_default = std::make_shared(); } DGriddDataStore::~DGriddDataStore() {} DGriddDataStore* DGriddDataStore::instance() { if (_instance == nullptr) { _instance = new DGriddDataStore(); } return _instance; } void DGriddDataStore::destroy() { if (_instance != nullptr) { delete _instance; _instance = nullptr; } } void DGriddDataStore::setNodeArbiter(DGriddNodeArbiter* a) { _node_arbiter = a; _simple_default->setNodeArbiter(_node_arbiter); } DGriddNodeArbiter* DGriddDataStore::getNodeArbiter() { return _node_arbiter; } void DGriddDataStore::showStats(const uint8_t detail_level) { uint64_t tables_total(0); std::string err_details; writeLog(LOG_INFO, "> DataStote stats (default): %d themes, %d keys, %lu tables", _simple_default->getThemesCount(), _simple_default->getKeysCount(), _table_default->getTablesCount()); simple_session_mutex.lock(); writeLog(LOG_INFO, "> DataStote stats (sessions): %d K-V, %d T", _simple_session.size(), _table_session.size()); if(detail_level>1) { for(auto s: _simple_session) { writeLog(LOG_INFO, " > %s: %d themes, %d keys", s.first.c_str(), s.second->getThemesCount(), s.second->getKeysCount()); } } simple_session_mutex.unlock(); table_session_mutex.lock(); if(detail_level>1) { for(auto ts: _table_session) { uint64_t cnt(0); for(auto t: ts.second) { cnt += t.second->getTablesCount(); } tables_total += cnt; writeLog(LOG_INFO, " > %s: %d tablespaces, %d tables", ts.first.c_str(), ts.second.size(), cnt); /* for(auto t: ts.second) { writeLog(LOG_INFO, " > %s: %lu tables", t.first.c_str(), t.second->getTablesCount()); for(std::string tname: t.second->getTablesList()) { t.second->getTableSize(tname, cnt, err_details); writeLog(LOG_INFO, " - %s (%d rows) '%s'", raw_to_hexstring(getTableID(ts.first, t.first, tname)).c_str(), cnt, tname.c_str()); } } */ } writeLog(LOG_INFO, "> Tables total: %lu", tables_total); } writeLog(LOG_INFO, "> Tables registered: %lu", registered_tables.size()); table_session_mutex.unlock(); } void DGriddDataStore::DGriddDataStore::clearSession(std::string session) { simple_session_mutex.lock(); _simple_session.erase(session); simple_session_mutex.unlock(); table_session_mutex.lock(); _table_session.erase(session); table_session_mutex.unlock(); table_reg_mutex.lock(); deregisterSessionTables_int(session); table_reg_mutex.unlock(); } void DGriddDataStore::list_sessions_simple(std::vector& list) { list.clear(); simple_session_mutex.lock(); list.reserve(_simple_session.size()); for(auto s: _simple_session) list.push_back(s.first); simple_session_mutex.unlock(); } void DGriddDataStore::list_sessions_table(std::vector& list) { list.clear(); table_session_mutex.lock(); list.reserve(_table_session.size()); for(auto s: _table_session) list.push_back(s.first); table_session_mutex.unlock(); } void DGriddDataStore::list_tablespaces(const std::string& session, std::vector& list) { list.clear(); if(session.empty()) return; table_session_mutex.lock(); auto its = _table_session.find(session); if(its != _table_session.end()) { for(auto it: its->second) { list.push_back(it.first); } } table_session_mutex.unlock(); } std::shared_ptr DGriddDataStore::simple(const std::string& session) { if(session.empty()) return _simple_default; simple_session_mutex.lock(); std::shared_ptr store = nullptr; auto it = _simple_session.find(session); if(it != _simple_session.end()) { store = it->second; } else { store = std::make_shared(session, _node_arbiter); _simple_session[session] = store; } simple_session_mutex.unlock(); return store; } std::shared_ptr DGriddDataStore::table(const std::string& session, const std::string& space) { if(session.empty()) return _table_default; table_session_mutex.lock(); std::shared_ptr store; auto it = _table_session[session].find(space); if(it != _table_session[session].end()) { store = it->second; } else { store = std::make_shared(session, _node_arbiter); _table_session[session][space] = store; } table_session_mutex.unlock(); return store; } std::shared_ptr DGriddDataStore::table(const TableID& table_id, std::string& tname) { std::shared_ptr descr = getTableDescription(table_id); if(nullptr == descr) return nullptr; tname = descr->tname; return table(descr->session, descr->space); } bool DGriddDataStore::hasTable(const TableID& table_id) { std::shared_ptr descr = getTableDescription(table_id); return descr != nullptr; } void DGriddDataStore::registerTable(const NodeID& master_node, TableFullID& id_table) { std::lock_guard lock(table_reg_mutex); if(id_table.id.empty()) id_table.id = createTableID(); registered_tables[id_table.id] = std::make_shared(id_table.session, id_table.space, id_table.name, master_node); } void DGriddDataStore::setMasterNode(const TableID& id_table, const NodeID& id_node) { std::lock_guard lock(table_reg_mutex); auto it = registered_tables.find(id_table); if(it != registered_tables.end()) it->second->master_node = id_node; } NodeID DGriddDataStore::getMasterNode(const TableID& id_table) { if(!id_table.empty()) { std::lock_guard lock(table_reg_mutex); auto it = registered_tables.find(id_table); if(it != registered_tables.end()) return it->second->master_node; } return NodeID(); } void DGriddDataStore::setFallbackNodes(const TableID& id_table, const std::vector& nodes) { std::lock_guard lock(table_reg_mutex); auto it = registered_tables.find(id_table); if(it != registered_tables.end()) it->second->fallback_nodes = nodes; } std::vector DGriddDataStore::getFallbackNodes(const TableID& id_table) { std::lock_guard lock(table_reg_mutex); auto it = registered_tables.find(id_table); if(it != registered_tables.end()) return it->second->fallback_nodes; return std::vector(); } const TableID DGriddDataStore::getTableID(const std::string& session, const std::string& space, const std::string& tname) { std::lock_guard lock(table_reg_mutex); return getTableID_int(session, space, tname); } bool DGriddDataStore::deregisterTable(const TableID& id) { std::lock_guard lock(table_reg_mutex); if(registered_tables.find(id) != registered_tables.end()) { registered_tables.erase(id); return true; } writeLog(LOG_ERR, "Can't deregister table id=%lu: not found", id); return false; } bool DGriddDataStore::deregisterTable(const std::string& session, const std::string& space, const std::string& tname) { std::lock_guard lock(table_reg_mutex); TableID id = getTableID_int(session, space, tname); if(!id.empty()) { registered_tables.erase(id); return true; } writeLog(LOG_ERR, "Can't deregister table '%s/%s/%s': not found", session.c_str(), space.c_str(), tname.c_str()); return false; } const TableID DGriddDataStore::createTableID() { TableID id; id.resize(16); uuid_generate(reinterpret_cast(id.data())); return id; } std::shared_ptr DGriddDataStore::getTableDescription(const TableID& id) { std::lock_guard lock(table_reg_mutex); auto it = registered_tables.find(id); if(it != registered_tables.end()) return it->second; return nullptr; } void DGriddDataStore::deregisterSessionTables_int(const std::string& session) { for(auto it = registered_tables.begin(); it != registered_tables.end(); ) { if(session == it->second->session) { it = registered_tables.erase(it); } else { ++it; } } } const TableID DGriddDataStore::getTableID_int(const std::string& session, const std::string& space, const std::string& tname) { for(auto it = registered_tables.begin(); it != registered_tables.end(); it++) { if(it->second->compare(session, space, tname)) return it->first; } return TableID(); } } // namespace DGrid