Skip to content

Clarify the semantics of atomic operations #679

Closed

Description

@AtnNn

All the docs for update have to say about atomicity is:

nonAtomic: set to true if you want to perform non-atomic updates (updates that require fetching data from another document).

That is not really what atomic means. And in practice it seems to be a lot more subtle. Users often ask if queries like this one are atomic:

r.table('foo').filter({ status: "inactive"}).sample(1).update({ status: "active", ... })

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.

r.db('foo').filter({ status: r.table('bar').get(id)('status')}).sample(1).update({ status: "active", ... })

How atomic is the update? Will the sub-query on bar be run once overall, once per shard or once per row?

@mlucy perhaps you or someone else can enlighten me.

Activity

  1. mlucy commented on Mar 17, 2015

    @mlucy
    Member

    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.

  2. Timer commented on Mar 17, 2015

    @Timer

    @mlucy so would something like this also have a similar effect (of guaranteeing transition ONLY from inactive -> active)?

    r.db('foo').table('bar').filter({ status: "inactive"}).sample(1).update(r.branch(r.row('status').eq("inactive"), { status: "active", ... }, { }))
    

    or would the branch have to be done within the object itself as you presented

    {status: r.branch(r.row('status').eq('inactive'), 'active', r.row('status'))}
    

    I'm not sure how the AST is evaluated or when the write-lock is established, thus the question.

  3. mlucy commented on Mar 17, 2015

    @mlucy
    Member

    @Timer -- that would work too.

  4. Timer commented on Mar 17, 2015

    @Timer

    Great, thank you for the insight.

  5. chipotle commented on Mar 20, 2015

    @chipotle
    Contributor

    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

    r.table('test').get(0).update({n: r.row('n').add(r.table('constants').get(1)['val'])}, non_atomic=true)
    

    and

    r.table('test').get(0).update({n: r.row('n').add(r.table('constants').get(1)['val'])})
    

    that would make you want to specify true in the first case?

  6. mlucy commented on Mar 20, 2015

    @mlucy
    Member

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

  7. chipotle commented on Mar 20, 2015

    @chipotle
    Contributor

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions