Исходный код СУБД 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
/***********************************************************************************************************************************
Check Command
***********************************************************************************************************************************/
#include <build.h>
 
#include <unistd.h>
 
#include "command/archive/find.h"
#include "command/check/check.h"
#include "command/check/common.h"
#include "command/check/report.h"
#include "command/stanza/common.h"
#include "common/debug.h"
#include "common/io/fdWrite.h"
#include "common/log.h"
#include "common/memContext.h"
#include "config/config.h"
#include "config/load.h"
#include "config/parse.h"
#include "db/helper.h"
#include "info/helper.h"
#include "postgres/interface.h"
#include "protocol/helper.h"
#include "storage/helper.h"
 
/***********************************************************************************************************************************
Helper functions (to assist with testing)
***********************************************************************************************************************************/
static unsigned int
checkManifest(void)
{
    FUNCTION_LOG_VOID(logLevelDebug);
 
    // Return the actual number of pg* defined
    unsigned int result = 0;
 
    MEM_CONTEXT_TEMP_BEGIN()
    {
        // Loop through all defined databases and attempt to build a manifest
        for (unsigned int pgIdx = 0; pgIdx < cfgOptionGroupIdxTotal(cfgOptGrpPg); pgIdx++)
        {
            result++;
            // ??? Placeholder for manifest build
            storageListP(storagePgIdx(pgIdx), cfgOptionIdxStr(cfgOptPgPath, pgIdx));
        }
    }
    MEM_CONTEXT_TEMP_END();
 
    FUNCTION_LOG_RETURN(UINT, result);
}
 
static void
checkStandby(const DbGetResult dbGroup, const unsigned int pgPathDefinedTotal)
{
    FUNCTION_LOG_BEGIN(logLevelDebug);
        FUNCTION_LOG_PARAM(DB_GET_RESULT, dbGroup);
        FUNCTION_LOG_PARAM(UINT, pgPathDefinedTotal);
    FUNCTION_LOG_END();
 
    // If a standby is defined, check the configuration
    if (dbGroup.standby != NULL)
    {
        // If primary was not found (only have 1 pg configured locally, and we want to still run because this is a standby)
        if (dbGroup.primary == NULL)
        {
            // If any repo is local or more than one pg-path is found then a primary should have been found so error
            bool error = pgPathDefinedTotal > 1;
            unsigned int repoIdx = 0;
 
            while (!error && repoIdx < cfgOptionGroupIdxTotal(cfgOptGrpRepo))
            {
                if (repoIsLocal(repoIdx))
                    error = true;
 
                repoIdx++;
            }
 
            if (error)
                THROW(ConfigError, "primary database not found\nHINT: check indexed pg-path/pg-host configurations");
        }
 
        // Check the user configured path and version against the database
        checkDbConfig(dbPgControl(dbGroup.standby).version, dbGroup.standbyIdx, dbGroup.standby, true);
 
        // Check each repository configured
        for (unsigned int repoIdx = 0; repoIdx < cfgOptionGroupIdxTotal(cfgOptGrpRepo); repoIdx++)
        {
            LOG_INFO_FMT(CFGCMD_CHECK " %s (standby)", cfgOptionGroupName(cfgOptGrpRepo, repoIdx));
 
            // Check that the backup and archive info files exist and are valid for the current database of the stanza
            checkStanzaInfoPg(repoIdx, dbPgControl(dbGroup.standby).version, dbPgControl(dbGroup.standby).systemId);
        }
 
        LOG_INFO("switch wal not performed because this is a standby");
 
        // Free the standby connection
        dbFree(dbGroup.standby);
    }
    // If backup from standby is true then warn when a standby not found
    else if (cfgOptionSeq(cfgOptBackupStandby) != CFGOPTVAL_BACKUP_STANDBY_N)
    {
        LOG_WARN("option '" CFGOPT_BACKUP_STANDBY "' is enabled but standby is not properly configured");
    }
 
    FUNCTION_LOG_RETURN_VOID();
}
 
static void
checkPrimary(const DbGetResult dbGroup)
{
    FUNCTION_LOG_BEGIN(logLevelDebug);
        FUNCTION_LOG_PARAM(DB_GET_RESULT, dbGroup);
    FUNCTION_LOG_END();
 
    FUNCTION_AUDIT_HELPER();
 
    // If a primary is defined, check the configuration and perform a WAL switch and make sure the WAL is archived
    if (dbGroup.primary != NULL)
    {
        // Check the user configured path and version against the database
        checkDbConfig(dbPgControl(dbGroup.primary).version, dbGroup.primaryIdx, dbGroup.primary, false);
 
        // Check configuration of each repo
        const String **const repoArchiveId = memNew(sizeof(String *) * cfgOptionGroupIdxTotal(cfgOptGrpRepo));
 
        for (unsigned int repoIdx = 0; repoIdx < cfgOptionGroupIdxTotal(cfgOptGrpRepo); repoIdx++)
        {
            LOG_INFO_FMT(CFGCMD_CHECK " %s configuration (primary)", cfgOptionGroupName(cfgOptGrpRepo, repoIdx));
 
            // Check that the backup and archive info files exist and are valid for the current database of the stanza
            checkStanzaInfoPg(repoIdx, dbPgControl(dbGroup.primary).version, dbPgControl(dbGroup.primary).systemId);
 
            // Retrieve the archiveId
            repoArchiveId[repoIdx] = infoArchiveId(infoArchiveRepoIdx(repoIdx));
        }
 
        // Perform a WAL switch
        const String *const walSegment = dbWalSwitch(dbGroup.primary);
 
        // Wait for the WAL to appear in each repo
        for (unsigned int repoIdx = 0; repoIdx < cfgOptionGroupIdxTotal(cfgOptGrpRepo); repoIdx++)
        {
            LOG_INFO_FMT(CFGCMD_CHECK " %s archive for WAL (primary)", cfgOptionGroupName(cfgOptGrpRepo, repoIdx));
 
            const String *const walSegmentFile = walSegmentFindOne(
                storageRepoIdx(repoIdx), repoArchiveId[repoIdx], walSegment, cfgOptionUInt64(cfgOptArchiveTimeout));
 
            LOG_INFO_FMT(
                "WAL segment %s successfully archived to '%s' on %s",
                strZ(walSegment),
                strZ(
                    storagePathP(
                        storageRepoIdx(repoIdx),
                        strNewFmt(STORAGE_REPO_ARCHIVE "/%s/%s", strZ(repoArchiveId[repoIdx]), strZ(walSegmentFile)))),
                cfgOptionGroupName(cfgOptGrpRepo, repoIdx));
        }
 
        dbFree(dbGroup.primary);
    }
 
    FUNCTION_LOG_RETURN_VOID();
}
 
// Warn about the old passphrase when it is set and otherwise about the passphrase
static void
checkCipherPass(void)
{
    FUNCTION_LOG_VOID(logLevelDebug);
 
    for (unsigned int repoIdx = 0; repoIdx < cfgOptionGroupIdxTotal(cfgOptGrpRepo); repoIdx++)
    {
        if (cfgOptionIdxTest(cfgOptRepoCipherPassOld, repoIdx))
        {
            if (infoArchiveCipherPrior(infoArchiveRepoIdx(repoIdx)) || infoBackupCipherPrior(infoBackupRepoIdxP(repoIdx)))
            {
                LOG_WARN_FMT(
                    "option '%s' is set and the stanza has not been rekeyed\n"
                    "HINT: run " CFGCMD_STANZA_REKEY " and then remove the option.",
                    cfgOptionIdxName(cfgOptRepoCipherPassOld, repoIdx));
            }
            else
            {
                LOG_WARN_FMT(
                    "option '%s' is set and the stanza has been rekeyed\n"
                    "HINT: remove the option.",
                    cfgOptionIdxName(cfgOptRepoCipherPassOld, repoIdx));
            }
        }
        else
            stanzaCipherPassCheck(repoIdx, infoBackupFormat(infoBackupRepoIdxP(repoIdx)), NULL);
    }
 
    FUNCTION_LOG_RETURN_VOID();
}
 
/**********************************************************************************************************************************/
FN_EXTERN void
cmdCheck(void)
{
    FUNCTION_LOG_VOID(logLevelDebug);
 
    MEM_CONTEXT_TEMP_BEGIN()
    {
        if (cfgOptionBool(cfgOptReport))
            ioFdWriteOneStr(STDOUT_FILENO, checkReport());
        else
        {
            // Build stanza list based on whether a stanza was specified or not
            StringList *stanzaList;
            bool stanzaSpecified = cfgOptionTest(cfgOptStanza);
 
            if (stanzaSpecified)
            {
                stanzaList = strLstNew();
                strLstAdd(stanzaList, cfgOptionStr(cfgOptStanza));
            }
            else
            {
                stanzaList = cfgParseStanzaList();
 
                if (strLstSize(stanzaList) == 0)
                {
                    LOG_WARN(
                        "no stanzas found to check\n"
                        "HINT: are there non-empty stanza sections in the configuration?");
                }
            }
 
            // Iterate stanzas
            for (unsigned int stanzaIdx = 0; stanzaIdx < strLstSize(stanzaList); stanzaIdx++)
            {
                // Switch stanza if required
                if (!stanzaSpecified)
                {
                    const String *const stanza = strLstGet(stanzaList, stanzaIdx);
                    LOG_INFO_FMT("check stanza '%s'", strZ(stanza));
 
                    // Free storage and protocol cache
                    storageHelperFree();
                    protocolFree();
 
                    // Reload config with new stanza
                    cfgLoadStanza(stanza);
                }
 
                // Get the primary/standby connections (standby is only required if backup from standby is enabled)
                DbGetResult dbGroup = dbGet(false, false, CFGOPTVAL_BACKUP_STANDBY_N);
 
                if (dbGroup.standby == NULL && dbGroup.primary == NULL)
                    THROW(ConfigError, "no database found\nHINT: check indexed pg-path/pg-host configurations");
 
                const unsigned int pgPathDefinedTotal = checkManifest();
                checkStandby(dbGroup, pgPathDefinedTotal);
                checkPrimary(dbGroup);
                checkCipherPass();
            }
        }
    }
    MEM_CONTEXT_TEMP_END();
 
    FUNCTION_LOG_RETURN_VOID();
}