From d907c83a12d78c8b4c0d96dc51320cf29f680728 Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Tue, 7 Feb 2023 15:06:54 +0100 Subject: Revert "Revert "Use steady_time for vdslib::NodeState MERGEOK"" --- vdslib/src/tests/state/nodestatetest.cpp | 2 +- vdslib/src/vespa/vdslib/state/nodestate.cpp | 60 ++++++++++++++--------------- vdslib/src/vespa/vdslib/state/nodestate.h | 28 +++++++------- 3 files changed, 45 insertions(+), 45 deletions(-) (limited to 'vdslib') diff --git a/vdslib/src/tests/state/nodestatetest.cpp b/vdslib/src/tests/state/nodestatetest.cpp index 000542e77fe..c854adf3915 100644 --- a/vdslib/src/tests/state/nodestatetest.cpp +++ b/vdslib/src/tests/state/nodestatetest.cpp @@ -20,7 +20,7 @@ TEST(NodeStateTest, test_parsing) { NodeState ns = NodeState("t:4"); EXPECT_EQ(std::string("s:u t:4"), ns.toString()); - EXPECT_EQ(uint64_t(4), ns.getStartTimestamp()); + EXPECT_EQ(4s, ns.getStartTimestamp().time_since_epoch()); } { NodeState ns = NodeState("s:u c:2.4 b:12"); diff --git a/vdslib/src/vespa/vdslib/state/nodestate.cpp b/vdslib/src/vespa/vdslib/state/nodestate.cpp index a7c5476456a..0dd7f5abb4c 100644 --- a/vdslib/src/vespa/vdslib/state/nodestate.cpp +++ b/vdslib/src/vespa/vdslib/state/nodestate.cpp @@ -8,9 +8,8 @@ #include #include #include -#include -#include +#include LOG_SETUP(".vdslib.nodestate"); namespace storage::lib { @@ -19,16 +18,16 @@ NodeState::NodeState(const NodeState &) = default; NodeState & NodeState::operator = (const NodeState &) = default; NodeState::NodeState(NodeState &&) noexcept = default; NodeState & NodeState::operator = (NodeState &&) noexcept = default; -NodeState::~NodeState() { } +NodeState::~NodeState() = default; NodeState::NodeState() - : _type(0), - _state(0), + : _type(nullptr), + _state(nullptr), _description(""), _capacity(1.0), _initProgress(0.0), _minUsedBits(16), - _startTimestamp(0) + _startTimestamp() { setState(State::UP); } @@ -36,12 +35,12 @@ NodeState::NodeState() NodeState::NodeState(const NodeType& type, const State& state, vespalib::stringref description, double capacity) : _type(&type), - _state(0), + _state(nullptr), _description(description), _capacity(1.0), _initProgress(0.0), _minUsedBits(16), - _startTimestamp(0) + _startTimestamp() { setState(state); if (type == NodeType::STORAGE) { @@ -56,25 +55,24 @@ NodeState::NodeState(vespalib::stringref serialized, const NodeType* type) _capacity(1.0), _initProgress(0.0), _minUsedBits(16), - _startTimestamp(0) + _startTimestamp() { vespalib::StringTokenizer st(serialized, " \t\f\r\n"); st.removeEmptyTokens(); - for (vespalib::StringTokenizer::Iterator it = st.begin(); - it != st.end(); ++it) + for (auto it : st) { - std::string::size_type index = it->find(':'); + std::string::size_type index = it.find(':'); if (index == std::string::npos) { throw vespalib::IllegalArgumentException( - "Token " + *it + " does not contain ':': " + serialized, + "Token " + it + " does not contain ':': " + serialized, VESPA_STRLOC); } - std::string key = it->substr(0, index); - std::string value = it->substr(index + 1); - if (key.size() > 0) switch (key[0]) { + std::string key = it.substr(0, index); + std::string value = it.substr(index + 1); + if (!key.empty()) switch (key[0]) { case 'b': - if (_type != 0 && *type != NodeType::STORAGE) break; + if (_type != nullptr && *type != NodeType::STORAGE) break; if (key.size() > 1) break; try{ setMinUsedBits(boost::lexical_cast(value)); @@ -91,7 +89,7 @@ NodeState::NodeState(vespalib::stringref serialized, const NodeType* type) continue; case 'c': if (key.size() > 1) break; - if (_type != 0 && *type != NodeType::STORAGE) break; + if (_type != nullptr && *type != NodeType::STORAGE) break; try{ setCapacity(boost::lexical_cast(value)); } catch (...) { @@ -115,7 +113,7 @@ NodeState::NodeState(vespalib::stringref serialized, const NodeType* type) case 't': if (key.size() > 1) break; try{ - setStartTimestamp(boost::lexical_cast(value)); + setStartTimestamp(vespalib::system_time(std::chrono::seconds(boost::lexical_cast(value)))); } catch (...) { throw vespalib::IllegalArgumentException( "Illegal start timestamp '" + value + "'. Start " @@ -165,7 +163,7 @@ NodeState::serialize(vespalib::asciistream & out, vespalib::stringref prefix, SeparatorPrinter sep; // Always give node state if not part of a system state // to prevent empty serialization - if (*_state != State::UP || prefix.size() == 0) { + if (*_state != State::UP || prefix.empty()) { out << sep << prefix << "s:"; out << _state->serialize(); } @@ -178,8 +176,8 @@ NodeState::serialize(vespalib::asciistream & out, vespalib::stringref prefix, if (*_state == State::INITIALIZING) { out << sep << prefix << "i:" << _initProgress; } - if (_startTimestamp != 0) { - out << sep << prefix << "t:" << _startTimestamp; + if (_startTimestamp != vespalib::system_time()) { + out << sep << prefix << "t:" << vespalib::count_s(_startTimestamp.time_since_epoch()); } if (includeDescription && ! _description.empty()) { out << sep << prefix << "m:" @@ -190,7 +188,7 @@ NodeState::serialize(vespalib::asciistream & out, vespalib::stringref prefix, void NodeState::setState(const State& state) { - if (_type != 0) { + if (_type != nullptr) { // We don't know whether you want to store reported, wanted or // current node state, so we must accept any. if (!state.validReportedNodeState(*_type) @@ -225,7 +223,7 @@ NodeState::setCapacity(vespalib::Double capacity) "must be a positive floating point number"; throw vespalib::IllegalArgumentException(ost.str(), VESPA_STRLOC); } - if (_type != 0 && *_type != NodeType::STORAGE) { + if (_type != nullptr && *_type != NodeType::STORAGE) { throw vespalib::IllegalArgumentException( "Capacity only make sense for storage nodes.", VESPA_STRLOC); } @@ -245,7 +243,7 @@ NodeState::setInitProgress(vespalib::Double initProgress) } void -NodeState::setStartTimestamp(uint64_t startTimestamp) +NodeState::setStartTimestamp(vespalib::system_time startTimestamp) { _startTimestamp = startTimestamp; } @@ -270,10 +268,10 @@ NodeState::print(std::ostream& out, bool verbose, if (*_state == State::INITIALIZING) { out << ", init progress " << _initProgress; } - if (_startTimestamp != 0) { - out << ", start timestamp " << _startTimestamp; + if (_startTimestamp != vespalib::system_time()) { + out << ", start timestamp " << vespalib::to_string(_startTimestamp); } - if (_description.size() > 0) { + if (!_description.empty()) { out << ": " << _description; } } @@ -317,7 +315,7 @@ NodeState::similarTo(const NodeState& other) const void NodeState::verifySupportForNodeType(const NodeType& type) const { - if (_type != 0 && *_type == type) return; + if (_type != nullptr && *_type == type) return; if (!_state->validReportedNodeState(type) && !_state->validWantedNodeState(type)) { @@ -357,8 +355,8 @@ NodeState::getTextualDifference(const NodeState& other) const { } } if (_startTimestamp != other._startTimestamp) { - source << ", start timestamp " << _startTimestamp; - target << ", start timestamp " << other._startTimestamp; + source << ", start timestamp " << vespalib::to_string(_startTimestamp); + target << ", start timestamp " << vespalib::to_string(other._startTimestamp); } if (source.str().length() < 2 || target.str().length() < 2) { diff --git a/vdslib/src/vespa/vdslib/state/nodestate.h b/vdslib/src/vespa/vdslib/state/nodestate.h index 541395e15cb..4fb035b6dcd 100644 --- a/vdslib/src/vespa/vdslib/state/nodestate.h +++ b/vdslib/src/vespa/vdslib/state/nodestate.h @@ -13,6 +13,7 @@ #include "state.h" #include #include +#include #include namespace storage::lib { @@ -25,7 +26,7 @@ class NodeState : public document::Printable vespalib::Double _capacity; vespalib::Double _initProgress; uint32_t _minUsedBits; - uint64_t _startTimestamp; + vespalib::system_time _startTimestamp; public: using CSP = std::shared_ptr; @@ -43,8 +44,8 @@ public: vespalib::stringref description = "", double capacity = 1.0); /** Set type if you want to verify that content fit with the given type. */ - NodeState(vespalib::stringref serialized, const NodeType* nodeType = 0); - ~NodeState(); + explicit NodeState(vespalib::stringref serialized, const NodeType* nodeType = nullptr); + ~NodeState() override; /** * Setting prefix to something implies using this function to write a @@ -54,26 +55,27 @@ public: void serialize(vespalib::asciistream & out, vespalib::stringref prefix = "", bool includeDescription = true) const; - const State& getState() const { return *_state; } - vespalib::Double getCapacity() const { return _capacity; } - uint32_t getMinUsedBits() const { return _minUsedBits; } - vespalib::Double getInitProgress() const { return _initProgress; } - const vespalib::string& getDescription() const { return _description; } - uint64_t getStartTimestamp() const { return _startTimestamp; } + [[nodiscard]] const State& getState() const { return *_state; } + [[nodiscard]] vespalib::Double getCapacity() const { return _capacity; } + [[nodiscard]] uint32_t getMinUsedBits() const { return _minUsedBits; } + [[nodiscard]] vespalib::Double getInitProgress() const { return _initProgress; } + [[nodiscard]] const vespalib::string& getDescription() const { return _description; } + [[nodiscard]] vespalib::system_time getStartTimestamp() const { return _startTimestamp; } void setState(const State& state); void setCapacity(vespalib::Double capacity); void setMinUsedBits(uint32_t usedBits); void setInitProgress(vespalib::Double initProgress); - void setStartTimestamp(uint64_t startTimestamp); + void setStartTimestamp(vespalib::system_time startTimestamp); void setDescription(vespalib::stringref desc) { _description = desc; } void print(std::ostream& out, bool verbose, const std::string& indent) const override; bool operator==(const NodeState& other) const; - bool operator!=(const NodeState& other) const - { return !(operator==(other)); } - bool similarTo(const NodeState& other) const; + bool operator!=(const NodeState& other) const { + return !(operator==(other)); + } + [[nodiscard]] bool similarTo(const NodeState& other) const; /** * Verify that the contents of this object fits with the given nodetype. -- cgit v1.2.3