Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Fix member count inaccurate in Guilds and Parties #15314

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

Zamoca42
Copy link

@Zamoca42 Zamoca42 commented Sep 6, 2024

Fixes #12286

Changes

Hi there! I've been working on this issue, continuing from the work done in #14203.

Based on the previously implemented content, I've implemented a transaction-based approach.
In addition, I've added error messages for situations that may occur if the transaction fails.

Questions

I have a couple of questions about the implementation.

  1. Regarding error handling, I've extended InternalServerError to create DatabaseError and TransactionError. I'm wondering if this is the right approach.

  2. For testing purposes, I'm trying to reproduce this issue locally.
    Do you have any suggestions on how to set up a group for a test account without triggering group plan payments? I'm not quite sure how to navigate this.

Any guidance you can provide on these questions would be really helpful. Thanks!


UUID: 02dac320-60bd-4178-8154-11bc222d4419

- This commit adds functionality to update the `memberCount` property of a group when a
member is successfully removed from the group. 
- It also includes error handling for
scenarios where the user or group save operations fail, ensuring the `memberCount` is
not updated in those cases. 
- Additionally, a transaction-based approach is
implemented to roll back the `memberCount` update if the overall transaction fails.
Comment on lines +1005 to +1026
const session = await mongoose.startSession();

try {
await session.withTransaction(async () => {
await member.save({ session });
await group.save({ session });
}, {
retryWrites: true,
});
} catch (err) {
if (err.name === 'MongoError') {
throw err.hasErrorLabel('TransactionTooLargeForCache')
? new TransactionError(`Transaction too large for cache: ${err.message}`)
: new DatabaseError(`Database error: ${err.message}`);
} else if (err.name === 'ValidationError') {
throw validationErrors;
} else {
throw new InternalServerError(`Unexpected error: ${err.message}`);
}
} finally {
session.endSession();
}
Copy link
Author

@Zamoca42 Zamoca42 Sep 6, 2024

Choose a reason for hiding this comment

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

Suggested change
const session = await mongoose.startSession();
try {
await session.withTransaction(async () => {
await member.save({ session });
await group.save({ session });
}, {
retryWrites: true,
});
} catch (err) {
if (err.name === 'MongoError') {
throw err.hasErrorLabel('TransactionTooLargeForCache')
? new TransactionError(`Transaction too large for cache: ${err.message}`)
: new DatabaseError(`Database error: ${err.message}`);
} else if (err.name === 'ValidationError') {
throw validationErrors;
} else {
throw new InternalServerError(`Unexpected error: ${err.message}`);
}
} finally {
session.endSession();
}
const originalMemberCount = group.memberCount;
try {
await Group.db.transaction(async session => {
group.memberCount -= 1;
await member.save({ session });
await group.save({ session });
}, {
retryWrites: true,
});
} catch (err) {
group.memberCount = originalMemberCount;
await group.save();
if (err.name === 'MongoError') {
throw err.hasErrorLabel('TransactionTooLargeForCache')
? new TransactionError(`Transaction too large for cache: ${err.message}`)
: new DatabaseError(`Database error: ${err.message}`);
} else if (err.name === 'ValidationError') {
throw validationErrors;
} else {
throw new InternalServerError(`Unexpected error: ${err.message}`);
}
}

When not import Mongoose with startSession, we could also consider an approach like this.

@Zamoca42 Zamoca42 changed the title Fix member count inaccurate in Guilds and Parties WIP: Fix member count inaccurate in Guilds and Parties Sep 6, 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.

Member count inaccurate in Guilds and Parties
1 participant