From 3c245c69d369d468c6e1b4243c9bc0cd12fe12d8 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 3 Nov 2021 01:36:39 -0700 Subject: [PATCH] handle possible race condition --- weed/server/master_server_cluster.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/weed/server/master_server_cluster.go b/weed/server/master_server_cluster.go index 19ecaff0a..fa5280ccd 100644 --- a/weed/server/master_server_cluster.go +++ b/weed/server/master_server_cluster.go @@ -14,6 +14,7 @@ const ( type ClusterNode struct { address pb.ServerAddress version string + counter int } type Cluster struct { @@ -32,12 +33,14 @@ func (cluster *Cluster) AddClusterNode(nodeType string, address pb.ServerAddress case "filer": cluster.filersLock.Lock() defer cluster.filersLock.Unlock() - if _, found := cluster.filers[address]; found { + if existingNode, found := cluster.filers[address]; found { + existingNode.counter++ return } cluster.filers[address] = &ClusterNode{ address: address, version: version, + counter: 1, } case "master": } @@ -48,10 +51,14 @@ func (cluster *Cluster) RemoveClusterNode(nodeType string, address pb.ServerAddr case "filer": cluster.filersLock.Lock() defer cluster.filersLock.Unlock() - if _, found := cluster.filers[address]; !found { + if existingNode, found := cluster.filers[address]; !found { return + } else { + existingNode.counter-- + if existingNode.counter <= 0 { + delete(cluster.filers, address) + } } - delete(cluster.filers, address) case "master": } }