From 97e4a1d6ebcfea626f0d9c2e9805c29a62df4b29 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Tue, 28 Aug 2012 01:04:39 -0700 Subject: [PATCH] add code to randomly select one new volume to grow --- weed-fs/bin/.gitignore | 1 - weed-fs/pkg/linux_amd64/.gitignore | 1 - weed-fs/src/pkg/topology/capacity.go | 8 ++++ weed-fs/src/pkg/topology/data_center.go | 46 +++++++++++++++++++- weed-fs/src/pkg/topology/node.go | 26 +++++------ weed-fs/src/pkg/topology/rack.go | 35 ++++++++++++++- weed-fs/src/pkg/topology/topology.go | 57 ++++++++++++++++++++++--- 7 files changed, 149 insertions(+), 25 deletions(-) delete mode 100644 weed-fs/bin/.gitignore delete mode 100644 weed-fs/pkg/linux_amd64/.gitignore create mode 100644 weed-fs/src/pkg/topology/capacity.go diff --git a/weed-fs/bin/.gitignore b/weed-fs/bin/.gitignore deleted file mode 100644 index aa3146581..000000000 --- a/weed-fs/bin/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/weed diff --git a/weed-fs/pkg/linux_amd64/.gitignore b/weed-fs/pkg/linux_amd64/.gitignore deleted file mode 100644 index fc5bd9083..000000000 --- a/weed-fs/pkg/linux_amd64/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/pkg diff --git a/weed-fs/src/pkg/topology/capacity.go b/weed-fs/src/pkg/topology/capacity.go new file mode 100644 index 000000000..57ba02cd3 --- /dev/null +++ b/weed-fs/src/pkg/topology/capacity.go @@ -0,0 +1,8 @@ +package topology + +import () + +type StorageCapacity struct { + countVolumeCount int + maxVolumeCount int +} diff --git a/weed-fs/src/pkg/topology/data_center.go b/weed-fs/src/pkg/topology/data_center.go index 5161234ec..cffcdec4a 100644 --- a/weed-fs/src/pkg/topology/data_center.go +++ b/weed-fs/src/pkg/topology/data_center.go @@ -1,10 +1,54 @@ package topology -import () +import ( + "pkg/storage" +) type DataCenterId string type DataCenter struct { Id DataCenterId racks map[RackId]*Rack ipRange IpRange + + //transient + allocation StorageCapacity + topology *Topology +} + +func (d *DataCenter) CreateOneVolume(r int, vid storage.VolumeId) storage.VolumeId { + for _, rack := range d.racks { + freeSpace := rack.allocation.maxVolumeCount - rack.allocation.countVolumeCount + if r > freeSpace { + r -= freeSpace + } else { + rack.CreateOneVolume(r, vid) + } + } + return vid +} +func (d *DataCenter) AddVolume(rack *Rack, v *storage.VolumeInfo) { + d.allocation.countVolumeCount += 1 + d.topology.AddVolume(d, v) +} +func (d *DataCenter) AddNode(rack *Rack, n *Node) { + d.allocation.countVolumeCount += len(n.volumes) + d.allocation.maxVolumeCount += n.maxVolumeCount + d.topology.AddNode(d, n) +} +func (d *DataCenter) RemoveNode(rack *Rack, n *Node) { + d.allocation.countVolumeCount -= len(n.volumes) + d.allocation.maxVolumeCount -= n.maxVolumeCount + d.topology.RemoveNode(d, n) +} +func (d *DataCenter) AddRack(rack *Rack) { + d.racks[rack.Id] = rack + d.allocation.countVolumeCount += rack.allocation.countVolumeCount + d.allocation.maxVolumeCount += rack.allocation.maxVolumeCount + d.topology.AddRack(d, rack) +} +func (d *DataCenter) RemoveRack(rack *Rack) { + delete(d.racks, rack.Id) + d.allocation.countVolumeCount -= rack.allocation.countVolumeCount + d.allocation.maxVolumeCount -= rack.allocation.maxVolumeCount + d.topology.AddRack(d, rack) } diff --git a/weed-fs/src/pkg/topology/node.go b/weed-fs/src/pkg/topology/node.go index 9c1a87f89..9b4ab27b5 100644 --- a/weed-fs/src/pkg/topology/node.go +++ b/weed-fs/src/pkg/topology/node.go @@ -4,26 +4,22 @@ import ( "pkg/storage" ) -type NodeId uint32 +type NodeId string type Node struct { - volumes map[storage.VolumeId]storage.VolumeInfo - volumeLimit int - Ip string + volumes map[storage.VolumeId]*storage.VolumeInfo + maxVolumeCount int + Ip NodeId Port int PublicUrl string //transient - allocation *Allocation + rack *Rack } -type Allocation struct { - count int - limit int +func (n *Node) CreateOneVolume(r int, vid storage.VolumeId) storage.VolumeId { + n.AddVolume(&storage.VolumeInfo{Id:vid, Size: 32*1024*1024*1024}) + return vid } - -func (n *Node) GetAllocation() *Allocation{ - if n.allocation == nil { - n.allocation = &Allocation{count:len(n.volumes), limit : n.volumeLimit} - } - return n.allocation +func (n *Node) AddVolume(v *storage.VolumeInfo){ + n.volumes[v.Id] = v + n.rack.AddVolume(n,v) } - diff --git a/weed-fs/src/pkg/topology/rack.go b/weed-fs/src/pkg/topology/rack.go index 3e06ea3af..0ed0600c5 100644 --- a/weed-fs/src/pkg/topology/rack.go +++ b/weed-fs/src/pkg/topology/rack.go @@ -1,10 +1,43 @@ package topology -import () +import ( + "pkg/storage" +) type RackId uint32 type Rack struct { Id RackId nodes map[NodeId]*Node ipRange IpRange + + //transient + allocation StorageCapacity + dataCenter *DataCenter +} +func (rack *Rack) CreateOneVolume(r int, vid storage.VolumeId) storage.VolumeId { + for _, node := range rack.nodes { + freeSpace := node.maxVolumeCount - len(node.volumes) + if r > freeSpace { + r -= freeSpace + } else { + node.CreateOneVolume(r, vid) + } + } + return vid +} +func (r *Rack) AddVolume(n *Node, v *storage.VolumeInfo){ + r.allocation.countVolumeCount += 1 + r.dataCenter.AddVolume(r,v) +} +func (r *Rack) AddNode(n *Node){ + r.nodes[n.Ip] = n + r.allocation.countVolumeCount += len(n.volumes) + r.allocation.maxVolumeCount += n.maxVolumeCount + r.dataCenter.AddNode(r,n) +} +func (r *Rack) RemoveNode(n *Node){ + delete(r.nodes,n.Ip) + r.allocation.countVolumeCount -= len(n.volumes) + r.allocation.maxVolumeCount -= n.maxVolumeCount + r.dataCenter.RemoveNode(r,n) } diff --git a/weed-fs/src/pkg/topology/topology.go b/weed-fs/src/pkg/topology/topology.go index 7d903b961..ffeff784c 100644 --- a/weed-fs/src/pkg/topology/topology.go +++ b/weed-fs/src/pkg/topology/topology.go @@ -1,16 +1,61 @@ package topology import ( - "pkg/storage" + "math/rand" + "pkg/storage" ) type Topology struct { - datacenters map[DataCenterId]*DataCenter + datacenters map[DataCenterId]*DataCenter + + //transient + allocation StorageCapacity } + //FIXME -func (t *Topology) RandomlyCreateOneVolume() storage.VolumeId{ - return t.findMaxVolumeId() +func (t *Topology) RandomlyCreateOneVolume() storage.VolumeId { + r := rand.Intn(t.allocation.maxVolumeCount-t.allocation.countVolumeCount) + vid := t.nextVolumeId() + for _, d := range t.datacenters { + freeSpace := d.allocation.maxVolumeCount-d.allocation.countVolumeCount + if r>freeSpace{ + r -= freeSpace + }else{ + d.CreateOneVolume(r, vid) + return vid + } + } + return storage.VolumeId(0) //FIXME +} +func (t *Topology) nextVolumeId() storage.VolumeId { + return storage.VolumeId(0) +} +func (t *Topology) AddVolume(d *DataCenter, v *storage.VolumeInfo) { + t.allocation.countVolumeCount += 1 +} +func (t *Topology) AddNode(d *DataCenter, n *Node){ + t.allocation.countVolumeCount += len(n.volumes) + t.allocation.maxVolumeCount += n.maxVolumeCount +} +func (t *Topology) RemoveNode(d *DataCenter, n *Node){ + t.allocation.countVolumeCount -= len(n.volumes) + t.allocation.maxVolumeCount -= n.maxVolumeCount +} +func (t *Topology) AddRack(d *DataCenter, rack *Rack){ + t.allocation.countVolumeCount += rack.allocation.countVolumeCount + t.allocation.maxVolumeCount += rack.allocation.maxVolumeCount +} +func (t *Topology) RemoveRack(d *DataCenter, rack *Rack){ + t.allocation.countVolumeCount -= rack.allocation.countVolumeCount + t.allocation.maxVolumeCount -= rack.allocation.maxVolumeCount +} +func (t *Topology) AddDataCenter(d *DataCenter) { + t.datacenters[d.Id] = d + t.allocation.countVolumeCount += d.allocation.countVolumeCount + t.allocation.maxVolumeCount += d.allocation.maxVolumeCount } -func (t *Topology) findMaxVolumeId() storage.VolumeId{ - return storage.VolumeId(0); +func (t *Topology) RemoveDataCenter(d *DataCenter) { + delete(t.datacenters,d.Id) + t.allocation.countVolumeCount -= d.allocation.countVolumeCount + t.allocation.maxVolumeCount -= d.allocation.maxVolumeCount }