summaryrefslogtreecommitdiffstats
path: root/vdslib/src/vespa/vdslib/state/clusterstate.h
blob: 44af81f52cedb705d27ef839b759dd913747179b (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>

namespace storage::lib {

class Distribution;
class Group;
struct NodeData;

class ClusterState : public document::Printable {
    uint32_t _version;
    const State* _clusterState;
    std::map<Node, NodeState> _nodeStates;
    std::vector<uint16_t> _nodeCount;
    vespalib::string _description;
    uint16_t _distributionBits;

    void getTextualDifference(std::ostringstream& builder, const NodeType& type,
                              const ClusterState& other) const;

public:
    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 { 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;
    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 = false,
                             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 printStateGroupwise(std::ostream& out, const Group&, bool verbose,
                             const std::string& indent, bool rootGroup) const;

};

}