From 0320228b99fed8cfa5d8f97d3349955b14e65df3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Podg=C3=B3rski?= Date: Wed, 3 Jun 2020 14:49:03 +0200 Subject: [PATCH] Change order of cluster/index locks --- cluster.go | 5 +++++ holder.go | 6 ++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/cluster.go b/cluster.go index 2c50bceb9..b3fc86e88 100644 --- a/cluster.go +++ b/cluster.go @@ -1044,6 +1044,11 @@ func (c *cluster) partitionNodes(partitionID int) []*Node { func (c *cluster) ownsPartition(nodeID string, partition int) bool { c.mu.RLock() defer c.mu.RUnlock() + return c.unprotectedOwnsPartition(nodeID, partition) +} + +// unprotectedOwnsPartition returns true if a host owns a partition. +func (c *cluster) unprotectedOwnsPartition(nodeID string, partition int) bool { return Nodes(c.partitionNodes(partition)).ContainsID(nodeID) } diff --git a/holder.go b/holder.go index 70501a4b1..de3cd0701 100644 --- a/holder.go +++ b/holder.go @@ -1120,7 +1120,8 @@ func (s *holderSyncer) stopTranslationSync() error { // writing new translation keys. Index stores are writable if the node owns the // partition. Field stores are writable if the node is the coordinator. func (s *holderSyncer) setTranslateReadOnlyFlags() { - isCoordinator := s.Cluster.isCoordinator() + s.Cluster.mu.RLock() + isCoordinator := s.Cluster.unprotectedIsCoordinator() for _, index := range s.Holder.Indexes() { // There is a race condition here: @@ -1140,7 +1141,7 @@ func (s *holderSyncer) setTranslateReadOnlyFlags() { // done using it. index.mu.RLock() for partitionID := 0; partitionID < s.Cluster.partitionN; partitionID++ { - ownsPartition := s.Cluster.ownsPartition(s.Node.ID, partitionID) + ownsPartition := s.Cluster.unprotectedOwnsPartition(s.Node.ID, partitionID) if ts := index.TranslateStore(partitionID); ts != nil { ts.SetReadOnly(!ownsPartition) } @@ -1151,6 +1152,7 @@ func (s *holderSyncer) setTranslateReadOnlyFlags() { field.TranslateStore().SetReadOnly(!isCoordinator) } } + s.Cluster.mu.RUnlock() } // initializeIndexTranslateReplication connects to each node that is the