Skip to content

Optimize space efficiency with respect to blob storage #939

Closed

Description

@danielmewes

@srh noted here #310 (comment) that RethinkDB is wasting a significant amount of space for documents that are larger than 250 bytes after internal encoding. While part of this might be solved by making the encoding more efficient, it also highlights an issue with the current tuning of in-tree vs. blob storage of values.

The problem is, that as soon as a document becomes larger than 250 bytes, it will use a whole block of its own, taking 4 KB of disk and cache space.

This is obviously relevant to disk usage concerns. It will also have an impact on speed for somewhat larger data sets, where cache efficiency matters.

We should do two things:

  • double-check the effect this has on real-world data sets
  • look into a solution. Probably, we should increase the threshold for blob storage. We could also consider sharing blocks between multiple blobs, but that might be extremely complicated and error-prone.

After some consideration, I've come to believe that the maximal in-tree value size should be close to the block size (minus max key size and metadata overhead).
As far as I can see, the only principal advantage of putting below-block-size values into a blob instead of inlining them into the main tree is that the btree can effectively have a depth which is one level larger for only part of the key range. This applies if large documents are clustered in only a few regions. If all or most documents are large, I think the blobs would be essentially just like having an additional level of leaf nodes with one key/value per leaf node.

I might well have made mistakes in my thought-model of how the btree behaves. Please correct me if you find an indication of such.

@srh, I think you have a good understanding of the btree code. Do you know if there's some consideration implementation-wise which would make large max values a problem?
One candidate: in config/args.hpp there is the following comment to MAX_IN_NODE_VALUE_SIZE: "This value needs to fit in a byte."
I assume that this is merely to make the the storage of small values slightly more space efficient by being able to store the value size in a single byte. Apparently, some btree structures will have to be changed for supporting larger values.

Comments?

Activity

  1. srh commented on Jun 3, 2013

    @srh
    Contributor

    We were talking about this in the office and came to the conclusion that instead the serializer (and buffer cache) should support variable-sized blocks, that is, any block size less than 4096 or some other fixed value that's small relative to extent size.

    This solves the underused blob problem and also makes more feasible other easy disk usage optimizations such as gzip compression of blocks, or more mundane truncation of underfull btree blocks.

    Changing the MAX_IN_NODE_VALUE_SIZE to something larger than half a block would make btree leveling/splitting/merging logic more complicated -- insertions would now have to accept creating underfull nodes in certain circumstances, without trying to level. And it would have worse performance, disk-usage-wise, than having the serializer support arbitrary block sizes.

  2. srh commented on Jun 3, 2013

    @srh
    Contributor

    Also, making the serializer support variable sized blocks is not that scary. It requires the following changes:

    • The LBA needs to store size alongside offset.
    • Serializer data block GC algorithms are directly generalizable to arbitrary block sizes.
    • Block reads and writes would need to be transformed into aligned reads and writes.
    • The buffer cache needs to support users marking blocks as truncated.
  3. jdoliner commented on Jun 3, 2013

    @jdoliner
    Contributor

    I talked about this with @srh I agree that it's the best direction.

  4. danielmewes commented on Jun 3, 2013

    @danielmewes
    MemberAuthor

    Ok, that makes sense. Except that range scans on out-of-memory tables will still be really slow due to the high number of random io ops.

    This should probably be combined with solving #940 once and for all, by making the max block id ridiculously high.

    -----Original Message-----
    From: srh [email protected]
    Date: Mon, 03 Jun 2013 10:26:59
    To: rethinkdb/[email protected]
    Reply-To: rethinkdb/rethinkdb [email protected]
    Cc: Daniel [email protected]
    Subject: Re: [rethinkdb] Optimize space efficiency with respect to blob
    storage (#939)

    We were talking about this in the office and came to the conclusion that instead the serializer (and buffer cache) should support variable-sized blocks, that is, any block size less than 4096 or some other fixed value that's small relative to extent size.

    This solves the underused blob problem and also makes more feasible other easy disk usage optimizations such as gzip compression of blocks, or more mundane truncation of underfull btree blocks.

    Changing the MAX_IN_NODE_VALUE_SIZE to something larger than half a block would make btree leveling/splitting/merging logic more complicated -- insertions would now have to accept creating underfull nodes in certain circumstances, without trying to level. And it would have worse performance, disk-usage-wise, than having the serializer support arbitrary block sizes.


    Reply to this email directly or view it on GitHub:
    #939 (comment)

  5. ghost assigned on Jun 3, 2013
  6. srh commented on Jun 3, 2013

    @srh
    Contributor

    I'm currently working on this issue in branch sam_939.

  7. danielmewes commented on Jun 3, 2013

    @danielmewes
    MemberAuthor

    I just had another thought, making me think that having variable-sized blocks is even more useful, even from the performance perspective (not that it matters too much anymore, as Sam is already on it anyways).

    -- Background: --
    Due to its initial focus on SSDs, RethinkDB does not make much effort to have on-disk data locality. Thus on rotational disks, I would expect it to be quite slow under read-heavy out-of-memory workloads. Back when we tested point gets with the memcached protocol, this wasn't much of a disadvantage. If you do a random point get, every database system has to perform at least one read, and it was the same with RethinkDB (to acquire the one leaf node that contained the value). However with somewhat larger documents, values would currently be split over multiple blocks that could in theory end up being scattered all over the disk. Depending on which order they are processed by the serializer, they will still be sequential, but I consider that to be more like a lucky coincidence of the current implementation, than a conceptual property.
    -- /Background --

    With variably sized blocks, we could increase the maximal block size somewhat (say to 128 KB), so relatively large documents would still fit into a single contiguous block. At the same time, btree nodes could keep the 4 KB size to maintain the advantages of more fine-grained locking, caching and CoW.
    This will give us lots of opportunities for tuning in the future. :-)

  8. coffeemug commented on Jun 7, 2013

    @coffeemug
    Contributor

    Moving this into 1.6 since this issue is really the one @srh is working on.

  9. danielmewes commented on Jun 17, 2013

    @danielmewes
    MemberAuthor

    @srh: Is this done? Which branch is it in?

  10. srh commented on Jun 18, 2013

    @srh
    Contributor

    Currently worked on in sam_939_alt.

  11. coffeemug commented on Jun 18, 2013

    @coffeemug
    Contributor

    Merging #940 into this issue.

  12. srh commented on Jun 26, 2013

    @srh
    Contributor

    My expectation is that this will be finished around July 5th.

  13. coffeemug commented on Jun 27, 2013

    @coffeemug
    Contributor

    Since this isn't making it into 1.7, moving to optional.

    @srh -- could you please give a more extensive status update as specified in http://www.defmacro.org/2013/04/03/issue-etiquette.html EG3/EG3a? Specifically, I would be very helpful to know the following:

    • What’s already been done
    • What remains to be done and a plan on how to do it
    • A list of known roadblocks and a plan on how to solve them
    • What you’re working on next
    • An idea of complications you ran into that extended the original ETA
  14. srh commented on Jul 1, 2013

    @srh
    Contributor

    The current todo list is:

    • making the data block manager support grouped writes instead of one block at a time (every other piece of infrastructure regarding oddly sized writes is in place in the serializer and data block manager)
    • making the buffer cache support specifying smaller blocks (this was done before in an abandoned branch that attacked the problem starting with the cache instead of the serializer, so its difficulty is known -- and not large)
    • actually specifying smaller blocks (in blob code)

    This is sufficient to meet the needs of the issue.

    A further nice-to-have would be:

    • not allocating a full 4K block in memory for a block that's actually only 300 bytes.

    That's somewhat tricky.

  15. coffeemug commented on Jul 1, 2013

    @coffeemug
    Contributor

    Why is the last bit tricky?

  16. 9 remaining items

  17. srh commented on Jul 12, 2013

    @srh
    Contributor

    Also, we still allocate 4KB blocks in RAM. Don't expect that to change soon. Unless we refactor the buffer cache.

  18. coffeemug commented on Jul 12, 2013

    @coffeemug
    Contributor

    The buffer cache blocks are concatenated on the serializer thread so that they can be written all squashed together.

    Ah, I see. I think the serializer is being smarter than required (and preferable). A third option is to write on a 512 byte granularity (i.e. if a block is smaller than 512 bytes, we write 512 bytes anyway), which is the smallest block size disks allow. This wouldn't require any copying at all and would still be ~8x better from the space efficiency POV than we are. It would also allow the disk controller's DMA to transfer data without blowing the cache. Is there a reason not to do it this way? I think copying memory blocks before handing them to disk is a big show stopper in this case.

    We still allocate 4KB blocks in RAM. Don't expect that to change soon. Unless we refactor the buffer cache.

    Can you explain what makes this is difficult?

  19. srh commented on Jul 15, 2013

    @srh
    Contributor

    Can you explain what makes this is difficult?

    Pointer comparison logic used on the void * buffers. It's not that difficult to work around, you could just add a 64-bit nonce field to everything to represent buffer identity instead of using the raw pointer values.

    A third option is to write on a 512 byte granularity (i.e. if a block is smaller than 512 bytes, we write 512 bytes anyway), which is the smallest block size disks allow.

    We are padding to 512 bytes now in sam_939_alt (the main development branch for this issue) but the buf concatenation logic has not been removed. In sam_939_iovec, this is replaced with a use of writev, so that we can avoid hogging the blocker pool threads.

  20. srh commented on Jul 15, 2013

    @srh
    Contributor

    We have these things to do:

    • Support file truncation.
    • Support larger block ids.
  21. coffeemug commented on Jul 15, 2013

    @coffeemug
    Contributor

    Are you also using readv on the way back?

  22. srh commented on Jul 15, 2013

    @srh
    Contributor

    No -- reads never need to be splayed out to individual buffers. Except maybe in the case of readahead. But that code hasn't been touched.

  23. coffeemug commented on Jul 16, 2013

    @coffeemug
    Contributor

    FYI, I created a separate issue to track the memory utilization aspect of this (#1173). This way we can schedule it separately.

  24. srh commented on Jul 17, 2013

    @srh
    Contributor

    This is currently in code review 744.

  25. srh commented on Jul 25, 2013

    @srh
    Contributor

    This is merged to next as of 91b53e3.

  26. srh commented on Jul 25, 2013

    @srh
    Contributor

    Just as a general description of what was done:

    • The buffer cache now supports specifying a smaller than typical block size, by passing a parameter to get_data_write.
    • The serializer pads out smaller blocks to the nearest multiple of 512 when writing them to disk.
    • Arbitrary limits like MAX_BLOCK_ID and the limit on the maximum number of data extents are removed.
    • block id's are now 64 bits.
  27. coffeemug commented on Jul 26, 2013

    @coffeemug
    Contributor

    @srh -- I didn't look at the code, but I assume the last two points above imply you've replaced the two level array and friends with a different data structure. Could you describe how that data structure works?

  28. srh commented on Jul 27, 2013

    @srh
    Contributor

    Right now they're replaced with infinite_array_t which uses a std::vector internally and a direct std::vector in one other case.

  29. coffeemug commented on Jul 27, 2013

    @coffeemug
    Contributor

    I think we have to fix it before 1.8 goes out. Having three second pauses while std::vector reallocs is a problem.

  30. srh commented on Jul 30, 2013

    @srh
    Contributor

    The fix for #1213 is now in next, so that is no longer an issue (in the places touched here).

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