[PATCH 1/3] xlog: erase eof marker when reopening existing file for writing

Vladimir Davydov vdavydov.dev at gmail.com
Fri Jun 15 18:48:20 MSK 2018


When reopening an existing xlog file (as in case of vylog), we do not
erase the eof marker immediately. Instead we reposition file offset
to (file_size - sizeof eof_marker), assuming the eof marker will be
overwritten on the first write.

However, it isn't enough if we want to reuse this function for reopening
WAL files, because when scanning the WAL directory we close a file if we
read eof marker and never reopen it again, see recover_remaining_wals().
So to avoid skipping rows written to a once closed WAL, we have to erase
the eof marker when reopening an xlog file. Let's do it with truncate().
---
 src/box/xlog.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/src/box/xlog.c b/src/box/xlog.c
index be7b3459..142262b4 100644
--- a/src/box/xlog.c
+++ b/src/box/xlog.c
@@ -787,10 +787,7 @@ xlog_open(struct xlog *xlog, const char *name)
 		goto err_read;
 	}
 
-	/*
-	 * If the file has eof marker, reposition the file pointer so
-	 * that the next write will overwrite it.
-	 */
+	/* Check if the file has EOF marker. */
 	xlog->offset = fio_lseek(xlog->fd, -(off_t)sizeof(magic), SEEK_END);
 	if (xlog->offset < 0)
 		goto no_eof;
@@ -809,6 +806,13 @@ no_eof:
 				 xlog->filename);
 			goto err_read;
 		}
+	} else {
+		/* Truncate the file to erase the EOF marker. */
+		if (ftruncate(xlog->fd, xlog->offset) != 0) {
+			diag_set(SystemError, "failed to truncate file '%s'",
+				 xlog->filename);
+			goto err_read;
+		}
 	}
 	return 0;
 err_read:
-- 
2.11.0




More information about the Tarantool-patches mailing list