diff options
| author | Konstantin Lebedev <9497591+kmlebedev@users.noreply.github.com> | 2022-08-26 22:18:49 +0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-26 10:18:49 -0700 |
| commit | e90ab4ac604a7bdc02a25bc8dde5b6dde52272a3 (patch) | |
| tree | 8ed31b01c041d341306b99741f5caeda870df11f /weed/wdclient/masterclient.go | |
| parent | 4f7a1f67cdecf36ae590d35eca0f8e8510fce715 (diff) | |
| download | seaweedfs-e90ab4ac604a7bdc02a25bc8dde5b6dde52272a3.tar.xz seaweedfs-e90ab4ac604a7bdc02a25bc8dde5b6dde52272a3.zip | |
avoid race conditions for OnPeerUpdate (#3525)
https://github.com/seaweedfs/seaweedfs/issues/3524
Diffstat (limited to 'weed/wdclient/masterclient.go')
| -rw-r--r-- | weed/wdclient/masterclient.go | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/weed/wdclient/masterclient.go b/weed/wdclient/masterclient.go index 2583bda80..6e91c31eb 100644 --- a/weed/wdclient/masterclient.go +++ b/weed/wdclient/masterclient.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "math/rand" + "sync" "time" "github.com/seaweedfs/seaweedfs/weed/stats" @@ -28,7 +29,8 @@ type MasterClient struct { vidMap vidMapCacheSize int - OnPeerUpdate func(update *master_pb.ClusterNodeUpdate, startFrom time.Time) + OnPeerUpdate func(update *master_pb.ClusterNodeUpdate, startFrom time.Time) + OnPeerUpdateAccessLock sync.RWMutex } func NewMasterClient(grpcDialOption grpc.DialOption, filerGroup string, clientType string, clientHost pb.ServerAddress, clientDataCenter string, rack string, masters map[string]pb.ServerAddress) *MasterClient { @@ -44,6 +46,12 @@ func NewMasterClient(grpcDialOption grpc.DialOption, filerGroup string, clientTy } } +func (mc *MasterClient) SetOnPeerUpdateFn(onPeerUpdate func(update *master_pb.ClusterNodeUpdate, startFrom time.Time)) { + mc.OnPeerUpdateAccessLock.Lock() + mc.OnPeerUpdate = onPeerUpdate + mc.OnPeerUpdateAccessLock.Unlock() +} + func (mc *MasterClient) GetLookupFileIdFunction() LookupFileIdFunctionType { return mc.LookupFileIdWithFallback } @@ -219,6 +227,7 @@ func (mc *MasterClient) tryConnectToMaster(master pb.ServerAddress) (nextHintedL if resp.ClusterNodeUpdate != nil { update := resp.ClusterNodeUpdate + mc.OnPeerUpdateAccessLock.RLock() if mc.OnPeerUpdate != nil { if update.FilerGroup == mc.FilerGroup { if update.IsAdd { @@ -230,6 +239,7 @@ func (mc *MasterClient) tryConnectToMaster(master pb.ServerAddress) (nextHintedL mc.OnPeerUpdate(update, time.Now()) } } + mc.OnPeerUpdateAccessLock.RUnlock() } } }) |
