mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
* MM-30882: Fix read-after-write issue for demoting user In (*App).DemoteUserToGuest, we would demote a user, and then immediately read it back to do future operations from the user. This reading back of the user had the effect of sticking the old value into the cache after which it would never be updated. There was another issue along with this, which was when the invalidation message would broadcast across the cluster, it would hit the cache invalidation problem where an unrelated store call would miss the cache because it was invalidated, and then again read from replica and stick the old value. To fix all these, we return the new value directly from the store method to avoid having the app to read it again. And we add a map in the localcache layer which tracks invalidations made, and then switch to use master if it's true. The core change is fairly limited, but due to changing the store method signatures, a lot of code needed to be updated to pass "context.Background". Therefore the PR just "appears" to be big, but the main changes are limited to app/user.go, sqlstore/user_store.go and user_layer.go https://mattermost.atlassian.net/browse/MM-30882 ```release-note Fix an issue where demoting a user to guest would not take effect in an environment with read replicas. ``` * Fix concurrent map access * Fixing mistakes * fix tests
35 lines
924 B
Go
35 lines
924 B
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package sqlstore
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
// storeContextKey is the base type for all context keys for the store.
|
|
type storeContextKey string
|
|
|
|
// contextValue is a type to hold some pre-determined context values.
|
|
type contextValue string
|
|
|
|
// Different possible values of contextValue.
|
|
const (
|
|
useMaster contextValue = "useMaster"
|
|
)
|
|
|
|
// WithMaster adds the context value that master DB should be selected for this request.
|
|
func WithMaster(ctx context.Context) context.Context {
|
|
return context.WithValue(ctx, storeContextKey(useMaster), true)
|
|
}
|
|
|
|
// hasMaster is a helper function to check whether master DB should be selected or not.
|
|
func hasMaster(ctx context.Context) bool {
|
|
if v := ctx.Value(storeContextKey(useMaster)); v != nil {
|
|
if res, ok := v.(bool); ok && res {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|