From 6ba93bee705698e599b6d494f679839514faf58b Mon Sep 17 00:00:00 2001 From: Lz Date: Sat, 26 Feb 2022 16:20:44 +0800 Subject: [PATCH 1/2] feat(writemarker): updated WriteMarker mutex --- .../blobbercore/datastore/postgres_schema.go | 2 +- .../blobbercore/writemarker/mutex.go | 36 +++++-------------- .../blobbercore/writemarker/mutext_test.go | 28 +++++++-------- 3 files changed, 23 insertions(+), 43 deletions(-) diff --git a/code/go/0chain.net/blobbercore/datastore/postgres_schema.go b/code/go/0chain.net/blobbercore/datastore/postgres_schema.go index 0ad6c3287..d0c1bc2d4 100644 --- a/code/go/0chain.net/blobbercore/datastore/postgres_schema.go +++ b/code/go/0chain.net/blobbercore/datastore/postgres_schema.go @@ -9,7 +9,7 @@ const ( // WriteLock WriteMarker lock type WriteLock struct { AllocationID string `gorm:"primaryKey, column:allocation_id"` - SessionID string `gorm:"column:session_id"` + ConnectionID string `gorm:"column:connection_id"` CreatedAt time.Time `gorm:"column:created_at"` } diff --git a/code/go/0chain.net/blobbercore/writemarker/mutex.go b/code/go/0chain.net/blobbercore/writemarker/mutex.go index 120163930..c5b42a7c9 100644 --- a/code/go/0chain.net/blobbercore/writemarker/mutex.go +++ b/code/go/0chain.net/blobbercore/writemarker/mutex.go @@ -7,7 +7,6 @@ import ( "github.com/0chain/blobber/code/go/0chain.net/blobbercore/config" "github.com/0chain/blobber/code/go/0chain.net/blobbercore/datastore" - "github.com/0chain/blobber/code/go/0chain.net/blobbercore/reference" "github.com/0chain/blobber/code/go/0chain.net/core/common" "github.com/0chain/errors" "github.com/0chain/gosdk/constants" @@ -24,9 +23,8 @@ const ( ) type LockResult struct { - Status LockStatus `json:"status,omitempty"` - CreatedAt time.Time `json:"created_at,omitempty"` - RootNode *reference.HashNode `json:"root_node,omitempty"` + Status LockStatus `json:"status,omitempty"` + CreatedAt time.Time `json:"created_at,omitempty"` } // Mutex WriteMarker mutex @@ -35,7 +33,7 @@ type Mutex struct { } // Lock -func (m *Mutex) Lock(ctx context.Context, allocationID, sessionID string, requestTime *time.Time) (*LockResult, error) { +func (m *Mutex) Lock(ctx context.Context, allocationID, connectionID string, requestTime *time.Time) (*LockResult, error) { m.Mutex.Lock() defer m.Mutex.Unlock() @@ -43,8 +41,8 @@ func (m *Mutex) Lock(ctx context.Context, allocationID, sessionID string, reques return nil, errors.Throw(constants.ErrInvalidParameter, "allocationID") } - if len(sessionID) == 0 { - return nil, errors.Throw(constants.ErrInvalidParameter, "sessionID") + if len(connectionID) == 0 { + return nil, errors.Throw(constants.ErrInvalidParameter, "connectionID") } if requestTime == nil { @@ -65,7 +63,7 @@ func (m *Mutex) Lock(ctx context.Context, allocationID, sessionID string, reques if errors.Is(err, gorm.ErrRecordNotFound) { lock = datastore.WriteLock{ AllocationID: allocationID, - SessionID: sessionID, + ConnectionID: connectionID, CreatedAt: *requestTime, } @@ -74,15 +72,9 @@ func (m *Mutex) Lock(ctx context.Context, allocationID, sessionID string, reques return nil, errors.ThrowLog(err.Error(), common.ErrBadDataStore) } - rootNode, err := reference.LoadRootNode(ctx, allocationID) - if err != nil { - return nil, errors.ThrowLog(err.Error(), common.ErrBadDataStore) - } - return &LockResult{ Status: LockStatusOK, CreatedAt: lock.CreatedAt, - RootNode: rootNode, }, nil } @@ -97,7 +89,7 @@ func (m *Mutex) Lock(ctx context.Context, allocationID, sessionID string, reques // locked, but it is timeout if now.After(timeout) { - lock.SessionID = sessionID + lock.ConnectionID = connectionID lock.CreatedAt = *requestTime err = db.Save(&lock).Error @@ -105,30 +97,18 @@ func (m *Mutex) Lock(ctx context.Context, allocationID, sessionID string, reques return nil, errors.ThrowLog(err.Error(), common.ErrBadDataStore) } - rootNode, err := reference.LoadRootNode(ctx, allocationID) - if err != nil { - return nil, errors.ThrowLog(err.Error(), common.ErrBadDataStore) - } - return &LockResult{ Status: LockStatusOK, CreatedAt: lock.CreatedAt, - RootNode: rootNode, }, nil } //try lock by same session, return old lock directly - if lock.SessionID == sessionID && lock.CreatedAt.Equal(*requestTime) { - rootNode, err := reference.LoadRootNode(ctx, allocationID) - if err != nil { - return nil, errors.ThrowLog(err.Error(), common.ErrBadDataStore) - } - + if lock.ConnectionID == connectionID && lock.CreatedAt.Equal(*requestTime) { return &LockResult{ Status: LockStatusOK, CreatedAt: lock.CreatedAt, - RootNode: rootNode, }, nil } diff --git a/code/go/0chain.net/blobbercore/writemarker/mutext_test.go b/code/go/0chain.net/blobbercore/writemarker/mutext_test.go index 0c09c4f47..e36f0fe15 100644 --- a/code/go/0chain.net/blobbercore/writemarker/mutext_test.go +++ b/code/go/0chain.net/blobbercore/writemarker/mutext_test.go @@ -23,7 +23,7 @@ func TestMutext_LockShouldWork(t *testing.T) { tests := []struct { name string allocationID string - sessionID string + connectionID string requestTime time.Time mock func() assert func(*testing.T, *LockResult, error) @@ -31,7 +31,7 @@ func TestMutext_LockShouldWork(t *testing.T) { { name: "Lock should work", allocationID: "lock_allocation_id", - sessionID: "lock_session_id", + connectionID: "lock_connection_id", requestTime: now, mock: func() { @@ -44,7 +44,7 @@ func TestMutext_LockShouldWork(t *testing.T) { { name: "retry lock by same request should work if it is not timeout", allocationID: "lock_same_allocation_id", - sessionID: "lock_same_session_id", + connectionID: "lock_same_connection_id", requestTime: now, mock: func() { gomocket.Catcher.NewMock(). @@ -53,7 +53,7 @@ func TestMutext_LockShouldWork(t *testing.T) { WithReply([]map[string]interface{}{ { "allocation_id": "lock_same_allocation_id", - "session_id": "lock_same_session_id", + "connection_id": "lock_same_connection_id", "created_at": now, }, }) @@ -66,7 +66,7 @@ func TestMutext_LockShouldWork(t *testing.T) { { name: "lock should be pending if it already is locked by other session ", allocationID: "lock_allocation_id", - sessionID: "lock_pending_session_id", + connectionID: "lock_pending_connection_id", requestTime: time.Now(), mock: func() { gomocket.Catcher.NewMock(). @@ -75,7 +75,7 @@ func TestMutext_LockShouldWork(t *testing.T) { WithReply([]map[string]interface{}{ { "allocation_id": "lock_allocation_id", - "session_id": "lock_session_id", + "connection_id": "lock_connection_id", "created_at": time.Now().Add(-5 * time.Second), }, }) @@ -88,7 +88,7 @@ func TestMutext_LockShouldWork(t *testing.T) { { name: "lock should ok if it is timeout", allocationID: "lock_timeout_allocation_id", - sessionID: "lock_timeout_2nd_session_id", + connectionID: "lock_timeout_2nd_connection_id", requestTime: now, mock: func() { gomocket.Catcher.NewMock(). @@ -97,7 +97,7 @@ func TestMutext_LockShouldWork(t *testing.T) { WithReply([]map[string]interface{}{ { "allocation_id": "lock_timeout_allocation_id", - "session_id": "lock_timeout_1st_session_id", + "connection_id": "lock_timeout_1st_connection_id", "created_at": time.Now().Add(31 * time.Second), }, }) @@ -116,7 +116,7 @@ func TestMutext_LockShouldWork(t *testing.T) { if it.mock != nil { it.mock() } - r, err := m.Lock(context.TODO(), it.allocationID, it.sessionID, &it.requestTime) + r, err := m.Lock(context.TODO(), it.allocationID, it.connectionID, &it.requestTime) it.assert(test, r, err) @@ -139,7 +139,7 @@ func TestMutext_LockShouldNotWork(t *testing.T) { tests := []struct { name string allocationID string - sessionID string + connectionID string requestTime time.Time mock func() assert func(*testing.T, *LockResult, error) @@ -147,7 +147,7 @@ func TestMutext_LockShouldNotWork(t *testing.T) { { name: "Lock should not work if request_time is timeout", allocationID: "lock_allocation_id", - sessionID: "lock_session_id", + connectionID: "lock_connection_id", requestTime: time.Now().Add(31 * time.Second), mock: func() { config.Configuration.WriteMarkerLockTimeout = 30 * time.Second @@ -160,7 +160,7 @@ func TestMutext_LockShouldNotWork(t *testing.T) { { name: "retry lock by same request should not work if it is timeout", allocationID: "lock_same_timeout_allocation_id", - sessionID: "lock_same_timeout_session_id", + connectionID: "lock_same_timeout_connection_id", requestTime: now, mock: func() { gomocket.Catcher.NewMock(). @@ -169,7 +169,7 @@ func TestMutext_LockShouldNotWork(t *testing.T) { WithReply([]map[string]interface{}{ { "allocation_id": "lock_same_timeout_allocation_id", - "session_id": "lock_same_timeout_session_id", + "connection_id": "lock_same_timeout_connection_id", "created_at": now.Add(-config.Configuration.WriteMarkerLockTimeout), }, }) @@ -188,7 +188,7 @@ func TestMutext_LockShouldNotWork(t *testing.T) { if it.mock != nil { it.mock() } - r, err := m.Lock(context.TODO(), it.allocationID, it.sessionID, &it.requestTime) + r, err := m.Lock(context.TODO(), it.allocationID, it.connectionID, &it.requestTime) it.assert(test, r, err) From aec480f918896483b5fba187e7fe3219ed74eec5 Mon Sep 17 00:00:00 2001 From: Lz Date: Sat, 26 Feb 2022 17:05:52 +0800 Subject: [PATCH 2/2] feat(writemarker): updated CreateAt with int64 --- code/go/0chain.net/blobbercore/writemarker/mutex.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/code/go/0chain.net/blobbercore/writemarker/mutex.go b/code/go/0chain.net/blobbercore/writemarker/mutex.go index c5b42a7c9..daeaddab0 100644 --- a/code/go/0chain.net/blobbercore/writemarker/mutex.go +++ b/code/go/0chain.net/blobbercore/writemarker/mutex.go @@ -24,7 +24,7 @@ const ( type LockResult struct { Status LockStatus `json:"status,omitempty"` - CreatedAt time.Time `json:"created_at,omitempty"` + CreatedAt int64 `json:"created_at,omitempty"` } // Mutex WriteMarker mutex @@ -74,7 +74,7 @@ func (m *Mutex) Lock(ctx context.Context, allocationID, connectionID string, req return &LockResult{ Status: LockStatusOK, - CreatedAt: lock.CreatedAt, + CreatedAt: lock.CreatedAt.Unix(), }, nil } @@ -99,7 +99,7 @@ func (m *Mutex) Lock(ctx context.Context, allocationID, connectionID string, req return &LockResult{ Status: LockStatusOK, - CreatedAt: lock.CreatedAt, + CreatedAt: lock.CreatedAt.Unix(), }, nil } @@ -108,14 +108,14 @@ func (m *Mutex) Lock(ctx context.Context, allocationID, connectionID string, req if lock.ConnectionID == connectionID && lock.CreatedAt.Equal(*requestTime) { return &LockResult{ Status: LockStatusOK, - CreatedAt: lock.CreatedAt, + CreatedAt: lock.CreatedAt.Unix(), }, nil } // pending return &LockResult{ Status: LockStatusPending, - CreatedAt: lock.CreatedAt, + CreatedAt: lock.CreatedAt.Unix(), }, nil }