From 4410399b2cab66ea0871ce61cab13bc204821988 Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Wed, 9 Aug 2023 13:28:20 +0000 Subject: - Use a hashmap for string Node/NodeState in ClusterState for fast lookup. - Do explicit sort when serializing. --- vdslib/src/vespa/vdslib/state/clusterstate.cpp | 23 +++++++++++++++-------- vdslib/src/vespa/vdslib/state/clusterstate.h | 8 +++++--- vdslib/src/vespa/vdslib/state/node.h | 1 + 3 files changed, 21 insertions(+), 11 deletions(-) (limited to 'vdslib') diff --git a/vdslib/src/vespa/vdslib/state/clusterstate.cpp b/vdslib/src/vespa/vdslib/state/clusterstate.cpp index b796e357d24..ef13c48a1e7 100644 --- a/vdslib/src/vespa/vdslib/state/clusterstate.cpp +++ b/vdslib/src/vespa/vdslib/state/clusterstate.cpp @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include #include @@ -193,11 +195,11 @@ struct SeparatorPrinter { namespace { void -serialize_node(vespalib::asciistream & out, const std::pair & entry) { +serialize_node(vespalib::asciistream & out, const Node & node, const NodeState & state) { vespalib::asciistream prefix; - prefix << "." << entry.first.getIndex() << "."; + prefix << "." << node.getIndex() << "."; vespalib::asciistream ost; - entry.second.serialize(ost, prefix.str(), false); + state.serialize(ost, prefix.str(), false); vespalib::stringref content = ost.str(); if ( !content.empty()) { out << " " << content; @@ -208,14 +210,15 @@ serialize_node(vespalib::asciistream & out, const std::pair & e void ClusterState::serialize_nodes(vespalib::asciistream & out, bool ignoreNewFeatures, - SeparatorPrinter & sep, const NodeType & nodeType) const + SeparatorPrinter & sep, const NodeType & nodeType, + const std::vector & nodeStates) const { uint16_t nodeCount = getNodeCount(nodeType); if (ignoreNewFeatures || nodeCount > 0) { out << sep.toString() << nodeType.serialize() << ":" << nodeCount; - for (const auto & entry : _nodeStates) { + for (const auto & entry : nodeStates) { if (entry.first.getType() == nodeType) { - serialize_node(out, entry); + serialize_node(out, entry.first, entry.second); } } } @@ -235,8 +238,12 @@ ClusterState::serialize(vespalib::asciistream & out, bool ignoreNewFeatures) con out << sep.toString() << "bits:" << _distributionBits; } - serialize_nodes(out, ignoreNewFeatures, sep, NodeType::DISTRIBUTOR); - serialize_nodes(out, ignoreNewFeatures, sep, NodeType::STORAGE); + if (! ignoreNewFeatures && ((getNodeCount(NodeType::DISTRIBUTOR) + getNodeCount(NodeType::STORAGE)) == 0u)) return; + + std::vector nodeStates(_nodeStates.cbegin(), _nodeStates.cend()); + std::sort(nodeStates.begin(), nodeStates.end(), [](const NodeStatePair &a, const NodeStatePair &b) { return a.first < b.first; }); + serialize_nodes(out, ignoreNewFeatures, sep, NodeType::DISTRIBUTOR, nodeStates); + serialize_nodes(out, ignoreNewFeatures, sep, NodeType::STORAGE, nodeStates); } bool diff --git a/vdslib/src/vespa/vdslib/state/clusterstate.h b/vdslib/src/vespa/vdslib/state/clusterstate.h index 70f4d59977a..4cf8ba20d81 100644 --- a/vdslib/src/vespa/vdslib/state/clusterstate.h +++ b/vdslib/src/vespa/vdslib/state/clusterstate.h @@ -10,7 +10,7 @@ #include "node.h" #include "nodestate.h" -#include +#include #include namespace storage::lib { @@ -22,7 +22,8 @@ struct SeparatorPrinter; class ClusterState : public document::Printable { public: - using NodeMap = std::map; + using NodeStatePair = std::pair; + using NodeMap = vespalib::hash_map; using NodeCounts = std::array; using CSP = std::shared_ptr; using SP = std::shared_ptr; @@ -71,7 +72,8 @@ private: void printStateGroupwise(std::ostream& out, const Group&, bool verbose, const std::string& indent, bool rootGroup) const; void getTextualDifference(std::ostringstream& builder, const NodeType& type, const ClusterState& other) const; size_t printStateGroupwise(std::ostream& out, const Group&, bool verbose, const std::string& indent, const NodeType& type) const; - void serialize_nodes(vespalib::asciistream & out, bool ignoreNewFeatures, SeparatorPrinter & sep, const NodeType & nodeType) const; + void serialize_nodes(vespalib::asciistream & out, bool ignoreNewFeatures, SeparatorPrinter & sep, + const NodeType & nodeType, const std::vector & nodeStates) const; uint32_t _version; NodeCounts _nodeCount; const State* _clusterState; diff --git a/vdslib/src/vespa/vdslib/state/node.h b/vdslib/src/vespa/vdslib/state/node.h index 5dfd6a2ccf4..49c8f0e641b 100644 --- a/vdslib/src/vespa/vdslib/state/node.h +++ b/vdslib/src/vespa/vdslib/state/node.h @@ -22,6 +22,7 @@ public: const NodeType& getType() const noexcept { return *_type; } uint16_t getIndex() const noexcept { return _index; } + uint32_t hash() const noexcept { return (_index << 1) | *_type; } bool operator==(const Node& other) const noexcept { return (other._index == _index && *other._type == *_type); -- cgit v1.2.3