Исходный код СУБД 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
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/***********************************************************************************************************************************
Protocol Server
***********************************************************************************************************************************/
#include <build.h>
 
#include <string.h>
 
#include "common/debug.h"
#include "common/error/retry.h"
#include "common/log.h"
#include "common/time.h"
#include "common/type/json.h"
#include "common/type/keyValue.h"
#include "common/type/list.h"
#include "protocol/helper.h"
#include "protocol/server.h"
#include "version.h"
 
/***********************************************************************************************************************************
Object type
***********************************************************************************************************************************/
struct ProtocolServer
{
    IoRead *read;                                                   // Read interface
    IoWrite *write;                                                 // Write interface
    const String *name;                                             // Name displayed in logging
    List *sessionList;                                              // List of active sessions
    uint64_t sessionId;                                             // Current session being processed
};
 
struct ProtocolServerResult
{
    void *sessionData;                                              // Session data
    PackWrite *data;                                                // Result data
    size_t extra;                                                   // Extra bytes for initializing data
    bool close;                                                     // Close session?
};
 
// Track server sessions
typedef struct ProtocolServerSession
{
    uint64_t id;                                                    // Session id
    void *data;                                                     // Data for the session
} ProtocolServerSession;
 
/**********************************************************************************************************************************/
FN_EXTERN ProtocolServer *
protocolServerNew(const String *const name, const String *const service, IoRead *const read, IoWrite *const write)
{
    FUNCTION_LOG_BEGIN(logLevelTrace);
        FUNCTION_LOG_PARAM(STRING, name);
        FUNCTION_LOG_PARAM(STRING, service);
        FUNCTION_LOG_PARAM(IO_READ, read);
        FUNCTION_LOG_PARAM(IO_WRITE, write);
    FUNCTION_LOG_END();
 
    ASSERT(name != NULL);
    ASSERT(read != NULL);
    ASSERT(write != NULL);
 
    OBJ_NEW_BEGIN(ProtocolServer, .childQty = MEM_CONTEXT_QTY_MAX)
    {
        *this = (ProtocolServer)
        {
            .read = read,
            .write = write,
            .name = strDup(name),
            .sessionList = lstNewP(sizeof(ProtocolServerSession)),
        };
 
        // Send the protocol greeting
        MEM_CONTEXT_TEMP_BEGIN()
        {
            JsonWrite *const json = jsonWriteObjectBegin(jsonWriteNewP());
 
            jsonWriteZ(jsonWriteKeyStrId(json, PROTOCOL_GREETING_NAME), PROJECT_NAME);
            jsonWriteStr(jsonWriteKeyStrId(json, PROTOCOL_GREETING_SERVICE), service);
            jsonWriteZ(jsonWriteKeyStrId(json, PROTOCOL_GREETING_VERSION), PROJECT_VERSION);
 
            ioWriteStrLine(this->write, jsonWriteResult(jsonWriteObjectEnd(json)));
            ioWriteFlush(this->write);
        }
        MEM_CONTEXT_TEMP_END();
    }
    OBJ_NEW_END();
 
    FUNCTION_LOG_RETURN(PROTOCOL_SERVER, this);
}
 
/**********************************************************************************************************************************/
FN_EXTERN void
protocolServerResponse(ProtocolServer *const this, const ProtocolServerResponseParam param)
{
    FUNCTION_TEST_BEGIN();
        FUNCTION_TEST_PARAM(PROTOCOL_SERVER, this);
        FUNCTION_TEST_PARAM(ENUM, param.type);
        FUNCTION_TEST_PARAM(BOOL, param.close);
        FUNCTION_TEST_PARAM(PACK_WRITE, param.data);
    FUNCTION_TEST_END();
 
    ASSERT(this != NULL);
 
    MEM_CONTEXT_TEMP_BEGIN()
    {
        // End the pack
        if (param.data != NULL)
            pckWriteEndP(param.data);
 
        // Write the result
        PackWrite *const resultMessage = pckWriteNewIo(this->write);
 
        pckWriteU64P(resultMessage, this->sessionId);
        pckWriteU32P(resultMessage, param.type, .defaultWrite = true);
        pckWriteBoolP(resultMessage, param.close);
        pckWritePackP(resultMessage, pckWriteResult(param.data));
        pckWriteEndP(resultMessage);
        ioWriteFlush(this->write);
    }
    MEM_CONTEXT_TEMP_END();
 
    FUNCTION_TEST_RETURN_VOID();
}
 
/**********************************************************************************************************************************/
FN_EXTERN void
protocolServerError(ProtocolServer *this, const int code, const String *const message, const String *const stack)
{
    FUNCTION_LOG_BEGIN(logLevelDebug);
        FUNCTION_LOG_PARAM(PROTOCOL_SERVER, this);
        FUNCTION_LOG_PARAM(INT, code);
        FUNCTION_LOG_PARAM(STRING, message);
        FUNCTION_LOG_PARAM(STRING, stack);
    FUNCTION_LOG_END();
 
    ASSERT(this != NULL);
    ASSERT(code != 0);
    ASSERT(message != NULL);
    ASSERT(stack != NULL);
 
    MEM_CONTEXT_TEMP_BEGIN()
    {
        PackWrite *const packWrite = protocolPackNew();
 
        pckWriteI32P(packWrite, code);
        pckWriteStrP(packWrite, message);
        pckWriteStrP(packWrite, stack);
 
        protocolServerResponseP(this, .type = protocolMessageTypeError, .data = packWrite);
    }
    MEM_CONTEXT_TEMP_END();
 
    FUNCTION_LOG_RETURN_VOID();
}
 
/**********************************************************************************************************************************/
FN_EXTERN ProtocolServerRequestResult
protocolServerRequest(ProtocolServer *const this)
{
    FUNCTION_LOG_BEGIN(logLevelTrace);
        FUNCTION_LOG_PARAM(PROTOCOL_SERVER, this);
    FUNCTION_LOG_END();
 
    FUNCTION_AUDIT_STRUCT();
 
    ProtocolServerRequestResult result = {0};
 
    MEM_CONTEXT_TEMP_BEGIN()
    {
        PackRead *const request = pckReadNewIo(this->read);
        const ProtocolMessageType type = (ProtocolMessageType)pckReadU32P(request);
 
        CHECK(FormatError, type == protocolMessageTypeRequest, "expected request message");
 
        MEM_CONTEXT_PRIOR_BEGIN()
        {
            result.id = pckReadStrIdP(request);
            result.type = (ProtocolCommandType)pckReadStrIdP(request);
            this->sessionId = pckReadU64P(request);
            result.sessionRequired = pckReadBoolP(request);
            result.param = pckReadPackP(request);
 
            ASSERT(this->sessionId != 0);
        }
        MEM_CONTEXT_PRIOR_END();
 
        pckReadEndP(request);
    }
    MEM_CONTEXT_TEMP_END();
 
    FUNCTION_LOG_RETURN_STRUCT(result);
}
 
/**********************************************************************************************************************************/
FN_EXTERN void
protocolServerProcess(ProtocolServer *const this, const VariantList *const retryInterval, const List *const handlerList)
{
    FUNCTION_LOG_BEGIN(logLevelDebug);
        FUNCTION_LOG_PARAM(PROTOCOL_SERVER, this);
        FUNCTION_LOG_PARAM(VARIANT_LIST, retryInterval);
        FUNCTION_LOG_PARAM(LIST, handlerList);
    FUNCTION_LOG_END();
 
    ASSERT(this != NULL);
    ASSERT(handlerList != NULL);
    ASSERT(!lstEmpty(handlerList));
 
    // Loop until exit request is received
    bool exit = false;
 
    do
    {
        TRY_BEGIN()
        {
            MEM_CONTEXT_TEMP_BEGIN()
            {
                // Get request
                ProtocolServerRequestResult request = protocolServerRequest(this);
 
                // Find the handler
                const ProtocolServerHandler *handler = NULL;
 
                for (unsigned int handlerIdx = 0; handlerIdx < lstSize(handlerList); handlerIdx++)
                {
                    const ProtocolServerHandler *const handlerMatch = lstGet(handlerList, handlerIdx);
 
                    if (request.id == handlerMatch->command)
                    {
                        handler = handlerMatch;
                        break;
                    }
                }
 
                // If handler was found then process
                if (handler != NULL)
                {
                    // Send the request to the handler
                    MEM_CONTEXT_TEMP_BEGIN()
                    {
                        // Variables to store first error message and retry messages
                        ErrorRetry *const errRetry = errRetryNew();
                        const String *errStackTrace = NULL;
 
                        // Initialize retries in case of request failure
                        bool retry = false;
                        unsigned int retryRemaining = retryInterval != NULL ? varLstSize(retryInterval) : 0;
                        TimeMSec retrySleepMs = 0;
 
                        // Handler retry loop
                        do
                        {
                            retry = false;
 
                            TRY_BEGIN()
                            {
                                // Process request type
                                switch (request.type)
                                {
                                    // Open a protocol session
                                    case protocolCommandTypeOpen:
                                    {
                                        ASSERT(handler->open != NULL);
 
                                        // Call open handler
                                        ProtocolServerResult *const openResult = handler->open(pckReadNew(request.param));
                                        ASSERT(openResult != NULL);
                                        ASSERT(!openResult->close);
 
                                        ProtocolServerSession session = {.id = this->sessionId};
 
                                        // Send data
                                        protocolServerResponseP(
                                            this, .data = openResult->data, .close = openResult->sessionData == NULL);
 
                                        // Create session if open returned session data
                                        if (openResult->sessionData != NULL)
                                        {
                                            session.data = objMove(openResult->sessionData, objMemContext(this->sessionList));
                                            lstAdd(this->sessionList, &session);
                                        }
 
                                        break;
                                    }
 
                                    // Process or close/cancel protocol session
                                    default:
                                    {
                                        // Find session data
                                        void *sessionData = NULL;
                                        unsigned int sessionListIdx = 0;
 
                                        if (request.sessionRequired)
                                        {
                                            ASSERT(handler->processSession != NULL);
                                            ASSERT(handler->process == NULL);
 
                                            for (; sessionListIdx < lstSize(this->sessionList); sessionListIdx++)
                                            {
                                                ProtocolServerSession *const session = lstGet(this->sessionList, sessionListIdx);
 
                                                if (session->id == this->sessionId)
                                                {
                                                    sessionData = session->data;
                                                    break;
                                                }
                                            }
 
                                            // Error when session not found
                                            if (sessionData == NULL && request.type != protocolCommandTypeCancel)
                                            {
                                                THROW_FMT(
                                                    ProtocolError, "unable to find session id %" PRIu64 " for request %s:%s",
                                                    this->sessionId, zNewStrId(request.id), zNewStrId(request.type));
                                            }
                                        }
 
                                        // Process request type
                                        switch (request.type)
                                        {
                                            // Process request
                                            case protocolCommandTypeProcess:
                                            {
                                                // Process session
                                                ProtocolServerResult *processResult;
 
                                                if (handler->processSession != NULL)
                                                {
                                                    ASSERT(handler->process == NULL);
                                                    CHECK_FMT(
                                                        ProtocolError, this->sessionId != 0, "no session id for request %s:%s",
                                                        zNewStrId(request.id), zNewStrId(request.type));
 
                                                    processResult = handler->processSession(pckReadNew(request.param), sessionData);
                                                }
                                                // Standalone process
                                                else
                                                {
                                                    ASSERT(handler->process != NULL);
                                                    ASSERT(!request.sessionRequired);
 
                                                    processResult = handler->process(pckReadNew(request.param));
                                                }
 
                                                if (processResult == NULL)
                                                    protocolServerResponseP(this);
                                                else
                                                {
                                                    ASSERT(processResult->sessionData == NULL);
                                                    ASSERT(handler->processSession != NULL || !processResult->close);
 
                                                    protocolServerResponseP(
                                                        this, .data = processResult->data, .close = processResult->close);
 
                                                    // Free session when close is true. This optimization allows an explicit
                                                    // close to be skipped.
                                                    if (processResult->close)
                                                    {
                                                        objFree(sessionData);
                                                        lstRemoveIdx(this->sessionList, sessionListIdx);
                                                    }
                                                }
 
                                                break;
                                            }
 
                                            // Close protocol session
                                            case protocolCommandTypeClose:
                                            {
                                                // If there is a close handler then call it
                                                PackWrite *data = NULL;
 
                                                if (handler->close != NULL)
                                                {
                                                    ProtocolServerResult *const closeResult = handler->close(
                                                        pckReadNew(request.param), sessionData);
                                                    ASSERT(closeResult != NULL);
                                                    ASSERT(closeResult->sessionData == NULL);
                                                    ASSERT(!closeResult->close);
 
                                                    data = closeResult->data;
                                                }
 
                                                // Send data
                                                protocolServerResponseP(this, .data = data, .close = true);
 
                                                // Free the session
                                                objFree(sessionData);
                                                lstRemoveIdx(this->sessionList, sessionListIdx);
 
                                                break;
                                            }
 
                                            // Cancel protocol session
                                            default:
                                            {
                                                CHECK_FMT(
                                                    ProtocolError, request.type == protocolCommandTypeCancel,
                                                    "unknown request type '%s'", zNewStrId(request.type));
 
                                                // Send NULL data
                                                protocolServerResponseP(this, .close = true);
 
                                                // Free the session
                                                if (sessionData != NULL)
                                                {
                                                    objFree(sessionData);
                                                    lstRemoveIdx(this->sessionList, sessionListIdx);
                                                }
 
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                            CATCH_ANY()
                            {
                                // Add the error retry info
                                errRetryAddP(errRetry);
 
                                // On first error record the stack trace. Only the first error will contain a stack trace since
                                // the first error is most likely to contain valuable information.
                                if (errStackTrace == NULL)
                                    errStackTrace = strNewZ(errorStackTrace());
 
                                // Are there retries remaining?
                                if (retryRemaining > 0)
                                {
                                    // Get the sleep interval for this retry
                                    retrySleepMs = varUInt64(varLstGet(retryInterval, varLstSize(retryInterval) - retryRemaining));
 
                                    // Log the retry
                                    LOG_DEBUG_FMT(
                                        "retry %s after %" PRIu64 "ms: %s", errorTypeName(errorType()), retrySleepMs,
                                        errorMessage());
 
                                    // Sleep for interval
                                    sleepMSec(retrySleepMs);
 
                                    // Decrement retries remaining and retry
                                    retryRemaining--;
                                    retry = true;
 
                                    // Send keep-alive to remotes. A retry means the request is taking longer than usual so make
                                    // sure the remote does not timeout.
                                    protocolKeepAlive();
                                }
                                // Else report error to the client
                                else
                                {
                                    protocolServerError(
                                        this, errorTypeCode(errRetryType(errRetry)), errRetryMessage(errRetry), errStackTrace);
                                }
                            }
                            TRY_END();
                        }
                        while (retry);
                    }
                    MEM_CONTEXT_TEMP_END();
                }
                // Else check built-in requests
                else
                {
                    switch (request.id)
                    {
                        case PROTOCOL_COMMAND_EXIT:
                            exit = true;
                            break;
 
                        case PROTOCOL_COMMAND_NOOP:
                            protocolServerResponseP(this);
                            break;
 
                        default:
                            THROW_FMT(
                                ProtocolError, "invalid request '%s' (0x%" PRIx64 ")", zNewStrId(request.id), request.id);
                    }
                }
 
                // Send keep-alive to remotes. When a local process is doing work that does not involve the remote it is important
                // that the remote does not timeout. This will send a keep alive once per unit of work that is performed by the
                // local process.
                protocolKeepAlive();
            }
            MEM_CONTEXT_TEMP_END();
        }
        CATCH_FATAL()
        {
            // Zero session id so a fatal error will be handled by the first client that sees it
            this->sessionId = 0;
 
            // Report error to the client
            protocolServerError(this, errorCode(), STR(errorMessage()), STR(errorStackTrace()));
 
            // Rethrow so the process exits with an error
            RETHROW();
        }
        TRY_END();
    }
    while (!exit);
 
    FUNCTION_LOG_RETURN_VOID();
}
 
/**********************************************************************************************************************************/
FN_EXTERN ProtocolServerResult *
protocolServerResultNew(const ProtocolServerResultNewParam param)
{
    FUNCTION_TEST_BEGIN();
        FUNCTION_TEST_PARAM(SIZE, param.extra);
    FUNCTION_TEST_END();
 
    OBJ_NEW_BEGIN(ProtocolServerResult, .childQty = MEM_CONTEXT_QTY_MAX)
    {
        *this = (ProtocolServerResult)
        {
            .extra = param.extra,
        };
    }
    OBJ_NEW_END();
 
    FUNCTION_TEST_RETURN(PROTOCOL_SERVER_RESULT, this);
}
 
/**********************************************************************************************************************************/
FN_EXTERN PackWrite *
protocolServerResultData(ProtocolServerResult *const this)
{
    FUNCTION_TEST_BEGIN();
        FUNCTION_TEST_PARAM(PROTOCOL_SERVER_RESULT, this);
    FUNCTION_TEST_END();
 
    ASSERT(this != NULL);
    ASSERT(this->data == NULL);
 
    MEM_CONTEXT_OBJ_BEGIN(this)
    {
        this->data = pckWriteNewP(.size = PROTOCOL_PACK_DEFAULT_SIZE + this->extra);
    }
    MEM_CONTEXT_OBJ_END();
 
    FUNCTION_TEST_RETURN(PACK_WRITE, this->data);
}
 
/**********************************************************************************************************************************/
FN_EXTERN void
protocolServerResultSessionDataSet(ProtocolServerResult *const this, void *const sessionData)
{
    FUNCTION_TEST_BEGIN();
        FUNCTION_TEST_PARAM(PROTOCOL_SERVER_RESULT, this);
        FUNCTION_TEST_PARAM_P(VOID, sessionData);
    FUNCTION_TEST_END();
 
    ASSERT(this != NULL);
    ASSERT(sessionData != NULL);
 
    this->sessionData = objMove(sessionData, objMemContext(this));
 
    FUNCTION_TEST_RETURN_VOID();
}
 
/**********************************************************************************************************************************/
FN_EXTERN void
protocolServerResultCloseSet(ProtocolServerResult *const this)
{
    FUNCTION_TEST_BEGIN();
        FUNCTION_TEST_PARAM(PROTOCOL_SERVER_RESULT, this);
    FUNCTION_TEST_END();
 
    ASSERT(this != NULL);
 
    this->close = true;
 
    FUNCTION_TEST_RETURN_VOID();
}
 
/**********************************************************************************************************************************/
FN_EXTERN void
protocolServerToLog(const ProtocolServer *const this, StringStatic *const debugLog)
{
    strStcFmt(debugLog, "{name: %s}", strZ(this->name));
}
 
/**********************************************************************************************************************************/
FN_EXTERN void
protocolServerResultToLog(const ProtocolServerResult *const this, StringStatic *const debugLog)
{
    strStcFmt(
        debugLog, "{data: %s, sessionData: %s, close: %s}", cvtBoolToConstZ(this->data != NULL),
        cvtBoolToConstZ(this->sessionData != NULL), cvtBoolToConstZ(this->close));
}