1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-01-18 04:58:51 +02:00

Add flag to dbGet() to require a standby.

This is needed from backup from standby functionality.
This commit is contained in:
David Steele 2019-12-02 07:39:42 -05:00
parent 28116918ff
commit 33a63aae50
6 changed files with 36 additions and 13 deletions

View File

@ -167,7 +167,7 @@ cmdCheck(void)
MEM_CONTEXT_TEMP_BEGIN()
{
// Get the primary/standby connections (standby is only required if backup from standby is enabled)
DbGetResult dbGroup = dbGet(false, false);
DbGetResult dbGroup = dbGet(false, false, false);
if (dbGroup.standby == NULL && dbGroup.primary == NULL)
THROW(ConfigError, "no database found\nHINT: check indexed pg-path/pg-host configurations");

View File

@ -53,7 +53,7 @@ pgValidate(void)
if (cfgOptionBool(cfgOptOnline))
{
// Check the connections of the master (and standby, if any) and return the master database object.
DbGetResult dbObject = dbGet(false, true);
DbGetResult dbObject = dbGet(false, true, false);
// Get the pgControl information from the pg*-path deemed to be the master
result = pgControlFromFile(storagePgId(dbObject.primaryId));

View File

@ -50,13 +50,16 @@ dbGetId(unsigned int pgId)
Get primary cluster or primary and standby cluster
***********************************************************************************************************************************/
DbGetResult
dbGet(bool primaryOnly, bool primaryRequired)
dbGet(bool primaryOnly, bool primaryRequired, bool standbyRequired)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(BOOL, primaryOnly);
FUNCTION_LOG_PARAM(BOOL, primaryRequired);
FUNCTION_LOG_PARAM(BOOL, standbyRequired);
FUNCTION_LOG_END();
ASSERT(!(primaryOnly && standbyRequired));
DbGetResult result = {0};
MEM_CONTEXT_TEMP_BEGIN()
@ -128,6 +131,10 @@ dbGet(bool primaryOnly, bool primaryRequired)
if (result.primaryId == 0 && primaryRequired)
THROW(DbConnectError, "unable to find primary cluster - cannot proceed");
// Error if no standby was found
if (result.standbyId == 0 && standbyRequired)
THROW(DbConnectError, "unable to find standby cluster - cannot proceed");
dbMove(result.primary, MEM_CONTEXT_OLD());
dbMove(result.standby, MEM_CONTEXT_OLD());
}

View File

@ -21,7 +21,7 @@ typedef struct DbGetResult
Db *standby;
} DbGetResult;
DbGetResult dbGet(bool primaryOnly, bool primaryRequired);
DbGetResult dbGet(bool primaryOnly, bool primaryRequired, bool standbyRequired);
/***********************************************************************************************************************************
Macros for function logging

View File

@ -367,7 +367,7 @@ testRun(void)
HRNPQ_MACRO_DONE()
});
TEST_ASSIGN(db, dbGet(false, false), "get primary and standby");
TEST_ASSIGN(db, dbGet(false, false, false), "get primary and standby");
TEST_RESULT_VOID(checkDbConfig(PG_VERSION_92, db.primaryId, db.primary, false), "valid db config");
@ -429,7 +429,7 @@ testRun(void)
HRNPQ_MACRO_DONE()
});
TEST_ASSIGN(db, dbGet(true, true), "get primary");
TEST_ASSIGN(db, dbGet(true, true, false), "get primary");
TEST_ERROR_FMT(
checkDbConfig(PG_VERSION_92, db.primaryId, db.primary, false), FeatureNotSupportedError,
"archive_mode=always not supported");

View File

@ -118,7 +118,7 @@ testRun(void)
{.function = NULL}
});
TEST_ERROR(dbGet(true, true), DbConnectError, "unable to find primary cluster - cannot proceed");
TEST_ERROR(dbGet(true, true, false), DbConnectError, "unable to find primary cluster - cannot proceed");
harnessLogResult(
"P00 WARN: unable to check pg-1: [DbConnectError] unable to connect to 'dbname='postgres' port=5432': error");
@ -135,7 +135,23 @@ testRun(void)
HRNPQ_MACRO_DONE()
});
TEST_ERROR(dbGet(true, true), DbConnectError, "unable to find primary cluster - cannot proceed");
TEST_ERROR(dbGet(true, true, false), DbConnectError, "unable to find primary cluster - cannot proceed");
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("standby cluster required but not found");
harnessPqScriptSet((HarnessPq [])
{
HRNPQ_MACRO_OPEN(1, "dbname='postgres' port=5432"),
HRNPQ_MACRO_SET_SEARCH_PATH(1),
HRNPQ_MACRO_VALIDATE_QUERY(1, PG_VERSION_94, "/pgdata", NULL, NULL),
HRNPQ_MACRO_SET_APPLICATION_NAME(1),
HRNPQ_MACRO_IS_STANDBY_QUERY(1, false),
HRNPQ_MACRO_CLOSE(1),
HRNPQ_MACRO_DONE()
});
TEST_ERROR(dbGet(false, false, true), DbConnectError, "unable to find standby cluster - cannot proceed");
// Primary cluster found
// -------------------------------------------------------------------------------------------------------------------------
@ -146,7 +162,7 @@ testRun(void)
HRNPQ_MACRO_DONE()
});
TEST_ASSIGN(result, dbGet(true, true), "get primary only");
TEST_ASSIGN(result, dbGet(true, true, false), "get primary only");
TEST_RESULT_INT(result.primaryId, 1, " check primary id");
TEST_RESULT_BOOL(result.primary != NULL, true, " check primary");
@ -178,7 +194,7 @@ testRun(void)
HRNPQ_MACRO_DONE()
});
TEST_ERROR(dbGet(true, true), DbConnectError, "more than one primary cluster found");
TEST_ERROR(dbGet(true, true, false), DbConnectError, "more than one primary cluster found");
// Two standbys found but no primary
// -------------------------------------------------------------------------------------------------------------------------
@ -193,7 +209,7 @@ testRun(void)
HRNPQ_MACRO_DONE()
});
TEST_ERROR(dbGet(false, true), DbConnectError, "unable to find primary cluster - cannot proceed");
TEST_ERROR(dbGet(false, true, false), DbConnectError, "unable to find primary cluster - cannot proceed");
// Two standbys and primary not required
// -------------------------------------------------------------------------------------------------------------------------
@ -208,7 +224,7 @@ testRun(void)
HRNPQ_MACRO_DONE()
});
TEST_ASSIGN(result, dbGet(false, false), "get standbys");
TEST_ASSIGN(result, dbGet(false, false, false), "get standbys");
TEST_RESULT_INT(result.primaryId, 0, " check primary id");
TEST_RESULT_BOOL(result.primary == NULL, true, " check primary");
@ -253,7 +269,7 @@ testRun(void)
HRNPQ_MACRO_DONE()
});
TEST_ASSIGN(result, dbGet(false, true), "get primary and standy");
TEST_ASSIGN(result, dbGet(false, true, false), "get primary and standy");
harnessLogResultRegExp(
"P00 WARN: unable to check pg-4: \\[DbConnectError\\] unable to connect to 'dbname='postgres' port=5433': error\n"
"P00 WARN: unable to check pg-5: \\[DbConnectError\\] raised from remote-0 protocol on 'localhost':"