Skip to content

Commit

Permalink
[contrib/zenith] [refer #225] if insert WAL position points at the en…
Browse files Browse the repository at this point in the history
…d of WAL page header, then return it back to the page origin
  • Loading branch information
knizhnik authored and tristan957 committed May 10, 2024
1 parent 45cb5c7 commit 96e2a16
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion contrib/zenith/pagestore_smgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "access/xlog.h"
#include "access/xloginsert.h"
#include "access/xlog_internal.h"
#include "pagestore_client.h"
#include "storage/relfilenode.h"
#include "storage/smgr.h"
Expand Down Expand Up @@ -358,6 +359,29 @@ zenith_init(void)
#endif
}

/*
* GetXLogInsertRecPtr uses XLogBytePosToRecPtr to convert logical insert (reserved) position
* to physical position in WAL. It always adds SizeOfXLogShortPHD:
* seg_offset += fullpages * XLOG_BLCKSZ + bytesleft + SizeOfXLogShortPHD;
* so even if there are no records on the page, offset will be SizeOfXLogShortPHD.
* It may cause problems with XLogFlush. So return pointer backward to the origin of the page.
*/
static XLogRecPtr
zm_adjust_lsn(XLogRecPtr lsn)
{
/* If lsn points to the beging of first record on page or segment,
* then "return" it back to the page origin
*/
if ((lsn & (XLOG_BLCKSZ-1)) == SizeOfXLogShortPHD)
{
lsn -= SizeOfXLogShortPHD;
}
else if ((lsn & (wal_segment_size-1)) == SizeOfXLogLongPHD)
{
lsn -= SizeOfXLogLongPHD;
}
return lsn;
}

/*
* Return LSN for requesting pages and number of blocks from page server
Expand Down Expand Up @@ -388,7 +412,6 @@ zenith_get_request_lsn(bool nonrel)
}
else
{
lsn = GetLastWrittenPageLSN();
flushlsn = GetFlushRecPtr();

/*
Expand All @@ -412,6 +435,8 @@ zenith_get_request_lsn(bool nonrel)
elog(DEBUG1, "zenith_get_request_lsn GetFlushRecPtr lsn %X/%X",
(uint32) ((lsn) >> 32), (uint32) (lsn));
}
else
lsn = zm_adjust_lsn(lsn);

/*
* Is it possible that the last-written LSN is ahead of last flush LSN? Probably not,
Expand Down Expand Up @@ -858,6 +883,8 @@ zenith_truncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
*/
lsn = GetXLogInsertRecPtr();

lsn = zm_adjust_lsn(lsn);

/*
* Flush it, too. We don't actually care about it here, but let's uphold
* the invariant that last-written LSN <= flush LSN.
Expand Down

0 comments on commit 96e2a16

Please sign in to comment.