From 6e9b6fdca919b9f6fe2bfdb88447ee1f4f7abea7 Mon Sep 17 00:00:00 2001 From: David Steele Date: Fri, 7 Sep 2018 08:03:05 -0700 Subject: [PATCH] Migrate control functions to detect stop files to C from Perl. Basic functions to detect the presence of stanza or all stop files and error when they are present. The functionality to detect stop files without error was not migrated. This functionality is only used by stanza-delete and will be migrated with that command. --- doc/xml/release.xml | 4 ++ src/Makefile | 4 ++ src/command/control/control.c | 48 +++++++++++++++++++++ src/command/control/control.h | 15 +++++++ test/define.yaml | 8 ++++ test/src/module/command/controlTest.c | 61 +++++++++++++++++++++++++++ 6 files changed, 140 insertions(+) create mode 100644 src/command/control/control.c create mode 100644 src/command/control/control.h create mode 100644 test/src/module/command/controlTest.c diff --git a/doc/xml/release.xml b/doc/xml/release.xml index 3492e9357..2855575c0 100644 --- a/doc/xml/release.xml +++ b/doc/xml/release.xml @@ -42,6 +42,10 @@

Migrate walIsPartial(), walIsSegment(), and walSegmentFind() from Perl to C.

+ + +

Migrate control functions to detect stop files to C from Perl.

+
diff --git a/src/Makefile b/src/Makefile index cc58b1e93..8eeabf862 100644 --- a/src/Makefile +++ b/src/Makefile @@ -59,6 +59,7 @@ SRCS = \ command/archive/push/push.c \ command/help/help.c \ command/command.c \ + command/control/control.c \ common/debug.c \ common/encode.c \ common/encode/base64.c \ @@ -152,6 +153,9 @@ command/archive/push/push.o: command/archive/push/push.c command/archive/common. command/command.o: command/command.c common/assert.h common/debug.h common/error.auto.h common/error.h common/lock.h common/log.h common/logLevel.h common/memContext.h common/stackTrace.h common/time.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/string.h common/type/stringList.h common/type/variant.h common/type/variantList.h config/config.auto.h config/config.h config/define.auto.h config/define.h version.h $(CC) $(CFLAGS) -c command/command.c -o command/command.o +command/control/control.o: command/control/control.c command/control/control.h common/debug.h common/error.auto.h common/error.h common/io/filter/filter.h common/io/filter/group.h common/io/read.h common/io/write.h common/lock.h common/log.h common/logLevel.h common/memContext.h common/stackTrace.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/string.h common/type/stringList.h common/type/variant.h common/type/variantList.h config/config.auto.h config/config.h config/define.auto.h config/define.h storage/driver/posix/driverRead.h storage/driver/posix/driverWrite.h storage/fileRead.h storage/fileWrite.h storage/helper.h storage/info.h storage/storage.h version.h + $(CC) $(CFLAGS) -c command/control/control.c -o command/control/control.o + command/help/help.o: command/help/help.c common/assert.h common/debug.h common/error.auto.h common/error.h common/io/handle.h common/lock.h common/log.h common/logLevel.h common/memContext.h common/stackTrace.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/string.h common/type/stringList.h common/type/variant.h common/type/variantList.h config/config.auto.h config/config.h config/define.auto.h config/define.h version.h $(CC) $(CFLAGS) -c command/help/help.c -o command/help/help.o diff --git a/src/command/control/control.c b/src/command/control/control.c new file mode 100644 index 000000000..bef4989db --- /dev/null +++ b/src/command/control/control.c @@ -0,0 +1,48 @@ +/*********************************************************************************************************************************** +Command Control +***********************************************************************************************************************************/ +#include "command/control/control.h" +#include "common/debug.h" +#include "config/config.h" +#include "storage/helper.h" + +/*********************************************************************************************************************************** +Create the stop filename +***********************************************************************************************************************************/ +String * +lockStopFileName(const String *stanza) +{ + FUNCTION_TEST_BEGIN(); + FUNCTION_TEST_PARAM(STRING, stanza); + FUNCTION_TEST_END(); + + String *result = strNewFmt("%s/%s.stop", strPtr(cfgOptionStr(cfgOptLockPath)), stanza != NULL ? strPtr(stanza) : "all"); + + FUNCTION_TEST_RESULT(STRING, result); +} + +/*********************************************************************************************************************************** +Test for the existence of a stop file +***********************************************************************************************************************************/ +void +lockStopTest(void) +{ + FUNCTION_DEBUG_VOID(logLevelDebug); + + MEM_CONTEXT_TEMP_BEGIN() + { + // Check the current stanza (if any) + if (cfgOptionTest(cfgOptStanza)) + { + if (storageExistsNP(storageLocal(), lockStopFileName(cfgOptionStr(cfgOptStanza)))) + THROW_FMT(StopError, "stop file exists for stanza %s", strPtr(cfgOptionStr(cfgOptStanza))); + } + + // Check all stanzas + if (storageExistsNP(storageLocal(), lockStopFileName(NULL))) + THROW(StopError, "stop file exists for all stanzas"); + } + MEM_CONTEXT_TEMP_END(); + + FUNCTION_DEBUG_RESULT_VOID(); +} diff --git a/src/command/control/control.h b/src/command/control/control.h new file mode 100644 index 000000000..b48fbe193 --- /dev/null +++ b/src/command/control/control.h @@ -0,0 +1,15 @@ +/*********************************************************************************************************************************** +Command Control +***********************************************************************************************************************************/ +#ifndef COMMAND_CONTROL_CONTROL_H +#define COMMAND_CONTROL_CONTROL_H + +#include "common/type/string.h" + +/*********************************************************************************************************************************** +Functions +***********************************************************************************************************************************/ +String *lockStopFileName(const String *stanza); +void lockStopTest(void); + +#endif diff --git a/test/define.yaml b/test/define.yaml index 64bf89d7e..1f621e487 100644 --- a/test/define.yaml +++ b/test/define.yaml @@ -587,6 +587,14 @@ unit: coverage: command/command: full + test: + # ---------------------------------------------------------------------------------------------------------------------------- + - name: control + total: 2 + + coverage: + command/control/control: full + # ******************************************************************************************************************************** - name: archive diff --git a/test/src/module/command/controlTest.c b/test/src/module/command/controlTest.c new file mode 100644 index 000000000..bb0b717ab --- /dev/null +++ b/test/src/module/command/controlTest.c @@ -0,0 +1,61 @@ +/*********************************************************************************************************************************** +Test Command Control +***********************************************************************************************************************************/ +#include "common/harnessConfig.h" + +/*********************************************************************************************************************************** +Test Run +***********************************************************************************************************************************/ +void +testRun(void) +{ + FUNCTION_HARNESS_VOID(); + + // Create default storage object for testing + Storage *storageTest = storageNewP(strNew(testPath()), .write = true); + + // ***************************************************************************************************************************** + if (testBegin("lockStopFileName()")) + { + // Load configuration so lock path is set + StringList *argList = strLstNew(); + strLstAddZ(argList, "pgbackrest"); + strLstAddZ(argList, "--stanza=db"); + strLstAddZ(argList, "--lock-path=/path/to/lock"); + strLstAddZ(argList, "archive-get"); + harnessCfgLoad(strLstSize(argList), strLstPtr(argList)); + + TEST_RESULT_STR(strPtr(lockStopFileName(NULL)), "/path/to/lock/all.stop", "stop file for all stanzas"); + TEST_RESULT_STR(strPtr(lockStopFileName(strNew("db"))), "/path/to/lock/db.stop", "stop file for on stanza"); + } + + // ***************************************************************************************************************************** + if (testBegin("lockStopTest()")) + { + StringList *argList = strLstNew(); + strLstAddZ(argList, "pgbackrest"); + strLstAdd(argList, strNewFmt("--lock-path=%s", testPath())); + strLstAddZ(argList, "start"); + harnessCfgLoad(strLstSize(argList), strLstPtr(argList)); + + TEST_RESULT_VOID(lockStopTest(), "no stop files without stanza"); + + // ------------------------------------------------------------------------------------------------------------------------- + argList = strLstNew(); + strLstAddZ(argList, "pgbackrest"); + strLstAddZ(argList, "--stanza=db"); + strLstAdd(argList, strNewFmt("--lock-path=%s", testPath())); + strLstAddZ(argList, "start"); + harnessCfgLoad(strLstSize(argList), strLstPtr(argList)); + + TEST_RESULT_VOID(lockStopTest(), "no stop files with stanza"); + + storagePutNP(storageNewWriteNP(storageTest, strNew("all.stop")), NULL); + TEST_ERROR(lockStopTest(), StopError, "stop file exists for all stanzas"); + + storagePutNP(storageNewWriteNP(storageTest, strNew("db.stop")), NULL); + TEST_ERROR(lockStopTest(), StopError, "stop file exists for stanza db"); + } + + FUNCTION_HARNESS_RESULT_VOID(); +}