summaryrefslogtreecommitdiffstats
path: root/vdslib/src/vespa/vdslib/state/clusterstate.h
blob: 70f4d59977a70524772b52ed6a5ba99fc21b96af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * @class vdslib::ClusterState
 * @ingroup state
 *
 * @brief Object to represent a system state
 */

#pragma once

#include "node.h"
#include "nodestate.h"
#include <map>
#include <array>

namespace storage::lib {

class Distribution;
class Group;
struct NodeData;
struct SeparatorPrinter;

class ClusterState : public document::Printable {
public:
    using NodeMap = std::map<Node, NodeState>;
    using NodeCounts = std::array<uint16_t, 2>;
    using CSP = std::shared_ptr<const ClusterState>;
    using SP = std::shared_ptr<ClusterState>;
    using UP = std::unique_ptr<ClusterState>;

    ClusterState();
    ClusterState(const ClusterState&);
    // FIXME make ClusterState parsing not require null termination of string,
    // then move to vespalib::stringref
    explicit ClusterState(const vespalib::string& serialized);
    ClusterState& operator=(const ClusterState& other) = delete;
    ~ClusterState();

    std::string getTextualDifference(const ClusterState& other) const;
    void serialize(vespalib::asciistream & out, bool ignoreNewFeatures) const;

    bool operator==(const ClusterState& other) const noexcept;
    bool operator!=(const ClusterState& other) const noexcept;

    uint32_t getVersion() const noexcept { return _version; }
    /**
     * Returns the smallest number above the highest node index found of the
     * given type that is not down.
     */
    uint16_t getNodeCount(const NodeType& type) const noexcept { return _nodeCount[type]; }
    uint16_t getDistributionBitCount() const noexcept { return _distributionBits; }
    const State& getClusterState() const noexcept { return *_clusterState; }
    const NodeState& getNodeState(const Node& node) const;

    void setVersion(uint32_t version) noexcept { _version = version; }
    void setClusterState(const State& state);
    void setNodeState(const Node& node, const NodeState& state);
    void setDistributionBitCount(uint16_t count) noexcept { _distributionBits = count; }

    void print(std::ostream& out, bool verbose, const std::string& indent) const override;

    void printStateGroupwise(std::ostream& out, const Distribution&, bool verbose, const std::string& indent) const;

private:
    // Preconditions: `key` and `value` MUST point into null-terminated strings.
    bool parse(vespalib::stringref key, vespalib::stringref value, NodeData & nodeData);
    // Preconditions: `key` and `value` MUST point into null-terminated strings.
    bool parseSorD(vespalib::stringref key, vespalib::stringref value, NodeData & nodeData);
    void removeExtraElements();
    void removeExtraElements(const NodeType& type);
    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;
    uint32_t           _version;
    NodeCounts         _nodeCount;
    const State*       _clusterState;
    NodeMap            _nodeStates;
    vespalib::string   _description;
    uint16_t           _distributionBits;
};

}