Skip to content

Commit

Permalink
Fix migration batch to activate the deactivated clients that have att…
Browse files Browse the repository at this point in the history
…ached documents
  • Loading branch information
raararaara committed Nov 13, 2024
1 parent 5482729 commit 0fa59bc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 42 deletions.
63 changes: 22 additions & 41 deletions migrations/v0.5.6/document-detach.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"

"github.com/yorkie-team/yorkie/api/types"
"github.com/yorkie-team/yorkie/server/backend/database"
)

Expand All @@ -33,15 +32,15 @@ const (
StatusKey = "status"
)

func validateDetach(ctx context.Context, collection *mongo.Collection) ([]string, error) {
func validateDetach(ctx context.Context, collection *mongo.Collection, filter bson.M) ([]string, error) {
var failedClients []string
totalCount, err := collection.CountDocuments(ctx, bson.M{})
if err != nil {
return nil, err
}
fmt.Printf("Validation check for %d clients\n", totalCount)

cursor, err := collection.Find(ctx, bson.M{})
cursor, err := collection.Find(ctx, filter)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -73,47 +72,34 @@ func validateDetach(ctx context.Context, collection *mongo.Collection) ([]string
return failedClients, nil
}

func progressMigrationBach(
func progressMigrationBatch(
ctx context.Context,
collection *mongo.Collection,
infos []*database.ClientInfo,
) error {
for _, info := range infos {
fmt.Printf("clientID: %s\n", info.ID)
for docID, clientDocInfo := range info.Documents {
if clientDocInfo.Status != database.DocumentAttached {
continue
}

updater := bson.M{
"$set": bson.M{
clientDocInfoKey(docID, "server_seq"): 0,
clientDocInfoKey(docID, "client_seq"): 0,
clientDocInfoKey(docID, StatusKey): database.DocumentDetached,
"updated_at": time.Now(),
},
}

result := collection.FindOneAndUpdate(ctx, bson.M{
"project_id": info.ProjectID,
"_id": info.ID,
}, updater)

if result.Err() != nil {
if result.Err() == mongo.ErrNoDocuments {
return fmt.Errorf("%s: %w", info.Key, database.ErrClientNotFound)
}
return fmt.Errorf("update client info: %w", result.Err())
result := collection.FindOneAndUpdate(ctx, bson.M{
"project_id": info.ProjectID,
"_id": info.ID,
}, bson.M{
"$set": bson.M{
StatusKey: database.ClientActivated,
"updated_at": time.Now().AddDate(-1, 0, 0),
},
})
if result.Err() != nil {
if result.Err() == mongo.ErrNoDocuments {
return fmt.Errorf("%s: %w", info.Key, database.ErrClientNotFound)
}
return fmt.Errorf("update client info: %w", result.Err())
}

}
return nil
}

// DetachDocumentsFromDeactivatedClients migrates the client collection
// to detach documents attached to a deactivated client.
func DetachDocumentsFromDeactivatedClients(
// ReactivateClients migrates the client collection to activate the clients
// that are in a deactivated but have attached documents.
func ReactivateClients(
ctx context.Context,
db *mongo.Client,
databaseName string,
Expand Down Expand Up @@ -171,7 +157,7 @@ func DetachDocumentsFromDeactivatedClients(
infos = append(infos, &clientInfo)

if len(infos) >= batchSize {
if err := progressMigrationBach(ctx, collection, infos); err != nil {
if err := progressMigrationBatch(ctx, collection, infos); err != nil {
return err
}

Expand All @@ -181,12 +167,12 @@ func DetachDocumentsFromDeactivatedClients(
}
}
if len(infos) > 0 {
if err := progressMigrationBach(ctx, collection, infos); err != nil {
if err := progressMigrationBatch(ctx, collection, infos); err != nil {
return err
}
}

res, err := validateDetach(ctx, collection)
res, err := validateDetach(ctx, collection, filter)
if err != nil {
return err
}
Expand All @@ -197,8 +183,3 @@ func DetachDocumentsFromDeactivatedClients(

return nil
}

// clientDocInfoKey returns the key for the client document info.
func clientDocInfoKey(docID types.ID, prefix string) string {
return fmt.Sprintf("documents.%s.%s", docID, prefix)
}
2 changes: 1 addition & 1 deletion migrations/v0.5.6/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

// RunMigration runs migrations for v0.5.6
func RunMigration(ctx context.Context, db *mongo.Client, databaseName string, batchSize int) error {
err := DetachDocumentsFromDeactivatedClients(ctx, db, databaseName, batchSize)
err := ReactivateClients(ctx, db, databaseName, batchSize)
if err != nil {
return err
}
Expand Down

0 comments on commit 0fa59bc

Please sign in to comment.