summaryrefslogtreecommitdiffstats
path: root/memfilepersistence/src/tests/init/filescannertest.cpp
blob: 8b49a21dad03ca6acbe37e7878a88aaa49e09af4 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/fastos/fastos.h>
#include <vespa/document/bucket/bucketid.h>
#include <iomanip>
#include <vespa/memfilepersistence/device/devicemanager.h>
#include <vespa/memfilepersistence/init/filescanner.h>
#include <vespa/memfilepersistence/mapper/bucketdirectorymapper.h>
#include <vespa/storageframework/defaultimplementation/component/componentregisterimpl.h>
#include <vespa/storageframework/defaultimplementation/clock/realclock.h>
#include <vespa/vdslib/state/nodestate.h>
#include <vespa/vdstestlib/cppunit/macros.h>
#include <vespa/vespalib/io/fileutil.h>
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/util/random.h>
#include <sys/errno.h>

namespace storage {
namespace memfile {

struct FileScannerTest : public CppUnit::TestFixture {
    struct TestParameters {
        uint32_t filesPerDisk;
        uint32_t diskCount;
        uint32_t bucketSplitBits;
        uint32_t dirLevels;
        uint32_t dirSpread;
        uint32_t parts;
        std::set<uint32_t> disksDown;
        bool diskDownWithBrokenSymlink;
        bool bucketWrongDir;
        bool bucketMultipleDirs;
        bool bucketMultipleDisks;
        bool addTemporaryFiles;
        bool addAlienFiles;
        bool dirWithNoListPermission;
        bool dirWithNoWritePermission;
        bool dirWithNoExecutePermission;
        bool fileWithNoReadPermission;
        bool fileWithNoWritePermission;

        TestParameters()
            : filesPerDisk(10), diskCount(5), bucketSplitBits(20),
              dirLevels(1), dirSpread(16), parts(1), disksDown(),
              diskDownWithBrokenSymlink(false),
              bucketWrongDir(false), bucketMultipleDirs(false),
              bucketMultipleDisks(false),
              addTemporaryFiles(false), addAlienFiles(false),
              dirWithNoListPermission(false),
              dirWithNoWritePermission(false),
              dirWithNoExecutePermission(false),
              fileWithNoReadPermission(false),
              fileWithNoWritePermission(false) {}
        void addAllComplexities() {
            disksDown.insert(0);
            disksDown.insert(2);
            disksDown.insert(4);
            bucketWrongDir = true;
            bucketMultipleDirs = true;
            bucketMultipleDisks = true;
            parts = 7;
            addTemporaryFiles = true;
            addAlienFiles = true;
            dirWithNoWritePermission = true;
            fileWithNoWritePermission = true;
            fileWithNoReadPermission = true;
        }
    };

    void testNormalUsage() {
        TestParameters params;
        runTest(params);
    }
    void testMultipleParts() {
        TestParameters params;
        params.parts = 3;
        runTest(params);
    }
    void testBucketInWrongDirectory() {
        TestParameters params;
        params.bucketWrongDir = true;
        runTest(params);
    }
    void testBucketInMultipleDirectories() {
        TestParameters params;
        params.bucketMultipleDirs = true;
        runTest(params);
    }
    void testZeroDirLevel() {
        TestParameters params;
        params.dirLevels = 0;
        runTest(params);
    }
    void testSeveralDirLevels() {
        TestParameters params;
        params.dirLevels = 3;
        runTest(params);
    }
    void testNonStandardDirSpread() {
        TestParameters params;
        params.dirSpread = 63;
        runTest(params);
    }
    void testDiskDown() {
        TestParameters params;
        params.disksDown.insert(1);
        runTest(params);
    }
    void testDiskDownBrokenSymlink() {
        TestParameters params;
        params.disksDown.insert(1);
        params.disksDown.insert(3);
        params.diskDownWithBrokenSymlink = true;
        runTest(params);
    }
    void testRemoveTemporaryFile() {
        TestParameters params;
        params.addTemporaryFiles = true;
        runTest(params);
    }
    void testAlienFile() {
        TestParameters params;
        params.addAlienFiles = true;
        runTest(params);
    }
    void testUnlistableDirectory() {
        TestParameters params;
        params.dirWithNoListPermission = true;
        runTest(params);
    }
    void testDirWithNoWritePermission() {
        TestParameters params;
        params.dirWithNoWritePermission = true;
        runTest(params);
    }
    void testDirWithNoExecutePermission() {
        TestParameters params;
        params.dirWithNoWritePermission = true;
        runTest(params);
    }
    void testFileWithNoReadPermission() {
        TestParameters params;
        params.bucketWrongDir = true;
        params.fileWithNoReadPermission = true;
        runTest(params);
    }
    void testFileWithNoWritePermission() {
        TestParameters params;
        params.bucketWrongDir = true;
        params.fileWithNoWritePermission = true;
        runTest(params);
    }
    void testAllFailuresCombined() {
        TestParameters params;
        params.addAllComplexities();
        runTest(params);
    }

    CPPUNIT_TEST_SUITE(FileScannerTest);
    CPPUNIT_TEST(testNormalUsage);
    CPPUNIT_TEST(testMultipleParts);
    CPPUNIT_TEST(testBucketInWrongDirectory);
    CPPUNIT_TEST(testBucketInMultipleDirectories);
    CPPUNIT_TEST(testZeroDirLevel);
    CPPUNIT_TEST(testSeveralDirLevels);
    CPPUNIT_TEST(testNonStandardDirSpread);
    CPPUNIT_TEST(testDiskDown);
    CPPUNIT_TEST(testDiskDownBrokenSymlink);
    CPPUNIT_TEST(testRemoveTemporaryFile);
    CPPUNIT_TEST(testAlienFile);
    CPPUNIT_TEST(testUnlistableDirectory);
    CPPUNIT_TEST(testDirWithNoWritePermission);
    CPPUNIT_TEST(testDirWithNoExecutePermission);
    CPPUNIT_TEST(testFileWithNoReadPermission);
    CPPUNIT_TEST(testFileWithNoWritePermission);
    CPPUNIT_TEST(testAllFailuresCombined);
    CPPUNIT_TEST_SUITE_END();

    // Actual implementation of the tests.

    /** Run a console command and fail test if it fails. */
    void run(std::string cmd);

    /** Struct containing metadata for a single bucket. */
    struct BucketData {
        document::BucketId bucket;
        uint32_t disk;
        std::vector<uint32_t> directory;
        bool shouldExist; // Set to false for buckets that won't exist due to
                          // some failure.

        BucketData() : shouldExist(true) {}

        bool sameDir(BucketData& other) const {
            return (disk == other.disk && directory == other.directory);
        }
    };

    /**
     * Create an overview of the buckets we're gonna use in the test.
     * (Without any failures introduced)
     */
    std::vector<BucketData> createBuckets(const TestParameters& params);

    /**
     * Create the data in the bucket map and introduce the failures specified
     * in the test. Mark buckets in bucket list that won't exist due to the
     * failures so we know how to verify result of test.
     */
    void createData(const TestParameters&, std::vector<BucketData>& buckets,
                    std::vector<std::string>& tempFiles,
                    std::vector<std::string>& alienFiles);

    /**
     * Run a test with a given set of parameters, calling createData to set up
     * the data, and then using a file scanner to actually list the files.
     */
    void runTest(const TestParameters&);

};

CPPUNIT_TEST_SUITE_REGISTRATION(FileScannerTest);

void
FileScannerTest::run(std::string cmd)
{
    int result = system(cmd.c_str());
    if (result != 0) {
        CPPUNIT_FAIL("Failed to run command '" + cmd + "'.");
    }
}

std::vector<FileScannerTest::BucketData>
FileScannerTest::createBuckets(const TestParameters& params)
{
    std::vector<BucketData> buckets;
    BucketDirectoryMapper dirMapper(params.dirLevels, params.dirSpread);
    for (uint32_t i=0; i<params.diskCount; ++i) {
        if (params.disksDown.find(i) != params.disksDown.end()) {
            continue;
        }
        for (uint32_t j=0; j<params.filesPerDisk; ++j) {
            BucketData data;
            data.bucket = document::BucketId(params.bucketSplitBits,
                                             params.filesPerDisk * i + j);
            data.disk = i;
            data.directory = dirMapper.getPath(data.bucket);
            buckets.push_back(data);
        }
    }
    return buckets;
}

void
FileScannerTest::createData(const TestParameters& params,
                            std::vector<BucketData>& buckets,
                            std::vector<std::string>& tempFiles,
                            std::vector<std::string>& alienFiles)
{
    if (params.bucketWrongDir) {
        CPPUNIT_ASSERT(params.dirLevels > 0);
        buckets[0].directory[0] = (buckets[0].directory[0] + 1)
                                % params.dirSpread;
    }
    if (params.bucketMultipleDirs) {
        CPPUNIT_ASSERT(params.dirLevels > 0);
        BucketData copy(buckets[1]);
        copy.directory[0] = (buckets[1].directory[0] + 1) % params.dirSpread;
        buckets.push_back(copy);
    }
    if (params.bucketMultipleDisks && params.dirLevels > 0) {
        BucketData copy(buckets[2]);
        uint32_t disk = 0;
        for (; disk<params.diskCount; ++disk) {
            if (disk == copy.disk) continue;
            if (params.disksDown.find(disk) == params.disksDown.end()) break;
        }
        CPPUNIT_ASSERT(disk < params.diskCount);
        copy.disk = disk;
        buckets.push_back(copy);
    }

    run("mkdir -p vdsroot");
    run("chmod -R a+rwx vdsroot");
    run("rm -rf vdsroot");
    run("mkdir -p vdsroot/disks");
    vespalib::RandomGen randomizer;
    uint32_t diskToHaveBrokenSymlink = (params.disksDown.empty()
            ? 0 : randomizer.nextUint32(0, params.disksDown.size()));
    uint32_t downIndex = 0;
    for (uint32_t i=0; i<params.diskCount; ++i) {
        if (params.disksDown.find(i) != params.disksDown.end()) {
            if (downIndex++ == diskToHaveBrokenSymlink
                && params.diskDownWithBrokenSymlink)
            {
                std::ostringstream path;
                path << "vdsroot/disks/d" << i;
                run("ln -s /non-existing-dir " + path.str());
            }
        } else {
            std::ostringstream path;
            path << "vdsroot/disks/d" << i;
            run("mkdir -p " + path.str());
            std::ofstream of((path.str() + "/chunkinfo").c_str());
            of << "#chunkinfo\n" << i << "\n" << params.diskCount << "\n";
        }
    }
    for (uint32_t i=0; i<buckets.size(); ++i) {
        if (!buckets[i].shouldExist) continue;
        std::ostringstream path;
        path << "vdsroot/disks/d" << buckets[i].disk << std::hex;
        for (uint32_t j=0; j<buckets[i].directory.size(); ++j) {
            path << '/' << std::setw(4) << std::setfill('0')
                 << buckets[i].directory[j];
        }
        run("mkdir -p " + path.str());
        if (params.dirWithNoListPermission && i == 8) {
            run("chmod a-r " + path.str());
                // Scanner will abort with exception, so we don't really know
                // how many docs will not be found due to this.
            continue;
        }
        if (params.dirWithNoExecutePermission && i == 9) {
            run("chmod a-x " + path.str());
                // Scanner will abort with exception, so we don't really know
                // how many docs will not be found due to this.
            continue;
        }
        path << '/' << std::setw(16) << std::setfill('0')
             << buckets[i].bucket.getId() << ".0";
        run("touch " + path.str());
        if (params.addTemporaryFiles && i == 4) {
            run("touch " + path.str() + ".tmp");
            tempFiles.push_back(path.str() + ".tmp");
        }
        if (params.addAlienFiles && i == 6) {
            run("touch " + path.str() + ".alien");
            alienFiles.push_back(path.str() + ".alien");
        }
        if (params.fileWithNoWritePermission && i == 0) {
            // Overlapping with wrong dir so it would want to move file
            run("chmod a-w " + path.str());
        }
        if (params.fileWithNoReadPermission && i == 0) {
            // Overlapping with wrong dir so it would want to move file
            run("chmod a-r " + path.str());
        }
        if (params.dirWithNoWritePermission && i == 9) {
            run("chmod a-w " + path.str());
        }
    }
}

namespace {
    struct BucketDataFound {
        uint16_t _disk;
        bool _checked;

        BucketDataFound() : _disk(65535), _checked(false) {}
        BucketDataFound(uint32_t disk) : _disk(disk), _checked(false) {}
    };
}

void
FileScannerTest::runTest(const TestParameters& params)
{
    std::vector<BucketData> buckets(createBuckets(params));
    std::vector<std::string> tempFiles;
    std::vector<std::string> alienFiles;
    createData(params, buckets, tempFiles, alienFiles);

    framework::defaultimplementation::RealClock clock;
    framework::defaultimplementation::ComponentRegisterImpl compReg;
    compReg.setClock(clock);

    MountPointList mountPoints("./vdsroot",
                               std::vector<vespalib::string>(),
                               vespalib::LinkedPtr<DeviceManager>(
                                       new DeviceManager(
                                               DeviceMapper::UP(new SimpleDeviceMapper),
                                               clock)));
    mountPoints.init(params.diskCount);

    FileScanner scanner(compReg, mountPoints,
                        params.dirLevels, params.dirSpread);
    std::map<document::BucketId, BucketDataFound> foundBuckets;
    uint32_t extraBucketsSameDisk = 0;
    uint32_t extraBucketsOtherDisk = 0;
    for (uint32_t j=0; j<params.diskCount; ++j) {
        // std::cerr << "Disk " << j << "\n";
        if (params.disksDown.find(j) != params.disksDown.end()) continue;
        for (uint32_t i=0; i<params.parts; ++i) {
           document::BucketId::List bucketList;
            try{
                scanner.buildBucketList(bucketList, j, i, params.parts);
                for (uint32_t k=0; k<bucketList.size(); ++k) {
                    if (foundBuckets.find(bucketList[k]) != foundBuckets.end())
                    {
                        if (j == foundBuckets[bucketList[k]]._disk) {
                            ++extraBucketsSameDisk;
                        } else {
                            ++extraBucketsOtherDisk;
                        }
//                        std::cerr << "Bucket " << bucketList[k]
//                                  << " on disk " << j << " is already found on disk "
//                                  << foundBuckets[bucketList[k]]._disk << ".\n";
                    }
                    foundBuckets[bucketList[k]] = BucketDataFound(j);
                }
            } catch (vespalib::IoException& e) {
                if (!(params.dirWithNoListPermission
                        && e.getType() == vespalib::IoException::NO_PERMISSION))
                {
                    throw;
                }
            }
        }
    }
    std::vector<BucketData> notFound;
    std::vector<BucketData> wasFound;
    std::vector<BucketDataFound> foundNonExisting;
        // Verify that found buckets match buckets expected.
    for (uint32_t i=0; i<buckets.size(); ++i) {
        std::map<document::BucketId, BucketDataFound>::iterator found(
                foundBuckets.find(buckets[i].bucket));
        if (buckets[i].shouldExist && found == foundBuckets.end()) {
            notFound.push_back(buckets[i]);
        } else if (!buckets[i].shouldExist && found != foundBuckets.end()) {
            wasFound.push_back(buckets[i]);
        }
        if (found != foundBuckets.end()) { found->second._checked = true; }
    }
    for (std::map<document::BucketId, BucketDataFound>::iterator it
            = foundBuckets.begin(); it != foundBuckets.end(); ++it)
    {
        if (!it->second._checked) {
            foundNonExisting.push_back(it->second);
        }
    }
    if (params.dirWithNoListPermission) {
        CPPUNIT_ASSERT(!notFound.empty());
    } else if (!notFound.empty()) {
        std::ostringstream ost;
        ost << "Failed to find " << notFound.size() << " of "
            << buckets.size() << " buckets. Including buckets:";
        for (uint32_t i=0; i<5 && i<notFound.size(); ++i) {
            ost << " " << notFound[i].bucket;
        }
        CPPUNIT_FAIL(ost.str());
    }
    CPPUNIT_ASSERT(wasFound.empty());
    CPPUNIT_ASSERT(foundNonExisting.empty());
    if (params.bucketMultipleDirs) {
        // TODO: Test something else here? This is not correct test, as when
        // there are two buckets on the same disk, one of them will be ignored by
        // the bucket lister.
        // CPPUNIT_ASSERT_EQUAL(1u, extraBucketsSameDisk);
    } else {
        CPPUNIT_ASSERT_EQUAL(0u, extraBucketsSameDisk);
    }
    if (params.bucketMultipleDisks) {
        CPPUNIT_ASSERT_EQUAL(1u, extraBucketsOtherDisk);
    } else {
        CPPUNIT_ASSERT_EQUAL(0u, extraBucketsOtherDisk);
    }
    if (params.addTemporaryFiles) {
        CPPUNIT_ASSERT_EQUAL(
                1, int(scanner.getMetrics()._temporaryFilesDeleted.getValue()));
    } else {
        CPPUNIT_ASSERT_EQUAL(
                0, int(scanner.getMetrics()._temporaryFilesDeleted.getValue()));
    }
    if (params.addAlienFiles) {
        CPPUNIT_ASSERT_EQUAL(
                1, int(scanner.getMetrics()._alienFileCounter.getValue()));
    } else {
        CPPUNIT_ASSERT_EQUAL(
                0, int(scanner.getMetrics()._alienFileCounter.getValue()));
    }
        // We automatically delete temporary files (created by VDS, indicating
        // an operation that only half finished.
    for (uint32_t i=0; i<tempFiles.size(); ++i) {
        CPPUNIT_ASSERT_MSG(tempFiles[i], !vespalib::fileExists(tempFiles[i]));
    }
        // We don't automatically delete alien files
    for (uint32_t i=0; i<alienFiles.size(); ++i) {
        CPPUNIT_ASSERT_MSG(alienFiles[i], vespalib::fileExists(alienFiles[i]));
    }
}

} // memfile
} // storage