From 6f8c45d61b2a60da0980fc02b52c0e9fa18278ae Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 18 Nov 2020 15:06:47 +0300 Subject: [PATCH] [#176] localstore: Change multiple access sync In previous implementation each operation on local storage locked engine mutex. This was done under the assumption that the weights of the shards change as a result of write operations. With the transition to static weights of shards, it is no longer necessary to lock the global mutex during the execution of operations. However, since the set of engine shards is dynamic, there is still a need to control multiple access to this set. The same mutex is used for synchronization. Signed-off-by: Leonard Lyubich --- pkg/local_object_storage/engine/delete.go | 3 --- pkg/local_object_storage/engine/get.go | 3 --- pkg/local_object_storage/engine/head.go | 3 --- pkg/local_object_storage/engine/put.go | 3 --- pkg/local_object_storage/engine/select.go | 3 --- pkg/local_object_storage/engine/shards.go | 3 +++ 6 files changed, 3 insertions(+), 15 deletions(-) diff --git a/pkg/local_object_storage/engine/delete.go b/pkg/local_object_storage/engine/delete.go index 47207c3bd..6992bfc61 100644 --- a/pkg/local_object_storage/engine/delete.go +++ b/pkg/local_object_storage/engine/delete.go @@ -30,9 +30,6 @@ func (p *DeletePrm) WithAddress(addr *objectSDK.Address) *DeletePrm { // Returns any error encountered that did not allow to completely // mark the object to delete. func (e *StorageEngine) Delete(prm *DeletePrm) (*DeleteRes, error) { - e.mtx.RLock() - defer e.mtx.RUnlock() - shPrm := new(shard.DeletePrm) e.iterateOverSortedShards(prm.addr, func(sh *shard.Shard) (stop bool) { diff --git a/pkg/local_object_storage/engine/get.go b/pkg/local_object_storage/engine/get.go index b3e5d41d6..56aa2cf9f 100644 --- a/pkg/local_object_storage/engine/get.go +++ b/pkg/local_object_storage/engine/get.go @@ -61,9 +61,6 @@ func (r *GetRes) Object() *object.Object { // // Returns ErrObjectNotFound if requested object is missing in local storage. func (e *StorageEngine) Get(prm *GetPrm) (*GetRes, error) { - e.mtx.RLock() - defer e.mtx.RUnlock() - var obj *object.Object shPrm := new(shard.GetPrm). diff --git a/pkg/local_object_storage/engine/head.go b/pkg/local_object_storage/engine/head.go index e366cb50d..153afe491 100644 --- a/pkg/local_object_storage/engine/head.go +++ b/pkg/local_object_storage/engine/head.go @@ -43,9 +43,6 @@ func (r *HeadRes) Header() *object.Object { // // Returns ErrObjectNotFound if requested object is missing in local storage. func (e *StorageEngine) Head(prm *HeadPrm) (*HeadRes, error) { - e.mtx.RLock() - defer e.mtx.RUnlock() - var head *object.Object shPrm := new(shard.GetPrm). diff --git a/pkg/local_object_storage/engine/put.go b/pkg/local_object_storage/engine/put.go index 1de882a59..ff085a958 100644 --- a/pkg/local_object_storage/engine/put.go +++ b/pkg/local_object_storage/engine/put.go @@ -34,9 +34,6 @@ func (p *PutPrm) WithObject(obj *object.Object) *PutPrm { // Returns any error encountered that // did not allow to completely save the object. func (e *StorageEngine) Put(prm *PutPrm) (*PutRes, error) { - e.mtx.Lock() - defer e.mtx.Unlock() - // choose shards through sorting by weight sortedShards := e.sortShardsByWeight(prm.obj.Address()) diff --git a/pkg/local_object_storage/engine/select.go b/pkg/local_object_storage/engine/select.go index 289de84f9..5ca1b84fd 100644 --- a/pkg/local_object_storage/engine/select.go +++ b/pkg/local_object_storage/engine/select.go @@ -35,9 +35,6 @@ func (r *SelectRes) AddressList() []*objectSDK.Address { // // Returns any error encountered that did not allow to completely select the objects. func (e *StorageEngine) Select(prm *SelectPrm) (*SelectRes, error) { - e.mtx.RLock() - defer e.mtx.RUnlock() - addrList := make([]*object.Address, 0) shPrm := new(shard.SelectPrm). diff --git a/pkg/local_object_storage/engine/shards.go b/pkg/local_object_storage/engine/shards.go index 3435b2c0c..ee41e2d69 100644 --- a/pkg/local_object_storage/engine/shards.go +++ b/pkg/local_object_storage/engine/shards.go @@ -49,6 +49,9 @@ func (e *StorageEngine) shardWeight(sh *shard.Shard) float64 { } func (e *StorageEngine) sortShardsByWeight(objAddr fmt.Stringer) []*shard.Shard { + e.mtx.RLock() + defer e.mtx.RUnlock() + shards := make([]*shard.Shard, 0, len(e.shards)) weights := make([]float64, 0, len(e.shards))