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

#pragma once

#include "operationtargetresolver.h"
#include "ideal_service_layer_nodes_bundle.h"
#include <vespa/storage/bucketdb/bucketdatabase.h>
#include <algorithm>

namespace storage::distributor {

class DistributorBucketSpace;

struct BucketInstance : public vespalib::AsciiPrintable {
    document::BucketId _bucket;
    api::BucketInfo    _info;
    lib::Node          _node;
    uint16_t           _idealLocationPriority;
    bool               _trusted;
    bool               _exist;

    BucketInstance() noexcept
        : _idealLocationPriority(0xffff), _trusted(false), _exist(false) {}
    BucketInstance(const document::BucketId& id, const api::BucketInfo& info,
                   lib::Node node, uint16_t idealLocationPriority, bool trusted,
                   bool exist) noexcept;

    void print(vespalib::asciistream& out, const PrintProperties&) const override;
};

class BucketInstanceList : public vespalib::AsciiPrintable {
    std::vector<BucketInstance> _instances;

    /**
     * Resolve and return the least specific bucket in the subtree of (and
     * including) candidateId that is a leaf node in the tree. I.e. a bucket
     * whose insertion will not cause an inconsistency with other leaf buckets
     * in the tree at the minimum possible depth at or below candidateId.
     *
     * Preconditions:
     *   candidateId.contains(mostSpecificId)
     * Postconditions:
     *   <return value>.contains(mostSpecificId)
     */
    static document::BucketId
    leastSpecificLeafBucketInSubtree(const document::BucketId& candidateId,
                                     const document::BucketId& mostSpecificId,
                                     const BucketDatabase& db);

public:
    void add(const BucketInstance& instance) { _instances.push_back(instance); }
    bool contains(lib::Node node) const;
    void removeNodeDuplicates();
    void limitToRedundancyCopies(uint16_t redundancy);
    /**
     * Preconditions:
     *   targetIfNonPreExisting.contains(mostSpecificId)
     * Postconditions:
     *   _instances.size() >= configured redundancy level, unless insufficient
     *   number of nodes are available
     */
    void extendToEnoughCopies(const DistributorBucketSpace& distributor_bucket_space,
                              const BucketDatabase& db,
                              const document::BucketId& targetIfNonPreExisting,
                              const document::BucketId& mostSpecificId);

    void populate(const document::BucketId&, const DistributorBucketSpace&, BucketDatabase&);
    void add(const BucketDatabase::Entry& e, const IdealServiceLayerNodesBundle::Node2Index & idealState);

    template <typename Order>
    void sort(const Order& order) {
        std::sort(_instances.begin(), _instances.end(), order);
    }

    OperationTargetList createTargets(document::BucketSpace bucketSpace);

    void print(vespalib::asciistream& out, const PrintProperties& p) const override;
};

class OperationTargetResolverImpl : public OperationTargetResolver {
    const DistributorBucketSpace& _distributor_bucket_space;
    BucketDatabase&       _bucketDatabase;
    uint32_t              _minUsedBucketBits;
    uint16_t              _redundancy;
    document::BucketSpace _bucketSpace;

public:
    OperationTargetResolverImpl(const DistributorBucketSpace& distributor_bucket_space,
                                BucketDatabase& bucketDatabase,
                                uint32_t minUsedBucketBits,
                                uint16_t redundancy,
                                document::BucketSpace bucketSpace)
        : _distributor_bucket_space(distributor_bucket_space),
          _bucketDatabase(bucketDatabase),
          _minUsedBucketBits(minUsedBucketBits),
          _redundancy(redundancy),
          _bucketSpace(bucketSpace)
    {}

    BucketInstanceList getAllInstances(OperationType type, const document::BucketId& id);
    BucketInstanceList getInstances(OperationType type, const document::BucketId& id) {
        BucketInstanceList result(getAllInstances(type, id));
        result.limitToRedundancyCopies(_redundancy);
        return result;
    }

    OperationTargetList getTargets(OperationType type, const document::BucketId& id) override {
        return getInstances(type, id).createTargets(_bucketSpace);
    }
};

}