Repository navigation
Make implementations of AsyncAggregateResponseBatchCursor more responsive to close signals - #654
Conversation
…hem more responsive to close signals AsyncAggregateResponseBatchCursor wraps AsyncQueryBatchCursor. AsyncQueryBatchCursor implements pending cancel functionality. AsyncQueryBatchCursor may enter a pending close state as a result of calling close concurrently with the object being in the pending operation state. AsyncAggregateResponseBatchCursor does not need to duplicate the pending functionality, in fact, it prevented AsyncQueryBatchCursor from receiving close signals while being in the pending operation state. JAVA-3907
…arly) The changes made is this commit appear to be correct from the standpoint of tests, but it is very hard for me to prove that by inspecting the code. There are two reasons: - the reference counting approach used in the code - the presence of multiple places in code where the instance field connectionSource is released, combined with the fact that it is not always retained in the constructor and must be released not more than once per the lifetime of an AsyncQueryBatchCursor object. The following text expresses my thoughts about reference counting and is not directly related to the changes made in this commit. The current approach for reference counting in the driver code is what we may call a release-early approach, similar to the one described in the Netty documentation (https://github.com/netty/netty/wiki/Reference-counted-objects#who-destroys-it). In this approach "the party that accesses a reference-counted object last is also responsible for the destruction of that reference-counted object". The problem with this approach is that it is not structured. "Structured" reference counting. The idea is to try and get our reference counting code closer to following the structured programming approach (https://en.wikipedia.org/wiki/Structured_programming) while fixing a bug: AsyncQueryBatchCursor does not always release a connection when it is not needed anymore. In simple words, the following is desirable: given a place in code where retain/release is called, it should be simple to tell where the corresponding release/retain is called. As a side note, structured programming applied to concurrent code is called structured concurrency. This idea is realized in Kotlin: https://kotlinlang.org/docs/reference/coroutines/basics.html#structured-concurrency, https://elizarov.medium.com/structured-concurrency-722d765aa952 (the article is written by the author of Kotlin coroutines functionality). As much as I would like us to use the structured approach, it is generally speaking not possible to reconcile it with the release-early in the same codebase. In other words, a complete refactoring may be needed to switch to the structured approach. Following is an example demonstrating a situation when synchronous code that was written with the release-early approach behaves differently when combined with the structured approach. Consider the following legacy code in which we want to refactor releaseEarlyLegacyMethod (the only code that will stay unchanged is the releaseEarlyUnchangedCode method): void releaseEarlyLegacyMethod() { ReferenceCounted resource = pool.borrow();//count is 1 /* Since ReleaseEarlyLegacyConsumer is the last party using the resource, it must release the resource. * A reader cannot find the release call corresponding to the borrow call inside the releaseEarlyLegacyMethod method.*/ releaseEarlyUnchangedCode(new ReleaseEarlyLegacyOneTimeConsumer(resource)); } class ReleaseEarlyLegacyOneTimeConsumer implements Runnable { private final ReferenceCounted resource; ReleaseEarlyLegacyOneTimeConsumer(ReferenceCounted resource) { this.resource = resource;//count is 1 } public void run() { try { System.out.println(resource.toString());//count is 1; use the resource } finally {//release the resource because ReleaseEarlyLegacyConsumer.accept is the last party accessing it /* Note that by looking at this release call, a reader cannot easily find the corresponding retain/borrow call. * The corresponding retain/borrow call is not even in this class.*/ resource.release();//count is 0; the resource returns to the pool } } } void releaseEarlyUnchangedCode(Runnable action) { action.run(); ReferenceCounted resource = pool.borrow();//count is 1; succeeds only if the single resource has been returned to the pool try {//use the resource System.out.println(resource.toString());//count is 1 } finally {//release the resource because releaseEarlyUnchangedCode is the last party accessing it resource.release();//count is 0; the resource is returned to the pool } } The pool.borrow call in releaseEarlyUnchangedCode works because the resource is returned to the pool before action.run() returns. Here is the refactored code that uses structured reference counting: void structuredNewMethod() { ReferenceCounted resource = pool.borrow();//count is 1 //a reader can easily find the release call corresponding to the borrow call because it is in the finally block in this method try { releaseEarlyUnchangedCode(new StructuredNewOneTimeConsumer(resource)); } finally { resource.release();//count is 0; the resource returns to the pool } } class StructuredNewOneTimeConsumer implements Runnable { private final ReferenceCounted resource; StructuredNewOneTimeConsumer(ReferenceCounted resource) { this.resource = resource.retain();//count is 2; we store a new reference to the resource } public void run() { try { System.out.println(resource.toString());//count is 2; use the resource } finally {//release the resource because StructuredNewConsumer is not going to use it anymore /* The corresponding retain call is in the constructor of the class because resource is the instance field. * A reader may easily locate it.*/ resource.release();//count is 1 } } } With this new code, the pool.borrow call in releaseEarlyUnchangedCode fails because the resource is not returned to the pool before action.run() returns. It is only returned to the pool before structuredNewMethod returns. Thereby, we have shown that the release-early and the structured approaches are not reconcilable. This would not be a problem if the code was written following the structured approach from the beginning because in that case, releaseEarlyUnchangedCode would have been written differently (using either return-early or structured reference counting definitely affects the structure of the rest of the code). JAVA-3907
rozza
left a comment
There was a problem hiding this comment.
I think adding a functional test cancelling the subscription to a change stream and the cursor is killed / bindings are released would be good.
| if (result.getCursor() == null) { | ||
| connectionSource.release(); | ||
| } | ||
| endOperationInProgress(); |
There was a problem hiding this comment.
Can we add a test to ensure that this happens and cursors are closed / killed?
There was a problem hiding this comment.
Seems like this is the spot where we would kill the cursor, if there is still one active. I don't see that it's being done though.
There was a problem hiding this comment.
It is the place, and the cursor is killed as a result of the following sequence:
isClosePending == true
endOperationInProgress()
close()
killCursorOnClose()This is exactly the same logic we had (and still have) in the else branch of the handleGetMoreQueryResult method:
else {
...
else {
connection.release();
if (result.getCursor() == null) {
connectionSource.release();
}
}
endOperationInProgress();
...
}There was a problem hiding this comment.
Can we add a test to ensure that this happens and cursors are closed / killed?
I thought about adding a test. While I can see how to create a test that checks that the subscription reacts to the cancellation as expected from the standpoint of the enduser, creating a similar test only for the AsyncQueryBatchCursor is not as straightforward. I would be glad to discuss how to proceed with such a test. My first thoughts are expressed in this comment (@rozza, it was written before your request, so we may continue discussing it there).
There was a problem hiding this comment.
I think my confusion was due to the fact that below the same connection is used to kill the cursor, while here we release the connection, and check out a new one for the killCursors. Does that difference matter?
There was a problem hiding this comment.
We use the provided connection to close the cursor only if we reached the limit, otherwise the code below kills the cursor in a different connection.
The only good reason I can think of for killing the server cursor in a different connection is if the connection we already have is closed or have to be closed due to a failed IO operation in it. I do not understand why the code in handleGetMoreQueryResult kills the cursor one way or another, and with regard to killing the cursor, I simply followed the logic that was already in the method.
There was a problem hiding this comment.
For tests also think of the change stream ticket that is linked. It would be good to confirm that is fixed.
| if (result.getCursor() == null) { | ||
| connectionSource.release(); | ||
| } | ||
| endOperationInProgress(); |
There was a problem hiding this comment.
Seems like this is the spot where we would kill the cursor, if there is still one active. I don't see that it's being done though.
This reverts commit 0893c18.
| given: | ||
| collectionHelper.create(collectionName, new CreateCollectionOptions().capped(true).sizeInBytes(1000)) | ||
| collectionHelper.insertDocuments(new DocumentCodec(), Document.parse('{}')) | ||
| def firstBatch = executeQuery(new BsonDocument('_id', BsonNull.VALUE), 0, 1, true, true); |
There was a problem hiding this comment.
executeQuery issues the following command:
{"find": "cappedCollectionName", "filter": {"_id": null}, "tailable": true, "awaitData": true, "limit": 0, "batchSize": 1, "$db": "JavaDriverTest", "lsid": {"id": {"$binary": {"base64": "Bfg1KfxTRE6SZBNt0yKANQ==", "subType": "04"}}}}
Surprisingly, if before it is issued, the collection contains 0 documents (line 314 is commented out), then the server does not create a cursor as a result of executing the command. I looked at https://docs.mongodb.com/manual/reference/command/find/, https://docs.mongodb.com/manual/core/capped-collections/, https://docs.mongodb.com/manual/core/tailable-cursors/, and the only possibly related piece of documentation is from the last link: "Tailable cursors may become dead, or invalid, if either: the query returns no match." However, the filter {"_id": null} does not match any documents regardless of whether the collection contains any or not.
@jyemin, @rozza do you happen to know the explanation for this behavior?
There was a problem hiding this comment.
I think it will be covered by the documented caveat. There looks to be an old server ticket about it: SERVER-13955
There was a problem hiding this comment.
Ah, so it's a known bug. Thank you!
…sive to close signals (mongodb#654) 1) Change implementations of AsyncAggregateResponseBatchCursor to make them more responsive to close signals AsyncAggregateResponseBatchCursor wraps AsyncQueryBatchCursor. AsyncQueryBatchCursor implements pending cancel functionality. AsyncQueryBatchCursor may enter a pending close state as a result of calling close concurrently with the object being in the pending operation state. AsyncAggregateResponseBatchCursor does not need to duplicate the pending functionality, in fact, it prevented AsyncQueryBatchCursor from receiving close signals while being in the pending operation state. 2) Release resources in AsyncQueryBatchCursor The changes made is this commit appear to be correct from the standpoint of tests, but it is very hard for me to prove that by inspecting the code. There are two reasons: - the reference counting approach used in the code - the presence of multiple places in code where the instance field connectionSource is released, combined with the fact that it is not always retained in the constructor and must be released not more than once per the lifetime of an AsyncQueryBatchCursor object. The following text expresses my thoughts about reference counting and is not directly related to the changes made in this commit. The current approach for reference counting in the driver code is what we may call a release-early approach, similar to the one described in the Netty documentation (https://github.com/netty/netty/wiki/Reference-counted-objects#who-destroys-it). In this approach "the party that accesses a reference-counted object last is also responsible for the destruction of that reference-counted object". The problem with this approach is that it is not structured. "Structured" reference counting. The idea is to try and get our reference counting code closer to following the structured programming approach (https://en.wikipedia.org/wiki/Structured_programming) while fixing a bug: AsyncQueryBatchCursor does not always release a connection when it is not needed anymore. In simple words, the following is desirable: given a place in code where retain/release is called, it should be simple to tell where the corresponding release/retain is called. As a side note, structured programming applied to concurrent code is called structured concurrency. This idea is realized in Kotlin: https://kotlinlang.org/docs/reference/coroutines/basics.html#structured-concurrency, https://elizarov.medium.com/structured-concurrency-722d765aa952 (the article is written by the author of Kotlin coroutines functionality). As much as I would like us to use the structured approach, it is generally speaking not possible to reconcile it with the release-early in the same codebase. In other words, a complete refactoring may be needed to switch to the structured approach. Following is an example demonstrating a situation when synchronous code that was written with the release-early approach behaves differently when combined with the structured approach. Consider the following legacy code in which we want to refactor releaseEarlyLegacyMethod (the only code that will stay unchanged is the releaseEarlyUnchangedCode method): void releaseEarlyLegacyMethod() { ReferenceCounted resource = pool.borrow();//count is 1 /* Since ReleaseEarlyLegacyConsumer is the last party using the resource, it must release the resource. * A reader cannot find the release call corresponding to the borrow call inside the releaseEarlyLegacyMethod method.*/ releaseEarlyUnchangedCode(new ReleaseEarlyLegacyOneTimeConsumer(resource)); } class ReleaseEarlyLegacyOneTimeConsumer implements Runnable { private final ReferenceCounted resource; ReleaseEarlyLegacyOneTimeConsumer(ReferenceCounted resource) { this.resource = resource;//count is 1 } public void run() { try { System.out.println(resource.toString());//count is 1; use the resource } finally {//release the resource because ReleaseEarlyLegacyConsumer.accept is the last party accessing it /* Note that by looking at this release call, a reader cannot easily find the corresponding retain/borrow call. * The corresponding retain/borrow call is not even in this class.*/ resource.release();//count is 0; the resource returns to the pool } } } void releaseEarlyUnchangedCode(Runnable action) { action.run(); ReferenceCounted resource = pool.borrow();//count is 1; succeeds only if the single resource has been returned to the pool try {//use the resource System.out.println(resource.toString());//count is 1 } finally {//release the resource because releaseEarlyUnchangedCode is the last party accessing it resource.release();//count is 0; the resource is returned to the pool } } The pool.borrow call in releaseEarlyUnchangedCode works because the resource is returned to the pool before action.run() returns. Here is the refactored code that uses structured reference counting: void structuredNewMethod() { ReferenceCounted resource = pool.borrow();//count is 1 //a reader can easily find the release call corresponding to the borrow call because it is in the finally block in this method try { releaseEarlyUnchangedCode(new StructuredNewOneTimeConsumer(resource)); } finally { resource.release();//count is 0; the resource returns to the pool } } class StructuredNewOneTimeConsumer implements Runnable { private final ReferenceCounted resource; StructuredNewOneTimeConsumer(ReferenceCounted resource) { this.resource = resource.retain();//count is 2; we store a new reference to the resource } public void run() { try { System.out.println(resource.toString());//count is 2; use the resource } finally {//release the resource because StructuredNewConsumer is not going to use it anymore /* The corresponding retain call is in the constructor of the class because resource is the instance field. * A reader may easily locate it.*/ resource.release();//count is 1 } } } With this new code, the pool.borrow call in releaseEarlyUnchangedCode fails because the resource is not returned to the pool before action.run() returns. It is only returned to the pool before structuredNewMethod returns. Thereby, we have shown that the release-early and the structured approaches are not reconcilable. This would not be a problem if the code was written following the structured approach from the beginning because in that case, releaseEarlyUnchangedCode would have been written differently (using either return-early or structured reference counting definitely affects the structure of the rest of the code). JAVA-3907
…sive to close signals (#654) 1) Change implementations of AsyncAggregateResponseBatchCursor to make them more responsive to close signals AsyncAggregateResponseBatchCursor wraps AsyncQueryBatchCursor. AsyncQueryBatchCursor implements pending cancel functionality. AsyncQueryBatchCursor may enter a pending close state as a result of calling close concurrently with the object being in the pending operation state. AsyncAggregateResponseBatchCursor does not need to duplicate the pending functionality, in fact, it prevented AsyncQueryBatchCursor from receiving close signals while being in the pending operation state. 2) Release resources in AsyncQueryBatchCursor The changes made is this commit appear to be correct from the standpoint of tests, but it is very hard for me to prove that by inspecting the code. There are two reasons: - the reference counting approach used in the code - the presence of multiple places in code where the instance field connectionSource is released, combined with the fact that it is not always retained in the constructor and must be released not more than once per the lifetime of an AsyncQueryBatchCursor object. The following text expresses my thoughts about reference counting and is not directly related to the changes made in this commit. The current approach for reference counting in the driver code is what we may call a release-early approach, similar to the one described in the Netty documentation (https://github.com/netty/netty/wiki/Reference-counted-objects#who-destroys-it). In this approach "the party that accesses a reference-counted object last is also responsible for the destruction of that reference-counted object". The problem with this approach is that it is not structured. "Structured" reference counting. The idea is to try and get our reference counting code closer to following the structured programming approach (https://en.wikipedia.org/wiki/Structured_programming) while fixing a bug: AsyncQueryBatchCursor does not always release a connection when it is not needed anymore. In simple words, the following is desirable: given a place in code where retain/release is called, it should be simple to tell where the corresponding release/retain is called. As a side note, structured programming applied to concurrent code is called structured concurrency. This idea is realized in Kotlin: https://kotlinlang.org/docs/reference/coroutines/basics.html#structured-concurrency, https://elizarov.medium.com/structured-concurrency-722d765aa952 (the article is written by the author of Kotlin coroutines functionality). As much as I would like us to use the structured approach, it is generally speaking not possible to reconcile it with the release-early in the same codebase. In other words, a complete refactoring may be needed to switch to the structured approach. Following is an example demonstrating a situation when synchronous code that was written with the release-early approach behaves differently when combined with the structured approach. Consider the following legacy code in which we want to refactor releaseEarlyLegacyMethod (the only code that will stay unchanged is the releaseEarlyUnchangedCode method): void releaseEarlyLegacyMethod() { ReferenceCounted resource = pool.borrow();//count is 1 /* Since ReleaseEarlyLegacyConsumer is the last party using the resource, it must release the resource. * A reader cannot find the release call corresponding to the borrow call inside the releaseEarlyLegacyMethod method.*/ releaseEarlyUnchangedCode(new ReleaseEarlyLegacyOneTimeConsumer(resource)); } class ReleaseEarlyLegacyOneTimeConsumer implements Runnable { private final ReferenceCounted resource; ReleaseEarlyLegacyOneTimeConsumer(ReferenceCounted resource) { this.resource = resource;//count is 1 } public void run() { try { System.out.println(resource.toString());//count is 1; use the resource } finally {//release the resource because ReleaseEarlyLegacyConsumer.accept is the last party accessing it /* Note that by looking at this release call, a reader cannot easily find the corresponding retain/borrow call. * The corresponding retain/borrow call is not even in this class.*/ resource.release();//count is 0; the resource returns to the pool } } } void releaseEarlyUnchangedCode(Runnable action) { action.run(); ReferenceCounted resource = pool.borrow();//count is 1; succeeds only if the single resource has been returned to the pool try {//use the resource System.out.println(resource.toString());//count is 1 } finally {//release the resource because releaseEarlyUnchangedCode is the last party accessing it resource.release();//count is 0; the resource is returned to the pool } } The pool.borrow call in releaseEarlyUnchangedCode works because the resource is returned to the pool before action.run() returns. Here is the refactored code that uses structured reference counting: void structuredNewMethod() { ReferenceCounted resource = pool.borrow();//count is 1 //a reader can easily find the release call corresponding to the borrow call because it is in the finally block in this method try { releaseEarlyUnchangedCode(new StructuredNewOneTimeConsumer(resource)); } finally { resource.release();//count is 0; the resource returns to the pool } } class StructuredNewOneTimeConsumer implements Runnable { private final ReferenceCounted resource; StructuredNewOneTimeConsumer(ReferenceCounted resource) { this.resource = resource.retain();//count is 2; we store a new reference to the resource } public void run() { try { System.out.println(resource.toString());//count is 2; use the resource } finally {//release the resource because StructuredNewConsumer is not going to use it anymore /* The corresponding retain call is in the constructor of the class because resource is the instance field. * A reader may easily locate it.*/ resource.release();//count is 1 } } } With this new code, the pool.borrow call in releaseEarlyUnchangedCode fails because the resource is not returned to the pool before action.run() returns. It is only returned to the pool before structuredNewMethod returns. Thereby, we have shown that the release-early and the structured approaches are not reconcilable. This would not be a problem if the code was written following the structured approach from the beginning because in that case, releaseEarlyUnchangedCode would have been written differently (using either return-early or structured reference counting definitely affects the structure of the rest of the code). JAVA-3907
…sive to close signals (mongodb#654) 1) Change implementations of AsyncAggregateResponseBatchCursor to make them more responsive to close signals AsyncAggregateResponseBatchCursor wraps AsyncQueryBatchCursor. AsyncQueryBatchCursor implements pending cancel functionality. AsyncQueryBatchCursor may enter a pending close state as a result of calling close concurrently with the object being in the pending operation state. AsyncAggregateResponseBatchCursor does not need to duplicate the pending functionality, in fact, it prevented AsyncQueryBatchCursor from receiving close signals while being in the pending operation state. 2) Release resources in AsyncQueryBatchCursor The changes made is this commit appear to be correct from the standpoint of tests, but it is very hard for me to prove that by inspecting the code. There are two reasons: - the reference counting approach used in the code - the presence of multiple places in code where the instance field connectionSource is released, combined with the fact that it is not always retained in the constructor and must be released not more than once per the lifetime of an AsyncQueryBatchCursor object. The following text expresses my thoughts about reference counting and is not directly related to the changes made in this commit. The current approach for reference counting in the driver code is what we may call a release-early approach, similar to the one described in the Netty documentation (https://github.com/netty/netty/wiki/Reference-counted-objects#who-destroys-it). In this approach "the party that accesses a reference-counted object last is also responsible for the destruction of that reference-counted object". The problem with this approach is that it is not structured. "Structured" reference counting. The idea is to try and get our reference counting code closer to following the structured programming approach (https://en.wikipedia.org/wiki/Structured_programming) while fixing a bug: AsyncQueryBatchCursor does not always release a connection when it is not needed anymore. In simple words, the following is desirable: given a place in code where retain/release is called, it should be simple to tell where the corresponding release/retain is called. As a side note, structured programming applied to concurrent code is called structured concurrency. This idea is realized in Kotlin: https://kotlinlang.org/docs/reference/coroutines/basics.html#structured-concurrency, https://elizarov.medium.com/structured-concurrency-722d765aa952 (the article is written by the author of Kotlin coroutines functionality). As much as I would like us to use the structured approach, it is generally speaking not possible to reconcile it with the release-early in the same codebase. In other words, a complete refactoring may be needed to switch to the structured approach. Following is an example demonstrating a situation when synchronous code that was written with the release-early approach behaves differently when combined with the structured approach. Consider the following legacy code in which we want to refactor releaseEarlyLegacyMethod (the only code that will stay unchanged is the releaseEarlyUnchangedCode method): void releaseEarlyLegacyMethod() { ReferenceCounted resource = pool.borrow();//count is 1 /* Since ReleaseEarlyLegacyConsumer is the last party using the resource, it must release the resource. * A reader cannot find the release call corresponding to the borrow call inside the releaseEarlyLegacyMethod method.*/ releaseEarlyUnchangedCode(new ReleaseEarlyLegacyOneTimeConsumer(resource)); } class ReleaseEarlyLegacyOneTimeConsumer implements Runnable { private final ReferenceCounted resource; ReleaseEarlyLegacyOneTimeConsumer(ReferenceCounted resource) { this.resource = resource;//count is 1 } public void run() { try { System.out.println(resource.toString());//count is 1; use the resource } finally {//release the resource because ReleaseEarlyLegacyConsumer.accept is the last party accessing it /* Note that by looking at this release call, a reader cannot easily find the corresponding retain/borrow call. * The corresponding retain/borrow call is not even in this class.*/ resource.release();//count is 0; the resource returns to the pool } } } void releaseEarlyUnchangedCode(Runnable action) { action.run(); ReferenceCounted resource = pool.borrow();//count is 1; succeeds only if the single resource has been returned to the pool try {//use the resource System.out.println(resource.toString());//count is 1 } finally {//release the resource because releaseEarlyUnchangedCode is the last party accessing it resource.release();//count is 0; the resource is returned to the pool } } The pool.borrow call in releaseEarlyUnchangedCode works because the resource is returned to the pool before action.run() returns. Here is the refactored code that uses structured reference counting: void structuredNewMethod() { ReferenceCounted resource = pool.borrow();//count is 1 //a reader can easily find the release call corresponding to the borrow call because it is in the finally block in this method try { releaseEarlyUnchangedCode(new StructuredNewOneTimeConsumer(resource)); } finally { resource.release();//count is 0; the resource returns to the pool } } class StructuredNewOneTimeConsumer implements Runnable { private final ReferenceCounted resource; StructuredNewOneTimeConsumer(ReferenceCounted resource) { this.resource = resource.retain();//count is 2; we store a new reference to the resource } public void run() { try { System.out.println(resource.toString());//count is 2; use the resource } finally {//release the resource because StructuredNewConsumer is not going to use it anymore /* The corresponding retain call is in the constructor of the class because resource is the instance field. * A reader may easily locate it.*/ resource.release();//count is 1 } } } With this new code, the pool.borrow call in releaseEarlyUnchangedCode fails because the resource is not returned to the pool before action.run() returns. It is only returned to the pool before structuredNewMethod returns. Thereby, we have shown that the release-early and the structured approaches are not reconcilable. This would not be a problem if the code was written following the structured approach from the beginning because in that case, releaseEarlyUnchangedCode would have been written differently (using either return-early or structured reference counting definitely affects the structure of the rest of the code). JAVA-3907
…e responsive to close signals (mongodb#654)" This reverts commit bcf1a42.
See also JAVA-3964.
JAVA-3907