Skip to content

Fix aggregate cursor als#15094

Merged
vkarpov15 merged 2 commits intoAutomattic:masterfrom
IchirokuXVI:fix-aggregate-cursor-als
Dec 16, 2024
Merged

Fix aggregate cursor als#15094
vkarpov15 merged 2 commits intoAutomattic:masterfrom
IchirokuXVI:fix-aggregate-cursor-als

Conversation

@IchirokuXVI
Copy link
Contributor

Summary

Aggregate cursors didn't automatically add session if it was present in AsyncLocalStorage. Also the options weren't the same for exec and cursor so I added a function to make them the same just like in QueryCursor.

Examples

const mongoose = require("mongoose");
mongoose.set("transactionAsyncLocalStorage", true);

mongoose.connect("mongodb://127.0.0.1:27017/cursortest");

const test = async () => {
  await mongoose.connection.transaction(async (session) => {
    const TestModel = mongoose.model(
      "Test",
      new mongoose.Schema({ name: String })
    );

    mongoose.set("debug", true);
    await TestModel.aggregate([{ $match: {} }]);
    const cursor1 = TestModel.aggregate([{ $match: {} }]).cursor();
    const cursor2 = TestModel.aggregate([{ $match: {} }])
      .session(session)
      .cursor();

    await cursor1.next();
    await cursor2.next();
    mongoose.set("debug", false);

    /*
        From the debug logs:
            Mongoose: TestModel.aggregate([ { '$match': {} } ], { session: ClientSession("bb843cfeb01d4ec3ad0373d2b7d3db20") })
            Mongoose: TestModel.aggregate([ { '$match': {} } ], { cursor: {}, session: ClientSession("bb843cfeb01d4ec3ad0373d2b7d3db20")})
            Mongoose: TestModel.aggregate([ { '$match': {} } ], { session: ClientSession("bb843cfeb01d4ec3ad0373d2b7d3db20"), cursor: {}})

        Before the fix the first cursor didn't have a session but aggregate and second cursor had a session.
    */
  });
};

test()
  .then(() => console.log("Finished"))
  .catch((error) => console.error(error))
  .finally(() => process.exit(0));

@IchirokuXVI IchirokuXVI force-pushed the fix-aggregate-cursor-als branch from 46fc7ed to 51e6b08 Compare December 13, 2024 17:09
Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a couple of minor style suggestions, but looks good overall.

lib/aggregate.js Outdated
*/

Aggregate.prototype._optionsForExec = function() {
const options = clone(this.options || {});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clone() shouldn't be necessary here, would be ideal to avoid cloning for performance purposes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous implementation also used clone(), so I retained it to ensure no existing functionality was unintentionally affected. However, I agree that removing clone() could improve performance. If it’s deemed unnecessary, I suggest calling the function directly in the constructor to initialize the options instead.

function _init(model, c, agg) {
if (!model.collection.buffer) {
model.hooks.execPre('aggregate', agg, function() {
Object.assign(c.options, agg._optionsForExec());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to previous comments, is there any need to set c.options at all? Could just call aggregate(agg._pipeline, agg.optionsForExec())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In QueryCursor, options is also set to a new object, so I followed the same approach here.
That said, QueryCursor uses the options object for some checks while AggregateCursor only uses it in _init, so it might make sense to remove it from AggregateCursor for now.

@IchirokuXVI
Copy link
Contributor Author

Removed the cloning in _optionsForExec and also sest the options before creating AggregationCursor in Aggregate.cursor(). Passed all tests with npm test.

@IchirokuXVI IchirokuXVI force-pushed the fix-aggregate-cursor-als branch from 6ec8cd5 to 4b8a25e Compare December 16, 2024 09:28
@vkarpov15 vkarpov15 added this to the 8.9.2 milestone Dec 16, 2024
@vkarpov15 vkarpov15 merged commit 341b238 into Automattic:master Dec 16, 2024
vkarpov15 added a commit that referenced this pull request Dec 16, 2024
vkarpov15 added a commit that referenced this pull request Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants