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

Fix phase locking #1230

Merged
merged 14 commits into from
Sep 3, 2023
15 changes: 8 additions & 7 deletions code/go/0chain.net/blobbercore/allocation/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (r *Repository) GetById(ctx context.Context, id string) (*Allocation, error
}

alloc := &Allocation{}
err = tx.Table(TableNameAllocation).Where(SQLWhereGetById, id).First(alloc).Error
err = tx.Table(TableNameAllocation).Where(SQLWhereGetById, id).Take(alloc).Error
if err != nil {
return alloc, err
}
Expand All @@ -73,7 +73,7 @@ func (r *Repository) GetByIdAndLock(ctx context.Context, id string) (*Allocation
err = tx.Model(&Allocation{}).
Clauses(clause.Locking{Strength: "NO KEY UPDATE"}).
Where("id=?", id).
First(alloc).Error
Take(alloc).Error
if err != nil {
return alloc, err
}
Expand All @@ -99,7 +99,7 @@ func (r *Repository) GetByTx(ctx context.Context, allocationID, txHash string) (
}

alloc := &Allocation{}
err = tx.Table(TableNameAllocation).Where(SQLWhereGetByTx, txHash).First(alloc).Error
err = tx.Table(TableNameAllocation).Where(SQLWhereGetByTx, txHash).Take(alloc).Error
if err != nil {
return alloc, err
}
Expand Down Expand Up @@ -139,7 +139,7 @@ func (r *Repository) GetAllocationIds(ctx context.Context) []Res {

}

func (r *Repository) UpdateAllocationRedeem(ctx context.Context, allocationID, AllocationRoot string) error {
func (r *Repository) UpdateAllocationRedeem(ctx context.Context, allocationID, AllocationRoot string, allocationObj *Allocation) error {
var tx = datastore.GetStore().GetTransaction(ctx)
if tx == nil {
logging.Logger.Panic("no transaction in the context")
Expand All @@ -151,9 +151,10 @@ func (r *Repository) UpdateAllocationRedeem(ctx context.Context, allocationID, A
}
delete(cache, allocationID)

err = tx.Exec("UPDATE allocations SET latest_redeemed_write_marker=?,is_redeem_required=? WHERE id=?",
AllocationRoot, false, allocationID).Error

allocationUpdates := make(map[string]interface{})
allocationUpdates["latest_redeemed_write_marker"] = AllocationRoot
allocationUpdates["is_redeem_required"] = false
err = tx.Model(allocationObj).Updates(allocationUpdates).Error
return err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,6 @@ func (fsh *StorageHandler) CommitWrite(ctx context.Context, r *http.Request) (*b
if err != nil {
return nil, common.NewError("invalid_parameters", "Invalid allocation id passed."+err.Error())
}

if allocationObj.FileOptions == 0 {
return nil, common.NewError("immutable_allocation", "Cannot write to an immutable allocation")
}
Expand Down Expand Up @@ -1407,6 +1406,7 @@ func (fsh *StorageHandler) Rollback(ctx context.Context, r *http.Request) (*blob
Logger.Info("rollback_writemarker", zap.Any("writemarker", writemarkerEntity.WM))

alloc, err := allocation.Repo.GetByIdAndLock(c, allocationID)
Logger.Info("[rollback]Lock Allocation", zap.Bool("is_redeem_required", alloc.IsRedeemRequired), zap.String("allocation_root", alloc.AllocationRoot), zap.String("latest_wm_redeemed", alloc.LatestRedeemedWM))
if err != nil {
txn.Rollback()
return &result, common.NewError("allocation_read_error", "Error reading the allocation object")
Expand Down
19 changes: 11 additions & 8 deletions code/go/0chain.net/blobbercore/writemarker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ func redeemWriteMarker(wm *WriteMarkerEntity) error {
logging.Logger.Info("Stale write marker. Allocation root mismatch",
zap.Any("allocation", allocationID),
zap.Any("wm", wm.WM.AllocationRoot), zap.Any("alloc_root", alloc.AllocationRoot))
_ = wm.UpdateStatus(ctx, Rollbacked, "rollbacked", "")
err = db.Commit().Error
mut := GetLock(allocationID)
if mut != nil {
mut.Release(1)
}
_ = wm.UpdateStatus(ctx, Rollbacked, "rollbacked", "")
err = db.Commit().Error
return err
}

Expand All @@ -100,14 +100,13 @@ func redeemWriteMarker(wm *WriteMarkerEntity) error {

return err
}
mut := GetLock(allocationID)
if mut != nil {
mut.Release(1)
}

err = allocation.Repo.UpdateAllocationRedeem(ctx, wm.WM.AllocationRoot, allocationID)

err = allocation.Repo.UpdateAllocationRedeem(ctx, wm.WM.AllocationRoot, allocationID, alloc)
if err != nil {
mut := GetLock(allocationID)
if mut != nil {
mut.Release(1)
}
logging.Logger.Error("Error redeeming the write marker. Allocation latest wm redeemed update failed",
zap.Any("allocation", allocationID),
zap.Any("wm", wm.WM.AllocationRoot), zap.Any("error", err))
Expand All @@ -116,6 +115,10 @@ func redeemWriteMarker(wm *WriteMarkerEntity) error {
}

err = db.Commit().Error
mut := GetLock(allocationID)
if mut != nil {
mut.Release(1)
}
if err != nil {
logging.Logger.Error("Error committing the writemarker redeem",
zap.Any("allocation", allocationID),
Expand Down
Loading