Skip to content

Commit ccd9e2f

Browse files
lpolipeterlimg
andauthored
Update writemarker lock function (#931)
Co-authored-by: peterlimg <54137706+peterlimg@users.noreply.github.com>
1 parent a264988 commit ccd9e2f

File tree

1 file changed

+12
-27
lines changed
  • code/go/0chain.net/blobbercore/writemarker

1 file changed

+12
-27
lines changed

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

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ type Mutex struct {
3232
ML *common.MapLocker
3333
}
3434

35-
// Lock
35+
// Lock will create/update lock in postgres.
36+
// If no lock exists for an allocation then new lock is created.
37+
// If lock exists and is of same connection ID then lock's createdAt is updated
38+
// If lock exists and is of other connection ID then `pending` response is sent.
3639
func (m *Mutex) Lock(ctx context.Context, allocationID, connectionID string, requestTime *time.Time) (*LockResult, error) {
3740
if allocationID == "" {
3841
return nil, errors.Throw(constants.ErrInvalidParameter, "allocationID")
@@ -77,48 +80,30 @@ func (m *Mutex) Lock(ctx context.Context, allocationID, connectionID string, req
7780
Status: LockStatusOK,
7881
CreatedAt: lock.CreatedAt.Unix(),
7982
}, nil
80-
8183
}
8284

8385
//native postgres error
8486
return nil, errors.ThrowLog(err.Error(), common.ErrBadDataStore)
85-
8687
}
8788

88-
timeout := lock.CreatedAt.Add(config.Configuration.WriteMarkerLockTimeout)
89-
90-
// locked, but it is timeout
91-
if now.After(timeout) {
92-
93-
lock.ConnectionID = connectionID
94-
lock.CreatedAt = *requestTime
95-
96-
err = db.Table(TableNameWriteLock).Where("allocation_id=?", allocationID).Save(&lock).Error
97-
if err != nil {
98-
return nil, errors.ThrowLog(err.Error(), common.ErrBadDataStore)
99-
}
100-
89+
if lock.ConnectionID != connectionID {
90+
// pending
10191
return &LockResult{
102-
Status: LockStatusOK,
92+
Status: LockStatusPending,
10393
CreatedAt: lock.CreatedAt.Unix(),
10494
}, nil
105-
10695
}
10796

108-
//try lock by same session, return old lock directly
109-
if lock.ConnectionID == connectionID && lock.CreatedAt.Equal(*requestTime) {
110-
return &LockResult{
111-
Status: LockStatusOK,
112-
CreatedAt: lock.CreatedAt.Unix(),
113-
}, nil
97+
lock.CreatedAt = *requestTime
98+
err = db.Table(TableNameWriteLock).Where("allocation_id=?", allocationID).Save(&lock).Error
99+
if err != nil {
100+
return nil, errors.ThrowLog(err.Error(), common.ErrBadDataStore)
114101
}
115102

116-
// pending
117103
return &LockResult{
118-
Status: LockStatusPending,
104+
Status: LockStatusOK,
119105
CreatedAt: lock.CreatedAt.Unix(),
120106
}, nil
121-
122107
}
123108

124109
func (*Mutex) Unlock(ctx context.Context, allocationID string, connectionID string) error {

0 commit comments

Comments
 (0)