pgbackrest.git / summary / log / commit / refs

commit    251dbede8ff8a7206986106b8df8b08257cd7ebd
Author:   David Steele <david@pgmasters.net>
Date:     Wed Mar 27 21:14:06 2019 +0000

    Add locking capability to the remote command.
    
    When a repository server is configured, commands that modify the repository acquire a remote lock as well as a local lock for extra protection against multiple writers.
    
    Instead of the custom logic used in Perl, make remote locking part of the command configuration.
    
    This also means that the C remote needs the stanza since it is used to construct the lock name.  We may need to revisit this at a later date.


build/lib/pgBackRestBuild/Config/Build.pm | 2 + build/lib/pgBackRestBuild/Config/Data.pm | 13 ++++ doc/xml/release.xml | 4 + src/command/remote/remote.c | 33 +++++++- src/config/config.auto.c | 18 +++++ src/config/config.c | 35 +++++++++ src/config/config.h | 2 + src/protocol/helper.c | 5 -- src/protocol/server.c | 33 ++++++-- src/protocol/server.h | 1 + test/src/module/command/remoteTest.c | 120 ++++++++++++++++++++++++++++-- test/src/module/config/configTest.c | 4 + test/src/module/protocol/protocolTest.c | 10 +-- 13 files changed, 258 insertions(+), 22 deletions(-) diff --git a/build/lib/pgBackRestBuild/Config/Build.pm b/build/lib/pgBackRestBuild/Config/Build.pm index 80310a421..75a992fa6 100644 --- a/build/lib/pgBackRestBuild/Config/Build.pm +++ b/build/lib/pgBackRestBuild/Config/Build.pm @@ -147,6 +147,8 @@ sub buildConfig " CONFIG_COMMAND_LOG_LEVEL_STDERR_MAX(logLevel" . ucfirst(lc($rhCommand->{&CFGDEF_LOG_LEVEL_STDERR_MAX})) . ")\n" . " CONFIG_COMMAND_LOCK_REQUIRED(" . ($rhCommand->{&CFGDEF_LOCK_REQUIRED} ? 'true' : 'false') . ")\n" . + " CONFIG_COMMAND_LOCK_REMOTE_REQUIRED(" . + ($rhCommand->{&CFGDEF_LOCK_REMOTE_REQUIRED} ? 'true' : 'false') . ")\n" . " CONFIG_COMMAND_LOCK_TYPE(lockType" . ucfirst(lc($rhCommand->{&CFGDEF_LOCK_TYPE})) . ")\n" . " CONFIG_COMMAND_PARAMETER_ALLOWED(" . ($rhCommand->{&CFGDEF_PARAMETER_ALLOWED} ? 'true' : 'false') . ")\n" . " )\n"; diff --git a/build/lib/pgBackRestBuild/Config/Data.pm b/build/lib/pgBackRestBuild/Config/Data.pm index 1bf56adce..3c2f0e3da 100644 --- a/build/lib/pgBackRestBuild/Config/Data.pm +++ b/build/lib/pgBackRestBuild/Config/Data.pm @@ -482,6 +482,10 @@ use constant CFGDEF_LOG_LEVEL_STDERR_MAX => 'log-leve use constant CFGDEF_LOCK_REQUIRED => 'lock-required'; push @EXPORT, qw(CFGDEF_LOCK_REQUIRED); +# Does the command require a lock on the remote? The lock will only be acquired for process id 0. +use constant CFGDEF_LOCK_REMOTE_REQUIRED => 'lock-remote-required'; + push @EXPORT, qw(CFGDEF_LOCK_REMOTE_REQUIRED); + # What type of lock is required? use constant CFGDEF_LOCK_TYPE => 'lock-type'; push @EXPORT, qw(CFGDEF_LOCK_TYPE); @@ -590,6 +594,7 @@ my $rhCommandDefine = &CFGCMD_ARCHIVE_PUSH => { &CFGDEF_LOG_FILE => false, + &CFGDEF_LOCK_REMOTE_REQUIRED => true, &CFGDEF_LOCK_TYPE => CFGDEF_LOCK_TYPE_ARCHIVE, &CFGDEF_PARAMETER_ALLOWED => true, }, @@ -598,6 +603,7 @@ my $rhCommandDefine = { &CFGDEF_LOG_FILE => true, &CFGDEF_LOCK_REQUIRED => true, + &CFGDEF_LOCK_REMOTE_REQUIRED => true, &CFGDEF_LOCK_TYPE => CFGDEF_LOCK_TYPE_ARCHIVE, &CFGDEF_PARAMETER_ALLOWED => true, }, @@ -605,6 +611,7 @@ my $rhCommandDefine = &CFGCMD_BACKUP => { &CFGDEF_LOCK_REQUIRED => true, + &CFGDEF_LOCK_REMOTE_REQUIRED => true, &CFGDEF_LOCK_TYPE => CFGDEF_LOCK_TYPE_BACKUP, }, @@ -2520,6 +2527,12 @@ foreach my $strCommand (sort(keys(%{$rhCommandDefine}))) $rhCommandDefine->{$strCommand}{&CFGDEF_LOCK_REQUIRED} = false; } + # Default lock remote required is false + if (!defined($rhCommandDefine->{$strCommand}{&CFGDEF_LOCK_REMOTE_REQUIRED})) + { + $rhCommandDefine->{$strCommand}{&CFGDEF_LOCK_REMOTE_REQUIRED} = false; + } + # Lock type must be set if a lock is required if (!defined($rhCommandDefine->{$strCommand}{&CFGDEF_LOCK_TYPE})) { diff --git a/doc/xml/release.xml b/doc/xml/release.xml index 16d03f164..b32bde45b 100644 --- a/doc/xml/release.xml +++ b/doc/xml/release.xml @@ -81,6 +81,10 @@ <p>Make notion of current <postgres/> info ID in C align with Perl.</p> </release-item> + <release-item> + <p>Add locking capability to the remote command.</p> + </release-item> + <release-item> <p>Add <code>httpHeaderDup()</code>.</p> </release-item> diff --git a/src/command/remote/remote.c b/src/command/remote/remote.c index dcebe3fe7..d4f3e2799 100644 --- a/src/command/remote/remote.c +++ b/src/command/remote/remote.c @@ -30,7 +30,38 @@ cmdRemote(int handleRead, int handleWrite) ProtocolServer *server = protocolServerNew(name, PROTOCOL_SERVICE_REMOTE_STR, read, write); protocolServerHandlerAdd(server, storageDriverRemoteProtocol); protocolServerHandlerAdd(server, configProtocol); - protocolServerProcess(server); + + // Acquire a lock if this command needs one. We'll use the noop that is always sent from the client right after the + // handshake to return an error. + volatile bool success = false; + + TRY_BEGIN() + { + // Read the command. No need to parse it since we know this is the first noop. + ioReadLine(read); + + // Only try the lock if this is process 0, i.e. the remote started from the main process + if (cfgOptionInt(cfgOptProcess) == 0) + { + ConfigCommand commandId = cfgCommandId(strPtr(cfgOptionStr(cfgOptCommand))); + + // Acquire a lock if this command requires a lock + if (cfgLockRemoteRequired(commandId)) + lockAcquire(cfgOptionStr(cfgOptLockPath), cfgOptionStr(cfgOptStanza), cfgLockRemoteType(commandId), 0, true); + } + + protocolServerResponse(server, NULL); + success = true; + } + CATCH_ANY() + { + protocolServerError(server, errorCode(), strNew(errorMessage())); + } + TRY_END(); + + // If not successful we'll just exit + if (success) + protocolServerProcess(server); } MEM_CONTEXT_TEMP_END(); diff --git a/src/config/config.auto.c b/src/config/config.auto.c index 0446b6204..36f36038e 100644 --- a/src/config/config.auto.c +++ b/src/config/config.auto.c @@ -17,6 +17,7 @@ static ConfigCommandData configCommandData[CFG_COMMAND_TOTAL] = CONFIG_COMMAND_L CONFIG_COMMAND_LOG_LEVEL_DEFAULT(logLevelInfo) CONFIG_COMMAND_LOG_LEVEL_STDERR_MAX(logLevelTrace) CONFIG_COMMAND_LOCK_REQUIRED(false) + CONFIG_COMMAND_LOCK_REMOTE_REQUIRED(false) CONFIG_COMMAND_LOCK_TYPE(lockTypeArchive) CONFIG_COMMAND_PARAMETER_ALLOWED(true) ) @@ -29,6 +30,7 @@ static ConfigCommandData configCommandData[CFG_COMMAND_TOTAL] = CONFIG_COMMAND_L CONFIG_COMMAND_LOG_LEVEL_DEFAULT(logLevelInfo) CONFIG_COMMAND_LOG_LEVEL_STDERR_MAX(logLevelTrace) CONFIG_COMMAND_LOCK_REQUIRED(true) + CONFIG_COMMAND_LOCK_REMOTE_REQUIRED(false) CONFIG_COMMAND_LOCK_TYPE(lockTypeArchive) CONFIG_COMMAND_PARAMETER_ALLOWED(true) ) @@ -41,6 +43,7 @@ static ConfigCommandData configCommandData[CFG_COMMAND_TOTAL] = CONFIG_COMMAND_L CONFIG_COMMAND_LOG_LEVEL_DEFAULT(logLevelInfo) CONFIG_COMMAND_LOG_LEVEL_STDERR_MAX(logLevelTrace) CONFIG_COMMAND_LOCK_REQUIRED(false) + CONFIG_COMMAND_LOCK_REMOTE_REQUIRED(true) CONFIG_COMMAND_LOCK_TYPE(lockTypeArchive) CONFIG_COMMAND_PARAMETER_ALLOWED(true) ) @@ -53,6 +56,7 @@ static ConfigCommandData configCommandData[CFG_COMMAND_TOTAL] = CONFIG_COMMAND_L CONFIG_COMMAND_LOG_LEVEL_DEFAULT(logLevelInfo) CONFIG_COMMAND_LOG_LEVEL_STDERR_MAX(logLevelTrace) CONFIG_COMMAND_LOCK_REQUIRED(true) + CONFIG_COMMAND_LOCK_REMOTE_REQUIRED(true) CONFIG_COMMAND_LOCK_TYPE(lockTypeArchive) CONFIG_COMMAND_PARAMETER_ALLOWED(true) ) @@ -65,6 +69,7 @@ static ConfigCommandData configCommandData[CFG_COMMAND_TOTAL] = CONFIG_COMMAND_L CONFIG_COMMAND_LOG_LEVEL_DEFAULT(logLevelInfo) CONFIG_COMMAND_LOG_LEVEL_STDERR_MAX(logLevelTrace) CONFIG_COMMAND_LOCK_REQUIRED(true) + CONFIG_COMMAND_LOCK_REMOTE_REQUIRED(true) CONFIG_COMMAND_LOCK_TYPE(lockTypeBackup) CONFIG_COMMAND_PARAMETER_ALLOWED(false) ) @@ -77,6 +82,7 @@ static ConfigCommandData configCommandData[CFG_COMMAND_TOTAL] = CONFIG_COMMAND_L CONFIG_COMMAND_LOG_LEVEL_DEFAULT(logLevelInfo) CONFIG_COMMAND_LOG_LEVEL_STDERR_MAX(logLevelTrace) CONFIG_COMMAND_LOCK_REQUIRED(false) + CONFIG_COMMAND_LOCK_REMOTE_REQUIRED(false) CONFIG_COMMAND_LOCK_TYPE(lockTypeNone) CONFIG_COMMAND_PARAMETER_ALLOWED(false) ) @@ -89,6 +95,7 @@ static ConfigCommandData configCommandData[CFG_COMMAND_TOTAL] = CONFIG_COMMAND_L CONFIG_COMMAND_LOG_LEVEL_DEFAULT(logLevelInfo) CONFIG_COMMAND_LOG_LEVEL_STDERR_MAX(logLevelTrace) CONFIG_COMMAND_LOCK_REQUIRED(true) + CONFIG_COMMAND_LOCK_REMOTE_REQUIRED(false) CONFIG_COMMAND_LOCK_TYPE(lockTypeBackup) CONFIG_COMMAND_PARAMETER_ALLOWED(false) ) @@ -101,6 +108,7 @@ static ConfigCommandData configCommandData[CFG_COMMAND_TOTAL] = CONFIG_COMMAND_L CONFIG_COMMAND_LOG_LEVEL_DEFAULT(logLevelDebug) CONFIG_COMMAND_LOG_LEVEL_STDERR_MAX(logLevelTrace) CONFIG_COMMAND_LOCK_REQUIRED(false) + CONFIG_COMMAND_LOCK_REMOTE_REQUIRED(false) CONFIG_COMMAND_LOCK_TYPE(lockTypeNone) CONFIG_COMMAND_PARAMETER_ALLOWED(true) ) @@ -113,6 +121,7 @@ static ConfigCommandData configCommandData[CFG_COMMAND_TOTAL] = CONFIG_COMMAND_L CONFIG_COMMAND_LOG_LEVEL_DEFAULT(logLevelDebug) CONFIG_COMMAND_LOG_LEVEL_STDERR_MAX(logLevelTrace) CONFIG_COMMAND_LOCK_REQUIRED(false) + CONFIG_COMMAND_LOCK_REMOTE_REQUIRED(false) CONFIG_COMMAND_LOCK_TYPE(lockTypeNone) CONFIG_COMMAND_PARAMETER_ALLOWED(false) ) @@ -125,6 +134,7 @@ static ConfigCommandData configCommandData[CFG_COMMAND_TOTAL] = CONFIG_COMMAND_L CONFIG_COMMAND_LOG_LEVEL_DEFAULT(logLevelInfo) CONFIG_COMMAND_LOG_LEVEL_STDERR_MAX(logLevelError) CONFIG_COMMAND_LOCK_REQUIRED(false) + CONFIG_COMMAND_LOCK_REMOTE_REQUIRED(false) CONFIG_COMMAND_LOCK_TYPE(lockTypeNone) CONFIG_COMMAND_PARAMETER_ALLOWED(false) ) @@ -137,6 +147,7 @@ static ConfigCommandData configCommandData[CFG_COMMAND_TOTAL] = CONFIG_COMMAND_L CONFIG_COMMAND_LOG_LEVEL_DEFAULT(logLevelInfo) CONFIG_COMMAND_LOG_LEVEL_STDERR_MAX(logLevelError) CONFIG_COMMAND_LOCK_REQUIRED(false) + CONFIG_COMMAND_LOCK_REMOTE_REQUIRED(false) CONFIG_COMMAND_LOCK_TYPE(lockTypeNone) CONFIG_COMMAND_PARAMETER_ALLOWED(false) ) @@ -149,6 +160,7 @@ static ConfigCommandData configCommandData[CFG_COMMAND_TOTAL] = CONFIG_COMMAND_L CONFIG_COMMAND_LOG_LEVEL_DEFAULT(logLevelInfo) CONFIG_COMMAND_LOG_LEVEL_STDERR_MAX(logLevelTrace) CONFIG_COMMAND_LOCK_REQUIRED(false) + CONFIG_COMMAND_LOCK_REMOTE_REQUIRED(false) CONFIG_COMMAND_LOCK_TYPE(lockTypeNone) CONFIG_COMMAND_PARAMETER_ALLOWED(false) ) @@ -161,6 +173,7 @@ static ConfigCommandData configCommandData[CFG_COMMAND_TOTAL] = CONFIG_COMMAND_L CONFIG_COMMAND_LOG_LEVEL_DEFAULT(logLevelInfo) CONFIG_COMMAND_LOG_LEVEL_STDERR_MAX(logLevelTrace) CONFIG_COMMAND_LOCK_REQUIRED(true) + CONFIG_COMMAND_LOCK_REMOTE_REQUIRED(false) CONFIG_COMMAND_LOCK_TYPE(lockTypeAll) CONFIG_COMMAND_PARAMETER_ALLOWED(false) ) @@ -173,6 +186,7 @@ static ConfigCommandData configCommandData[CFG_COMMAND_TOTAL] = CONFIG_COMMAND_L CONFIG_COMMAND_LOG_LEVEL_DEFAULT(logLevelInfo) CONFIG_COMMAND_LOG_LEVEL_STDERR_MAX(logLevelTrace) CONFIG_COMMAND_LOCK_REQUIRED(true) + CONFIG_COMMAND_LOCK_REMOTE_REQUIRED(false) CONFIG_COMMAND_LOCK_TYPE(lockTypeAll) CONFIG_COMMAND_PARAMETER_ALLOWED(false) ) @@ -185,6 +199,7 @@ static ConfigCommandData configCommandData[CFG_COMMAND_TOTAL] = CONFIG_COMMAND_L CONFIG_COMMAND_LOG_LEVEL_DEFAULT(logLevelInfo) CONFIG_COMMAND_LOG_LEVEL_STDERR_MAX(logLevelTrace) CONFIG_COMMAND_LOCK_REQUIRED(true) + CONFIG_COMMAND_LOCK_REMOTE_REQUIRED(false) CONFIG_COMMAND_LOCK_TYPE(lockTypeAll) CONFIG_COMMAND_PARAMETER_ALLOWED(false) ) @@ -197,6 +212,7 @@ static ConfigCommandData configCommandData[CFG_COMMAND_TOTAL] = CONFIG_COMMAND_L CONFIG_COMMAND_LOG_LEVEL_DEFAULT(logLevelInfo) CONFIG_COMMAND_LOG_LEVEL_STDERR_MAX(logLevelTrace) CONFIG_COMMAND_LOCK_REQUIRED(false) + CONFIG_COMMAND_LOCK_REMOTE_REQUIRED(false) CONFIG_COMMAND_LOCK_TYPE(lockTypeNone) CONFIG_COMMAND_PARAMETER_ALLOWED(false) ) @@ -209,6 +225,7 @@ static ConfigCommandData configCommandData[CFG_COMMAND_TOTAL] = CONFIG_COMMAND_L CONFIG_COMMAND_LOG_LEVEL_DEFAULT(logLevelInfo) CONFIG_COMMAND_LOG_LEVEL_STDERR_MAX(logLevelTrace) CONFIG_COMMAND_LOCK_REQUIRED(false) + CONFIG_COMMAND_LOCK_REMOTE_REQUIRED(false) CONFIG_COMMAND_LOCK_TYPE(lockTypeNone) CONFIG_COMMAND_PARAMETER_ALLOWED(false) ) @@ -221,6 +238,7 @@ static ConfigCommandData configCommandData[CFG_COMMAND_TOTAL] = CONFIG_COMMAND_L CONFIG_COMMAND_LOG_LEVEL_DEFAULT(logLevelDebug) CONFIG_COMMAND_LOG_LEVEL_STDERR_MAX(logLevelTrace) CONFIG_COMMAND_LOCK_REQUIRED(false) + CONFIG_COMMAND_LOCK_REMOTE_REQUIRED(false) CONFIG_COMMAND_LOCK_TYPE(lockTypeNone) CONFIG_COMMAND_PARAMETER_ALLOWED(false) ) diff --git a/src/config/config.c b/src/config/config.c index 4fb3d61d5..34945545f 100644 --- a/src/config/config.c +++ b/src/config/config.c @@ -16,6 +16,7 @@ typedef struct ConfigCommandData const char *name; bool lockRequired:1; + bool lockRemoteRequired:1; unsigned int lockType:2; bool logFile:1; @@ -33,6 +34,8 @@ typedef struct ConfigCommandData #define CONFIG_COMMAND_LOCK_REQUIRED(lockRequiredParam) \ .lockRequired = lockRequiredParam, +#define CONFIG_COMMAND_LOCK_REMOTE_REQUIRED(lockRemoteRequiredParam) \ + .lockRemoteRequired = lockRemoteRequiredParam, #define CONFIG_COMMAND_LOCK_TYPE(lockTypeParam) \ .lockType = lockTypeParam, #define CONFIG_COMMAND_LOG_FILE(logFileParam) \ @@ -336,6 +339,21 @@ cfgLockRequired(void) FUNCTION_TEST_RETURN(configCommandData[cfgCommand()].lockRequired); } +/*********************************************************************************************************************************** +Does the command require an immediate lock? +***********************************************************************************************************************************/ +bool +cfgLockRemoteRequired(ConfigCommand commandId) +{ + FUNCTION_TEST_BEGIN(); + FUNCTION_TEST_PARAM(ENUM, commandId); + FUNCTION_TEST_END(); + + ASSERT(commandId < cfgCmdNone); + + FUNCTION_TEST_RETURN(configCommandData[commandId].lockRemoteRequired); +} + /*********************************************************************************************************************************** Get the lock type required for this command ***********************************************************************************************************************************/ @@ -349,6 +367,23 @@ cfgLockType(void) FUNCTION_TEST_RETURN((LockType)configCommandData[cfgCommand()].lockType); } +/*********************************************************************************************************************************** +Get the remote lock type required for the command +***********************************************************************************************************************************/ +LockType +cfgLockRemoteType(ConfigCommand commandId) +{ + FUNCTION_TEST_VOID(); + + FUNCTION_TEST_BEGIN(); + FUNCTION_TEST_PARAM(ENUM, commandId); + FUNCTION_TEST_END(); + + ASSERT(commandId < cfgCmdNone); + + FUNCTION_TEST_RETURN((LockType)configCommandData[commandId].lockType); +} + /*********************************************************************************************************************************** Does this command log to a file? ***********************************************************************************************************************************/ diff --git a/src/config/config.h b/src/config/config.h index 07b68fbeb..4676ffd24 100644 --- a/src/config/config.h +++ b/src/config/config.h @@ -32,7 +32,9 @@ ConfigCommand cfgCommand(void); const char *cfgCommandName(ConfigCommand commandId); bool cfgLockRequired(void); +bool cfgLockRemoteRequired(ConfigCommand commandId); LockType cfgLockType(void); +LockType cfgLockRemoteType(ConfigCommand commandId); bool cfgLogFile(void); LogLevel cfgLogLevelDefault(void); diff --git a/src/protocol/helper.c b/src/protocol/helper.c index 182299f2e..65a25243a 100644 --- a/src/protocol/helper.c +++ b/src/protocol/helper.c @@ -230,11 +230,6 @@ protocolRemoteParam(ProtocolStorageType protocolStorageType, unsigned int protoc // Always output errors on stderr for debugging purposes kvPut(optionReplace, varNewStr(strNew(cfgOptionName(cfgOptLogLevelStderr))), varNewStrZ("error")); - // Don't pass the stanza if it is set. It is better if the remote is stanza-agnostic so the client can operate on multiple - // stanzas without starting a new remote. Once the Perl code is removed the stanza option can be removed from the remote - // command. - kvPut(optionReplace, varNewStr(strNew(cfgOptionName(cfgOptStanza))), NULL); - // Add the type kvPut(optionReplace, varNewStr(strNew(cfgOptionName(cfgOptType))), varNewStr(strNew("backup"))); diff --git a/src/protocol/server.c b/src/protocol/server.c index a4d8d8b64..e1ff0a0a7 100644 --- a/src/protocol/server.c +++ b/src/protocol/server.c @@ -89,6 +89,32 @@ protocolServerHandlerAdd(ProtocolServer *this, ProtocolServerProcessHandler hand FUNCTION_LOG_RETURN_VOID(); } +/*********************************************************************************************************************************** +Return an error +***********************************************************************************************************************************/ +void +protocolServerError(ProtocolServer *this, int code, const String *message) +{ + FUNCTION_LOG_BEGIN(logLevelTrace); + FUNCTION_LOG_PARAM(PROTOCOL_SERVER, this); + FUNCTION_LOG_PARAM(INT, code); + FUNCTION_LOG_PARAM(STRING, message); + FUNCTION_LOG_END(); + + ASSERT(this != NULL); + ASSERT(code != 0); + ASSERT(message != NULL); + + KeyValue *error = kvNew(); + kvPut(error, varNewStr(PROTOCOL_ERROR_STR), varNewInt(errorCode())); + kvPut(error, varNewStr(PROTOCOL_OUTPUT_STR), varNewStr(strNew(errorMessage()))); + + ioWriteLine(this->write, kvToJson(error, 0)); + ioWriteFlush(this->write); + + FUNCTION_LOG_RETURN_VOID(); +} + /*********************************************************************************************************************************** Process requests ***********************************************************************************************************************************/ @@ -148,12 +174,7 @@ protocolServerProcess(ProtocolServer *this) } CATCH_ANY() { - KeyValue *error = kvNew(); - kvPut(error, varNewStr(PROTOCOL_ERROR_STR), varNewInt(errorCode())); - kvPut(error, varNewStr(PROTOCOL_OUTPUT_STR), varNewStr(strNew(errorMessage()))); - - ioWriteLine(this->write, kvToJson(error, 0)); - ioWriteFlush(this->write); + protocolServerError(this, errorCode(), strNew(errorMessage())); } TRY_END(); } diff --git a/src/protocol/server.h b/src/protocol/server.h index 2d47a3cf8..8a5a05f92 100644 --- a/src/protocol/server.h +++ b/src/protocol/server.h @@ -25,6 +25,7 @@ ProtocolServer *protocolServerNew(const String *name, const String *service, IoR /*********************************************************************************************************************************** Functions ***********************************************************************************************************************************/ +void protocolServerError(ProtocolServer *this, int code, const String *message); void protocolServerProcess(ProtocolServer *this); void protocolServerResponse(ProtocolServer *this, const Variant *output); void protocolServerHandlerAdd(ProtocolServer *this, ProtocolServerProcessHandler handler); diff --git a/test/src/module/command/remoteTest.c b/test/src/module/command/remoteTest.c index 0a35a8a7c..666128488 100644 --- a/test/src/module/command/remoteTest.c +++ b/test/src/module/command/remoteTest.c @@ -5,6 +5,7 @@ Test Remote Command #include "common/io/handleWrite.h" #include "protocol/client.h" #include "protocol/server.h" +#include "storage/driver/posix/storage.h" #include "common/harnessConfig.h" #include "common/harnessFork.h" @@ -20,12 +21,12 @@ testRun(void) // ***************************************************************************************************************************** if (testBegin("cmdRemote()")) { - // Create pipes for testing. Read/write is from the perspective of the client. - int pipeRead[2]; - int pipeWrite[2]; - THROW_ON_SYS_ERROR(pipe(pipeRead) == -1, KernelError, "unable to read test pipe"); - THROW_ON_SYS_ERROR(pipe(pipeWrite) == -1, KernelError, "unable to write test pipe"); + // Create default storage object for testing + Storage *storageTest = storageDriverPosixInterface( + storageDriverPosixNew(strNew(testPath()), STORAGE_MODE_FILE_DEFAULT, STORAGE_MODE_PATH_DEFAULT, true, NULL)); + // No remote lock required + // ------------------------------------------------------------------------------------------------------------------------- HARNESS_FORK_BEGIN() { HARNESS_FORK_CHILD_BEGIN(0, true) @@ -57,6 +58,115 @@ testRun(void) HARNESS_FORK_PARENT_END(); } HARNESS_FORK_END(); + + // Remote lock not required + // ------------------------------------------------------------------------------------------------------------------------- + HARNESS_FORK_BEGIN() + { + HARNESS_FORK_CHILD_BEGIN(0, true) + { + StringList *argList = strLstNew(); + strLstAddZ(argList, "pgbackrest"); + strLstAddZ(argList, "--command=archive-get-async"); + strLstAddZ(argList, "--process=0"); + strLstAddZ(argList, "--type=backup"); + strLstAddZ(argList, "--lock-path=/bogus"); + strLstAddZ(argList, "remote"); + harnessCfgLoad(strLstSize(argList), strLstPtr(argList)); + + cmdRemote(HARNESS_FORK_CHILD_READ(), HARNESS_FORK_CHILD_WRITE()); + } + HARNESS_FORK_CHILD_END(); + + HARNESS_FORK_PARENT_BEGIN() + { + IoRead *read = ioHandleReadIo(ioHandleReadNew(strNew("server read"), HARNESS_FORK_PARENT_READ_PROCESS(0), 2000)); + ioReadOpen(read); + IoWrite *write = ioHandleWriteIo(ioHandleWriteNew(strNew("server write"), HARNESS_FORK_PARENT_WRITE_PROCESS(0))); + ioWriteOpen(write); + + ProtocolClient *client = NULL; + TEST_ASSIGN(client, protocolClientNew(strNew("test"), PROTOCOL_SERVICE_REMOTE_STR, read, write), "create client"); + protocolClientNoOp(client); + protocolClientFree(client); + } + HARNESS_FORK_PARENT_END(); + } + HARNESS_FORK_END(); + + // Remote lock required but errors out + // ------------------------------------------------------------------------------------------------------------------------- + HARNESS_FORK_BEGIN() + { + HARNESS_FORK_CHILD_BEGIN(0, true) + { + StringList *argList = strLstNew(); + strLstAddZ(argList, "pgbackrest"); + strLstAddZ(argList, "--stanza=test"); + strLstAddZ(argList, "--command=archive-push-async"); + strLstAddZ(argList, "--process=0"); + strLstAddZ(argList, "--type=backup"); + strLstAddZ(argList, "--lock-path=/bogus"); + strLstAddZ(argList, "remote"); + harnessCfgLoad(strLstSize(argList), strLstPtr(argList)); + + cmdRemote(HARNESS_FORK_CHILD_READ(), HARNESS_FORK_CHILD_WRITE()); + } + HARNESS_FORK_CHILD_END(); + + HARNESS_FORK_PARENT_BEGIN() + { + IoRead *read = ioHandleReadIo(ioHandleReadNew(strNew("server read"), HARNESS_FORK_PARENT_READ_PROCESS(0), 2000)); + ioReadOpen(read); + IoWrite *write = ioHandleWriteIo(ioHandleWriteNew(strNew("server write"), HARNESS_FORK_PARENT_WRITE_PROCESS(0))); + ioWriteOpen(write); + + TEST_ERROR( + protocolClientNew(strNew("test"), PROTOCOL_SERVICE_REMOTE_STR, read, write), PathCreateError, + "raised from test: unable to create path '/bogus': [13] Permission denied"); + } + HARNESS_FORK_PARENT_END(); + } + HARNESS_FORK_END(); + + // Remote lock required + // ------------------------------------------------------------------------------------------------------------------------- + HARNESS_FORK_BEGIN() + { + HARNESS_FORK_CHILD_BEGIN(0, true) + { + StringList *argList = strLstNew(); + strLstAddZ(argList, "pgbackrest"); + strLstAddZ(argList, "--stanza=test"); + strLstAddZ(argList, "--command=archive-push-async"); + strLstAddZ(argList, "--process=0"); + strLstAddZ(argList, "--type=backup"); + strLstAdd(argList, strNewFmt("--lock-path=%s/lock", testPath())); + strLstAddZ(argList, "remote"); + harnessCfgLoad(strLstSize(argList), strLstPtr(argList)); + + cmdRemote(HARNESS_FORK_CHILD_READ(), HARNESS_FORK_CHILD_WRITE()); + } + HARNESS_FORK_CHILD_END(); + + HARNESS_FORK_PARENT_BEGIN() + { + IoRead *read = ioHandleReadIo(ioHandleReadNew(strNew("server read"), HARNESS_FORK_PARENT_READ_PROCESS(0), 2000)); + ioReadOpen(read); + IoWrite *write = ioHandleWriteIo(ioHandleWriteNew(strNew("server write"), HARNESS_FORK_PARENT_WRITE_PROCESS(0))); + ioWriteOpen(write); + + ProtocolClient *client = NULL; + TEST_ASSIGN(client, protocolClientNew(strNew("test"), PROTOCOL_SERVICE_REMOTE_STR, read, write), "create client"); + protocolClientNoOp(client); + + storageExistsNP(storageTest, strNewFmt("--lock-path=%s/lock/test-archive.lock", testPath())); + + protocolClientFree(client); + } + HARNESS_FORK_PARENT_END(); + } + HARNESS_FORK_END(); } FUNCTION_HARNESS_RESULT_VOID(); diff --git a/test/src/module/config/configTest.c b/test/src/module/config/configTest.c index b3abe7d0b..c61ccf02a 100644 --- a/test/src/module/config/configTest.c +++ b/test/src/module/config/configTest.c @@ -63,7 +63,9 @@ testRun(void) TEST_RESULT_INT(cfgLogLevelDefault(), logLevelInfo, "default log level is info"); TEST_RESULT_BOOL(cfgLogFile(), true, "log file is on"); TEST_RESULT_BOOL(cfgLockRequired(), true, "lock is required"); + TEST_RESULT_BOOL(cfgLockRemoteRequired(cfgCmdBackup), true, "remote lock is required"); TEST_RESULT_INT(cfgLockType(), lockTypeBackup, "lock is type backup"); + TEST_RESULT_INT(cfgLockRemoteType(cfgCmdBackup), lockTypeBackup, "remote lock is type backup"); TEST_RESULT_BOOL(cfgParameterAllowed(), false, "parameters not allowed"); TEST_RESULT_VOID(cfgCommandSet(cfgCmdInfo), "command set to info"); @@ -71,7 +73,9 @@ testRun(void) TEST_RESULT_INT(cfgLogLevelStdErrMax(), logLevelTrace, "max stderr log level is trace"); TEST_RESULT_BOOL(cfgLogFile(), false, "log file is off"); TEST_RESULT_BOOL(cfgLockRequired(), false, "lock is not required"); + TEST_RESULT_BOOL(cfgLockRemoteRequired(cfgCmdInfo), false, "remote lock is not required"); TEST_RESULT_INT(cfgLockType(), lockTypeNone, "lock is type none"); + TEST_RESULT_INT(cfgLockRemoteType(cfgCmdInfo), lockTypeNone, "remote lock is type none"); TEST_RESULT_VOID(cfgCommandSet(cfgCmdStanzaCreate), "command set to stanza-create"); TEST_RESULT_BOOL(cfgLockRequired(), true, "lock is required"); diff --git a/test/src/module/protocol/protocolTest.c b/test/src/module/protocol/protocolTest.c index c8405706d..8a855f828 100644 --- a/test/src/module/protocol/protocolTest.c +++ b/test/src/module/protocol/protocolTest.c @@ -136,8 +136,8 @@ testRun(void) strPtr( strNew( "-o|LogLevel=error|-o|Compression=no|-o|PasswordAuthentication=no|repo-host-user@repo-host" - "|pgbackrest --command=archive-get --log-level-file=off --log-level-stderr=error --process=0 --type=backup" - " remote")), + "|pgbackrest --command=archive-get --log-level-file=off --log-level-stderr=error --process=0 --stanza=test1" + " --type=backup remote")), "remote protocol params"); // ------------------------------------------------------------------------------------------------------------------------- @@ -161,7 +161,7 @@ testRun(void) "-o|LogLevel=error|-o|Compression=no|-o|PasswordAuthentication=no|-p|444|repo-host-user@repo-host" "|pgbackrest --command=archive-get --config=/path/pgbackrest.conf --config-include-path=/path/include" " --config-path=/path/config --log-level-file=info --log-level-stderr=error --log-subprocess --process=1" - " --type=backup remote")), + " --stanza=test1 --type=backup remote")), "remote protocol params with replacements"); // ------------------------------------------------------------------------------------------------------------------------- @@ -181,8 +181,8 @@ testRun(void) strPtr( strNew( "-o|LogLevel=error|-o|Compression=no|-o|PasswordAuthentication=no|pgbackrest@repo-host" - "|pgbackrest --command=archive-get --log-level-file=off --log-level-stderr=error --process=3 --type=backup" - " remote")), + "|pgbackrest --command=archive-get --log-level-file=off --log-level-stderr=error --process=3 --stanza=test1" + " --type=backup remote")), "remote protocol params for local"); } [parent: 7db8cedd6847]