aboutsummaryrefslogtreecommitdiffstats
path: root/memfilepersistence/src/tests/spi/joinoperationhandlertest.cpp
blob: 7da80a789139a36d667f6ac5e313967147b2d196 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
// 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 JoinOperationHandlerTest : public MemFileTestUtils
{
    CPPUNIT_TEST_SUITE(JoinOperationHandlerTest);
    CPPUNIT_TEST(testSimple);
    CPPUNIT_TEST(testTargetExists);
    CPPUNIT_TEST(testTargetWithOverlap);
    CPPUNIT_TEST(testMultiDisk);
    CPPUNIT_TEST(testMultiDiskFlushed);
    CPPUNIT_TEST(testInternalJoin);
    CPPUNIT_TEST(testInternalJoinDiskFull);
    CPPUNIT_TEST(testTargetIoWriteExceptionEvictsTargetFromCache);
    CPPUNIT_TEST(test1stSourceIoReadExceptionEvictsSourceFromCache);
    CPPUNIT_TEST(test2ndSourceExceptionEvictsExistingTargetFromCache);
    CPPUNIT_TEST_SUITE_END();

public:
    void testSimple();
    void testTargetExists();
    void testTargetWithOverlap();
    void testMultiDisk();
    void testMultiDiskFlushed();
    void testInternalJoin();
    void testInternalJoinDiskFull();
    void testTargetIoWriteExceptionEvictsTargetFromCache();
    void test1stSourceIoReadExceptionEvictsSourceFromCache();
    void test2ndSourceExceptionEvictsExistingTargetFromCache();

    void insertDocumentInBucket(uint64_t location,
                                Timestamp timestamp,
                                document::BucketId bucket);

private:
    void feedSingleDisk();
    void feedMultiDisk();
    std::string getStandardMemFileStatus(uint32_t disk = 0);

    spi::Result doJoin(const document::BucketId to,
                       const document::BucketId from1,
                       const document::BucketId from2);
};

namespace {

document::BucketId TARGET = document::BucketId(15, 4);
document::BucketId SOURCE1 = document::BucketId(16, 4);
document::BucketId SOURCE2 = document::BucketId(16, (uint64_t)4 | ((uint64_t)1 << 15));
}

CPPUNIT_TEST_SUITE_REGISTRATION(JoinOperationHandlerTest);

void
JoinOperationHandlerTest::feedSingleDisk()
{
    for (uint32_t i = 0; i < 100; i++) {
        std::ostringstream ost;
        ost << "userdoc:storage_test:1234:" << i;
        const DocumentType& type(
                *getTypeRepo()->getDocumentType("testdoctype1"));
        document::Document::SP doc(
                new document::Document(type, document::DocumentId(ost.str())));

        document::BucketId bucket(
                getBucketIdFactory().getBucketId(doc->getId()));
        bucket.setUsedBits(33);
        doPut(doc, Timestamp(1000 + i), 0, 33);
        flush(bucket);
    }
}

void
JoinOperationHandlerTest::feedMultiDisk()
{
    for (uint32_t i = 0; i < 100; i += 2) {
        doPutOnDisk(7, 4 | (1 << 15), Timestamp(1000 + i));
    }
    flush(SOURCE2);

    for (uint32_t i = 1; i < 100; i += 2) {
        doPutOnDisk(4, 4, Timestamp(1000 + i));
    }
    flush(SOURCE1);

    {
        MemFilePtr file(getMemFile(SOURCE1, 4));
        CPPUNIT_ASSERT_EQUAL(50, (int)file->getSlotCount());
        CPPUNIT_ASSERT_EQUAL(4, (int)file->getDisk());
    }

    {
        MemFilePtr file(getMemFile(SOURCE2, 7));
        CPPUNIT_ASSERT_EQUAL(50, (int)file->getSlotCount());
        CPPUNIT_ASSERT_EQUAL(7, (int)file->getDisk());
    }
}

std::string
JoinOperationHandlerTest::getStandardMemFileStatus(uint32_t disk)
{
    std::ostringstream ost;

    ost << getMemFileStatus(TARGET, disk) << "\n"
        << getMemFileStatus(SOURCE1, disk ) << "\n"
        << getMemFileStatus(SOURCE2, disk) << "\n";

    return ost.str();
}

void
JoinOperationHandlerTest::insertDocumentInBucket(
        uint64_t location,
        Timestamp timestamp,
        document::BucketId bucket)
{
    Document::SP doc(
            createRandomDocumentAtLocation(
                    location, timestamp.getTime(), 100, 100));
    doPut(doc, bucket, timestamp);
}

spi::Result
JoinOperationHandlerTest::doJoin(const document::BucketId to,
                                 const document::BucketId from1,
                                 const document::BucketId from2)
{
    spi::Context context(defaultLoadType, spi::Priority(0),
                         spi::Trace::TraceLevel(0));
    return getPersistenceProvider().join(
            spi::Bucket(from1, spi::PartitionId(0)),
            spi::Bucket(from2, spi::PartitionId(0)),
            spi::Bucket(to, spi::PartitionId(0)),
            context);
}

void
JoinOperationHandlerTest::testSimple()
{
    setupDisks(1);
    feedSingleDisk();

    {
        MemFilePtr file(getMemFile(document::BucketId(33, 1234)));
        CPPUNIT_ASSERT_EQUAL(50, (int)file->getSlotCount());
    }

    {
        MemFilePtr file(getMemFile(document::BucketId(33, (uint64_t)1234 | ((uint64_t)1 << 32))));
        CPPUNIT_ASSERT_EQUAL(50, (int)file->getSlotCount());
    }

    spi::Result result =
        doJoin(document::BucketId(32, 1234),
               document::BucketId(33, 1234),
               document::BucketId(33, (uint64_t)1234 | ((uint64_t)1 << 32)));

    {
        MemFilePtr file(getMemFile(document::BucketId(32, (uint64_t)1234)));
        CPPUNIT_ASSERT_EQUAL(100, (int)file->getSlotCount());
        CPPUNIT_ASSERT(!file->slotsAltered());
    }
}

void
JoinOperationHandlerTest::testTargetExists()
{
    setupDisks(1);

    for (uint32_t i = 0; i < 100; i += 2) {
        doPut(4 | (1 << 15), Timestamp(1000 + i));
    }
    flush(SOURCE2);

    for (uint32_t i = 1; i < 100; i += 2) {
        doPut(4, Timestamp(1000 + i));
    }
    flush(SOURCE1);

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

        insertDocumentInBucket(location, Timestamp(500 + i), TARGET);
    }
    flush(TARGET);

    doJoin(TARGET, SOURCE1, SOURCE2);

    CPPUNIT_ASSERT_EQUAL(
            std::string(
                    "BucketId(0x3c00000000000004): 200,0\n"
                    "BucketId(0x4000000000000004): 0,0\n"
                    "BucketId(0x4000000000008004): 0,0\n"),
            getStandardMemFileStatus());
}

void
JoinOperationHandlerTest::testTargetWithOverlap()
{
    setupDisks(1);

    for (uint32_t i = 0; i < 100; i += 2) {
        doPut(4 | (1 << 15), Timestamp(1000 + i));
    }
    flush(SOURCE2);

    for (uint32_t i = 1; i < 100; i += 2) {
        doPut(4, Timestamp(1000 + i));
    }
    flush(SOURCE1);

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

        insertDocumentInBucket(location, Timestamp(950 + i), TARGET);
    }
    flush(TARGET);

    doJoin(TARGET, SOURCE1, SOURCE2);

    CPPUNIT_ASSERT_EQUAL(
            std::string(
                    "BucketId(0x3c00000000000004): 150,0\n"
                    "BucketId(0x4000000000000004): 0,0\n"
                    "BucketId(0x4000000000008004): 0,0\n"),
            getStandardMemFileStatus());
}

void
JoinOperationHandlerTest::testMultiDisk()
{
    spi::Context context(defaultLoadType, spi::Priority(0),
                         spi::Trace::TraceLevel(0));
    setupDisks(10);
    feedMultiDisk();

    getPersistenceProvider().join(spi::Bucket(SOURCE2, spi::PartitionId(7)),
                                  spi::Bucket(SOURCE1, spi::PartitionId(4)),
                                  spi::Bucket(TARGET, spi::PartitionId(3)),
                                  context);

    CPPUNIT_ASSERT_EQUAL(
            std::string(
                    "BucketId(0x3c00000000000004): 100,3\n"
                    "BucketId(0x4000000000000004): 0,0\n"
                    "BucketId(0x4000000000008004): 0,0\n"),
            getStandardMemFileStatus());
}

void
JoinOperationHandlerTest::testMultiDiskFlushed()
{
    spi::Context context(defaultLoadType, spi::Priority(0),
                         spi::Trace::TraceLevel(0));
    setupDisks(10);
    feedMultiDisk();

    // Flush everything to disk, to check that we can join even
    // if it's not in cache before.
    env()._cache.flushDirtyEntries();
    env()._cache.clear();

    getPersistenceProvider().join(spi::Bucket(SOURCE2, spi::PartitionId(7)),
                                  spi::Bucket(SOURCE1, spi::PartitionId(4)),
                                  spi::Bucket(TARGET, spi::PartitionId(3)),
                                  context);

    CPPUNIT_ASSERT_EQUAL(
            std::string(
                    "BucketId(0x3c00000000000004): 100,3\n"
                    "BucketId(0x4000000000000004): 0,3\n"
                    "BucketId(0x4000000000008004): 0,3\n"),
            getStandardMemFileStatus(3));
}

void
JoinOperationHandlerTest::testInternalJoin()
{
    spi::Context context(defaultLoadType, spi::Priority(0),
                         spi::Trace::TraceLevel(0));
    setupDisks(10);

    for (uint32_t i = 4; i < 6; i++) {
        for (uint32_t j = 0; j < 10; j++) {
            uint32_t location = 4;
            doPutOnDisk(i, location, Timestamp(i * 1000 + j));
        }
        flush(document::BucketId(16, 4), i);
        env()._cache.clear();
    }

    std::string fileName1 =
        env().calculatePathInDir(SOURCE1, (*env()._mountPoints)[4]);
    std::string fileName2 =
        env().calculatePathInDir(SOURCE1, (*env()._mountPoints)[5]);

    CPPUNIT_ASSERT(vespalib::stat(fileName1).get());
    vespalib::FileInfo::UP file2(vespalib::stat(fileName2));

    CPPUNIT_ASSERT(file2.get());
    CPPUNIT_ASSERT(file2->_size > 0);

    PartitionMonitor* mon = env().getDirectory(5).getPartition().getMonitor();
    // Set disk under 80% full. Over 80%, we shouldn't move buckets to the target.
    mon->setStatOncePolicy();
    mon->overrideRealStat(512, 100000, 50000);
    CPPUNIT_ASSERT(!mon->isFull(0, .80f));

    getPersistenceProvider().join(spi::Bucket(SOURCE1, spi::PartitionId(4)),
                                  spi::Bucket(SOURCE1, spi::PartitionId(4)),
                                  spi::Bucket(SOURCE1, spi::PartitionId(5)),
                                  context);

    env()._cache.clear();

    CPPUNIT_ASSERT(!vespalib::stat(fileName1).get());
    CPPUNIT_ASSERT(vespalib::stat(fileName2).get());
}

void
JoinOperationHandlerTest::testInternalJoinDiskFull()
{
    spi::Context context(defaultLoadType, spi::Priority(0),
                         spi::Trace::TraceLevel(0));
    setupDisks(10);

    for (uint32_t i = 4; i < 6; i++) {
        for (uint32_t j = 0; j < 10; j++) {
            uint32_t location = 4;
            doPutOnDisk(i, location, Timestamp(i * 1000 + j));
        }
        flush(document::BucketId(16, 4), i);
        env()._cache.clear();
    }

    std::string fileName1 =
        env().calculatePathInDir(SOURCE1, (*env()._mountPoints)[4]);
    std::string fileName2 =
        env().calculatePathInDir(SOURCE1, (*env()._mountPoints)[5]);

    CPPUNIT_ASSERT(vespalib::stat(fileName1).get());
    vespalib::FileInfo::UP file2(vespalib::stat(fileName2));

    CPPUNIT_ASSERT(file2.get());
    CPPUNIT_ASSERT(file2->_size > 0);

    PartitionMonitor* mon = env().getDirectory(5).getPartition().getMonitor();
    // Set disk to 81% full. Over 80%, we shouldn't move buckets to the target.
    mon->setStatOncePolicy();
    mon->overrideRealStat(512, 100000, 81000);
    CPPUNIT_ASSERT(!mon->isFull());
    CPPUNIT_ASSERT(mon->isFull(0, .08f));

    spi::Result result =
        getPersistenceProvider().join(spi::Bucket(SOURCE1, spi::PartitionId(4)),
                                      spi::Bucket(SOURCE1, spi::PartitionId(4)),
                                      spi::Bucket(SOURCE1, spi::PartitionId(5)),
                                      context);

    CPPUNIT_ASSERT(result.hasError());
}

void
JoinOperationHandlerTest::testTargetIoWriteExceptionEvictsTargetFromCache()
{
    setupDisks(1);
    feedSingleDisk();

    document::BucketId src1(33, 1234);
    document::BucketId src2(33, 1234ULL | (1ULL << 32));
    document::BucketId target(32, 1234);

    CPPUNIT_ASSERT(env()._cache.contains(src1));
    CPPUNIT_ASSERT(env()._cache.contains(src2));
    CPPUNIT_ASSERT(!env()._cache.contains(target));

    // Reading existing (fully cached) files will go fine, but writing
    // new file will not.
    simulateIoErrorsForSubsequentlyOpenedFiles();

    spi::Result result = doJoin(target, src1, src2);
    CPPUNIT_ASSERT(result.hasError());
    CPPUNIT_ASSERT(result.getErrorMessage().find("A simulated I/O write")
                   != vespalib::string::npos);

    CPPUNIT_ASSERT(!env()._cache.contains(target));
    // NOTE: since we end up renaming src1 -> target during the first
    // iteration of join, src1 will actually be empty. This should not
    // matter since the service layer will query the bucket info for
    // all these afterwards and will thus pick up on this automatically.
    unSimulateIoErrorsForSubsequentlyOpenedFiles();
    {
        MemFilePtr file(getMemFile(src1));
        CPPUNIT_ASSERT_EQUAL(0, (int)file->getSlotCount());
        CPPUNIT_ASSERT(!file->slotsAltered());
    }
    {
        MemFilePtr file(getMemFile(src2));
        CPPUNIT_ASSERT_EQUAL(50, (int)file->getSlotCount());
        CPPUNIT_ASSERT(!file->slotsAltered());
    }
    {
        MemFilePtr file(getMemFile(target));
        // Renamed from src1
        CPPUNIT_ASSERT_EQUAL(50, (int)file->getSlotCount());
        CPPUNIT_ASSERT(!file->slotsAltered());
    }
}

void
JoinOperationHandlerTest::test1stSourceIoReadExceptionEvictsSourceFromCache()
{
    setupDisks(1);
    feedSingleDisk();

    document::BucketId src1(33, 1234);
    document::BucketId src2(33, 1234ULL | (1ULL << 32));
    document::BucketId target(32, 1234);

    env()._cache.clear();
    // Allow for reading in initial metadata so that loadFile itself doesn't
    // fail. This could otherwise cause a false negative since that happens
    // during initial cache lookup on a cache miss, at which point any
    // exception will always stop a file from being added to the cache. Here
    // we want to test the case where a file has been successfully hoisted
    // out of the cache initially.
    simulateIoErrorsForSubsequentlyOpenedFiles(IoErrors().afterReads(1));

    spi::Result result = doJoin(target, src1, src2);
    CPPUNIT_ASSERT(result.hasError());
    CPPUNIT_ASSERT(result.getErrorMessage().find("A simulated I/O read")
                   != vespalib::string::npos);

    CPPUNIT_ASSERT(!env()._cache.contains(src1));
    CPPUNIT_ASSERT(!env()._cache.contains(src2));
    CPPUNIT_ASSERT(!env()._cache.contains(target));
}

/**
 * It must be exception safe for any source bucket to throw an exception during
 * processing. Otherwise the node will core due to cache sanity checks.
 *
 * See VESPA-674 for context. In this scenario, it was not possible to write
 * to the target file when attempting to join in the 2nd source bucket due to
 * the disk fill ratio exceeding configured limits.
 */
void
JoinOperationHandlerTest::test2ndSourceExceptionEvictsExistingTargetFromCache()
{
    setupDisks(1);
    feedSingleDisk();

    constexpr uint64_t location = 1234;

    document::BucketId src1(33, location);
    document::BucketId src2(33, location | (1ULL << 32));
    document::BucketId target(32, location);

    // Ensure target file is _not_ empty so that copySlots is triggered for
    // each source bucket (rather than just renaming the file, which does not
    // invoke the file read/write paths).
    insertDocumentInBucket(location, Timestamp(100000), target);
    flush(target);

    env()._cache.clear();
    // File rewrites are buffered before ever reaching the failure simulation
    // layer, so only 1 actual write is used to flush the target file after
    // the first source file has been processed. Attempting to flush the writes
    // for the second source file should fail with an exception.
    simulateIoErrorsForSubsequentlyOpenedFiles(
            IoErrors().afterReads(INT_MAX).afterWrites(1));

    spi::Result result = doJoin(target, src1, src2);
    CPPUNIT_ASSERT(result.hasError());
    CPPUNIT_ASSERT(result.getErrorMessage().find("A simulated I/O write")
                   != vespalib::string::npos);

    CPPUNIT_ASSERT(!env()._cache.contains(src1));
    CPPUNIT_ASSERT(!env()._cache.contains(src2));
    CPPUNIT_ASSERT(!env()._cache.contains(target));
}

}

}