aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/tests/index/disk_indexes/disk_indexes_test.cpp
blob: c45b5318d6614696884109f575ab98f9c670fdb7 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/searchcorespi/index/disk_indexes.h>
#include <vespa/searchcorespi/index/index_disk_dir.h>
#include <vespa/searchcorespi/index/indexdisklayout.h>
#include <vespa/vespalib/util/size_literals.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <filesystem>
#include <fstream>

namespace {

vespalib::string base_dir("base");

constexpr uint32_t block_size = 4_Ki;

}

namespace searchcorespi::index {

class DiskIndexesTest : public ::testing::Test,
                              public DiskIndexes
{
    IndexDiskLayout _layout;
protected:
    DiskIndexesTest();
    ~DiskIndexesTest();

    static IndexDiskDir get_index_disk_dir(const vespalib::string& dir) {
        return IndexDiskLayout::get_index_disk_dir(dir);
    }

    void assert_transient_size(uint64_t exp, IndexDiskDir index_disk_dir) {
        EXPECT_EQ(exp, get_transient_size(_layout, index_disk_dir));
    }
};

DiskIndexesTest::DiskIndexesTest()
    : ::testing::Test(),
      DiskIndexes(),
      _layout(base_dir)
{
}

DiskIndexesTest::~DiskIndexesTest() = default;

TEST_F(DiskIndexesTest, simple_set_active_works)
{
    EXPECT_FALSE(isActive("index.flush.1"));
    setActive("index.flush.1", 0);
    EXPECT_TRUE(isActive("index.flush.1"));
    notActive("index.flush.1");
    EXPECT_FALSE(isActive("index.flush.1"));
}

TEST_F(DiskIndexesTest, nested_set_active_works)
{
    setActive("index.flush.1", 0);
    setActive("index.flush.1", 0);
    EXPECT_TRUE(isActive("index.flush.1"));
    notActive("index.flush.1");
    EXPECT_TRUE(isActive("index.flush.1"));
    notActive("index.flush.1");
    EXPECT_FALSE(isActive("index.flush.1"));
}

TEST_F(DiskIndexesTest, is_active_returns_false_for_bad_name)
{
    EXPECT_FALSE(isActive("foo/bar/baz"));
    EXPECT_FALSE(isActive("index.flush.0"));
}

TEST_F(DiskIndexesTest, remove_works)
{
    EXPECT_TRUE(remove(IndexDiskDir()));
    auto fusion1 = get_index_disk_dir("index.fusion.1");
    EXPECT_TRUE(remove(fusion1));
    add_not_active(fusion1);
    EXPECT_TRUE(remove(fusion1));
    setActive("index.fusion.1", 0);
    EXPECT_FALSE(remove(fusion1));
    notActive("index.fusion.1");
    EXPECT_TRUE(remove(fusion1));
}

TEST_F(DiskIndexesTest, basic_get_transient_size_works)
{
    /*
     * When starting to use a new fusion index, we have a transient
     * period with two ISearchableIndexCollection instances:
     * - old, containing index.fusion.1 and index.flush.2
     * - new, containing index.fusion.2
     */
    setActive("index.fusion.1", 1000000);
    setActive("index.flush.2", 500000);
    setActive("index.fusion.2", 1200000);
    auto fusion1 = get_index_disk_dir("index.fusion.1");
    auto flush2 = get_index_disk_dir("index.flush.2");
    auto fusion2 = get_index_disk_dir("index.fusion.2");
    {
        /*
         * When using the old index collection, disk space used by
         * index.fusion.2 is considered transient.
         */
        SCOPED_TRACE("index.fusion.1");
        assert_transient_size(1200000, fusion1);
    }
    {
        SCOPED_TRACE("index.flush.2");
        assert_transient_size(0, flush2);
    }
    {
        /*
         * When using the new index collection, disk space used by
         * index.fusion.1 and index.flush.2 is considered transient.
         */
        SCOPED_TRACE("index.fusion.2");
        assert_transient_size(1500000, fusion2);
    }
    notActive("index.fusion.1");
    notActive("index.flush.2");
    {
        /*
         * old index collection removed.
         */
        SCOPED_TRACE("index.fusion.2 after remove of index.fusion.1 and index.flush.1");
        assert_transient_size(0, fusion2);
    }
}

TEST_F(DiskIndexesTest, get_transient_size_during_ongoing_fusion)
{
    /*
     * During ongoing fusion, we have one ISearchableIndexCollection instance:
     * - old, containing index.fusion.1 and index.flush.2
     *
     * Fusion output directory is index.fusion.2
     */
    setActive("index.fusion.1", 1000000);
    setActive("index.flush.2", 500000);
    auto fusion1 = get_index_disk_dir("index.fusion.1");
    auto fusion2 = get_index_disk_dir("index.fusion.2");
    add_not_active(fusion2); // start tracking disk space for fusion output
    {
        /*
         * Fusion not yet started.
         */
        SCOPED_TRACE("dir missing");
        assert_transient_size(0, fusion1);
    }
    auto dir = base_dir + "/index.fusion.2";
    std::filesystem::create_directories(std::filesystem::path(dir));
    {
        /*
         * Fusion started, but no files written yet.
         */
        SCOPED_TRACE("empty dir");
        assert_transient_size(0, fusion1);
    }
    constexpr uint32_t seek_pos = 999999;
    {
        std::string name = dir + "/foo";
        std::ofstream ostr(name, std::ios::binary);
        ostr.seekp(seek_pos);
        ostr.write(" ", 1);
        ostr.flush();
        ostr.close();
    }
    {
        /*
         * Fusion started, one file written.
         */
        SCOPED_TRACE("single file");
        assert_transient_size((seek_pos + block_size) / block_size * block_size, fusion1);
    }
    EXPECT_TRUE(remove(fusion2)); // stop tracking disk space for fusion output
    {
        /*
         * Fusion aborted.
         */
        SCOPED_TRACE("removed");
        assert_transient_size(0, fusion1);
    }
}

}

int
main(int argc, char* argv[])
{
    std::filesystem::remove_all(std::filesystem::path(base_dir));
    ::testing::InitGoogleTest(&argc, argv);
    auto result = RUN_ALL_TESTS();
    std::filesystem::remove_all(std::filesystem::path(base_dir));
    return result;
}