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
Investigate and throttle secondary index creation #1556
There were a number of reports that creating secondary indexes on largish dbs has significant impact on realtime performance (see #1358 (comment)). We need to investigate where the bottlenecks are coming from and throttle secondary index creation appropriately.
Everything is in-memory: secondary index post-production slows down point-read queries by about 50%. This is already with a couple of small tweaks applied.
Some of the data is out-of-memory: The impact of secondary index on point-reads is drastic. I'm seeing query throughput drop from 1500 qps to ~200 qps. I've tried playing with i/o accounts but that doesn't seem to have any effect at all. I don't know yet if this is actually i/o contention in the i/o layer or if this is some locking artefact in the cache layer. Also note that the cache was still warming up in this experiment, so we probably did read-ahead, causing somewhat larger reads than normal.
It turns out this is a locking issue (in combination with missing yields).
If you take a look at post_construct_traversal_helper_t::process_a_leaf() in rdb_protocol/btree.cc, then you can see that this function does the following things:
It gets a sindex token_pair, which guarantees the ordering of sindex operations. No other sindex operation can continue before the token_pair is released as far as I understand.
It acquires the superblock of the btree in write mode
It acquires the sindex block in write mode. This waits on the token_pair.
It acquires the superblocks of the sindex btrees in write mode
It releases the locks on the btree superblock (from step 1) and on the sindex block (step 2)
It updates the secondary index
It releases the remaining locks and the token_pair
Step 5 can take quite a while, and the sindex token_pair is held during that time. If the function is now called a second time, it will go though up to step 3. Then it has to wait for the previous call to release its token_pair. During all that time it is holding on to the superblock, blocking just every other operation. In practice, up to four calls to the function are active per btree during the construction of a secondary index, so this happens frequently.
The solution turns out to be quite simple: We can actually release the superblock almost immediately after we have acquired it. It also seems that we can acquire it in read instead of write mode, which is even better as it doesn't lock out other reads on the btree.
In addition to this change, we can get secondary index construction to behave extremely benign towards concurrent read operations by adding a yield at a certain place and having it run at low i/o and coroutine priorities. It still impacts concurrent write operations a little, but not as dramatically as it used to impact reads and writes alike.
Hehe I thought I would share this one and not the somewhat less impressive one for write queries (which are still affected, even though less so than before). Seemed more motivating. ;-)
The changes are in next. 4a6aef0. They passed review 1003 by @jdoliner. This version is not as good as what's shown above in the screenshot because it ended up being implemented in a way which interacts with the flush lock of the cache. With the new cache, we should be back to having a close-to-zero impact on reads secondary index creation.
Activity
danielmewes commentedon Oct 30, 2013
Taking this.
danielmewes commentedon Oct 30, 2013
There seem to be two aspects to this:
danielmewes commentedon Oct 31, 2013
It turns out this is a locking issue (in combination with missing yields).
If you take a look at
post_construct_traversal_helper_t::process_a_leaf()in rdb_protocol/btree.cc, then you can see that this function does the following things:Step 5 can take quite a while, and the sindex token_pair is held during that time. If the function is now called a second time, it will go though up to step 3. Then it has to wait for the previous call to release its token_pair. During all that time it is holding on to the superblock, blocking just every other operation. In practice, up to four calls to the function are active per btree during the construction of a secondary index, so this happens frequently.
The solution turns out to be quite simple: We can actually release the superblock almost immediately after we have acquired it. It also seems that we can acquire it in read instead of write mode, which is even better as it doesn't lock out other reads on the btree.
In addition to this change, we can get secondary index construction to behave extremely benign towards concurrent read operations by adding a yield at a certain place and having it run at low i/o and coroutine priorities. It still impacts concurrent write operations a little, but not as dramatically as it used to impact reads and writes alike.
danielmewes commentedon Oct 31, 2013
The fix is in code review 1003 by @jdoliner, and available in branch daniel_backfill_latency_tweaks.
danielmewes commentedon Oct 31, 2013
Before:

After:

:-)
mlucy commentedon Nov 1, 2013
applause
How much does this slow down sindex creation, out of curiosity?
danielmewes commentedon Nov 1, 2013
@mlucy: On an idle database probably not at all. I will measure it in a moment. Would be interesting to have some actual numbers on it.
neumino commentedon Nov 1, 2013
Woow, nice @danielmewes!
danielmewes commentedon Nov 1, 2013
Hehe I thought I would share this one and not the somewhat less impressive one for write queries (which are still affected, even though less so than before). Seemed more motivating. ;-)
danielmewes commentedon Nov 1, 2013
@mlucy: Here are the index creation times for a table of 260K documents
No load, cold cache:
No load, warm cache:
Read load, cold cache (note that the reads are against the same table as the index, so they are not really competing about i/o very much):
Read load, warm cache:
mlucy commentedon Nov 1, 2013
Awesome.
danielmewes commentedon Nov 13, 2013
The changes are in next. 4a6aef0. They passed review 1003 by @jdoliner. This version is not as good as what's shown above in the screenshot because it ended up being implemented in a way which interacts with the flush lock of the cache. With the new cache, we should be back to having a close-to-zero impact on reads secondary index creation.