pgbackrest.git / summary / log / commit / refs
commit 0eaedc9a6ae98eaf4e733a3f63cb462a5ebcfa05
Author: David Steele <david@pgmasters.net>
Date: Tue Feb 11 02:17:11 2020 +0000
Improve async archive error file removal.
2a06df93 removed the error file so an old error would not be reported before the async process had a chance to try again. However, if the async process was already running this might lead to a timeout error before reporting the correct error.
Instead, remove the error files once we know that the async process will start, i.e. after the archive lock has been acquired.
This effectively reverts 2a06df93.
src/command/archive/common.c | 39 ++++++++++++++++++-----------
src/command/archive/common.h | 4 +++
src/command/archive/get/get.c | 3 +++
src/command/archive/push/push.c | 3 +++
test/src/module/command/archiveCommonTest.c | 22 ++++++++++------
5 files changed, 49 insertions(+), 22 deletions(-)
diff --git a/src/command/archive/common.c b/src/command/archive/common.c
index 12d387ba9..57b0e8306 100644
--- a/src/command/archive/common.c
+++ b/src/command/archive/common.c
@@ -33,7 +33,8 @@ Global error file constant
#define STATUS_FILE_GLOBAL "global"
STRING_STATIC(STATUS_FILE_GLOBAL_STR, STATUS_FILE_GLOBAL);
-STRING_STATIC(STATUS_FILE_GLOBAL_ERROR_STR, STATUS_FILE_GLOBAL STATUS_EXT_ERROR);
+#define STATUS_FILE_GLOBAL_ERROR STATUS_FILE_GLOBAL STATUS_EXT_ERROR
+ STRING_STATIC(STATUS_FILE_GLOBAL_ERROR_STR, STATUS_FILE_GLOBAL_ERROR);
/***********************************************************************************************************************************
Get the correct spool queue based on the archive mode
@@ -48,6 +49,22 @@ archiveAsyncSpoolQueue(ArchiveMode archiveMode)
FUNCTION_TEST_RETURN((archiveMode == archiveModeGet ? STORAGE_SPOOL_ARCHIVE_IN_STR : STORAGE_SPOOL_ARCHIVE_OUT_STR));
}
+/**********************************************************************************************************************************/
+void
+archiveAsyncErrorClear(ArchiveMode archiveMode, const String *archiveFile)
+{
+ FUNCTION_LOG_BEGIN(logLevelDebug);
+ FUNCTION_LOG_PARAM(ENUM, archiveMode);
+ FUNCTION_LOG_PARAM(STRING, archiveFile);
+ FUNCTION_LOG_END();
+
+ storageRemoveP(
+ storageSpoolWrite(), strNewFmt(STORAGE_SPOOL_ARCHIVE_OUT "/%s" STATUS_EXT_ERROR, strPtr(archiveFile)));
+ storageRemoveP(storageSpoolWrite(), STRDEF(STORAGE_SPOOL_ARCHIVE_OUT "/" STATUS_FILE_GLOBAL_ERROR));
+
+ FUNCTION_LOG_RETURN_VOID();
+}
+
/***********************************************************************************************************************************
Check for ok/error status files in the spool in/out directory
***********************************************************************************************************************************/
@@ -137,22 +154,14 @@ archiveAsyncStatus(ArchiveMode archiveMode, const String *walSegment, bool throw
result = true;
}
- else
+ else if (throwOnError)
{
- if (throwOnError)
- {
- // Error status files must have content
- if (strSize(content) == 0)
- THROW_FMT(AssertError, "status file '%s' has no content", strPtr(statusFile));
-
- // Throw error using the code passed in the file
- THROW_CODE(code, strPtr(message));
- }
+ // Error status files must have content
+ if (strSize(content) == 0)
+ THROW_FMT(AssertError, "status file '%s' has no content", strPtr(statusFile));
- // Remove the error if it will not be thrown to give the async process a chance to retry. Otherwise, it could be a
- // race between the foreground process retrying and the async process removing error files. Ignore missing since the
- // async process may have already removed the file.
- storageRemoveP(storageSpoolWrite(), strNewFmt("%s/%s", strPtr(spoolQueue), strPtr(errorFile)));
+ // Throw error using the code passed in the file
+ THROW_CODE(code, strPtr(message));
}
}
}
diff --git a/src/command/archive/common.h b/src/command/archive/common.h
index 4ebe1934c..8e92885ea 100644
--- a/src/command/archive/common.h
+++ b/src/command/archive/common.h
@@ -58,6 +58,10 @@ WAL segment constants
/***********************************************************************************************************************************
Functions
***********************************************************************************************************************************/
+// Remove errors for an archive file. This should be done before forking the async process to prevent a race condition where an
+// old error may be reported rather than waiting for the async process to succeed or fail.
+void archiveAsyncErrorClear(ArchiveMode archiveMode, const String *archiveFile);
+
bool archiveAsyncStatus(ArchiveMode archiveMode, const String *walSegment, bool throwOnError);
void archiveAsyncStatusOkWrite(ArchiveMode archiveMode, const String *walSegment, const String *warning);
void archiveAsyncStatusErrorWrite(ArchiveMode archiveMode, const String *walSegment, int code, const String *message);
diff --git a/src/command/archive/get/get.c b/src/command/archive/get/get.c
index f3c034ec1..0dc98f999 100644
--- a/src/command/archive/get/get.c
+++ b/src/command/archive/get/get.c
@@ -229,6 +229,9 @@ cmdArchiveGet(void)
for (unsigned int queueIdx = 0; queueIdx < strLstSize(queue); queueIdx++)
strLstAdd(commandExec, strLstGet(queue, queueIdx));
+ // Clear errors for the current wal segment
+ archiveAsyncErrorClear(archiveModeGet, walSegment);
+
// Release the lock so the child process can acquire it
lockRelease(true);
diff --git a/src/command/archive/push/push.c b/src/command/archive/push/push.c
index bdd7d6659..5ba106e2c 100644
--- a/src/command/archive/push/push.c
+++ b/src/command/archive/push/push.c
@@ -305,6 +305,9 @@ cmdArchivePush(void)
strLstInsert(commandExec, 0, cfgExe());
strLstAdd(commandExec, strPath(walFile));
+ // Clear errors for the current archive file
+ archiveAsyncErrorClear(archiveModePush, archiveFile);
+
// Release the lock so the child process can acquire it
lockRelease(true);
diff --git a/test/src/module/command/archiveCommonTest.c b/test/src/module/command/archiveCommonTest.c
index 753e4659a..86c72466e 100644
--- a/test/src/module/command/archiveCommonTest.c
+++ b/test/src/module/command/archiveCommonTest.c
@@ -22,7 +22,7 @@ testRun(void)
strNew(testPath()), STORAGE_MODE_FILE_DEFAULT, STORAGE_MODE_PATH_DEFAULT, true, NULL);
// *****************************************************************************************************************************
- if (testBegin("archiveAsyncStatus()"))
+ if (testBegin("archiveAsyncErrorClear() and archiveAsyncStatus()"))
{
StringList *argList = strLstNew();
strLstAdd(argList, strNewFmt("--spool-path=%s", testPath()));
@@ -44,6 +44,20 @@ testRun(void)
TEST_RESULT_BOOL(archiveAsyncStatus(archiveModePush, segment, false), false, "status file not present");
+ // -------------------------------------------------------------------------------------------------------------------------
+ TEST_TITLE("clear archive file errors");
+
+ const String *errorSegment = strNewFmt(STORAGE_SPOOL_ARCHIVE_OUT "/%s.error", strPtr(segment));
+ const String *errorGlobal = strNew(STORAGE_SPOOL_ARCHIVE_OUT "/global.error");
+
+ storagePutP(storageNewWriteP(storageSpoolWrite(), errorSegment), NULL);
+ storagePutP(storageNewWriteP(storageSpoolWrite(), errorGlobal), NULL);
+
+ TEST_RESULT_VOID(archiveAsyncErrorClear(archiveModePush, segment), "clear error");
+
+ TEST_RESULT_BOOL(storageExistsP(storageSpool(), errorSegment), false, " check segment error");
+ TEST_RESULT_BOOL(storageExistsP(storageSpool(), errorGlobal), false, " check global error");
+
// -------------------------------------------------------------------------------------------------------------------------
storagePutP(
storageNewWriteP(storageSpoolWrite(), strNewFmt(STORAGE_SPOOL_ARCHIVE_OUT "/%s.ok", strPtr(segment))),
@@ -95,14 +109,8 @@ testRun(void)
storageNewWriteP(storageSpoolWrite(), strNewFmt(STORAGE_SPOOL_ARCHIVE_OUT "/%s.error", strPtr(segment))),
BUFSTRDEF("25\nmessage"));
TEST_ERROR(archiveAsyncStatus(archiveModePush, segment, true), AssertError, "message");
- TEST_RESULT_BOOL(
- storageExistsP(storageSpool(), strNewFmt(STORAGE_SPOOL_ARCHIVE_OUT "/%s.error", strPtr(segment))), true,
- "error file was not removed");
TEST_RESULT_BOOL(archiveAsyncStatus(archiveModePush, segment, false), false, "suppress error");
- TEST_RESULT_BOOL(
- storageExistsP(storageSpool(), strNewFmt(STORAGE_SPOOL_ARCHIVE_OUT "/%s.error", strPtr(segment))), false,
- "error file was removed");
// -------------------------------------------------------------------------------------------------------------------------
storagePutP(
[parent: 8cfbc294fcc0]