-
-
Notifications
You must be signed in to change notification settings - Fork 147
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
Add migration script to detach documents from deactivated clients #1062
Conversation
WalkthroughThis pull request introduces a new file, Changes
Possibly related PRs
Suggested reviewers
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1062 +/- ##
=======================================
Coverage 46.79% 46.79%
=======================================
Files 84 84
Lines 12219 12219
=======================================
Hits 5718 5718
Misses 5928 5928
Partials 573 573 ☔ View full report in Codecov by Sentry. 🚨 Try these New Features:
|
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.
Actionable comments posted: 7
🧹 Outside diff range and nitpick comments (2)
migrations/v0.5.6/main.go (1)
17-18
: Enhance package documentation with migration purposeThe package documentation should provide more context about the specific purpose of this migration. Consider adding details about reactivating clients with attached documents to enable proper document detachment.
-// Package v056 provides migration for v0.5.6 +// Package v056 provides migration for v0.5.6 to handle document detachment from +// deactivated clients. It temporarily reactivates clients that have attached +// documents to allow the housekeeping process to properly detach these documents. package v056cmd/yorkie/migration.go (1)
46-48
: Consider migration safety and rollback strategy.Given that this migration modifies client activation states which affects document attachments:
- Ensure the migration is idempotent and can be safely re-run
- Consider adding a dry-run mode to preview the changes
- Document the rollback procedure in case of partial migration failure
Would you like me to help draft the documentation for these safety considerations?
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (3)
cmd/yorkie/migration.go
(2 hunks)migrations/v0.5.6/document-detach.go
(1 hunks)migrations/v0.5.6/main.go
(1 hunks)
🔇 Additional comments (5)
migrations/v0.5.6/main.go (3)
20-24
: LGTM!
The imports are clean and contain only the necessary packages.
1-34
: Verify migration system integration
Let's ensure this migration is properly integrated into the migration system and follows the correct version sequence.
#!/bin/bash
# Check migration registration and version sequence
echo "Checking migration registration..."
rg -A 5 'migrationMap\[' cmd/yorkie/migration.go
echo "Checking for other v0.5.x migrations..."
fd -t d '^v0\.5\.' migrations/
26-34
: 🛠️ Refactor suggestion
Improve function documentation and error handling
The function implementation needs several improvements:
- The function documentation should describe the purpose and parameters
- The error handling could be more specific about what went wrong
- The batchSize parameter's valid range should be validated
-// RunMigration runs migrations for v0.5.6
+// RunMigration executes the v0.5.6 migration to reactivate clients that have
+// attached documents, allowing the housekeeping process to properly detach them.
+// The batchSize parameter controls how many clients are processed in each batch.
+//
+// Parameters:
+// - ctx: Context for the operation
+// - db: MongoDB client
+// - databaseName: Name of the database
+// - batchSize: Number of clients to process in each batch (must be > 0)
func RunMigration(ctx context.Context, db *mongo.Client, databaseName string, batchSize int) error {
+ if batchSize <= 0 {
+ return fmt.Errorf("invalid batch size: %d, must be greater than 0", batchSize)
+ }
+
err := ReactivateClients(ctx, db, databaseName, batchSize)
if err != nil {
- return err
+ return fmt.Errorf("failed to reactivate clients: %w", err)
}
return nil
}
Let's verify the ReactivateClients implementation:
cmd/yorkie/migration.go (2)
33-33
: Verify the v0.5.6 migration package implementation.
The import looks correct. Let's verify the implementation of the migration package.
#!/bin/bash
# Description: Verify the v0.5.6 migration package implementation
# Check if the migration package exists and contains the required files
fd -t f "main.go|document-detach.go" -p "migrations/v0.5.6"
# Verify RunMigration function signature in the v0.5.6 package
ast-grep --pattern 'func RunMigration(ctx context.Context, db *mongo.Client, dbName string, batchSize int) error'
47-47
: Verify version ordering in migrationMap.
The new version entry follows the correct pattern. However, let's ensure there are no missing versions between v0.5.3 and v0.5.6 that should be included.
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.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (1)
migrations/v0.5.6/main.go (1)
33-35
: Consider adding logging and transaction wrappingWhile the integration is correct, consider these improvements for better observability and reliability:
- Add logging before starting the reactivation step
- Consider wrapping the operation in a transaction for atomicity
Example enhancement:
+ fmt.Println("Starting client reactivation migration...") + + session, err := db.StartSession() + if err != nil { + return fmt.Errorf("failed to start session: %w", err) + } + defer session.EndSession(ctx) + + if err := session.StartTransaction(); err != nil { + return fmt.Errorf("failed to start transaction: %w", err) + } + if err := ReactivateClients(ctx, db, databaseName, batchSize); err != nil { + session.AbortTransaction(ctx) return err } + + if err := session.CommitTransaction(ctx); err != nil { + return fmt.Errorf("failed to commit transaction: %w", err) + } + + fmt.Println("Client reactivation migration completed successfully")🧰 Tools
🪛 GitHub Check: build
[failure] 33-33:
undefined: err
[failure] 34-34:
undefined: err (typecheck)
[failure] 33-33:
undefined: err
[failure] 34-34:
undefined: err) (typecheck)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
migrations/v0.5.6/main.go
(1 hunks)
🧰 Additional context used
🪛 GitHub Check: build
migrations/v0.5.6/main.go
[failure] 33-33:
undefined: err
[failure] 34-34:
undefined: err (typecheck)
[failure] 33-33:
undefined: err
[failure] 34-34:
undefined: err) (typecheck)
🔇 Additional comments (1)
migrations/v0.5.6/main.go (1)
33-35
: Verify migration safety and rollback mechanism
Since this migration modifies client states which triggers document detachment via housekeeping:
- Ensure there's a rollback strategy if the migration fails midway
- Verify the housekeeping process can handle the potential surge in activated clients
Let's check for related housekeeping code:
✅ Verification successful
Let me gather more information about the migration and rollback mechanisms.
Let me check the complete implementation of the migration and its error handling.
Migration is safe with proper error handling and batch processing
The implementation shows a well-designed migration with:
- Batch processing with progress tracking to handle large datasets
- Proper error handling and logging for each client update
- Post-migration validation to ensure consistency
- Idempotent operations using FindOneAndUpdate
- No risk of document detachment surge as it only activates clients that already have attached documents
The housekeeping process won't be impacted as this migration is corrective in nature - fixing an inconsistency where deactivated clients had attached documents.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Search for housekeeping-related code that handles document detachment
rg -A 5 "func.*[Hh]ousekeeping|func.*[Dd]etach[Dd]ocument"
# Look for any rate limiting or batch processing in housekeeping
ast-grep --pattern 'func $FUNC($_) {
$$$
batch$_ := $_
$$$
}'
Length of output: 15195
Script:
#!/bin/bash
# Check the migration file for rollback mechanism and batch processing
rg -A 10 "func ReactivateClients|func Rollback" migrations/
# Look for any error handling and transaction management in the migration
ast-grep --pattern 'func $FUNC($_) {
$$$
tx := $_.Begin()
$$$
}'
# Check for any batch size configuration or rate limiting
rg -A 5 "batchSize.*:=|const.*[Bb]atch[Ss]ize"
Length of output: 836
Script:
#!/bin/bash
# Get the complete implementation of the migration file
cat migrations/v0.5.6/document-detach.go
# Check for any transaction or error handling patterns in migrations
rg -A 10 "func.*Up.*|func.*Down.*" migrations/v0.5.6/
# Look for any MongoDB bulk operation patterns
rg -A 5 "UpdateMany|BulkWrite" migrations/v0.5.6/
Length of output: 5148
🧰 Tools
🪛 GitHub Check: build
[failure] 33-33:
undefined: err
[failure] 34-34:
undefined: err (typecheck)
[failure] 33-33:
undefined: err
[failure] 34-34:
undefined: err) (typecheck)
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.
Thanks for your contribution.
What this PR does / why we need it?
This PR introduces a migration script to properly detach documents that are still attached to deactivated clients.
While PRs #907 and #1036 improved the housekeeping process to ensure documents can be detached correctly, there may still be documents attached to deactivated clients from before these changes were applied.
Given the complexity of handling presence clears and version vector deletions during the detachment process, this migration script does not directly detach the documents. Instead, it updates the relevant clients from deactivated to activated, allowing the housekeeping process to manage the actual detachment of documents as intended.
Any background context you want to provide?
Deactivated clients may still have attached documents, which can lead to complications in managing document states. By running this migration script, we ensure a cleaner and more reliable detachment process for documents linked to clients that are no longer active.
What are the relevant tickets?
Related #602
Special notes for your reviewer:
Does this PR introduce a user-facing change?:
Additional documentation:
Checklist:
Summary by CodeRabbit
New Features
Bug Fixes
Documentation