Skip to content

Proposal: atomic set and get #976

Closed
Closed

Description

@mlucy

See #194.

I propose that we add an additional optional argument return_vals to update and replace that works as follows:

> table.update(:return_vals => true) {|row| {:x => row[:id] * 2}}
{:replaced => 2,
 :vals => [{:old => {:id => 1}, :new => {:id => 1, :x => 2}},
           {:old => {:id => 2}, :new => {:id => 2, :x => 4}}],
 ...}

Activity

  1. ghost assigned on Jun 10, 2013
  2. jdoliner commented on Jun 10, 2013

    @jdoliner
    Contributor

    Could you clarify with this proposal how an implementation might work? Or at least make an argument that this API is possible to implement.

  3. mlucy commented on Jun 10, 2013

    @mlucy
    MemberAuthor

    We already return a summary object from rdb_replace_and_return_superblock, which contains things like :replaced => 1. That function has two variables new_val and old_val which contain the data we want. If the optarg is set, we simply include :vals => [{:old => old_val, :new => new_val}] in the summary object. The algorithm we use to combine summary objects concatenates arrays, so we'll end up with an array of all the replaced vals.

  4. jdoliner commented on Jun 10, 2013

    @jdoliner
    Contributor

    But won't you now have an array in memory twice the size of the table?

  5. mlucy commented on Jun 10, 2013

    @mlucy
    MemberAuthor

    If you're updating a whole table that way, yeah. I don't think we need to support atomic set-and-get for large range updates. (Honestly, we could probably get away with just offering this for point updates.)

  6. jdoliner commented on Jun 10, 2013

    @jdoliner
    Contributor

    We should probably consider just doing point updates. I suppose we should wait on discussing further but I think it's important for those thinking about this issue to know about the constraints of the API when considering it. So I wanted to make sure this was noted before discussion began.

  7. neumino commented on Jun 10, 2013

    @neumino
    Member

    I don't really know how hard it is to do an atomic get/set for a range of documents, but I also tend to think that offering this feature for point updates is enough.

    The main use case is when an user update its own settings. I can't really think of a frequent use case where someone would have to do a range update and retrieve all the updated results.

    Also, if I'm not mistaken, mongo's findAndModify doesn't exists for range updates.

  8. mlucy commented on Jun 10, 2013

    @mlucy
    MemberAuthor

    Also, if people want to do this for range updates they could always force it by doing:

    table.foreach {|row|
      table.get(row[:id]).update(...)
    }
  9. mlucy commented on Jun 10, 2013

    @mlucy
    MemberAuthor

    But anyway, I agree with @jdoliner, let's put off discussing this further until the 3-day period at the start of 1.7.

  10. jdoliner commented on Jun 10, 2013

    @jdoliner
    Contributor

    Yeah, I sort of like that idea. I'm a bit uneasy about having something work with ranges but blow up if the ranges are too big.

  11. wmrowan commented on Jun 11, 2013

    @wmrowan
    Contributor

    So I'm about to get radical on y'all since this is the time for such ideas.

    What if we rethink(tm) this whole write reporting business and return the updated value by default? The current write summarizing behavior could then be provided by a new summarize or report command.

    Here's something like what I'm imagining:

    • table.get(key).update(...).run() => new_val
    • table.get(non_existant).update(...).run() => error
    • table.update(...).run(...) => stream_of_new_vals
    • table.get(key).update(...).report() => {'updated': 1}
    • table.get(non_existant).update(...).report() => {'error': 1}
    • table.update(...).report() => {'updated': >1, 'errors': >1}
    • table.get(key).do({'old': r.row, 'new': r.row.update(...)}) => ...
    • table.map({'old': r.row, 'new': r.row.update(...)}) => ...

    The main issue with this idea is how we deal with lazy streams of updated values, especially when the user doesn't actually want to read the values. Maybe we only do it for point updates and replaces, as if range updates have .report() tacked on by default.

    This is probably not the way forward but I would at least like to explore alternatives to the initial proposal. A weird optional argument plus an even weirder output object are just too much for me.

  12. coffeemug commented on Jun 11, 2013

    @coffeemug
    Contributor

    I'm strongly opposed to returning the results by default because most of the time users don't actually care about getting back the values -- they already know what they are and are just setting them. The case where they want to get the values back (e.g. increment and read) is very important but is fairly rare.

  13. mlucy commented on Jun 12, 2013

    @mlucy
    MemberAuthor

    An interesting proposal, but it looks like it may have some problems. Off the top of my head:

    • If we return a stream from a range update, how do we report errors? Do we change to aborting halfway through rather than continuing like we do now?
    • We'd be moving back to having different error behavior for point updates and range updates, which kinda sucks.
    • The do and map examples don't actually work because we'll see the row that we read on the machine serving the query rather than the row that the update function is run on (these may be different if the user is doing something racy). We need a way to get the old row back from the shard after the update if we want to have atomic updates.
  14. mlucy commented on Jun 12, 2013

    @mlucy
    MemberAuthor

    Also, right now once you store a single row selection in a variable it turns into an object, to prevent the problem I mentioned above where you do a read and a write on it and expect them both to see the same thing.

  15. jdoliner commented on Jun 12, 2013

    @jdoliner
    Contributor

    Streams of the values from range updates is just never going to work. You either need to store all of the values in memory or have incredibly strange lazy evaluation semantics for the write.

  16. mlucy commented on Jun 18, 2013

    @mlucy
    MemberAuthor

    Alright, here's what I think we should do here:

    • Add an optarg return_vals to update that defaults to false.
    • If return_vals is set to true for a point update, we return a summary object that looks like:
    {:replaced => 1,
     :old_val => ...,
     :new_val => ...,
     ...}
    • If return_vals is set to true for a range update, we error.
  17. coffeemug commented on Jun 18, 2013

    @coffeemug
    Contributor

    This wouldn't just be update but replace as well, correct? (is there another command I'm missing?)

  18. mlucy commented on Jun 18, 2013

    @mlucy
    MemberAuthor

    I'll probably just do it for all the write ops because it's easy. It's useful for insert if you're doing an upsert, and it's useful for delete (maybe).

  19. mlucy commented on Jun 18, 2013

    @mlucy
    MemberAuthor

    (but, yes, we should certainly do it for replace)

  20. srh commented on Jun 18, 2013

    @srh
    Contributor

    We don't need to return new_val, do we? Most people just want old_val. Or they want just new_val. Maybe they should be separate options.

  21. mlucy commented on Jun 18, 2013

    @mlucy
    MemberAuthor

    I thought about making the optarg take :old, :new, or :all, but this seemed simpler. Assuming that they aren't drastically changing the size of the row, it's only a factor of 2 performance hit in the worst worst worst case, which didn't seem like a big enough deal to complicate the interface.

  22. jdoliner commented on Jun 19, 2013

    @jdoliner
    Contributor

    And if people are really worried about the performance they can always tack a pluck on the end of it.

  23. mlucy commented on Jun 21, 2013

    @mlucy
    MemberAuthor

    Alright, this is settled with old_val and new_val both being included. If people complain it will be easy to update return_vals to take :old and :new as well as true.

  24. mlucy commented on Jun 26, 2013

    @mlucy
    MemberAuthor

    This is in review 670 by @jdoliner .

  25. mlucy commented on Jun 27, 2013

    @mlucy
    MemberAuthor

    This is in next.

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