You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Exact timestamps for every row, no sindex post construction queues. #2134
A long time ago when implementing two-machine replication, in order to prevent costly memory overhead for rows, we decided to only store replication timestamps for the first few rows in a leaf node, under the idea that a replica would only be disconnected for a short amount of time.
Instead, we should store repli_timestamp_t values for every row. That will make backfilling do less work and it will make sindex post construction not have to queue writes -- we know which write "wins" in the secondary index by seeing which has the later timestamp.
One problem here is that we do want to drop deletion entries (that are stored in the b-tree) at some point. We can't just store deletion entries for all these ancient documents. Fortunately, some of the benefits from exact timestamps -- such as more efficient secondary index post-construction, wouldn't require us to store all of our deletion entries.
We will NOT have a long-running snapshot of the main b-tree while post-constructing a secondary index.
The important timestamps are in the main b-tree. Secondary index entries need a different sort of metadata.
All new key/value pairs in the main b-tree are timestamped. Old ones, from version 1.14 and earlier, might not be, and that's OK, because the only question we'll need to answer, when looking at a timestamp, is whether it's before or after the time when the secondary index began post-constructing. (This differs from the existing implementation, where only some of the newest key/value pairs are timestamped. We will continue rapidly expiring deletion entries, which are always timestamped.)
Define "old{t0} value" to mean a value whose timestamp is <= t0. And "new{t0} value" is one that is not old{t0}.
When a post-construction begins with the latest value in the tree having timestamp t0, the post-constructing secondary index's entries will have metadata identifying it as a member of one of the following categories:
An old{t0} value, that was inserted by the post-construction process.
A new{t0} deletion entry.
A new{t0} value: also, a deletion entry should be left behind when the value is removed.
A new{t0} value: also, NO deletion entry should be left behind when the value is removed.
The post-construction "process" that walks the b-tree does a series of small range traversals that covers the entire key range. When it comes across an old{t0} key/value pair, it throws it at the secondary index in a separate write transaction. In that write transaction, if a new{t0} value or deletion entry is present, it is untouched, except to say that if the value is removed, a deletion entry should be left behind. Otherwise, the old{t0} value gets inserted, if one isn't already present.
When a new write comes in and modifies a value, it (naturally) updates the secondary indexes. Generally speaking, it has to remove the value from its old location (if an old value existed) and insert a value at the new location (if it's not a deletion). In a post-constructing index, the old value (which exists if the write is not a fresh insertion) and secondary index might be in one of several states:
An old{t0} value, with no entry present in the secondary index: The post-construction process has not yet inserted it. We must leave behind a deletion entry for its key.
An old{t0} value, that was inserted by the post-construction process: We must replace it with a deletion entry.
A new{t0} value, and the entry says that a deletion entry should be left behind: We must replace it with a deletion entry.
A new{t0} value, and the entry says that NO deletion entry should be left behind: We must remove it.
We find a deletion entry already present: Crash, this is impossible.
In a post-constructing index, the new value (which exists if the write is not a deletion) is always a new{t0} value. The secondary index, after removing the old value, might be in one of several states:
Has a deletion entry for that value's key: Replace it with a new{t0} value, with the entry saying that a deletion entry should be left behind.
Has nothing for the value's key: Insert a new{t0} value, with the entry saying that NO deletion entry should be left behind.
It is permissible for the post-construction process to accidentally re-scan the same section of the b-tree and reprocess the same key/value pairs. If the post-construction process walks the b-tree from left to right, we could save the end of the last completed post-construction key range in the secondary index superblock (or somewhere) to allow post-construction to resume after a shutdown and restart.
Could we avoid having to add timestamps to the main btree if we instead introduced a guarantee that all writes update the secondary index even if it's still in post construction?
In that case we could detect values that fall into the new{t0} category simply by the fact that they already have an entry (possibly a deletion entry) in the secondary index tree when post construction gets to them. Post construction would tag all pairs it inserts into the secondary index as old, and every sindex update caused by a regular write would be one of the three possible new{t0} entries.
We'd still use timestamps in the main btree for backfilling.
Sure, but we could keep timestamp deprecation and could leave the main btree format unchanged. Not sure how much that would save in terms of implementation costs.
Sure, but we could keep timestamp deprecation and could leave the main btree format unchanged. Not sure how much that would save in terms of implementation costs.
If we did that, then if there's an active write workload the secondary index would need arbitrarily many deletion entries, instead of a number bounded by the size of the main b-tree. Unless post-construction exposed information about what it was currently looking at and we decided to get real clever about not leaving deletion entries most of the time.
Activity
srh commentedon Apr 1, 2014
One problem here is that we do want to drop deletion entries (that are stored in the b-tree) at some point. We can't just store deletion entries for all these ancient documents. Fortunately, some of the benefits from exact timestamps -- such as more efficient secondary index post-construction, wouldn't require us to store all of our deletion entries.
coffeemug commentedon Jun 14, 2014
Moving to 1.14, assigning to @srh.
srh commentedon Aug 16, 2014
How this will work:
We will NOT have a long-running snapshot of the main b-tree while post-constructing a secondary index.
The important timestamps are in the main b-tree. Secondary index entries need a different sort of metadata.
Define "old{t0} value" to mean a value whose timestamp is <= t0. And "new{t0} value" is one that is not old{t0}.
When a post-construction begins with the latest value in the tree having timestamp t0, the post-constructing secondary index's entries will have metadata identifying it as a member of one of the following categories:
The post-construction "process" that walks the b-tree does a series of small range traversals that covers the entire key range. When it comes across an old{t0} key/value pair, it throws it at the secondary index in a separate write transaction. In that write transaction, if a new{t0} value or deletion entry is present, it is untouched, except to say that if the value is removed, a deletion entry should be left behind. Otherwise, the old{t0} value gets inserted, if one isn't already present.
When a new write comes in and modifies a value, it (naturally) updates the secondary indexes. Generally speaking, it has to remove the value from its old location (if an old value existed) and insert a value at the new location (if it's not a deletion). In a post-constructing index, the old value (which exists if the write is not a fresh insertion) and secondary index might be in one of several states:
In a post-constructing index, the new value (which exists if the write is not a deletion) is always a new{t0} value. The secondary index, after removing the old value, might be in one of several states:
It is permissible for the post-construction process to accidentally re-scan the same section of the b-tree and reprocess the same key/value pairs. If the post-construction process walks the b-tree from left to right, we could save the end of the last completed post-construction key range in the secondary index superblock (or somewhere) to allow post-construction to resume after a shutdown and restart.
danielmewes commentedon Aug 18, 2014
👍 Sounds solid.
Could we avoid having to add timestamps to the main btree if we instead introduced a guarantee that all writes update the secondary index even if it's still in post construction?
In that case we could detect values that fall into the new{t0} category simply by the fact that they already have an entry (possibly a deletion entry) in the secondary index tree when post construction gets to them. Post construction would tag all pairs it inserts into the secondary index as old, and every sindex update caused by a regular write would be one of the three possible new{t0} entries.
srh commentedon Aug 18, 2014
We'd still use timestamps in the main btree for backfilling.
danielmewes commentedon Aug 18, 2014
Sure, but we could keep timestamp deprecation and could leave the main btree format unchanged. Not sure how much that would save in terms of implementation costs.
srh commentedon Aug 18, 2014
If we did that, then if there's an active write workload the secondary index would need arbitrarily many deletion entries, instead of a number bounded by the size of the main b-tree. Unless post-construction exposed information about what it was currently looking at and we decided to get real clever about not leaving deletion entries most of the time.
14 remaining items