aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storage/distributor/maintenance/node_maintenance_stats_tracker.h
blob: 011b388029d92f9a42cdee9eb89ea8c13f9ca123 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/document/bucket/bucketspace.h>
#include <vespa/vespalib/util/time.h>
#include <vespa/vespalib/stllike/hash_map.h>

namespace storage::distributor {

struct NodeMaintenanceStats
{
    uint64_t movingOut;
    uint64_t syncing;
    uint64_t copyingIn;
    uint64_t copyingOut;
    uint64_t total;

    constexpr NodeMaintenanceStats() noexcept
        : movingOut(0), syncing(0), copyingIn(0), copyingOut(0), total(0)
    {}

    constexpr NodeMaintenanceStats(uint64_t movingOut_, uint64_t syncing_, uint64_t copyingIn_,
                                   uint64_t copyingOut_, uint64_t total_) noexcept
        : movingOut(movingOut_), syncing(syncing_),
          copyingIn(copyingIn_), copyingOut(copyingOut_),
          total(total_)
    {}

    bool operator==(const NodeMaintenanceStats& other) const noexcept {
        return (movingOut == other.movingOut
                && syncing == other.syncing
                && copyingIn == other.copyingIn
                && copyingOut == other.copyingOut
                && total == other.total);
    }
    bool operator!=(const NodeMaintenanceStats& other) const noexcept {
        return !(*this == other);
    }

    void merge(const NodeMaintenanceStats& rhs) noexcept {
        movingOut  += rhs.movingOut;
        syncing    += rhs.syncing;
        copyingIn  += rhs.copyingIn;
        copyingOut += rhs.copyingOut;
        total      += rhs.total;
    }
};

std::ostream& operator<<(std::ostream&, const NodeMaintenanceStats&);

class NodeMaintenanceStatsTracker
{
public:
    class BucketSpaceAndNode {
    public:
        BucketSpaceAndNode(uint16_t node_in, document::BucketSpace bucketSpace_in) noexcept
            : _bucketSpace(bucketSpace_in),
              _node(node_in)
        {}
        uint32_t hash() const noexcept { return (uint32_t(_node) << 2) | (_bucketSpace.getId() & 0x3); }
        bool operator == (const BucketSpaceAndNode & b) const noexcept {
            return (_bucketSpace == b._bucketSpace) && (_node == b._node);
        }
        document::BucketSpace bucketSpace() const noexcept { return _bucketSpace; }
        uint16_t node() const noexcept { return _node; }
    private:
        document::BucketSpace _bucketSpace;
        uint16_t              _node;
    };
    using PerNodeStats = vespalib::hash_map<BucketSpaceAndNode, NodeMaintenanceStats>;

private:
    PerNodeStats         _node_stats;
    NodeMaintenanceStats _total_stats;
    vespalib::duration   _max_observed_time_since_last_gc;

    static const NodeMaintenanceStats _emptyNodeMaintenanceStats;

    NodeMaintenanceStats & stats(uint16_t node, document::BucketSpace bucketSpace);
    const NodeMaintenanceStats & stats(uint16_t node, document::BucketSpace bucketSpace) const noexcept;
public:
    NodeMaintenanceStatsTracker() noexcept;
    NodeMaintenanceStatsTracker(NodeMaintenanceStatsTracker &&) noexcept;
    NodeMaintenanceStatsTracker & operator =(NodeMaintenanceStatsTracker &&) noexcept;
    NodeMaintenanceStatsTracker(const NodeMaintenanceStatsTracker &);
    ~NodeMaintenanceStatsTracker();
    void reset(size_t nodes);
    size_t numNodes() const { return _node_stats.size(); }

    void incMovingOut(uint16_t node, document::BucketSpace bucketSpace) {
        ++stats(node, bucketSpace).movingOut;
        ++_total_stats.movingOut;
    }

    void incSyncing(uint16_t node, document::BucketSpace bucketSpace) {
        ++stats(node, bucketSpace).syncing;
        ++_total_stats.syncing;
    }

    void incCopyingIn(uint16_t node, document::BucketSpace bucketSpace) {
        ++stats(node, bucketSpace).copyingIn;
        ++_total_stats.copyingIn;
    }

    void incCopyingOut(uint16_t node, document::BucketSpace bucketSpace) {
        ++stats(node, bucketSpace).copyingOut;
        ++_total_stats.copyingOut;
    }

    void incTotal(uint16_t node, document::BucketSpace bucketSpace) {
        ++stats(node, bucketSpace).total;
        ++_total_stats.total;
    }

    void update_observed_time_since_last_gc(vespalib::duration time_since_gc) noexcept {
        _max_observed_time_since_last_gc = std::max(time_since_gc, _max_observed_time_since_last_gc);
    }

    /**
     * Returned statistics for a given node index and bucket space, or all zero statistics
     * if none have been recorded yet
     */
    const NodeMaintenanceStats& forNode(uint16_t node, document::BucketSpace bucketSpace) const noexcept;

    const PerNodeStats& perNodeStats() const noexcept {
        return _node_stats;
    }

    // Note: the total statistics are across all replicas across all buckets across all bucket spaces.
    // That means it's possible for a single bucket to count more than once, up to once per replica.
    // So this should not be treated as a bucket-level statistic.
    const NodeMaintenanceStats& total_replica_stats() const noexcept {
        return _total_stats;
    }

    vespalib::duration max_observed_time_since_last_gc() const noexcept {
        return _max_observed_time_since_last_gc;
    }

    bool operator==(const NodeMaintenanceStatsTracker& rhs) const noexcept;
    void merge(const NodeMaintenanceStatsTracker& rhs);
};

} // storage::distributor