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 deadlock in sharded nosql store #6493

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions common/persistence/nosql/sharded_nosql_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ func (sn *shardedNosqlStoreImpl) GetDefaultShard() nosqlStore {
}

func (sn *shardedNosqlStoreImpl) Close() {
sn.RLock()
defer sn.RUnlock()
for name, shard := range sn.connectedShards {
sn.logger.Warn("Closing store shard", tag.StoreShard(name))
shard.Close()
Expand Down Expand Up @@ -135,8 +137,8 @@ func (sn *shardedNosqlStoreImpl) getShard(shardName string) (*nosqlStore, error)
}

sn.Lock()
defer sn.Unlock()
if shard, ok := sn.connectedShards[shardName]; ok { // read again to double-check
sn.Unlock()
return &shard, nil
}

Expand All @@ -146,7 +148,6 @@ func (sn *shardedNosqlStoreImpl) getShard(shardName string) (*nosqlStore, error)
}
sn.connectedShards[shardName] = *s
sn.logger.Info("Connected to store shard", tag.StoreShard(shardName))
sn.Unlock()
return s, nil
}

Expand Down
48 changes: 31 additions & 17 deletions common/persistence/nosql/sharded_nosql_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package nosql

import (
"errors"
"testing"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -66,12 +67,7 @@ func TestShardedNosqlStoreTestSuite(t *testing.T) {
}

func (s *shardedNosqlStoreTestSuite) TestValidConfiguration() {
cfg := getValidShardedNoSQLConfig()

storeInterface, err := newShardedNosqlStore(cfg, log.NewNoop(), nil)
s.NoError(err)
store := storeInterface.(*shardedNosqlStoreImpl)

store := s.newShardedStoreForTest()
s.Equal(1, len(store.connectedShards))
s.Contains(store.connectedShards, "shard-1")
s.Equal(store.GetDefaultShard(), store.defaultShard)
Expand All @@ -83,25 +79,27 @@ func (s *shardedNosqlStoreTestSuite) TestValidConfiguration() {

func (s *shardedNosqlStoreTestSuite) TestStoreSelectionForHistoryShard() {
mockDB1 := nosqlplugin.NewMockDB(s.mockController)
mockDB1.EXPECT().Close().Times(1)
mockDB2 := nosqlplugin.NewMockDB(s.mockController)
mockDB2.EXPECT().Close().Times(1)

mockPlugin := nosqlplugin.NewMockPlugin(s.mockController)
gomock.InOrder(
mockPlugin.EXPECT().
CreateDB(gomock.Any(), gomock.Any(), gomock.Any()).
Return(mockDB1, nil),
mockPlugin.EXPECT().
CreateDB(gomock.Any(), gomock.Any(), gomock.Any()).
Return(nil, errors.New("error creating db")),
mockPlugin.EXPECT().
CreateDB(gomock.Any(), gomock.Any(), gomock.Any()).
Return(mockDB2, nil),
)
delete(supportedPlugins, "cassandra")
RegisterPlugin("cassandra", mockPlugin)

cfg := getValidShardedNoSQLConfig()

storeInterface, err := newShardedNosqlStore(cfg, log.NewNoop(), nil)
s.NoError(err)
store := storeInterface.(*shardedNosqlStoreImpl)
store := s.newShardedStoreForTest()
defer store.Close()

s.Equal(1, len(store.connectedShards))
s.True(mockDB1 == store.defaultShard.db)
Expand All @@ -118,8 +116,13 @@ func (s *shardedNosqlStoreTestSuite) TestStoreSelectionForHistoryShard() {
s.Equal(1, len(store.connectedShards))
s.True(mockDB1 == storeShard1.db)

// Getting a new shard should create a new connection
// Getting a new shard should create a new connection but it will fail on first attempt
storeShard2, err := store.GetStoreShardByHistoryShard(1)
s.Error(err)
s.Equal(1, len(store.connectedShards))

// Getting a new shard should create a new connection on second attempt
storeShard2, err = store.GetStoreShardByHistoryShard(1)
s.NoError(err)
s.Equal(2, len(store.connectedShards))
s.True(mockDB2 == storeShard2.db)
Expand All @@ -146,9 +149,23 @@ func (s *shardedNosqlStoreTestSuite) TestStoreSelectionForHistoryShard() {
s.True(mockDB2 == storeShard2.db)
}

func (s *shardedNosqlStoreTestSuite) newShardedStoreForTest() *shardedNosqlStoreImpl {
cfg := getValidShardedNoSQLConfig()
logger := log.NewNoop()
storeInterface, err := newShardedNosqlStore(cfg, logger, nil)
s.NoError(err)
s.Equal("shardedNosql", storeInterface.GetName())
s.Equal(logger, storeInterface.GetLogger())
store := storeInterface.(*shardedNosqlStoreImpl)
s.Equal(storeInterface.GetShardingPolicy(), store.shardingPolicy)
return store
}

func (s *shardedNosqlStoreTestSuite) TestStoreSelectionForTasklist() {
mockDB1 := nosqlplugin.NewMockDB(s.mockController)
mockDB1.EXPECT().Close().Times(1)
mockDB2 := nosqlplugin.NewMockDB(s.mockController)
mockDB2.EXPECT().Close().Times(1)

mockPlugin := nosqlplugin.NewMockPlugin(s.mockController)
gomock.InOrder(
Expand All @@ -162,11 +179,8 @@ func (s *shardedNosqlStoreTestSuite) TestStoreSelectionForTasklist() {
delete(supportedPlugins, "cassandra")
RegisterPlugin("cassandra", mockPlugin)

cfg := getValidShardedNoSQLConfig()

storeInterface, err := newShardedNosqlStore(cfg, log.NewNoop(), nil)
s.NoError(err)
store := storeInterface.(*shardedNosqlStoreImpl)
store := s.newShardedStoreForTest()
defer store.Close()

s.Equal(1, len(store.connectedShards))
s.True(mockDB1 == store.defaultShard.db)
Expand Down
Loading