-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Zamoca42
wants to merge
1
commit into
HabitRPG:develop
Choose a base branch
from
Zamoca42:patch/member-count
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- 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.
Zamoca42
commented
Sep 6, 2024
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(); | ||
} |
There was a problem hiding this comment.
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Regarding error handling, I've extended
InternalServerError
to createDatabaseError
andTransactionError
. I'm wondering if this is the right approach.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