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
The second seems murkier to me. If we aren't sorting by a primary key, then our only real options (in the general case) are to go to disk or kill the query. It wouldn't be at all unreasonable for a 3GB dataset to use up 6GB of memory when we load it up, and with an extra 2GB or so for RethinkDB to do everything else that would get us up to 8GB. I would guess we get even worse than a factor of two right now, and that we could improve the ratio, but the general problem wouldn't go away.
Clearly writing data to disk ourselves is better than going to swap (not least because most people don't give servers much, if any, swap), so we should probably do that when we sort large streams, but that's about the best we can do. Once we have secondary indices, that would be another solution to this problem that we could suggest to people.
I might say something stupid but I don't get why we would use 6 GB for a dataset that is 3 GB.
We should probably even use less than 3GB (except if the documents just contain the field id), no?
I have another question by the way, how hard would it be to only sort what we need? That is to say to do a kind of lazy sort.
Here using a heap sort should be pretty fast if we return as soon as we get the first element.
I feel like this is what the user was testing.
@neumino -- there are no fundamental technical reasons why we should use so much space in this case, nor why we can't use a heap sort. The reason here is that sorting this out (pun intended) is work, and it hasn't made it high enough on the list of priorities yet.
@mlucy -- I think we should limit the product to sorts that fit into RAM for the time being. There are much better products that can do sorts on very large datasets, and investing time into doing this sort of thing with RethinkDB isn't worth it right now. Once we sort out the primary key issue, I'd do two things for this:
Work out why we use so much RAM and minimize it if possible
If the sort requires too much RAM, exit with an error
Eventually we can also support proper distributed merge sorts, and possibly even serialize interim datasets to disk to allow sorting huge datasets, but for the time being returning an error on sorts that require too much RAM is ok.
It might be 3GB on disk, but when we load it into memory as a cJSON object we need more space for the same thing. A cJSON object contains 6 pointers, 2 ints, and a double. On a 64-bit platform that's going to take at least 64 bytes per node in the cJSON tree, even if it's just storing an 8-byte double. In practice it will probably be more than that because we allocate each cJSON node separately, so tcmalloc is going to need to keep track of metadata about it which might take up a little more space (I'm not too clear on tcmalloc's architecture, so I might be off-base here).
So that's the bare minimum. In practice, the current design is passing around boost shared pointers to scoped_cJSON_t objects, which is even worse.
We could improve this in a number of ways (enums, xor-ed forward and backward pointers, coming up with smarter code that doesn't need boost shared pointers, etc. etc.), but it wouldn't solve the fundamental problem.
I'm not quite clear what you mean by lazy sorting. If we're sorting by an arbitrary value, the nth element in the stream could be anywhere in the btree on any shard.
Using heapsort would let us return the first few values faster, but it wouldn't reduce the amount of memory needed and would probably increase the total sorting time. (Actually, I'm not sure what sorting algorithm we're using now; it's possible we could make this better by changing it.)
We could have each shard internally sort based on the value and then do some sort of batched mergesort over the network, but then instead of one shard holding 6GB of data you have 6 shards each holding 1GB of data, which isn't really a win if your cluster is handling lots of queries, which is the most common use-case for a cluster.
@coffeemug -- My intuition here (which might be wildly off-base) is that it's unreasonable to expect our in-memory JSON representation to be less than double the size of our serialized JSON representation.
If we really need better memory performance when we sort by a non-primary key, I think a better system is having the shards return pairs of <value to sort by, id> rather than the whole object, sorting those (which will fit in much less memory), and then reading the objects off by their id one at a time and returning them to the user. (This will be much less time-inefficient when we have batched reads.)
If we really need better memory performance for JSON in general, I think the solution is to move off of cJSON to our own memory-optimized JSON implementation.
@neumino -- Ah, I get what you mean now. Yeah, in the case where they only want the first N values we could use a lot less memory. I'm not sure how common that is though.
I think basically the best we can do here is to say that sorting on an arbitrary value requires that each shard be able to fit its data set in memory. We could make that better but as @coffeemug points out that's sort of a problem unto itself.
I'd say there are 2 parts to this project, and there were very specific reasons that we didn't do either for the first release.
Distributing sort. Right now shards just return unsorted data and it's all sorted on the parser node. The reason is because this requires keeping state on the shards which is something that nothing else does. I think in order to do this we're going to want to modify the ICL so that it supports streaming queries.
Index optimizations. The reason we didn't do this is because we didn't want to have to build an optimizer for the first product. We're clearly going to need one to make secondary indexes usable so I think it's time to consider how to make one. We probably are only going to be doing trivial optimizations to start but I suspect we'll want to eventually support more complex things. Take this query for example:
The reason we didn't do this is because we didn't want to have to build an optimizer for the first product. We're clearly going to need one to make secondary indexes usable so I think it's time to consider how to make one. We probably are only going to be doing trivial optimizations to start but I suspect we'll want to eventually support more complex things. Take this query for example:
table.map(lambda x: x.extend(...)).order_by("id")
It's not trivial to detect that we can use indexes for this but the performance on this query sucks if we don't because you need to fit everything in memory and there's actually no way around this without an optimizer. Reversing the order of the operations doesn't fix the problem (in fact it makes the performance worse).
Reversing the order of the operations doesn't fix the problem (in fact it makes the performance worse).
Hmm, the trouble here is that you can't easily reverse operations without potentially giving the wrong result. You've got to prove that extend doesn't override id, which in many cases isn't even possible.
That's cool though, we can say that initially order_by is only guaranteed to use an index when run on a selection, and not when run on a stream. We could also consider changing the API a little to make it clear for users when the index is and is not used.
I think you misunderstood me but what I said was wrong so that's probably a
good thing. I wasn't suggesting we switch the order in the optimizer I was
saying that unlike with other things like join then filter this was a case
where the user couldn't get something performant by switching the
operations themselves but that's actually not true if we had orberby
working as proposed then orberby followed by map would actually be
performant except that all of the mapping would happen on the coordinating
node. Making that map be distributed would actually not be a tough
optimization to make so we could do that too.
On Sunday, December 16, 2012, coffeemug wrote:
Reversing the order of the operations doesn't fix the problem (in fact it
makes the performance worse).
Hmm, the trouble here is that you can't easily reverse operations without
potentially giving the wrong result. You've got to prove that extenddoesn't override
id, which in many cases isn't even possible.
That's cool though, we can say that initially order_by is only guaranteed
to use an index when run on a selection, and not when run on a stream. We
could also consider changing the API a little to make it clear for users
when the index is and is not used.
—
Reply to this email directly or view it on GitHubhttps://github.com//issues/159#issuecomment-11424041.
except that all of the mapping would happen on the coordinating node
Hmm, I don't see why this has to be the case. If the user does order_by.map, and the order_by clause is executed via using an index, it should be pretty trivial to push map down (I guess that's what you said above, but I'm not sure why we aren't doing it already).
I think that in general we're dealing with these issues in a somewhat ad-hoc way (mainly because we could get away with it), but I'm wondering if we could develop a more formal way of thinking about it. Something like a table of what combinations of operations could be pushed down, which ones couldn't, and an operator that can combine stacks of operations. Then we could generate higher-order transformations based on this table. (These are still very vague ideas, but I think we'll need to think about this more deeply post 1.4 release).
To me a formal way of thinking about these is basically looks like an
algebra. We can then think of things in terms of valid algebraic
transformations. The optimization a we want to make actually all map to
fairly basic algebraic properties. The important question seems to be which
operators distribute and commute with one another and when. For example
join distributes over union (which is really important because we're in
essence unioning when we unshard) meaning:
This is what allows us to do distributed joins. Another important question
is commutativity for example join doesn't commute with limit. Limit
followed by join happens centrally while join followed by limit doesn't and
there's not a lot we can do to optimize that.
In my mind what an awesome optimizer looks like is one that knows these
rules and can make transformations of asts based on them. Coupled with some
algorithms to allow it to compute the relative performance of the asts.
Which might wind up needing to be heuristic in some cases.
On Sunday, December 16, 2012, coffeemug wrote:
except that all of the mapping would happen on the coordinating node
Hmm, I don't see why this has to be the case. If the user does
order_by.map, and the order_by clause is executed via using an index, it
should be pretty trivial to push map down (I guess that's what you said
above, but I'm not sure why we aren't doing it already).
I think that in general we're dealing with these issues in a somewhat
ad-hoc way (mainly because we could get away with it), but I'm wondering if
we could develop a more formal way of thinking about it. Something like a
table of what combinations of operations could be pushed down, which ones
couldn't, and an operator that can combine stacks of operations. Then we
could generate higher-order transformations based on this table. (These are
still very vague ideas, but I think we'll need to think about this more
deeply post 1.4 release).
—
Reply to this email directly or view it on GitHubhttps://github.com//issues/159#issuecomment-11426457.
Just add another bit of info, this goes beyond order by and applies to all array operations. We'll have to add some quota (e.g. mongo uses 30mb) to array sizes in memory.
So I realized we could do a temporary fix for this and rather than sort by loading things in to a std::vector and sorting we could load them in to a BTree using the order_by values as keys and then read them back out again. It's a bit of a hack but I actually think it would work pretty well and it means that we won't crash by going out of memory. What do people think? It's a small amount of work and I don't think there's much chance we're actually going to solve this the right way anytime within the next 6 months.
I think we should reject table.order_by("foo", index="bar") with a clear error -- that behavior is just too confusing. If people want to do that, we should force them to write table.order_by(index="bar").order_by("foo).
@neumino well first off it's not as if hashes allow you to have 2 copies of the same key any more than name arguments do. (In fact named arguments just turn in to a hash.)
But second off this is by design; you can't order by 2 indexes at all it's not allowed.
@jdoliner Oh, I see what you mean. They will have to create a compound index right?
About the syntax, I'm not a big fan or silently re-ordering things.
Maybe python users would be fine with it, but in javascript, I would expect table.orderBy("foo", { index:"bar"}) to throw.
We could introduce a term r.index?
table.order_by("bar", r.index("foo")) # throws
table.order_by(r.index("foo"), "bar") # works
table.order_by(r.desc(r.index("foo")), "bar") # works
table.order_by(r.asc(r.index("foo")), "bar") # works
I don't think r.index is a good idea, since we don't use it elsewhere.
I'd be ok with @mlucy's proposal (i.e. order_by('foo', index='bar') returns an error, tells them to chain two order_by commands).
Also, since this is getting a bit complicated, what do you guys think of moving this issue out of 1.8? (This bit is slightly complicated, but also the fact that we truncate keys in secondary indexes makes this hard). @jdoliner -- your call.
I do worry about increasing the scope of 1.8 though (I know we want to get this out, but the issue was added to 1.8 after initial planning on the assumption that it's easy to do, and we just discovered that it may not be so easy; I think it's good policy not to increase scope in the middle of a sprint).
Activity
mlucy commentedon Dec 16, 2012
The first is clearly a problem.
The second seems murkier to me. If we aren't sorting by a primary key, then our only real options (in the general case) are to go to disk or kill the query. It wouldn't be at all unreasonable for a 3GB dataset to use up 6GB of memory when we load it up, and with an extra 2GB or so for RethinkDB to do everything else that would get us up to 8GB. I would guess we get even worse than a factor of two right now, and that we could improve the ratio, but the general problem wouldn't go away.
Clearly writing data to disk ourselves is better than going to swap (not least because most people don't give servers much, if any, swap), so we should probably do that when we sort large streams, but that's about the best we can do. Once we have secondary indices, that would be another solution to this problem that we could suggest to people.
neumino commentedon Dec 16, 2012
I might say something stupid but I don't get why we would use 6 GB for a dataset that is 3 GB.
We should probably even use less than 3GB (except if the documents just contain the field
id), no?I have another question by the way, how hard would it be to only sort what we need? That is to say to do a kind of lazy sort.
Here using a heap sort should be pretty fast if we return as soon as we get the first element.
I feel like this is what the user was testing.
coffeemug commentedon Dec 16, 2012
@neumino -- there are no fundamental technical reasons why we should use so much space in this case, nor why we can't use a heap sort. The reason here is that sorting this out (pun intended) is work, and it hasn't made it high enough on the list of priorities yet.
@mlucy -- I think we should limit the product to sorts that fit into RAM for the time being. There are much better products that can do sorts on very large datasets, and investing time into doing this sort of thing with RethinkDB isn't worth it right now. Once we sort out the primary key issue, I'd do two things for this:
Eventually we can also support proper distributed merge sorts, and possibly even serialize interim datasets to disk to allow sorting huge datasets, but for the time being returning an error on sorts that require too much RAM is ok.
mlucy commentedon Dec 16, 2012
It might be 3GB on disk, but when we load it into memory as a cJSON object we need more space for the same thing. A cJSON object contains 6 pointers, 2 ints, and a double. On a 64-bit platform that's going to take at least 64 bytes per node in the cJSON tree, even if it's just storing an 8-byte double. In practice it will probably be more than that because we allocate each cJSON node separately, so tcmalloc is going to need to keep track of metadata about it which might take up a little more space (I'm not too clear on tcmalloc's architecture, so I might be off-base here).
So that's the bare minimum. In practice, the current design is passing around boost shared pointers to scoped_cJSON_t objects, which is even worse.
We could improve this in a number of ways (enums, xor-ed forward and backward pointers, coming up with smarter code that doesn't need boost shared pointers, etc. etc.), but it wouldn't solve the fundamental problem.
I'm not quite clear what you mean by lazy sorting. If we're sorting by an arbitrary value, the nth element in the stream could be anywhere in the btree on any shard.
Using heapsort would let us return the first few values faster, but it wouldn't reduce the amount of memory needed and would probably increase the total sorting time. (Actually, I'm not sure what sorting algorithm we're using now; it's possible we could make this better by changing it.)
We could have each shard internally sort based on the value and then do some sort of batched mergesort over the network, but then instead of one shard holding 6GB of data you have 6 shards each holding 1GB of data, which isn't really a win if your cluster is handling lots of queries, which is the most common use-case for a cluster.
coffeemug commentedon Dec 16, 2012
We dump data into an
std::vectorand callsorton it. (I'm very ashamed of this, but hey, sometimes worse really is better).neumino commentedon Dec 16, 2012
@mlucy the point of using a heapsort would be for queries like that
We don't have to sort the whole table to get the X smallest element. The smaller X, the less work we have to do.
If we have N shards, we can still look for the X smallest element in each shard, merge them, then do another heap sort.
It's more work that using std::vector and sort for sure. I let @coffeemug decide where to put that on the roadmap.
mlucy commentedon Dec 16, 2012
@coffeemug -- My intuition here (which might be wildly off-base) is that it's unreasonable to expect our in-memory JSON representation to be less than double the size of our serialized JSON representation.
If we really need better memory performance when we sort by a non-primary key, I think a better system is having the shards return pairs of
<value to sort by, id>rather than the whole object, sorting those (which will fit in much less memory), and then reading the objects off by their id one at a time and returning them to the user. (This will be much less time-inefficient when we have batched reads.)If we really need better memory performance for JSON in general, I think the solution is to move off of cJSON to our own memory-optimized JSON implementation.
@neumino -- Ah, I get what you mean now. Yeah, in the case where they only want the first N values we could use a lot less memory. I'm not sure how common that is though.
jdoliner commentedon Dec 16, 2012
I think basically the best we can do here is to say that sorting on an arbitrary value requires that each shard be able to fit its data set in memory. We could make that better but as @coffeemug points out that's sort of a problem unto itself.
I'd say there are 2 parts to this project, and there were very specific reasons that we didn't do either for the first release.
Distributing sort. Right now shards just return unsorted data and it's all sorted on the parser node. The reason is because this requires keeping state on the shards which is something that nothing else does. I think in order to do this we're going to want to modify the ICL so that it supports streaming queries.
Index optimizations. The reason we didn't do this is because we didn't want to have to build an optimizer for the first product. We're clearly going to need one to make secondary indexes usable so I think it's time to consider how to make one. We probably are only going to be doing trivial optimizations to start but I suspect we'll want to eventually support more complex things. Take this query for example:
The reason we didn't do this is because we didn't want to have to build an optimizer for the first product. We're clearly going to need one to make secondary indexes usable so I think it's time to consider how to make one. We probably are only going to be doing trivial optimizations to start but I suspect we'll want to eventually support more complex things. Take this query for example:
It's not trivial to detect that we can use indexes for this but the performance on this query sucks if we don't because you need to fit everything in memory and there's actually no way around this without an optimizer. Reversing the order of the operations doesn't fix the problem (in fact it makes the performance worse).
coffeemug commentedon Dec 16, 2012
Hmm, the trouble here is that you can't easily reverse operations without potentially giving the wrong result. You've got to prove that
extenddoesn't overrideid, which in many cases isn't even possible.That's cool though, we can say that initially
order_byis only guaranteed to use an index when run on aselection, and not when run on a stream. We could also consider changing the API a little to make it clear for users when the index is and is not used.jdoliner commentedon Dec 16, 2012
I think you misunderstood me but what I said was wrong so that's probably a
good thing. I wasn't suggesting we switch the order in the optimizer I was
saying that unlike with other things like join then filter this was a case
where the user couldn't get something performant by switching the
operations themselves but that's actually not true if we had orberby
working as proposed then orberby followed by map would actually be
performant except that all of the mapping would happen on the coordinating
node. Making that map be distributed would actually not be a tough
optimization to make so we could do that too.
On Sunday, December 16, 2012, coffeemug wrote:
coffeemug commentedon Dec 17, 2012
Hmm, I don't see why this has to be the case. If the user does
order_by.map, and theorder_byclause is executed via using an index, it should be pretty trivial to pushmapdown (I guess that's what you said above, but I'm not sure why we aren't doing it already).I think that in general we're dealing with these issues in a somewhat ad-hoc way (mainly because we could get away with it), but I'm wondering if we could develop a more formal way of thinking about it. Something like a table of what combinations of operations could be pushed down, which ones couldn't, and an operator that can combine stacks of operations. Then we could generate higher-order transformations based on this table. (These are still very vague ideas, but I think we'll need to think about this more deeply post 1.4 release).
jdoliner commentedon Dec 17, 2012
To me a formal way of thinking about these is basically looks like an
algebra. We can then think of things in terms of valid algebraic
transformations. The optimization a we want to make actually all map to
fairly basic algebraic properties. The important question seems to be which
operators distribute and commute with one another and when. For example
join distributes over union (which is really important because we're in
essence unioning when we unshard) meaning:
This is what allows us to do distributed joins. Another important question
is commutativity for example join doesn't commute with limit. Limit
followed by join happens centrally while join followed by limit doesn't and
there's not a lot we can do to optimize that.
In my mind what an awesome optimizer looks like is one that knows these
rules and can make transformations of asts based on them. Coupled with some
algorithms to allow it to compute the relative performance of the asts.
Which might wind up needing to be heuristic in some cases.
On Sunday, December 16, 2012, coffeemug wrote:
coffeemug commentedon Jan 10, 2013
Moving to post-protobuf-improvements.
coffeemug commentedon Feb 14, 2013
Just add another bit of info, this goes beyond order by and applies to all array operations. We'll have to add some quota (e.g. mongo uses 30mb) to array sizes in memory.
jdoliner commentedon Feb 18, 2013
So I realized we could do a temporary fix for this and rather than sort by loading things in to a std::vector and sorting we could load them in to a BTree using the order_by values as keys and then read them back out again. It's a bit of a hack but I actually think it would work pretty well and it means that we won't crash by going out of memory. What do people think? It's a small amount of work and I don't think there's much chance we're actually going to solve this the right way anytime within the next 6 months.
21 remaining items
coffeemug commentedon Jul 18, 2013
I think that's ok. We'd just have to document it clearly.
mlucy commentedon Jul 18, 2013
I think we should reject
table.order_by("foo", index="bar")with a clear error -- that behavior is just too confusing. If people want to do that, we should force them to writetable.order_by(index="bar").order_by("foo).mlucy commentedon Jul 18, 2013
Er, wait, I didn't think that through for even half a second. Ignore my previous comment.
neumino commentedon Jul 18, 2013
Hum, can Python users order using two indexes?
With named argument they cannot, right? Shouldn't we force them to pass a hash?
jdoliner commentedon Jul 18, 2013
@neumino well first off it's not as if hashes allow you to have 2 copies of the same key any more than name arguments do. (In fact named arguments just turn in to a hash.)
But second off this is by design; you can't order by 2 indexes at all it's not allowed.
neumino commentedon Jul 19, 2013
@jdoliner Oh, I see what you mean. They will have to create a compound index right?
About the syntax, I'm not a big fan or silently re-ordering things.
Maybe python users would be fine with it, but in javascript, I would expect
table.orderBy("foo", { index:"bar"})to throw.We could introduce a term
r.index?coffeemug commentedon Jul 19, 2013
I don't think
r.indexis a good idea, since we don't use it elsewhere.I'd be ok with @mlucy's proposal (i.e.
order_by('foo', index='bar')returns an error, tells them to chain twoorder_bycommands).Also, since this is getting a bit complicated, what do you guys think of moving this issue out of 1.8? (This bit is slightly complicated, but also the fact that we truncate keys in secondary indexes makes this hard). @jdoliner -- your call.
coffeemug commentedon Jul 19, 2013
Err, @jdoliner pointed out in person that chaining two
order_bycommands has a different meaning. Please disregard that.coffeemug commentedon Jul 19, 2013
I do worry about increasing the scope of 1.8 though (I know we want to get this out, but the issue was added to 1.8 after initial planning on the assumption that it's easy to do, and we just discovered that it may not be so easy; I think it's good policy not to increase scope in the middle of a sprint).
mlucy commentedon Jul 19, 2013
@coffeemug -- And I even went to all that trouble to tell you not to read my comment ^.^ .
jdoliner commentedon Aug 2, 2013
This is fixed and in next as of 079eb00 CR was 754 by @mlucy.
coffeemug commentedon Aug 7, 2013
@jdoliner -- should we close this issue? (your last comment suggests that it's done, but I'm not sure)
jdoliner commentedon Aug 7, 2013
Yup, meant to do that. My bad.
board: tick rethinkdb#159 — LIGHT AUDIT idle (0 pending tasks; gitrei…