aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storage/distributor/bucket_spaces_stats_provider.h
blob: 18dc3c93cd824195ecdc290f9227999b95c68f8e (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/vespalib/stllike/string.h>
#include <map>
#include <ostream>
#include <unordered_map>

namespace storage::distributor {

/**
 * Statistics for a single bucket space on a content node.
 */
class BucketSpaceStats {
private:
    bool   _valid;
    size_t _bucketsTotal;
    size_t _bucketsPending;
public:
    constexpr BucketSpaceStats(size_t bucketsTotal_, size_t bucketsPending_) noexcept
        : _valid(true),
          _bucketsTotal(bucketsTotal_),
          _bucketsPending(bucketsPending_)
    {}
    constexpr BucketSpaceStats() noexcept
        : _valid(false),
          _bucketsTotal(0),
          _bucketsPending(0)
    {}

    static constexpr BucketSpaceStats make_invalid() noexcept { return {}; }

    [[nodiscard]] bool valid() const noexcept { return _valid; }
    size_t bucketsTotal() const noexcept { return _bucketsTotal; }
    size_t bucketsPending() const noexcept { return _bucketsPending; }

    bool operator==(const BucketSpaceStats& rhs) const noexcept {
        return (_valid == rhs._valid) &&
                (_bucketsTotal == rhs._bucketsTotal) &&
                (_bucketsPending == rhs._bucketsPending);
    }

    void merge(const BucketSpaceStats& rhs) noexcept {
        _valid = _valid && rhs._valid;
        _bucketsTotal += rhs._bucketsTotal;
        _bucketsPending += rhs._bucketsPending;
    }
};

std::ostream& operator<<(std::ostream& out, const BucketSpaceStats& stats);

/**
 * Interface that provides snapshots of bucket spaces statistics per content node.
 */
class BucketSpacesStatsProvider {
public:
    // Mapping from bucket space name to statistics for that bucket space.
    using BucketSpacesStats = std::map<vespalib::string, BucketSpaceStats>;
    // Mapping from content node index to statistics for all bucket spaces on that node.
    using PerNodeBucketSpacesStats = std::unordered_map<uint16_t, BucketSpacesStats>;

    virtual ~BucketSpacesStatsProvider() {}
    virtual PerNodeBucketSpacesStats getBucketSpacesStats() const = 0;
};

void merge_bucket_spaces_stats(BucketSpacesStatsProvider::BucketSpacesStats& dest,
                               const BucketSpacesStatsProvider::BucketSpacesStats& src);

void merge_per_node_bucket_spaces_stats(BucketSpacesStatsProvider::PerNodeBucketSpacesStats& dest,
                                        const BucketSpacesStatsProvider::PerNodeBucketSpacesStats& src);

}