Skip to content

Get initial batch from changefeed #3709

Closed

Description

@hpx7

For example, consider

r.table('scores').orderBy({index: r.desc('score')}).limit(10).changes()

There doesn't seem to be an easy way to determine on the client when the initial batch (of <= 10 items) has been recieved.

Having this information allows a client to distinguish between "data loading" and "data changing".

Activity

  1. hpx7 commented on Jan 31, 2015

    @hpx7
    Author

    I do understand that the absence of the old_val key indicates that a particular row is part of the initial batch; however, this is not sufficient in telling us which row represents the final one of the initial batch.

    Consider the example above. In the case where the scores table contains less than 10 rows to begin with, we are unable to determine the end of the initial batch on the client.

  2. added this to the milestone on Jan 31, 2015
  3. mlucy commented on Jan 31, 2015

    @mlucy
    Member

    @hpx7 -- there isn't really a good way right now. We went back and forth on this, but decided against complicating the format for the initial release. What sort of interface would you like to see?

  4. hpx7 commented on Jan 31, 2015

    @hpx7
    Author

    Looking at the javascript driver, it seems like the EventEmitter interface could be useful - there could be a loaded event which indicates the end of the initial batch. Not sure how this would be implemented in the other drivers though.

    Another option I see is simply adding a field to the rows returned from the changefeed (in addition to new_val/old_val). Perhaps an integer remaining field or simply a boolean lastOfBatch field.

    Something like the "remaining" concept could possibly be implemented on the cursor level instead as well.

  5. mlucy commented on Jan 31, 2015

    @mlucy
    Member

    A pretty reasonable protocol implementation would be to add a field {note: "last_initial_val"} to the last initial document. That would let people who don't care about this information ignore it, and it would be backwards-compatible so it would avoid breaking any code that was written for 1.16 changefeeds.

  6. kofalt commented on Jan 31, 2015

    @kofalt

    In general, with the scope of queries that can be watched for changes expanding, it would be nice to have a way to clearly distinguish between:

    1. The results of the query as though I had not appended .changes()
    2. Any changes / deltas following that result set.

    From the current documentation:

    The first notification object in the changefeed stream will contain the query's initial value in new_val and have no old_val field.

    This sounds a little ambiguous in the case of multiple initial documents, but it sounds like one can unambiguously distinguish any query's initial results from its change events by waiting for the first response with an old_val key.

    If so, then adding a note key makes sense as we won't have to block on the first change to make that distinction. This conversation is strongly related to the return_initial proposal in #3579.

  7. Tryneus commented on Feb 1, 2015

    @Tryneus
    Member

    Maybe this is crazy, it'd probably require redoing the protocol a bit, but what if we returned two cursors, one for the initial value and one for new changes?

  8. kofalt commented on Feb 1, 2015

    @kofalt

    Handling the case where a query may return more than one cursor sounds like a lot of complexity; can you envision a use case where you'd both want the initial data set, and want to read new changes before reading said initial data?

  9. hpx7 commented on Feb 1, 2015

    @hpx7
    Author

    The only issue I see with the {note: "last_initial_val"} solution is that it requires the database to know, when publishing a particular row, whether that row is the final one. I can imagine scenarios in which this may not be a desirable property.

    One solution that doesn't have this problem is simple adding a {ready: true} message to the protocol, but extending the protocol to support this may break backwards compatibility.

  10. kofalt commented on Feb 1, 2015

    @kofalt

    I suppose the {note: "last_initial_val"} option will also not work in the case where a query happens to not return any values initially, which is something you may not be able to predict depending on your current application state.

  11. mlucy commented on Feb 1, 2015

    @mlucy
    Member

    That's a fair point about not being able to attach the note if there are no initial values. Maybe breaking backwards compatibility is better then. (We could also omit the {ready: true} documents by default and have an optarg to turn them on; that would preserve backwards compatibility while letting people who really need this information get it.)

  12. kofalt commented on Feb 3, 2015

    @kofalt

    That seems reasonable. To be clear, its sounds like the resultant usage would be:

    1. Set an optarg, mark_ready or something, on a query with .changes()
    2. Handle documents lacking a ready key as a query result (extracting from new_val key)
    3. Read a document with a ready key, discard
    4. Handle all future documents as a changefeed event (with new_val and old_val keys).

    Which does not require the database know which is the final document in a query result, does not block on change events for a client to know the full result set, and also handles the case of no results.

    As a bonus, aside from extracting the new_val key in step 2, you can pretty much use identical code to process query results with or without changefeeds.

  13. neumino commented on Feb 3, 2015

    @neumino
    Member

    Is it possible to add the documents to the feed only when their value is added to the initial stream?
    The "only" consistency required is that the documents are up to date.

    So I'm fine with the following situation:

    • Ask for a change feed on a range that contains doc1 and doc2.
    • Update doc1.0 to doc1.1 and doc2.0 do doc2.1
    • Retrieve doc1.0 and doc2.1 and get only the change for doc1.0 -> doc1.1
  14. kofalt commented on Feb 3, 2015

    @kofalt

    If I understand your example correctly, that sounds like it has more to do the MVCC nature of the database than separating initial results from changefeed events.

    I don't think adding {ready: true} behaviour would would effect your example in any way.

    I would expect: doc1.0, doc2.1, ready, doc1.0 -> doc1.1.

  15. 12 remaining items

  16. mlucy commented on Feb 9, 2015

    @mlucy
    Member
    • We add an optarg note_states to changes. It defaults to false but can be set to true on any changefeed.
    • If note_states is true, we will occasionally insert documents of the form {state: X} into the stream of changes.
    • One value for X will be initializing. We'll send {state: 'initializing'} as the first document of a feed which returns initial values.
    • Another value for X will be ready. We'll send {state: 'ready'} whenever we're done sending initial values. (So for a changefeed which doesn't return initial values, {state: 'ready'} will be the first document.)
    • We reserve the right to add more states in the future.
    • We reserve the write to send changes before {state: 'ready'}, even though we don't do so right now, but if we do so then we'll guarantee that the initial value for a particular primary key is always seen before any changes to that primary key.
  17. mglukhovsky commented on Feb 9, 2015

    @mglukhovsky
    Member

    Is note being used as a verb here? I assumed it was a noun, and got confused. Can we use a clearer verb, like include_states?

  18. coffeemug commented on Feb 10, 2015

    @coffeemug
    Contributor

    👍 for include_states

    Also, moving to 2.0 after talking to @mlucy (since it's easy to do and pretty important).

  19. modified the milestones: 2.0, on Feb 10, 2015
  20. danielmewes commented on Feb 10, 2015

    @danielmewes
    Member

    I didn't like include_status in this context at first. But I can't tell why and also cannot come up with a better name (I had considered send_status, but don't like that much either).

    Hence include_states is good.

  21. danielmewes commented on Feb 10, 2015

    @danielmewes
    Member

    Also 👍 on all the other details in #3709 (comment)

    I think we can mark this settled.

  22. kofalt commented on Feb 10, 2015

    @kofalt

    +1 on @mlucy's proposal and +1 and for include_states. LGTM

  23. danielmewes commented on Feb 10, 2015

    @danielmewes
    Member

    Thanks @hpx7, @marshall007, @kofalt for all your feedback in this and other threads by the way! Was very helpful.

  24. neumino commented on Feb 10, 2015

    @neumino
    Member

    Just to be sure that I understood the proposal. Suppose I have 3 documents in my table.

    I'll first get {state: 'initializing'}, then some changes, and then {state: 'ready'}.

    Suppose that no writes happen on the table, I'll get

    [
      {new_val: {id: 1}}, 
      {new_val: {id: 2}}, 
      {new_val: {id: 3}}
    ]
    

    If some writes happen on the table, I can get changes about the document with id: x only after I receive {new_val: {id: x}}.

    If someone insert {id: 4} before ready is sent, will I get {new_val: {id: 4}} then {new_val: {id: 4}, old_val: null}? Or can I get straight {new_val: {id: 4}, old_val: null}? Or just {new_val: {id: 4}}?

    Could we have a note to distinguish this feed from other feeds? Such that generic libraries that want to build a higher API (like maintaining a list of changing documents) don't have to wait for the first element to know what they are dealing with (similar to the atom feed)?

    One last picky thing. While include_states sounds better than note_states, it doesn't quite describe what the option is for. Shouldn't it be more something like initial_values: true (or something better)?

    :+1 overall it looks like a clean API :) I would be happy to add that on thinky :-)

  25. danielmewes commented on Feb 10, 2015

    @danielmewes
    Member

    @neumino

    Could we have a note to distinguish this feed from other feeds?

    You mean a feed that has include_states enabled? That's a good idea, I think we should do that through our ResponseHints interface (#3715).

    Shouldn't it be more something like initial_values: true (or something better)?

    That seems like a much worse description of what include_states does to me. I mean you can get the initial values without include_states.

    If someone insert {id: 4} before ready is sent, will I get {new_val: {id: 4}} then {new_val: {id: 4}, old_val: null}? Or can I get straight {new_val: {id: 4}, old_val: null}? Or just {new_val: {id: 4}}?

    I think you would either get only {new_val: {id: 4}, old_val: null} or only {new_val: {id: 4}}, but not both.

  26. mlucy commented on Mar 10, 2015

    @mlucy
    Member

    This is in next (CR by Marc).

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