From 15ced2398e3eabd9ffc5f8056e989f2796c14c43 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 16 Sep 2012 19:18:37 -0700 Subject: [PATCH] adjusting maxVolumeCount if volume server changes it --- weed-fs/src/pkg/replication/volume_growth.go | 12 ++-- weed-fs/src/pkg/topology/rack.go | 67 ++++++++++---------- weed-fs/src/pkg/topology/topology.go | 13 ++-- 3 files changed, 50 insertions(+), 42 deletions(-) diff --git a/weed-fs/src/pkg/replication/volume_growth.go b/weed-fs/src/pkg/replication/volume_growth.go index f6dec8a65..97316cb57 100644 --- a/weed-fs/src/pkg/replication/volume_growth.go +++ b/weed-fs/src/pkg/replication/volume_growth.go @@ -63,8 +63,10 @@ func (vg *VolumeGrowth) GrowByCountAndType(count int, repType storage.Replicatio if ret { var servers []*topology.DataNode for _, n := range picked { - if ok, server := n.ReserveOneVolume(rand.Intn(n.FreeSpace()), vid); ok { - servers = append(servers, server) + if n.FreeSpace() > 0 { + if ok, server := n.ReserveOneVolume(rand.Intn(n.FreeSpace()), vid); ok { + servers = append(servers, server) + } } } if len(servers) == 2 { @@ -80,8 +82,10 @@ func (vg *VolumeGrowth) GrowByCountAndType(count int, repType storage.Replicatio if ret { var servers []*topology.DataNode for _, n := range picked { - if ok, server := n.ReserveOneVolume(rand.Intn(n.FreeSpace()), vid); ok { - servers = append(servers, server) + if n.FreeSpace() > 0 { + if ok, server := n.ReserveOneVolume(rand.Intn(n.FreeSpace()), vid); ok { + servers = append(servers, server) + } } } if len(servers) == 3 { diff --git a/weed-fs/src/pkg/topology/rack.go b/weed-fs/src/pkg/topology/rack.go index 8a09d0bfe..f685f1a82 100644 --- a/weed-fs/src/pkg/topology/rack.go +++ b/weed-fs/src/pkg/topology/rack.go @@ -1,7 +1,7 @@ package topology import ( - "strconv" + "strconv" ) type Rack struct { @@ -11,43 +11,44 @@ type Rack struct { func NewRack(id string) *Rack { r := &Rack{} - r.id = NodeId(id) - r.nodeType = "Rack" - r.children = make(map[NodeId]Node) + r.id = NodeId(id) + r.nodeType = "Rack" + r.children = make(map[NodeId]Node) return r } -func (r *Rack) MatchLocationRange(ip string) bool{ - if r.ipRange == nil { - return true - } - return r.ipRange.Match(ip) +func (r *Rack) MatchLocationRange(ip string) bool { + if r.ipRange == nil { + return true + } + return r.ipRange.Match(ip) } -func (r *Rack) GetOrCreateDataNode(ip string, port int, publicUrl string, maxVolumeCount int) *DataNode{ - for _, c := range r.Children() { - dn := c.(*DataNode) - if dn.MatchLocation(ip,port) { - return dn - } - } - dn := NewDataNode("DataNode"+ip+":"+strconv.Itoa(port)) - dn.Ip = ip - dn.Port = port - dn.PublicUrl = publicUrl - dn.maxVolumeCount = maxVolumeCount - r.LinkChildNode(dn) - return dn +func (r *Rack) GetOrCreateDataNode(ip string, port int, publicUrl string, maxVolumeCount int) *DataNode { + for _, c := range r.Children() { + dn := c.(*DataNode) + if dn.MatchLocation(ip, port) { + dn.NodeImpl.UpAdjustMaxVolumeCountDelta(maxVolumeCount - dn.maxVolumeCount) + return dn + } + } + dn := NewDataNode("DataNode" + ip + ":" + strconv.Itoa(port)) + dn.Ip = ip + dn.Port = port + dn.PublicUrl = publicUrl + dn.maxVolumeCount = maxVolumeCount + r.LinkChildNode(dn) + return dn } -func (rack *Rack) ToMap() interface{}{ - m := make(map[string]interface{}) - m["Free"] = rack.FreeSpace() - var dns []interface{} - for _, c := range rack.Children() { - dn := c.(*DataNode) - dns = append(dns, dn.ToMap()) - } - m["DataNodes"] = dns - return m +func (rack *Rack) ToMap() interface{} { + m := make(map[string]interface{}) + m["Free"] = rack.FreeSpace() + var dns []interface{} + for _, c := range rack.Children() { + dn := c.(*DataNode) + dns = append(dns, dn.ToMap()) + } + m["DataNodes"] = dns + return m } diff --git a/weed-fs/src/pkg/topology/topology.go b/weed-fs/src/pkg/topology/topology.go index 7a96665af..3a6bce4c3 100644 --- a/weed-fs/src/pkg/topology/topology.go +++ b/weed-fs/src/pkg/topology/topology.go @@ -34,22 +34,25 @@ func NewTopology(id string, dirname string, filename string, volumeSizeLimit uin } func (t *Topology) RandomlyReserveOneVolume() (bool, *DataNode, *storage.VolumeId) { - if t.FreeSpace()<=0 { - return false, nil, nil - } + if t.FreeSpace() <= 0 { + return false, nil, nil + } vid := t.NextVolumeId() ret, node := t.ReserveOneVolume(rand.Intn(t.FreeSpace()), vid) return ret, node, &vid } -func (t *Topology) RandomlyReserveOneVolumeExcept(except []Node) (bool, *DataNode, storage.VolumeId) { +func (t *Topology) RandomlyReserveOneVolumeExcept(except []Node) (bool, *DataNode, *storage.VolumeId) { freeSpace := t.FreeSpace() for _, node := range except { freeSpace -= node.FreeSpace() } + if freeSpace <= 0 { + return false, nil, nil + } vid := t.NextVolumeId() ret, node := t.ReserveOneVolume(rand.Intn(freeSpace), vid) - return ret, node, vid + return ret, node, &vid } func (t *Topology) NextVolumeId() storage.VolumeId {