aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storage/distributor/bucket_ownership_calculator.cpp
blob: 6f94235e5485f1f04235e53d99b2316aeab679c7 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "bucket_ownership_calculator.h"
#include <vespa/document/bucket/bucket.h>
#include <vespa/vdslib/distribution/distribution.h>
#include <vespa/vdslib/state/clusterstate.h>

namespace storage::distributor {

namespace {

uint64_t superbucket_from_id(const document::BucketId& id, uint16_t distribution_bits) noexcept {
    // The n LSBs of the bucket ID contain the superbucket number. Mask off the rest.
    return id.getRawId() & ~(UINT64_MAX << distribution_bits);
}

}

bool
BucketOwnershipCalculator::this_distributor_owns_bucket(const document::BucketId& bucket_id) const
{
    // TODO "no distributors available" case is the same for _all_ buckets; cache once in constructor.
    // TODO "too few bits used" case can be cheaply checked without needing exception
    try {
        const auto bits = _state.getDistributionBitCount();
        const auto this_superbucket = superbucket_from_id(bucket_id, bits);
        if (_cached_decision_superbucket == this_superbucket) {
            return _cached_owned;
        }
        uint16_t distributor = _distribution.getIdealDistributorNode(_state, bucket_id, "uim");
        _cached_decision_superbucket = this_superbucket;
        _cached_owned = (distributor == _this_node_index);
        return _cached_owned;
    } catch (lib::TooFewBucketBitsInUseException&) {
        // Ignore; implicitly not owned
    } catch (lib::NoDistributorsAvailableException&) {
        // Ignore; implicitly not owned
    }
    return false;
}

}