With mongodb java driver, maxAwaitTime not working on a change stream

106 views
Skip to first unread message

Chris

unread,
Sep 8, 2018, 8:44:30 PM9/8/18
to mongodb-user

I'm using the Java mongodb driver version 3.8.0 and mongodb 3.6.3.

I created a watch on a collection with this:

MongoCursor<ChangeStreamDocument<Document>> cursor = collection.watch().maxAwaitTime(500, TimeUnit.MILLISECONDS).iterator();

The documentation here states about maxAwaitTime: 

The maximum amount of time for the server to wait on new documents to satisfy a change stream query.


However, what I'm seeing is that cursor.hasNext() returns only if there is a change on the collection, never when the time passed to maxAwaitTime has elapsed.

When I turn on mongodb's verbose logging I see maxWaitTime set as expected in the getMore command.

How do I cause my watch to timeout when there are no changes? It seems as though maxAwaitTime should do this. Why doesn't it work?

Robert Cochran

unread,
Sep 8, 2018, 9:09:53 PM9/8/18
to mongodb-user
Hi!

I can't answer your question. I do however want to note that the Java driver is at version 3.8.1 and you might want to try updating to that. Likewise, change streams were introduced with MongoDB 3.6, and version 3.6.3 is pretty early in the 3.6 series. There have been a lot of updates since then. You might want to try testing your change streams on the most recent 3.6.x version (or on MongoDB 4.x.x). 

Bob

Wan Bachtiar

unread,
Sep 10, 2018, 2:22:40 AM9/10/18
to mongodb-user

what I’m seeing is that cursor.hasNext() returns only if there is a change on the collection, never when the time passed to maxAwaitTime has elapsed.

Hi Chris,

maxAwaitTimeMS is not to trigger a timeout on your change stream client cursor.

MongoDB drivers implement change stream as an abstraction of a TAILABLE_AWAIT cursor, and maxAwaitTimeMS is to specify a time limit for a getMore command on on TAILABLE_AWAIT cursor. The option is also called maxTimeMS option on getMore.

If maxAwaitTimeMS is set to a value, the server will wait for the maximum amount specified before returning on an empty getMore.

When I turn on mongodb’s verbose logging I see maxWaitTime set as expected in the getMore command.

You can test this by specifying a timeout of 30 seconds, and you’ll see the getMore being logged every 30 seconds. Compare this to a value lesser, or no value specified. 

How do I cause my watch to timeout when there are no changes?

One idea is to create a separate thread for a timer. The timer resets whenever there’s an activity returned by change stream. If the timer reached a certain value an event is triggered.

Regards,
Wan.

Reply all
Reply to author
Forward
0 new messages