Repository navigation
Description
@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
srh commentedon Jun 3, 2013
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.
srh commentedon Jun 3, 2013
Also, making the serializer support variable sized blocks is not that scary. It requires the following changes:
jdoliner commentedon Jun 3, 2013
I talked about this with @srh I agree that it's the best direction.
danielmewes commentedon Jun 3, 2013
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)
srh commentedon Jun 3, 2013
I'm currently working on this issue in branch sam_939.
danielmewes commentedon Jun 3, 2013
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. :-)
coffeemug commentedon Jun 7, 2013
Moving this into 1.6 since this issue is really the one @srh is working on.
danielmewes commentedon Jun 17, 2013
@srh: Is this done? Which branch is it in?
srh commentedon Jun 18, 2013
Currently worked on in sam_939_alt.
coffeemug commentedon Jun 18, 2013
Merging #940 into this issue.
srh commentedon Jun 26, 2013
My expectation is that this will be finished around July 5th.
coffeemug commentedon Jun 27, 2013
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:
srh commentedon Jul 1, 2013
The current todo list is:
This is sufficient to meet the needs of the issue.
A further nice-to-have would be:
That's somewhat tricky.
coffeemug commentedon Jul 1, 2013
Why is the last bit tricky?
9 remaining items
srh commentedon Jul 12, 2013
Also, we still allocate 4KB blocks in RAM. Don't expect that to change soon. Unless we refactor the buffer cache.
coffeemug commentedon Jul 12, 2013
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.
Can you explain what makes this is difficult?
srh commentedon Jul 15, 2013
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.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.
srh commentedon Jul 15, 2013
We have these things to do:
coffeemug commentedon Jul 15, 2013
Are you also using
readvon the way back?srh commentedon Jul 15, 2013
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.
coffeemug commentedon Jul 16, 2013
FYI, I created a separate issue to track the memory utilization aspect of this (#1173). This way we can schedule it separately.
srh commentedon Jul 17, 2013
This is currently in code review 744.
srh commentedon Jul 25, 2013
This is merged to next as of 91b53e3.
srh commentedon Jul 25, 2013
Just as a general description of what was done:
coffeemug commentedon Jul 26, 2013
@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?
srh commentedon Jul 27, 2013
Right now they're replaced with infinite_array_t which uses a std::vector internally and a direct std::vector in one other case.
coffeemug commentedon Jul 27, 2013
I think we have to fix it before 1.8 goes out. Having three second pauses while std::vector reallocs is a problem.
srh commentedon Jul 30, 2013
The fix for #1213 is now in next, so that is no longer an issue (in the places touched here).