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
Could a document that is not "inactive" ever become "active"? I'm never quite sure. What if there was no sample? What about this other query? It fetches data from another document but does not require the nonAtomic flag.
The current description of nonAtomic is overly specific -- updates that require fetching data from another document are one case of operations we can't do atomically, but there are others as well (e.g. functions using r.random). A better description might be "set to true if you need to do a non-deterministic update, for example an update that involves reading from another table".
If you write r.table('foo').filter({status: 'inactive'}).update({status: 'active'}), we don't guarantee that status only ever moves from inactive to active. (We could add that guarantee by re-running the filter before applying the update and only applying it if it's still true, but we don't. Thinking about this, I think maybe we should do that. Do you want to open a ReQL proposal for it?) If you want that guarantee right now, you should write r.table('foo').update({status: r.branch(r.row('status').eq('inactive'), 'active', r.row('status'))}).
What we mean by atomic is that if you run the query r.table('test').get(0).update({n: r.row('n').add(1)})m times, we guarantee that the value of n is incremented by exactly m, and that if you read while the updates are running you only ever see the value of n increase. If you were to write r.table('test').get(0).update({n: r.row('n').add(r.table('constants').get(1)['val'])}, non_atomic=true) instead, though, we wouldn't offer that guarantee.
I think I might need a description of a use case for the non_atomic flag; @mlucy's suggested rephrasing really just changes "non-atomic" to "non-deterministic," and the example that begins "What we mean by atomic..." above doesn't give two queries that are identical save for the value of non_atomic, which makes the distinction kind of murky. What's the difference in practice between
@chipotle -- the non_atomic flag isn't something you ever want to specify, it's something we make people specify so that they know that their query is doing something different. Its only purpose is to let the user say "yes, I know this function is non-deterministic and can't be applied atomically, run it anyway".
For example, if you run r.table('test').update{|row| {a: row['a']+1}}, we can evaluate the function lambda{|row| {a: row['a']+1}} deterministically, which means that we can just send the function to all the replicas and have them apply it. Because the replicas are applying the function themselves, they can apply it while holding onto the write lock, which means that we can guarantee that a is always incremented by exactly 1 per write query.
If you run r.table('test').update{|row| {a: row['a']+r.table('constants').get(1)}}, however, we can't evaluate the function deterministically, which means that we can't send the function directly to the replicas (since they might read different values from constants and we'd get data divergence). So instead we read each row across the network, evaluate the function once on the machine handling the query, and then send the new row to all the replicas. This is no longer atomic; if the value of a changed between when we did the read and when the new row reached the replicas, that change will just be clobbered. Since this is dangerous, we don't do it silently; we produce an error in this case unless the user writes r.table('test').update(lambda{|row| {a: row['a']+r.table('constants').get(1)}}, non_atomic: true) instead.
Okay, I think maybe that's the key that was missing: if you don't specify non_atomic on a query that's non-deterministic, RethinkDB will raise an error.
Activity
mlucy commentedon Mar 17, 2015
The current description of
nonAtomicis overly specific -- updates that require fetching data from another document are one case of operations we can't do atomically, but there are others as well (e.g. functions usingr.random). A better description might be "set to true if you need to do a non-deterministic update, for example an update that involves reading from another table".If you write
r.table('foo').filter({status: 'inactive'}).update({status: 'active'}), we don't guarantee thatstatusonly ever moves frominactivetoactive. (We could add that guarantee by re-running the filter before applying the update and only applying it if it's still true, but we don't. Thinking about this, I think maybe we should do that. Do you want to open a ReQL proposal for it?) If you want that guarantee right now, you should writer.table('foo').update({status: r.branch(r.row('status').eq('inactive'), 'active', r.row('status'))}).What we mean by atomic is that if you run the query
r.table('test').get(0).update({n: r.row('n').add(1)})mtimes, we guarantee that the value ofnis incremented by exactlym, and that if you read while the updates are running you only ever see the value ofnincrease. If you were to writer.table('test').get(0).update({n: r.row('n').add(r.table('constants').get(1)['val'])}, non_atomic=true)instead, though, we wouldn't offer that guarantee.Timer commentedon Mar 17, 2015
@mlucy so would something like this also have a similar effect (of guaranteeing transition ONLY from inactive -> active)?
or would the branch have to be done within the object itself as you presented
I'm not sure how the AST is evaluated or when the write-lock is established, thus the question.
mlucy commentedon Mar 17, 2015
@Timer -- that would work too.
Timer commentedon Mar 17, 2015
Great, thank you for the insight.
chipotle commentedon Mar 20, 2015
I think I might need a description of a use case for the
non_atomicflag; @mlucy's suggested rephrasing really just changes "non-atomic" to "non-deterministic," and the example that begins "What we mean by atomic..." above doesn't give two queries that are identical save for the value ofnon_atomic, which makes the distinction kind of murky. What's the difference in practice betweenand
that would make you want to specify
truein the first case?mlucy commentedon Mar 20, 2015
@chipotle -- the
non_atomicflag isn't something you ever want to specify, it's something we make people specify so that they know that their query is doing something different. Its only purpose is to let the user say "yes, I know this function is non-deterministic and can't be applied atomically, run it anyway".For example, if you run
r.table('test').update{|row| {a: row['a']+1}}, we can evaluate the functionlambda{|row| {a: row['a']+1}}deterministically, which means that we can just send the function to all the replicas and have them apply it. Because the replicas are applying the function themselves, they can apply it while holding onto the write lock, which means that we can guarantee thatais always incremented by exactly1per write query.If you run
r.table('test').update{|row| {a: row['a']+r.table('constants').get(1)}}, however, we can't evaluate the function deterministically, which means that we can't send the function directly to the replicas (since they might read different values fromconstantsand we'd get data divergence). So instead we read each row across the network, evaluate the function once on the machine handling the query, and then send the new row to all the replicas. This is no longer atomic; if the value ofachanged between when we did the read and when the new row reached the replicas, that change will just be clobbered. Since this is dangerous, we don't do it silently; we produce an error in this case unless the user writesr.table('test').update(lambda{|row| {a: row['a']+r.table('constants').get(1)}}, non_atomic: true)instead.chipotle commentedon Mar 20, 2015
Okay, I think maybe that's the key that was missing: if you don't specify
non_atomicon a query that's non-deterministic, RethinkDB will raise an error.