Skip to content

Clarify includeInitial docs wrt updates while initialising #1137

Open

Description

@danielcompton

I'm building state on the client by processing changefeeds to build up an in memory subset of a 'table', by using includeInitial.

includeInitial: if true, the changefeed stream will begin with the current contents of the table or selection being monitored. These initial results will have new_val fields, but no old_val fields. The initial results may be intermixed with actual changes, as long as an initial result for the changed document has already been given. If an initial result for a document has been sent and a change is made to that document that would move it to the unsent part of the result set (e.g., a changefeed monitors the top 100 posters, the first 50 have been sent, and poster 48 has become poster 52), an “uninitial” notification will be sent, with an old_val field but no new_val field.

After reading the docs, it's not clear to me what the expected results in a changefeed are when a document is updated while a changefeed is initialising.

There is a scenario where the docs are a little unclear to me

1. Issue changefeed query for a table with 1000 records
2. RethinkDB starts returning results
3. Issue a mutation query for record 750. At this point in time, the changefeed initialising cursor is at record 500.
4. A change record will be generated with a `new_val`, and an `old_val` and returned over the changefeed.
5. The changefeed cursor gets to record 750. It generates an initial state with just a `new_val`.

What will that new_val in 5 be? The value written in 3, or the initial state of the record at the time of 1? I'm guessing it will be the value written in 3, as RethinkDB doesn't have MVCC, or transactions while initialising a changefeed.

Is there any way for a document modified before a changefeed cursor reaches that document to have it's changefeed returned after the initial state? Or are those two operations serialised?

Thinking about this, I'm not sure if it even matters too much. If you collected all changefeeds and didn't process them until after you had built up your initial state, then I think you will always end up with the correct results, just that some changefeeds would be no-ops as you would already have received the change as part of initial state.

I guess fundamentally, what I'm asking is, is there a reliable and consistent way to process changefeeds, such that you will always get a correct view of a table?

Activity

  1. danielmewes commented on Jun 9, 2016

    @danielmewes
    Member

    First to answer your question:
    In the scenario you describe, step 4 will never happen. The changefeed will only pass on changes for records that have already been initialized.
    In general you will never get a document from the changefeed that has an old_val value for which you haven't seen a corresponding initial {new_val: ...} document before.
    In step 5, the new_val will be that of the document after the change from step 3.

    Is there any way for a document modified before a changefeed cursor reaches that document to have it's changefeed returned after the initial state? Or are those two operations serialised?

    In general you can see either a sequence of {new_val: value_before_modification}, {old_val: value_before_modification, new_val: value_after_modification} or {new_val: value_after_modification} for a given document. The first one will happen if the "initial value cursor" passes the document before the modification happens, and the second one will happen if the modification happens before the cursor passes it.
    This "cursor" is purely internal though, and you can't rely on its state in your application. It might have different key boundaries on different shards for example.

    is there a reliable and consistent way to process changefeeds, such that you will always get a correct view of a table?

    Yes, something like this (pseudo code):

    // A data structure with set semantics
    var Set documents;
    
    feed = r.table(...).changes({includeInitial: true, includeStates: true}).run();
    
    for change in feed {
      if (change has field "state" and change.state == "ready") {
        // We have reached a consistent state of the documents in the table.
        // This might be a good time for rendering the results etc.
        // We can simply stay in the `for` loop to keep it updated.
      }
      if (change has field "old_val" and change.old_val != null) {
        // Remove the old value from the set
        documents.remove(change.old_val);
      }
      if (change has field "new_val" and change.new_val != null) { 
        // Add the new value to the set
        documents.add(change.new_val);
      }
    }

    I think we should add example code like this for all supported languages to http://www.rethinkdb.com/docs/changefeeds/javascript/ , and probably link to it from the the paragraph @danielcompton mentioned in the API docs. @chipotle what do you think?

  2. danielcompton commented on Jun 9, 2016

    @danielcompton
    ContributorAuthor

    Thanks for the detailed explanation (as always :) )

    In general you will never get a document from the changefeed that has an old_val value for which you haven't seen a corresponding initial {new_val: ...} document before.

    One question, what are the scenarios where this can't happen?

    Rereading the docs on initialValue, I see it answered my original question here:

    The initial results may be intermixed with actual changes, as long as an initial result for the changed document has already been given.

  3. danielmewes commented on Jun 10, 2016

    @danielmewes
    Member

    One question, what are the scenarios where this can't happen?

    Whenever includeInitial is set.

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