summaryrefslogtreecommitdiffstats
path: root/persistence/src/vespa/persistence/spi/clusterstate.h
blob: e0db8cc7196cfca5180607cbb703ffa090f00e2c (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "bucket.h"

namespace storage {

namespace lib {
    class ClusterState;
    class Distribution;
}

namespace spi {

/**
 * Used to determine the state of the current node and its buckets.
 */
class ClusterState {
public:
    typedef std::shared_ptr<ClusterState> SP;

    ClusterState(const lib::ClusterState& state,
                 uint16_t nodeIndex,
                 const lib::Distribution& distribution);

    ClusterState(vespalib::nbostream& i);

    ClusterState(const ClusterState& other);
    ClusterState& operator=(const ClusterState& other);
    ~ClusterState();

    /**
     * Returns true if the system has been set up to have
     * "ready" nodes, and the given bucket is in the ideal state
     * for readiness.
     *
     * @param b The bucket to check.
     */
    bool shouldBeReady(const Bucket& b) const;

    /**
     * Returns false if the cluster has been deemed down. This can happen
     * if the fleet controller has detected that too many nodes are down
     * compared to the complete list of nodes, and deigns the system to be
     * unusable.
     */
    bool clusterUp() const;

    /**
     * Returns false if this node has been set in a state where it should not
     * receive external load.
     */
    bool nodeUp() const;

    /**
     * Returns true iff this node is marked as Initializing in the cluster state.
     */
    bool nodeInitializing() const;

    /**
     * Returns true iff this node is marked as Retired in the cluster state.
     */
    bool nodeRetired() const;

    /**
     * Returns a serialized form of this object.
     */
    void serialize(vespalib::nbostream& o) const;

private:
    std::unique_ptr<lib::ClusterState> _state;
    uint16_t _nodeIndex;
    std::unique_ptr<lib::Distribution> _distribution;

    void deserialize(vespalib::nbostream&);
    bool nodeHasStateOneOf(const char* states) const;
};

}

}