Make index rebuilds slightly more efficient by writing prepares from the same stream in a batch (DB-369) #3942
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixed: Make index rebuilds slightly more efficient by writing prepares from the same stream in a batch
Flags.HasAnyOf() bug
During index rebuilds at startup, this snippet of code had a bug but it turns out that it did not have any noticeable negative impact:
The bug is that
prepare.Flags.HasAnyOf(PrepareFlags.SingleWrite)
should have beenprepare.Flags.HasAllOf(PrepareFlags.SingleWrite)
as SingleWrite is a composite flag:SingleWrite = Data | TransactionBegin | TransactionEnd
HasAnyOf
returns true if any of theData
,TransactionBegin
orTransactionEnd
flags are present on the prepare log record.Due to the bug, the
else
part in theif-else
condition was never executed as described below:IsCommitted
flag is set: a normal event write, an empty write and a stream delete (tombstone)Data
flag, so theif
block is enteredTransactionBegin
/TransactionEnd
flags set, so theif
block is enteredTransactionBegin
/TransactionEnd
flags set (together with theStreamDelete
flag), so theif
block is enteredImpact
The net effect is that:
IsCommitted
flag were committed to the index one at a time (this is not necessarily a bad thing in terms of performance as it would have happened for single writes anyway)This PR
The current code works fine but it is misleading due to the above bug. This PR rewrites this part to remove any ambiguity. It also takes the opportunity to add the events from the same stream in a batch to the index (it's not a very significant optimization but it's still better than committing to the index one at a time)
There should be no change in behaviour, except that:
Storage chaser initialization
Note that the index committer rebuilds the index from the index checkpoint up to the chaser checkpoint at startup - not the writer checkpoint. The remaining index rebuild (from the chaser to the writer checkpoint) is done by the storage chaser at startup. So if the chaser = writer at startup, then we can be sure that all events will be added to the index. but if chaser < writer and there is a torn transaction at the end of the log at startup, it will not be added by the storage chaser to the index (this can have unintended consequences such as duplicate event numbers). This is no longer a problem due to the 3 PRs (#3808, #3918 and #3896) that prevent torn transactions in the log. Note that there can still be a torn transaction at startup in the log when upgrading to the version with these fixes but the problem will no longer occur as from the next startup.