Skip to content

Commit e69c130

Browse files
committed
fix(writemarker):smaller transaction on redeem WM for #624
1 parent ee7306d commit e69c130

File tree

1 file changed

+45
-28
lines changed
  • code/go/0chain.net/blobbercore/writemarker

1 file changed

+45
-28
lines changed

code/go/0chain.net/blobbercore/writemarker/worker.go

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,9 @@ func SetupWorkers(ctx context.Context) {
1717
}
1818

1919
func redeemWriterMarkersForAllocation(allocationObj *allocation.Allocation) {
20-
ctx := datastore.GetStore().CreateTransaction(context.TODO())
21-
db := datastore.GetStore().GetTransaction(ctx)
22-
var err error
2320

24-
done := false
25-
26-
defer func() {
27-
28-
if !done {
29-
if err := db.Rollback().Error; err != nil {
30-
logging.Logger.Error("Error rollbacking the writemarker redeem",
31-
zap.Any("allocation", allocationObj.ID),
32-
zap.Error(err))
33-
}
34-
}
35-
ctx.Done()
36-
}()
21+
db := datastore.GetStore().GetDB()
22+
var err error
3723

3824
var writemarkers []*WriteMarkerEntity
3925

@@ -53,37 +39,68 @@ func redeemWriterMarkersForAllocation(allocationObj *allocation.Allocation) {
5339
startredeem = true
5440
}
5541
if startredeem || allocationObj.LatestRedeemedWM == "" {
56-
err = wm.RedeemMarker(ctx)
42+
43+
err = redeemWriteMarker(allocationObj, wm)
5744
if err != nil {
58-
logging.Logger.Error("Error redeeming the write marker.",
59-
zap.Any("wm", wm.WM.AllocationID), zap.Any("error", err))
6045
return
6146
}
62-
err = db.Model(allocationObj).Updates(allocation.Allocation{LatestRedeemedWM: wm.WM.AllocationRoot}).Error
63-
if err != nil {
64-
logging.Logger.Error("Error redeeming the write marker. Allocation latest wm redeemed update failed",
65-
zap.Any("wm", wm.WM.AllocationRoot), zap.Any("error", err))
6647

67-
return
68-
}
69-
allocationObj.LatestRedeemedWM = wm.WM.AllocationRoot
70-
logging.Logger.Info("Success Redeeming the write marker", zap.Any("wm", wm.WM.AllocationRoot), zap.Any("txn", wm.CloseTxnID))
7148
}
7249
}
50+
7351
if allocationObj.LatestRedeemedWM == allocationObj.AllocationRoot {
7452
db.Model(allocationObj).
7553
Where("allocation_root = ? AND allocation_root = latest_redeemed_write_marker", allocationObj.AllocationRoot).
7654
Update("is_redeem_required", false)
7755
}
7856

57+
}
58+
59+
func redeemWriteMarker(allocationObj *allocation.Allocation, wm *WriteMarkerEntity) error {
60+
ctx := datastore.GetStore().CreateTransaction(context.TODO())
61+
db := datastore.GetStore().GetTransaction(ctx)
62+
63+
shouldRollback := false
64+
65+
defer func() {
66+
if shouldRollback {
67+
if err := db.Rollback(); err != nil {
68+
logging.Logger.Error("Error rollback on redeeming the write marker.",
69+
zap.Any("wm", wm.WM.AllocationID), zap.Any("error", err))
70+
}
71+
}
72+
}()
73+
74+
err := wm.RedeemMarker(ctx)
75+
if err != nil {
76+
logging.Logger.Error("Error redeeming the write marker.",
77+
zap.Any("wm", wm.WM.AllocationID), zap.Any("error", err))
78+
79+
shouldRollback = true
80+
81+
return err
82+
}
83+
err = db.Model(allocationObj).Updates(allocation.Allocation{LatestRedeemedWM: wm.WM.AllocationRoot}).Error
84+
if err != nil {
85+
logging.Logger.Error("Error redeeming the write marker. Allocation latest wm redeemed update failed",
86+
zap.Any("wm", wm.WM.AllocationRoot), zap.Any("error", err))
87+
shouldRollback = true
88+
89+
return err
90+
}
91+
allocationObj.LatestRedeemedWM = wm.WM.AllocationRoot
92+
logging.Logger.Info("Success Redeeming the write marker", zap.Any("wm", wm.WM.AllocationRoot), zap.Any("txn", wm.CloseTxnID))
93+
7994
err = db.Commit().Error
8095
if err != nil {
8196
logging.Logger.Error("Error committing the writemarker redeem",
8297
zap.Any("allocation", allocationObj.ID),
8398
zap.Error(err))
99+
shouldRollback = true
100+
return err
84101
}
85102

86-
done = true
103+
return nil
87104
}
88105

89106
func startRedeemWriteMarkers(ctx context.Context) {

0 commit comments

Comments
 (0)