aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/tests/distributor/joinbuckettest.cpp
blob: b79284c77b67f3f06b8a9df591cbfd194d45ec4a (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "dummy_cluster_context.h"
#include <tests/distributor/distributor_stripe_test_util.h>
#include <vespa/document/test/make_document_bucket.h>
#include <vespa/storage/distributor/top_level_distributor.h>
#include <vespa/storage/distributor/operations/idealstate/joinoperation.h>
#include <vespa/storageapi/message/bucketsplitting.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <gmock/gmock.h>

using document::test::makeDocumentBucket;
using namespace ::testing;

namespace storage::distributor {

struct JoinOperationTest : Test, DistributorStripeTestUtil {
    void checkSourceBucketsAndSendReply(
            JoinOperation& op,
            size_t msgIndex,
            const std::vector<document::BucketId>& wantedIds);

    void SetUp() override {
        createLinks();
    };

    void TearDown() override {
        close();
    }
};

TEST_F(JoinOperationTest, simple) {
    auto cfg = make_config();
    cfg->setJoinCount(100);
    cfg->setJoinSize(1000);
    configure_stripe(cfg);

    addNodesToBucketDB(document::BucketId(33, 1), "0=250/50/300");
    addNodesToBucketDB(document::BucketId(33, 0x100000001), "0=300/40/200");

    enable_cluster_state("distributor:1 storage:1");

    JoinOperation op(dummy_cluster_context,
                     BucketAndNodes(makeDocumentBucket(document::BucketId(32, 0)),
                                    toVector<uint16_t>(0)),
                     toVector(document::BucketId(33, 1),
                              document::BucketId(33, 0x100000001)));

    op.setIdealStateManager(&getIdealStateManager());
    op.start(_sender);

    checkSourceBucketsAndSendReply(op, 0, {{33, 1}, {33, 0x100000001}});

    EXPECT_FALSE(getBucket(document::BucketId(33, 0x100000001)).valid());
    EXPECT_FALSE(getBucket(document::BucketId(33, 1)).valid());

    BucketDatabase::Entry entry = getBucket(document::BucketId(32, 0));
    ASSERT_TRUE(entry.valid());
    EXPECT_EQ(0, entry->getNodeRef(0).getNode());
    EXPECT_EQ(api::BucketInfo(666, 90, 500), entry->getNodeRef(0).getBucketInfo());
}

void
JoinOperationTest::checkSourceBucketsAndSendReply(
        JoinOperation& op,
        size_t msgIndex,
        const std::vector<document::BucketId>& wantedIds)
{
    ASSERT_GT(_sender.commands().size(), msgIndex);

    std::shared_ptr<api::StorageCommand> msg(_sender.command(msgIndex));
    ASSERT_EQ(api::MessageType::JOINBUCKETS, msg->getType());

    auto& joinCmd = dynamic_cast<api::JoinBucketsCommand&>(*msg);
    EXPECT_THAT(joinCmd.getSourceBuckets(), ContainerEq(wantedIds));

    std::shared_ptr<api::StorageReply> reply(joinCmd.makeReply());
    auto& sreply = dynamic_cast<api::JoinBucketsReply&>(*reply);
    sreply.setBucketInfo(api::BucketInfo(666, 90, 500));

    op.receive(_sender, reply);
}

/**
 * If the set of buckets kept on nodes is disjoint, send sparse joins (same
 * bucket id used as both source buckets) for those nodes having only one of
 * the buckets.
 */
TEST_F(JoinOperationTest, send_sparse_joins_to_nodes_without_both_source_buckets) {
    auto cfg = make_config();
    cfg->setJoinCount(100);
    cfg->setJoinSize(1000);
    configure_stripe(cfg);

    addNodesToBucketDB(document::BucketId(33, 1), "0=250/50/300,1=250/50/300");
    addNodesToBucketDB(document::BucketId(33, 0x100000001), "0=300/40/200");

    enable_cluster_state("distributor:1 storage:2");

    JoinOperation op(dummy_cluster_context,
                     BucketAndNodes(makeDocumentBucket(document::BucketId(32, 0)),
                                    toVector<uint16_t>(0, 1)),
                     toVector(document::BucketId(33, 1),
                              document::BucketId(33, 0x100000001)));

    op.setIdealStateManager(&getIdealStateManager());
    op.start(_sender);

    ASSERT_NO_FATAL_FAILURE(checkSourceBucketsAndSendReply(op, 0, {{33, 1}, {33, 0x100000001}}));
    ASSERT_NO_FATAL_FAILURE(checkSourceBucketsAndSendReply(op, 1, {{33, 1}, {33, 1}}));
}

TEST_F(JoinOperationTest, cancelled_node_does_not_update_bucket_db) {
    auto cfg = make_config();
    cfg->setJoinCount(100);
    cfg->setJoinSize(1000);
    configure_stripe(cfg);

    addNodesToBucketDB(document::BucketId(33, 1), "0=250/50/300");
    addNodesToBucketDB(document::BucketId(33, 0x100000001), "0=300/40/200");
    enable_cluster_state("distributor:1 storage:1");

    JoinOperation op(dummy_cluster_context,
                     BucketAndNodes(makeDocumentBucket(document::BucketId(32, 0)), toVector<uint16_t>(0)),
                     {document::BucketId(33, 1), document::BucketId(33, 0x100000001)});

    op.setIdealStateManager(&getIdealStateManager());
    op.start(_sender);

    op.cancel(_sender, CancelScope::of_node_subset({0}));

    checkSourceBucketsAndSendReply(op, 0, {{33, 1}, {33, 0x100000001}});

    // DB is not touched, so source buckets remain unchanged and target buckets is not created
    EXPECT_TRUE(getBucket(document::BucketId(33, 0x100000001)).valid());
    EXPECT_TRUE(getBucket(document::BucketId(33, 1)).valid());
    EXPECT_FALSE(getBucket(document::BucketId(32, 0)).valid());
    EXPECT_FALSE(op.ok());
}

}