aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/tests/distributor/operation_sequencer_test.cpp
blob: 7e2cc30392b3ce6886606f9bd9a34bb63cee1b00 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/document/base/documentid.h>
#include <vespa/document/bucket/fixed_bucket_spaces.h>
#include <vespa/storage/distributor/operation_sequencer.h>
#include <vespa/vespalib/gtest/gtest.h>

namespace storage::distributor {

using document::DocumentId;
using namespace ::testing;

namespace {

constexpr document::BucketSpace default_space() {
    return document::FixedBucketSpaces::default_space();
}

constexpr document::BucketSpace global_space() {
    return document::FixedBucketSpaces::global_space();
}

}

struct OperationSequencerTest : Test {
    OperationSequencer sequencer;
};

TEST_F(OperationSequencerTest, can_get_sequencing_handle_for_id_without_existing_handle) {
    auto handle = sequencer.try_acquire(default_space(), DocumentId("id:foo:test::abcd"));
    EXPECT_TRUE(handle.valid());
    EXPECT_FALSE(handle.is_blocked());
}

TEST_F(OperationSequencerTest, cannot_get_sequencing_handle_for_id_with_existing_handle) {
    auto first_handle = sequencer.try_acquire(default_space(), DocumentId("id:foo:test::abcd"));
    auto second_handle = sequencer.try_acquire(default_space(), DocumentId("id:foo:test::abcd"));
    EXPECT_FALSE(second_handle.valid());
    ASSERT_TRUE(second_handle.is_blocked());
    EXPECT_TRUE(second_handle.is_blocked_by_pending_operation());
    EXPECT_FALSE(second_handle.is_blocked_by_bucket());
}

TEST_F(OperationSequencerTest, can_get_sequencing_handle_for_different_ids) {
    auto first_handle = sequencer.try_acquire(default_space(), DocumentId("id:foo:test::abcd"));
    auto second_handle = sequencer.try_acquire(default_space(), DocumentId("id:foo:test::efgh"));
    EXPECT_TRUE(first_handle.valid());
    EXPECT_TRUE(second_handle.valid());
}

TEST_F(OperationSequencerTest, releasing_handle_allows_for_getting_new_handles_for_id) {
    auto first_handle = sequencer.try_acquire(default_space(), DocumentId("id:foo:test::abcd"));
    // Explicit release
    first_handle.release();
    {
        auto second_handle = sequencer.try_acquire(default_space(), DocumentId("id:foo:test::abcd"));
        EXPECT_TRUE(second_handle.valid());
        // Implicit release by scope exit
    }
    auto third_handle = sequencer.try_acquire(default_space(), DocumentId("id:foo:test::abcd"));
    EXPECT_TRUE(third_handle.valid());
}

TEST_F(OperationSequencerTest, cannot_get_handle_for_gid_contained_in_locked_bucket) {
    const auto bucket = document::Bucket(default_space(), document::BucketId(16, 1));
    EXPECT_FALSE(sequencer.is_blocked(bucket));
    auto bucket_handle = sequencer.try_acquire(bucket, "foo");
    EXPECT_TRUE(bucket_handle.valid());
    EXPECT_TRUE(sequencer.is_blocked(bucket));
    auto doc_handle = sequencer.try_acquire(default_space(), DocumentId("id:foo:test:n=1:abcd"));
    EXPECT_FALSE(doc_handle.valid());
    ASSERT_TRUE(doc_handle.is_blocked());
    ASSERT_TRUE(doc_handle.is_blocked_by_bucket());
    EXPECT_TRUE(doc_handle.is_bucket_blocked_with_token("foo"));
    EXPECT_FALSE(doc_handle.is_bucket_blocked_with_token("bar"));
}

TEST_F(OperationSequencerTest, can_get_handle_for_gid_not_contained_in_active_bucket) {
    auto bucket_handle = sequencer.try_acquire(document::Bucket(default_space(), document::BucketId(16, 1)), "foo");
    EXPECT_TRUE(bucket_handle.valid());
    // Note: different sub-bucket than the lock
    auto doc_handle = sequencer.try_acquire(default_space(), DocumentId("id:foo:test:n=2:abcd"));
    EXPECT_TRUE(doc_handle.valid());
}

TEST_F(OperationSequencerTest, releasing_bucket_lock_allows_gid_handles_to_be_acquired) {
    const auto bucket = document::Bucket(default_space(), document::BucketId(16, 1));
    auto bucket_handle = sequencer.try_acquire(bucket, "foo");
    bucket_handle.release();
    auto doc_handle = sequencer.try_acquire(default_space(), DocumentId("id:foo:test:n=1:abcd"));
    EXPECT_TRUE(doc_handle.valid());
    EXPECT_FALSE(sequencer.is_blocked(bucket));
}

TEST_F(OperationSequencerTest, can_get_handle_for_gid_when_locked_bucket_is_in_separate_bucket_space) {
    auto bucket_handle = sequencer.try_acquire(document::Bucket(default_space(), document::BucketId(16, 1)), "foo");
    EXPECT_TRUE(bucket_handle.valid());
    auto doc_handle = sequencer.try_acquire(global_space(), DocumentId("id:foo:test:n=1:abcd"));
    EXPECT_TRUE(doc_handle.valid());
}

TEST_F(OperationSequencerTest, is_blocked_is_bucket_space_aware) {
    auto bucket_handle = sequencer.try_acquire(document::Bucket(default_space(), document::BucketId(16, 1)), "foo");
    EXPECT_FALSE(sequencer.is_blocked(document::Bucket(global_space(), document::BucketId(16, 1))));
}

} // storage::distributor