[JAVA-2592] Force change stream resume token to encode as a type 4 binary Created: 30/Aug/17  Updated: 29/Oct/23  Resolved: 15/Sep/17

Status: Closed
Project: Java Driver
Component/s: Query Operations
Affects Version/s: None
Fix Version/s: 3.6.0

Type: Improvement Priority: Critical - P2
Reporter: Jeffrey Yemin Assignee: Ross Lawley
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-29716 Keystring-encode ResumeTokens to allo... Closed
Epic Link: Java MongoDB 3.6 Support
Case:

 Description   

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.


Generated at Thu Feb 08 08:57:37 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.