Skip to content

Add a "type" field to changefeed entries #5188

Closed

Description

@deontologician

Every program I've written using changefeeds has had boilerplate akin to the following in it:

if (change.new_val != null && change.old_val == null ) {
  change.type = 'added'
} else if ( change.new_val == null && change.old_val != null ) {
  change.type = 'removed'
} else if (change.new_val != null && change.old_val != null ) {
  change.type = 'changed'
// And the new guys:
} else if (change.new_val != undefined && change.old_val == undefined) {
  change.type = 'initial'
} else if (change.new_val == undefined && change.old_val != undefined) {
  change.type = 'uninitial'
}

This is pretty mind-numbing, and I would guess almost always necessary ( @segphault and @dalanmiller have written way more apps than me and could comment). In addition, it's also error prone. (There are two errors I made in the example above, can you spot them?) The drivers can provide convenience wrappers, but I think we should just make the changefeeds themselves more user friendly.

I propose we add a type field to changefeed entries, and just put the type in as a string. This would also be a natural place to put type: "error" (see #5041).

Cons:

  1. slightly more cluttered
  2. slightly more bandwidth
  3. sometimes you can just delete old_val, add the new_val and you don't need to exactly classify the type. Even in this case, you can just ignore the type field.

Pros:

  1. Obviously what everyone really wants
  2. Come on

(I would also extend this behavior to the results from return_changes but I would guess the code is shared and so this would just happen as a side-effect)

Activity

  1. danielmewes commented on Dec 10, 2015

    @danielmewes
    Member

    This is almost the same as #4194, except that the field name and string values are different:

    The proposal is to encode this information explicitly by adding an additional field. For example
    action: 'delete'
    action: 'insert'
    action: 'replace'
    action: 'initial'

    I'm going to close the other one, because this one has the nicer write up.

    Another "Pro" from the other issue:
    The lack of a type/action field sometimes actually makes it impossible to know what happened.
    a) Setting the value of a changefeed item to the value null and
    b) deleting a changefeed value
    are currently undistinguishable.

  2. added this to the subsequent milestone on Dec 10, 2015
  3. deontologician commented on Dec 10, 2015

    @deontologician
    ContributorAuthor

    Would this be a lot extra to implement on top of #5041? I.e. could we do it in 2.3?

  4. danielmewes commented on Dec 10, 2015

    @danielmewes
    Member

    @deontologician Did you mean a different issue? I'm not sure how this is related to #5041.

    Would this help with Fusion? I'm not opposed to bumping this into 2.3. Just want to understand how urgently we need this.

  5. deontologician commented on Dec 10, 2015

    @deontologician
    ContributorAuthor

    Oh, sorry, #5041 has the concept of "error" changes, which I suggest become {type: "error"}.

    I don't think this is "urgent" since there's a workaround everyone is already doing (barring the edge case correctness issues). But it would be a huge interface improvement for (what I would guess) is not a lot of effort

  6. modified the milestones: , subsequent on Dec 10, 2015
  7. danielmewes commented on Dec 10, 2015

    @danielmewes
    Member

    @deontologician That's true. Let me sneak this in.

  8. encryptio commented on Dec 10, 2015

    @encryptio
    Contributor

    I like this, for readability/maintainability's sake.

    There's not a lot of difference, but I prefer @deontologician's "type" values in the OP ("added" etc), because they more clearly imply that this is an event stream, as opposed to the "action" values like "insert" a la #4194 which could be construed as commands (these objects are not always going to be used that way.)

    As secondary benefit, languages like Go have very verbose "exists in a map" syntax (it requires multiple statements), so the new_val/old_val if-else chains are even worse there, and Go has switch on strings, which would be very clean.

  9. danielmewes commented on Dec 10, 2015

    @danielmewes
    Member

    I like the type values @deontologician listed above as well.

  10. danielmewes commented on Dec 10, 2015

    @danielmewes
    Member

    In addition we should have type: "state" (for includeStates).

    I'm not sure how type: "error" would work. Are you suggesting that we emit an event on the changefeed if someone tries to perform a write, but the write fails? I assumed #5041 was only about the returnChanges result, not for changefeeds. I don't see a reason for why we shouldn't be able to do the same for changefeeds though, if it's desired.

  11. danielmewes commented on Dec 10, 2015

    @danielmewes
    Member

    Actually I take that back:

    I don't see a reason for why we shouldn't be able to do the same for changefeeds though, if it's desired.

    Usually a write fails if not enough replicas are available, or the primary isn't reachable, right? Neither case would allow us to generate corresponding changefeed events with our current architecture.
    If the primary isn't available, we can't generate an event because the changefeed events are generated on the primary.
    If not enough replicas are available, we only realize that after we've already emitted the change event from the primary, the way things currently work.

    I was originally thinking of unchanged writes, that leave a document at the same value. Emitting an event for those might be possible.

  12. mlucy commented on Dec 10, 2015

    @mlucy
    Member

    I think this would be OK. Two things:

    • Do we want an optarg to turn these on or off? (I think so.)
      • Should the optarg default to on or off? (Not sure.)
    • Do we want those particular names?
      • I think the past-tense verbs like "removed" are a little weird as type names. How would people feel about:
        • {type: 'insert'} or {type: 'add'}
        • {type: 'delete'} or {type: 'remove'}
        • {type: 'change}
        • {type: initial}
        • {type: uninitial}.
  13. deontologician commented on Dec 11, 2015

    @deontologician
    ContributorAuthor

    I agree the present tense names sound better. It does seem weird to use command names like insert and delete and then call it change instead of update or replace.

  14. 30 remaining items

  15. mlucy commented on Dec 18, 2015

    @mlucy
    Member

    It would also be pretty easy to change the default to on if it turns out literally everybody is always turning them on. (Whereas if the default is on it would be break a lot of code to change the default to off.)

  16. deontologician commented on Dec 18, 2015

    @deontologician
    ContributorAuthor

    It won't be terrible to add an option, and it's certainly better than not
    getting the tags at all. I'm fine with 5/off if that's the consensus
    otherwise

    On Fri, Dec 18, 2015, 16:11 Michael Lucy [email protected] wrote:

    It would also be pretty easy to change the default to on if it turns out
    literally everybody is always turning them on. (Whereas if the default is
    on it would be break a lot of code to change the default to off.)

    —
    Reply to this email directly or view it on GitHub
    #5188 (comment)
    .

  17. encryptio commented on Dec 21, 2015

    @encryptio
    Contributor

    A bunch of little things (incremental API improvements, implying use of old_val/new_val null-ness is better if useful, and consistency with the other options being default-off optargs) now add up to me now preferring 5/off for this. As @mlucy said, moving to 5/on later (my second preference) is an easy, compatible change to make.

  18. danielmewes commented on Dec 21, 2015

    @danielmewes
    Member

    I think there's a majority for off by default.
    Marking as settled for 6/off (the 5 ones listed above in present tense + "state").

  19. self-assigned this
    on Mar 10, 2016
  20. nighelles commented on Mar 11, 2016

    @nighelles

    This is in review #3525.
    Implemented in nighelles/5188

  21. nighelles commented on Mar 25, 2016

    @nighelles

    This is in next.

  22. modified the milestones: , 2.3 on Mar 30, 2016
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