Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
/***********************************************************************************************************************************
Test Type Performance
 
Test the performance of various types and data structures. Generally speaking, the starting values should be high enough to "blow
up" in terms of execution time if there are performance problems without taking very long if everything is running smoothly.
 
These starting values can then be scaled up for profiling and stress testing as needed. In general we hope to scale to 1000 without
running out of memory on the test systems or taking an undue amount of time. It should be noted that in this context scaling to
1000 is nowhere near turning it up to 11.
***********************************************************************************************************************************/
#include <unistd.h>
 
#include "common/ini.h"
#include "common/io/bufferRead.h"
#include "common/io/bufferWrite.h"
#include "common/io/socket/client.h"
#include "common/stat.h"
#include "common/time.h"
#include "common/type/list.h"
#include "common/type/object.h"
#include "info/manifest/manifest.h"
#include "postgres/version.h"
#include "storage/posix/storage.h"
 
#include "harness/info.h"
#include "harness/storage.h"
 
/***********************************************************************************************************************************
Test sort comparator
***********************************************************************************************************************************/
static int
testComparator(const void *item1, const void *item2)
{
    return LST_COMPARATOR_CMP(*(const int *)item1, *(const int *)item2);
}
 
/***********************************************************************************************************************************
Driver to test manifestNewBuild(). Generates files for a valid-looking PostgreSQL cluster that can be scaled to any size.
***********************************************************************************************************************************/
typedef struct
{
    STORAGE_COMMON_MEMBER;
    uint64_t fileTotal;
} StorageTestManifestNewBuild;
 
STRING_STATIC(TEST_MANIFEST_PATH_USER_STR,                          "test");
 
static StorageInfo
storageTestManifestNewBuildInfo(THIS_VOID, const String *file, StorageInfoLevel level, StorageInterfaceInfoParam param)
{
    (void)thisVoid;
    (void)level;
    (void)param;
 
    StorageInfo result =
    {
        .level = storageInfoLevelDetail,
        .exists = true,
        .type = storageTypePath,
        .mode = 0600,
        .userId = 100,
        .groupId = 100,
        .user = TEST_MANIFEST_PATH_USER_STR,
        .group = TEST_MANIFEST_PATH_USER_STR,
    };
 
    if (strEq(file, STRDEF("/pg")))
    {
        result.type = storageTypePath;
    }
    else
        THROW_FMT(AssertError, "unhandled file info '%s'", strZ(file));
 
    return result;
}
 
static StorageList *
storageTestManifestNewBuildList(THIS_VOID, const String *path, StorageInfoLevel level, StorageInterfaceListParam param)
{
    THIS(StorageTestManifestNewBuild);
 
    (void)path;
    (void)level;
    (void)param;
 
    StorageList *const result = storageLstNew(storageInfoLevelDetail);
 
    MEM_CONTEXT_TEMP_RESET_BEGIN()
    {
        StorageInfo info =
        {
            .level = storageInfoLevelDetail,
            .exists = true,
            .type = storageTypePath,
            .mode = 0700,
            .userId = 100,
            .groupId = 100,
            .user = STRDEF("test"),
            .group = STRDEF("test"),
        };
 
        if (strEq(path, STRDEF("/pg")))
        {
            info.name = STRDEF("base");
            storageLstAdd(result, &info);
        }
        else if (strEq(path, STRDEF("/pg/base")))
        {
            info.name = STRDEF("1000000000");
            storageLstAdd(result, &info);
        }
        else if (strEq(path, STRDEF("/pg/base/1000000000")))
        {
            info.type = storageTypeFile;
            info.size = 8192;
            info.mode = 0600;
            info.timeModified = 1595627966;
 
            // Multiple owners to check performance of the list lookups. Test owner is repeated here to make it more frequent.
            const String *const ownerList[] =
            {
                STRDEF("test"), STRDEF("test"), STRDEF("test"), STRDEF("postgres"), STRDEF("root"),
            };
 
            for (unsigned int fileIdx = 0; fileIdx < this->fileTotal; fileIdx++)
            {
                info.name = strNewFmt("%u", 1000000000 + fileIdx);
                info.user = ownerList[fileIdx % LENGTH_OF(ownerList)];
                info.group = ownerList[fileIdx % LENGTH_OF(ownerList)];
                storageLstAdd(result, &info);
                MEM_CONTEXT_TEMP_RESET(10000);
            }
        }
        else
            THROW_FMT(AssertError, "unhandled file list info '%s'", strZ(path));
    }
    MEM_CONTEXT_TEMP_END();
 
    return result;
}
 
/***********************************************************************************************************************************
Test Run
***********************************************************************************************************************************/
static void
testRun(void)
{
    FUNCTION_HARNESS_VOID();
 
    // *****************************************************************************************************************************
    if (testBegin("lstFind()"))
    {
        ASSERT(TEST_SCALE <= 10000);
        int testMax = 100000 * (int)TEST_SCALE;
 
        // Generate a large list of values (use int instead of string so there fewer allocations)
        List *list = lstNewP(sizeof(int), .comparator = testComparator);
 
        for (int listIdx = 0; listIdx < testMax; listIdx++)
            lstAdd(list, &listIdx);
 
        ASSERT(lstSize(list) == (unsigned int)testMax);
 
        TEST_LOG_FMT("generated %d item list", testMax);
 
        // Search for all values with an ascending sort
        lstSort(list, sortOrderAsc);
 
        TimeMSec timeBegin = timeMSec();
 
        for (int listIdx = 0; listIdx < testMax; listIdx++)
            ASSERT(*(int *)lstFind(list, &listIdx) == listIdx);
 
        TEST_LOG_FMT("asc search completed in %ums", (unsigned int)(timeMSec() - timeBegin));
 
        // Search for all values with an descending sort
        lstSort(list, sortOrderDesc);
 
        timeBegin = timeMSec();
 
        for (int listIdx = 0; listIdx < testMax; listIdx++)
            ASSERT(*(int *)lstFind(list, &listIdx) == listIdx);
 
        TEST_LOG_FMT("desc search completed in %ums", (unsigned int)(timeMSec() - timeBegin));
    }
 
    // *****************************************************************************************************************************
    if (testBegin("lstRemoveIdx()"))
    {
        ASSERT(TEST_SCALE <= 10000);
        uint64_t testMax = (uint64_t)1000000 * TEST_SCALE;
 
        // Generate a large list of values (use int instead of string so there fewer allocations)
        List *list = lstNewP(sizeof(int));
 
        for (uint64_t listIdx = 0; listIdx < testMax; listIdx++)
            lstAdd(list, &listIdx);
 
        ASSERT(lstSize(list) == (unsigned int)testMax);
 
        TEST_LOG_FMT("generated %" PRIu64 " item list", testMax);
 
        // Remove all values from index 0
        TimeMSec timeBegin = timeMSec();
 
        for (uint64_t listIdx = 0; listIdx < testMax; listIdx++)
            lstRemoveIdx(list, 0);
 
        TEST_LOG_FMT("remove completed in %ums", (unsigned int)(timeMSec() - timeBegin));
 
        ASSERT(lstEmpty(list));
    }
 
    // *****************************************************************************************************************************
    if (testBegin("iniValueNext()"))
    {
        ASSERT(TEST_SCALE <= 10000);
 
        String *iniStr = strCatZ(strNew(), "[section1]\n");
        unsigned int iniMax = 100000 * (unsigned int)TEST_SCALE;
 
        for (unsigned int keyIdx = 0; keyIdx < iniMax; keyIdx++)
            strCatFmt(iniStr, "key%u=\"value%u\"\n", keyIdx, keyIdx);
 
        TEST_LOG_FMT("ini size = %s, keys = %u", strZ(strSizeFormat(strSize(iniStr))), iniMax);
 
        TimeMSec timeBegin = timeMSec();
        Ini *ini = iniNewP(ioBufferReadNew(BUFSTR(iniStr)), .strict = true);
 
        unsigned int iniTotal = 0;
        const IniValue *value = iniValueNext(ini);
 
        while (value != NULL)
        {
            iniTotal++;
            value = iniValueNext(ini);
        }
 
        TEST_RESULT_INT(iniTotal, iniMax, "check ini value total");
        TEST_LOG_FMT("parse completed in %ums", (unsigned int)(timeMSec() - timeBegin));
    }
 
    // Build/load/save a larger manifest to test performance and memory usage. The default sizing is for a "typical" large cluster
    // but this can be scaled to test larger cluster sizes.
    // *****************************************************************************************************************************
    if (testBegin("manifestNewBuild()/manifestNewLoad()/manifestSave()"))
    {
        ASSERT(TEST_SCALE <= 1000000);
 
        // Create a storage driver to test manifest build with an arbitrary number of files
        StorageTestManifestNewBuild *driver = NULL;
 
        OBJ_NEW_BASE_BEGIN(StorageTestManifestNewBuild, .childQty = MEM_CONTEXT_QTY_MAX)
        {
            driver = OBJ_NEW_ALLOC();
 
            *driver = (StorageTestManifestNewBuild)
            {
                .interface = hrnStorageInterfaceDummy,
                .fileTotal = 100000 * (unsigned int)TEST_SCALE,
            };
        }
        OBJ_NEW_END();
 
        driver->interface.info = storageTestManifestNewBuildInfo;
        driver->interface.list = storageTestManifestNewBuildList;
 
        const Storage *const storagePg = storageNew(
            strIdFromZ("test"), STRDEF("/pg"), 0, 0, false, 0, NULL, driver, driver->interface);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("build manifest");
 
        MemContext *testContext = memContextNewP("test", .childQty = MEM_CONTEXT_QTY_MAX);
        memContextKeep();
        Manifest *manifest = NULL;
        TimeMSec timeBegin = timeMSec();
 
        MEM_CONTEXT_BEGIN(testContext)
        {
            TEST_ASSIGN(
                manifest, manifestNewBuild(
                    storagePg, PG_VERSION_15, 999999999, REPOSITORY_FORMAT_DEFAULT, 0, false, false, false, false, NULL, NULL,
                    NULL),
                "build files");
        }
        MEM_CONTEXT_END();
 
        TEST_LOG_FMT("completed in %ums", (unsigned int)(timeMSec() - timeBegin));
        // TEST_LOG_FMT("memory used %zu", memContextSize(testContext));
 
        TEST_RESULT_UINT(manifestFileTotal(manifest), driver->fileTotal, "   check file total");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("save manifest");
 
        Buffer *contentSave = bufNew(0);
        timeBegin = timeMSec();
 
        manifestSave(manifest, ioBufferWriteNew(contentSave));
 
        TEST_LOG_FMT("completed in %ums", (unsigned int)(timeMSec() - timeBegin));
 
        memContextFree(testContext);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("load manifest");
 
        testContext = memContextNewP("test", .childQty = MEM_CONTEXT_QTY_MAX);
        memContextKeep();
        timeBegin = timeMSec();
 
        MEM_CONTEXT_BEGIN(testContext)
        {
            manifest = manifestNewLoad(ioBufferReadNew(contentSave), cipherSpecNewNone());
        }
        MEM_CONTEXT_END();
 
        TEST_LOG_FMT("completed in %ums", (unsigned int)(timeMSec() - timeBegin));
        // TEST_LOG_FMT("memory used %zu", memContextSize(testContext));
 
        TEST_RESULT_UINT(manifestFileTotal(manifest), driver->fileTotal, "   check file total");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("find all files");
 
        timeBegin = timeMSec();
 
        for (unsigned int fileIdx = 0; fileIdx < manifestFileTotal(manifest); fileIdx++)
        {
            const ManifestFile file = manifestFile(manifest, fileIdx);
            ASSERT(strEq(file.name, manifestFileFind(manifest, file.name).name));
        }
 
        TEST_LOG_FMT("completed in %ums", (unsigned int)(timeMSec() - timeBegin));
    }
 
    // Make sure statistics collector performs well
    // *****************************************************************************************************************************
    if (testBegin("statistics collector"))
    {
        ASSERT(TEST_SCALE <= 1000000);
 
        // Setup a list of stats to use for testing
        #define TEST_STAT_TOTAL 100
        String *statList[TEST_STAT_TOTAL];
 
        for (unsigned int statIdx = 0; statIdx < TEST_STAT_TOTAL; statIdx++)
            statList[statIdx] = strNewFmt("STAT%u", statIdx);
 
        uint64_t runTotal = (uint64_t)TEST_SCALE * (uint64_t)100000;
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE_FMT("update %d stats %" PRIu64 " times", TEST_STAT_TOTAL, runTotal);
 
        TimeMSec timeBegin = timeMSec();
 
        for (uint64_t runIdx = 0; runIdx < runTotal; runIdx++)
        {
            for (unsigned int statIdx = 0; statIdx < TEST_STAT_TOTAL; statIdx++)
                statInc(statList[statIdx]);
        }
 
        TEST_LOG_FMT("completed in %ums", (unsigned int)(timeMSec() - timeBegin));
    }
 
    // *****************************************************************************************************************************
    if (testBegin("SocketClient"))
    {
        // This test must be done here because the problem with variables being clobbered after a long jump is only present in
        // optimized builds, so the unit test will not notice if the volatile keyword goes missing in sckClientOpen(). Since the
        // performance tests are built with optimization is it more likely to be caught here.
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("create socket with error to check for leaks");
 
        const Storage *const storageFd = storagePosixNewP(strNewFmt("/proc/%d/fd", getpid()));
        unsigned int fdBefore = strLstSize(storageListP(storageFd, NULL));
 
        TEST_ERROR(
            ioClientOpen(sckClientNew(STRDEF("172.31.255.255"), 7777, 0, 0)), HostConnectError,
            "timeout connecting to '172.31.255.255:7777'");
 
        TEST_RESULT_UINT(strLstSize(storageListP(storageFd, NULL)), fdBefore, "socket was freed");
    }
 
    FUNCTION_HARNESS_RETURN_VOID();
}