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

#include <vespa/storage/bucketdb/bucketdatabase.h>
#include <memory>

namespace storage::distributor {

class BucketSpaceDistributionContext;
class DistributorBucketSpaceRepo;

/**
 * An "operation routing snapshot" is intended to provide a stable means of computing
 * bucket routing targets and performing database lookups for a particular bucket space
 * in a potentially multi-threaded setting. When using multiple threads, both the current
 * cluster/distribution state as well as the underlying bucket database may change
 * independent of each other when observed from any other thread than the main distributor
 * thread. Additionally, the bucket management system may operate with separate read-only
 * databases during state transitions, complicating things further.
 *
 * By using an OperationRoutingSnapshot, a caller gets a consistent view of the world
 * that stays valid throughout the operation's life time.
 *
 * Note that holding the DB read guard should be done for as short a time as possible to
 * avoid elevated memory usage caused by data stores not being able to free on-hold items.
 */
class OperationRoutingSnapshot {
    std::shared_ptr<const BucketSpaceDistributionContext> _context;
    std::shared_ptr<BucketDatabase::ReadGuard> _read_guard;
    const DistributorBucketSpaceRepo* _bucket_space_repo;
public:
    OperationRoutingSnapshot(std::shared_ptr<const BucketSpaceDistributionContext> context,
                             std::shared_ptr<BucketDatabase::ReadGuard> read_guard,
                             const DistributorBucketSpaceRepo* bucket_space_repo);

    static OperationRoutingSnapshot make_not_routable_in_state(std::shared_ptr<const BucketSpaceDistributionContext> context);
    static OperationRoutingSnapshot make_routable_with_guard(std::shared_ptr<const BucketSpaceDistributionContext> context,
                                                             std::shared_ptr<BucketDatabase::ReadGuard> read_guard,
                                                             const DistributorBucketSpaceRepo& bucket_space_repo);

    OperationRoutingSnapshot(const OperationRoutingSnapshot&) noexcept = default;
    OperationRoutingSnapshot& operator=(const OperationRoutingSnapshot&) noexcept = default;
    OperationRoutingSnapshot(OperationRoutingSnapshot&&) noexcept = default;
    OperationRoutingSnapshot& operator=(OperationRoutingSnapshot&&) noexcept = default;

    ~OperationRoutingSnapshot();

    const BucketSpaceDistributionContext& context() const noexcept { return *_context; }
    std::shared_ptr<BucketDatabase::ReadGuard> steal_read_guard() noexcept {
        return std::move(_read_guard);
    }
    bool is_routable() const noexcept {
        return (_read_guard.get() != nullptr);
    }
    const DistributorBucketSpaceRepo* bucket_space_repo() const noexcept {
        return _bucket_space_repo;
    }
};

}