From 54723c3713d8ba3843b43cbe6fad40e36c7bdfb7 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Tue, 29 Oct 2013 12:48:31 -0700 Subject: [PATCH] Issue 51: Assign on empty cluster sometime fails under high concurrency load Contributed by philoops --- go/weed/master.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/go/weed/master.go b/go/weed/master.go index 7aa48e59c..c1ada76fb 100644 --- a/go/weed/master.go +++ b/go/weed/master.go @@ -14,6 +14,7 @@ import ( "runtime" "strconv" "strings" + "sync" "time" ) @@ -48,6 +49,7 @@ var ( var topo *topology.Topology var vg *replication.VolumeGrowth +var vgLock sync.Mutex func dirLookupHandler(w http.ResponseWriter, r *http.Request) { vid := r.FormValue("volumeId") @@ -97,9 +99,13 @@ func dirAssignHandler(w http.ResponseWriter, r *http.Request) { writeJsonQuiet(w, r, map[string]string{"error": "No free volumes left!"}) return } else { - if _, err = vg.AutomaticGrowByType(rt, dataCenter, topo); err != nil { - writeJsonQuiet(w, r, map[string]string{"error": "Cannot grow volume group! " + err.Error()}) - return + vgLock.Lock() + defer vgLock.Unlock() + if topo.GetVolumeLayout(rt).GetActiveVolumeCount(dataCenter) <= 0 { + if _, err = vg.AutomaticGrowByType(rt, dataCenter, topo); err != nil { + writeJsonQuiet(w, r, map[string]string{"error": "Cannot grow volume group! " + err.Error()}) + return + } } } }