diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2023-08-09 13:28:20 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2023-08-09 13:41:31 +0000 |
commit | 4410399b2cab66ea0871ce61cab13bc204821988 (patch) | |
tree | 6a7c9455efd337562e45761ba4a04b6b59ea91ae | |
parent | eed1ed31709908ec5317369af9955a9f2862fbfe (diff) |
- Use a hashmap for string Node/NodeState in ClusterState for fast lookup.
- Do explicit sort when serializing.
-rw-r--r-- | vdslib/src/vespa/vdslib/state/clusterstate.cpp | 23 | ||||
-rw-r--r-- | vdslib/src/vespa/vdslib/state/clusterstate.h | 8 | ||||
-rw-r--r-- | vdslib/src/vespa/vdslib/state/node.h | 1 |
3 files changed, 21 insertions, 11 deletions
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 <vespa/vdslib/distribution/distribution.h> #include <vespa/vespalib/util/exceptions.h> #include <vespa/vespalib/stllike/asciistream.h> +#include <vespa/vespalib/stllike/hash_map.hpp> +#include <vespa/vespalib/stllike/hash_map_equal.hpp> #include <sstream> #include <cassert> @@ -193,11 +195,11 @@ struct SeparatorPrinter { namespace { void -serialize_node(vespalib::asciistream & out, const std::pair<Node, NodeState> & 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<Node, NodeState> & e void ClusterState::serialize_nodes(vespalib::asciistream & out, bool ignoreNewFeatures, - SeparatorPrinter & sep, const NodeType & nodeType) const + SeparatorPrinter & sep, const NodeType & nodeType, + const std::vector<NodeStatePair> & 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<NodeStatePair> 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 <map> +#include <vespa/vespalib/stllike/hash_map.h> #include <array> namespace storage::lib { @@ -22,7 +22,8 @@ struct SeparatorPrinter; class ClusterState : public document::Printable { public: - using NodeMap = std::map<Node, NodeState>; + using NodeStatePair = std::pair<Node, NodeState>; + using NodeMap = vespalib::hash_map<Node, NodeState>; using NodeCounts = std::array<uint16_t, 2>; using CSP = std::shared_ptr<const ClusterState>; using SP = std::shared_ptr<ClusterState>; @@ -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<NodeStatePair> & 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); |