Skip to content

Investigate and throttle secondary index creation #1556

Closed

Description

@coffeemug

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.

Activity

  1. danielmewes commented on Oct 30, 2013

    @danielmewes
    Member

    Taking this.

  2. danielmewes commented on Oct 30, 2013

    @danielmewes
    Member

    There seem to be two aspects to this:

    1. 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.
    2. 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.
  3. danielmewes commented on Oct 31, 2013

    @danielmewes
    Member

    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:

    1. 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.
    2. It acquires the superblock of the btree in write mode
    3. It acquires the sindex block in write mode. This waits on the token_pair.
    4. It acquires the superblocks of the sindex btrees in write mode
    5. It releases the locks on the btree superblock (from step 1) and on the sindex block (step 2)
    6. It updates the secondary index
    7. 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.

  4. danielmewes commented on Oct 31, 2013

    @danielmewes
    Member

    The fix is in code review 1003 by @jdoliner, and available in branch daniel_backfill_latency_tweaks.

  5. danielmewes commented on Oct 31, 2013

    @danielmewes
    Member

    Before:
    sindex before 4 22 15

    After:
    sindex after 4 07 15

    :-)

  6. mlucy commented on Nov 1, 2013

    @mlucy
    Member

    applause

    How much does this slow down sindex creation, out of curiosity?

  7. danielmewes commented on Nov 1, 2013

    @danielmewes
    Member

    @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.

  8. neumino commented on Nov 1, 2013

    @neumino
    Member

    Woow, nice @danielmewes!

  9. danielmewes commented on Nov 1, 2013

    @danielmewes
    Member

    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. ;-)

  10. danielmewes commented on Nov 1, 2013

    @danielmewes
    Member

    @mlucy: Here are the index creation times for a table of 260K documents

    No load, cold cache:

    Before:   1:05
    After:    1:06
    

    No load, warm cache:

    Before:   0:23
    After:    0:23
    

    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):

    Before:   1:07
    After:    1:08
    

    Read load, warm cache:

    Before:   0:25
    After:    0:27
    
  11. mlucy commented on Nov 1, 2013

    @mlucy
    Member

    Awesome.

  12. danielmewes commented on Nov 13, 2013

    @danielmewes
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions