aboutsummaryrefslogtreecommitdiffstats
path: root/memfilepersistence/src/tests/spi/splitoperationhandlertest.cpp
blob: e1c87dd1cf374dc05a22b50f76d0aad04264b58b (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "memfiletestutils.h"
#include <vespa/document/datatype/documenttype.h>

using document::DocumentType;

namespace storage {
namespace memfile {
namespace {
    spi::LoadType defaultLoadType(0, "default");
}

class SplitOperationHandlerTest : public SingleDiskMemFileTestUtils
{

    void doTestMultiDisk(uint16_t sourceDisk,
                         uint16_t targetDisk0,
                         uint16_t targetDisk1);


    CPPUNIT_TEST_SUITE(SplitOperationHandlerTest);
    CPPUNIT_TEST(testSimple);
    CPPUNIT_TEST(testMultiDisk);
    CPPUNIT_TEST(testMultiDiskNonZeroSourceIndex);
    CPPUNIT_TEST(testExceptionDuringSplittingEvictsAllBuckets);
    CPPUNIT_TEST_SUITE_END();

public:
    void testSimple();
    void testMultiDisk();
    void testMultiDiskNonZeroSourceIndex();
    void testExceptionDuringSplittingEvictsAllBuckets();
};

CPPUNIT_TEST_SUITE_REGISTRATION(SplitOperationHandlerTest);

void
SplitOperationHandlerTest::testSimple()
{
    spi::Context context(defaultLoadType, spi::Priority(0),
                         spi::Trace::TraceLevel(0));
    setupDisks(1);

    for (uint32_t i = 0; i < 100; i++) {
        uint32_t location = 4;
        if (i % 2 == 0) {
            location |= (1 << 16);
        }

        doPut(location, Timestamp(1000 + i));
    }
    flush(document::BucketId(16, 4));

    env()._cache.clear();

    document::BucketId sourceBucket = document::BucketId(16, 4);
    document::BucketId target1 = document::BucketId(17, 4);
    document::BucketId target2 = document::BucketId(17, 4 | (1 << 16));

    SplitOperationHandler handler(env());
    spi::Result result = getPersistenceProvider().split(
            spi::Bucket(sourceBucket, spi::PartitionId(0)),
            spi::Bucket(target1, spi::PartitionId(0)),
            spi::Bucket(target2, spi::PartitionId(0)),
            context);

    env()._cache.clear();

    {
        MemFilePtr file(handler.getMemFile(sourceBucket, 0));
        CPPUNIT_ASSERT_EQUAL(0, (int)file->getSlotCount());
    }

    {
        MemFilePtr file(handler.getMemFile(target1, 0));
        CPPUNIT_ASSERT_EQUAL(50, (int)file->getSlotCount());
        for (uint32_t i = 0; i < file->getSlotCount(); ++i) {
            file->getDocument((*file)[i], ALL);
        }
    }

    {
        MemFilePtr file(handler.getMemFile(target2, 0));
        CPPUNIT_ASSERT_EQUAL(50, (int)file->getSlotCount());
        for (uint32_t i = 0; i < file->getSlotCount(); ++i) {
            file->getDocument((*file)[i], ALL);
        }
    }
}

void
SplitOperationHandlerTest::doTestMultiDisk(uint16_t sourceDisk,
                                           uint16_t targetDisk0,
                                           uint16_t targetDisk1)
{
    spi::Context context(defaultLoadType, spi::Priority(0),
                         spi::Trace::TraceLevel(0));
    setupDisks(3);

    for (uint32_t i = 0; i < 100; i++) {
        uint32_t location = 4;
        if (i % 2 == 0) {
            location |= (1 << 16);
        }

        doPutOnDisk(sourceDisk, location, Timestamp(1000 + i));
    }
    flush(document::BucketId(16, 4));

    env()._cache.clear();

    document::BucketId sourceBucket = document::BucketId(16, 4);
    document::BucketId target1 = document::BucketId(17, 4);
    document::BucketId target2 = document::BucketId(17, 4 | (1 << 16));

    SplitOperationHandler handler(env());
    spi::Result result = getPersistenceProvider().split(
            spi::Bucket(sourceBucket, spi::PartitionId(sourceDisk)),
            spi::Bucket(target1, spi::PartitionId(targetDisk0)),
            spi::Bucket(target2, spi::PartitionId(targetDisk1)),
            context);

    env()._cache.clear();

    {
        MemFilePtr file(handler.getMemFile(sourceBucket, sourceDisk));
        CPPUNIT_ASSERT_EQUAL(0, (int)file->getSlotCount());
    }

    {
        MemFilePtr file(handler.getMemFile(target1, targetDisk0));
        CPPUNIT_ASSERT_EQUAL(50, (int)file->getSlotCount());
        for (uint32_t i = 0; i < file->getSlotCount(); ++i) {
            file->getDocument((*file)[i], ALL);
        }
    }

    {
        MemFilePtr file(handler.getMemFile(target2, targetDisk1));
        CPPUNIT_ASSERT_EQUAL(50, (int)file->getSlotCount());
        for (uint32_t i = 0; i < file->getSlotCount(); ++i) {
            file->getDocument((*file)[i], ALL);
        }
    }
}

void
SplitOperationHandlerTest::testMultiDisk()
{
    doTestMultiDisk(0, 1, 2);
}

void
SplitOperationHandlerTest::testMultiDiskNonZeroSourceIndex()
{
    doTestMultiDisk(1, 2, 0);
}

void
SplitOperationHandlerTest::testExceptionDuringSplittingEvictsAllBuckets()
{
    spi::Context context(defaultLoadType, spi::Priority(0),
                         spi::Trace::TraceLevel(0));
    setupDisks(1);

    for (uint32_t i = 0; i < 100; i++) {
        uint32_t location = 4;
        if (i % 2 == 0) {
            location |= (1 << 16);
        }

        doPut(location, Timestamp(1000 + i));
    }
    flush(document::BucketId(16, 4));

    simulateIoErrorsForSubsequentlyOpenedFiles();

    document::BucketId sourceBucket(16, 4);
    document::BucketId target1(17, 4);
    document::BucketId target2(17, 4 | (1 << 16));

    try {
        SplitOperationHandler handler(env());
        spi::Result result = getPersistenceProvider().split(
                spi::Bucket(sourceBucket, spi::PartitionId(0)),
                spi::Bucket(target1, spi::PartitionId(0)),
                spi::Bucket(target2, spi::PartitionId(0)),
                context);
        CPPUNIT_FAIL("Exception not thrown on flush failure");
    } catch (std::exception&) {
    }

    CPPUNIT_ASSERT(!env()._cache.contains(sourceBucket));
    CPPUNIT_ASSERT(!env()._cache.contains(target1));
    CPPUNIT_ASSERT(!env()._cache.contains(target2));

    unSimulateIoErrorsForSubsequentlyOpenedFiles();

    // Source must not have been deleted
    {
        SplitOperationHandler handler(env());
        MemFilePtr file(handler.getMemFile(sourceBucket, 0));
        CPPUNIT_ASSERT_EQUAL(100, (int)file->getSlotCount());
    }
}

}

}