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

[MM-28397] Fix data race in SqlSupplier #15419

Merged
merged 3 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 9 additions & 3 deletions store/sqlstore/supplier.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ type SqlSupplier struct {
lockedToMaster bool
context context.Context
license *model.License
licenseMutex sync.Mutex
licenseMutex sync.RWMutex
}

type TraceOnAdapter struct{}
Expand Down Expand Up @@ -330,7 +330,10 @@ func (ss *SqlSupplier) GetMaster() *gorp.DbMap {
}

func (ss *SqlSupplier) GetSearchReplica() *gorp.DbMap {
if ss.license == nil {
ss.licenseMutex.RLock()
license := ss.license
ss.licenseMutex.RUnlock()
if license == nil {
return ss.GetMaster()
}

Expand All @@ -343,7 +346,10 @@ func (ss *SqlSupplier) GetSearchReplica() *gorp.DbMap {
}

func (ss *SqlSupplier) GetReplica() *gorp.DbMap {
if len(ss.settings.DataSourceReplicas) == 0 || ss.lockedToMaster || ss.license == nil {
ss.licenseMutex.RLock()
license := ss.license
ss.licenseMutex.RUnlock()
if len(ss.settings.DataSourceReplicas) == 0 || ss.lockedToMaster || license == nil {
return ss.GetMaster()
}

Expand Down
27 changes: 27 additions & 0 deletions store/sqlstore/supplier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,33 @@ import (
"github.com/mattermost/mattermost-server/v5/store/storetest"
)

func TestSupplierLicenseRace(t *testing.T) {
isacikgoz marked this conversation as resolved.
Show resolved Hide resolved
settings := makeSqlSettings(model.DATABASE_DRIVER_SQLITE)
settings.DataSourceReplicas = []string{":memory:"}
settings.DataSourceSearchReplicas = []string{":memory:"}
supplier := sqlstore.NewSqlSupplier(*settings, nil)

wg := sync.WaitGroup{}
wg.Add(3)

go func() {
supplier.UpdateLicense(&model.License{})
wg.Done()
}()

go func() {
supplier.GetReplica()
wg.Done()
}()

go func() {
supplier.GetSearchReplica()
wg.Done()
}()

wg.Wait()
}

func TestGetReplica(t *testing.T) {
t.Parallel()
testCases := []struct {
Expand Down