Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-2592

Force change stream resume token to encode as a type 4 binary

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Critical - P2 Critical - P2
    • 3.6.0
    • Affects Version/s: None
    • Component/s: Query Operations
    • Labels:
      None

      Currently, the resumeToken for a ChangeStreamIterable is encoded using the default codec registry for the collection. But that can cause an issue because the code registry may be (and is by default) configured with a registry that encodes UUID as type 3 (this goes back to a very early incompatibility between different MongoDB drivers), and the server will error if the resumeToken is anything but type 4.

      To work around this, an application can configure a different registry for the MongoCollection used to create the change stream:

      CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
                 CodecRegistries.fromCodecs(new UuidCodec(UuidRepresentation.STANDARD)),
                 collection.getCodecRegistry());
      collection = collection.withCodecRegistry(codecRegistry);
      
      Document resumeToken = next.get("_id", Document.class);
      MongoCursor<Document> iterator = collection.watch().resumeAfter(resumeToken).iterator();
      

      But this is only safe to do if the actual documents in the watched collection do not contain any type 3 UUIDs. Otherwise, they will be decoded incorrectly in the fullDocument field of the change stream documents and also when re-encoding the resumeToken, as the resumeToken itself contains the _id of the document.

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: