2012-08-27 20:52:02 +00:00
|
|
|
package replication
|
|
|
|
|
|
|
|
import (
|
2013-09-02 06:58:21 +00:00
|
|
|
"code.google.com/p/weed-fs/go/glog"
|
2013-02-10 11:49:51 +00:00
|
|
|
"code.google.com/p/weed-fs/go/storage"
|
|
|
|
"code.google.com/p/weed-fs/go/topology"
|
2013-02-27 06:54:22 +00:00
|
|
|
"errors"
|
|
|
|
"math/rand"
|
2013-01-17 08:56:56 +00:00
|
|
|
"sync"
|
2012-08-27 20:52:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
This package is created to resolve these replica placement issues:
|
|
|
|
1. growth factor for each replica level, e.g., add 10 volumes for 1 copy, 20 volumes for 2 copies, 30 volumes for 3 copies
|
|
|
|
2. in time of tight storage, how to reduce replica level
|
|
|
|
3. optimizing for hot data on faster disk, cold data on cheaper storage,
|
|
|
|
4. volume allocation for each bucket
|
|
|
|
*/
|
|
|
|
|
|
|
|
type VolumeGrowth struct {
|
2013-01-17 08:56:56 +00:00
|
|
|
accessLock sync.Mutex
|
2012-08-27 20:52:02 +00:00
|
|
|
}
|
|
|
|
|
2012-09-17 00:31:15 +00:00
|
|
|
func NewDefaultVolumeGrowth() *VolumeGrowth {
|
2014-03-03 06:16:54 +00:00
|
|
|
return &VolumeGrowth{}
|
2012-09-17 00:31:15 +00:00
|
|
|
}
|
|
|
|
|
2014-03-03 06:16:54 +00:00
|
|
|
// one replication type may need rp.GetCopyCount() actual volumes
|
|
|
|
// given copyCount, how many logical volumes to create
|
|
|
|
func (vg *VolumeGrowth) findVolumeCount(copyCount int) (count int) {
|
|
|
|
switch copyCount {
|
|
|
|
case 1:
|
|
|
|
count = 7
|
|
|
|
case 2:
|
|
|
|
count = 6
|
|
|
|
case 3:
|
|
|
|
count = 3
|
2013-07-24 17:31:51 +00:00
|
|
|
default:
|
2014-03-03 06:16:54 +00:00
|
|
|
count = 1
|
2012-09-17 00:31:15 +00:00
|
|
|
}
|
2014-03-03 06:16:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vg *VolumeGrowth) AutomaticGrowByType(collection string, rp *storage.ReplicaPlacement, preferredDataCenter string, topo *topology.Topology) (count int, err error) {
|
|
|
|
count, err = vg.GrowByCountAndType(vg.findVolumeCount(rp.GetCopyCount()), collection, rp, preferredDataCenter, topo)
|
|
|
|
if count > 0 && count%rp.GetCopyCount() == 0 {
|
2013-07-24 17:31:51 +00:00
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
return count, err
|
2012-09-17 00:31:15 +00:00
|
|
|
}
|
2014-03-03 06:16:54 +00:00
|
|
|
func (vg *VolumeGrowth) GrowByCountAndType(targetCount int, collection string, rp *storage.ReplicaPlacement, preferredDataCenter string, topo *topology.Topology) (counter int, err error) {
|
2013-01-17 08:56:56 +00:00
|
|
|
vg.accessLock.Lock()
|
|
|
|
defer vg.accessLock.Unlock()
|
2012-11-13 20:13:40 +00:00
|
|
|
|
2014-03-03 06:16:54 +00:00
|
|
|
for i := 0; i < targetCount; i++ {
|
|
|
|
if c, e := vg.findAndGrow(topo, preferredDataCenter, collection, rp); e == nil {
|
|
|
|
counter += c
|
|
|
|
} else {
|
|
|
|
return counter, e
|
2012-09-30 09:20:33 +00:00
|
|
|
}
|
2014-03-03 06:16:54 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vg *VolumeGrowth) findAndGrow(topo *topology.Topology, preferredDataCenter string, collection string, rp *storage.ReplicaPlacement) (int, error) {
|
|
|
|
servers, e := vg.findEmptySlotsForOneVolume(topo, preferredDataCenter, rp)
|
|
|
|
if e != nil {
|
|
|
|
return 0, e
|
|
|
|
}
|
|
|
|
vid := topo.NextVolumeId()
|
|
|
|
err := vg.grow(topo, vid, collection, rp, servers...)
|
|
|
|
return len(servers), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vg *VolumeGrowth) findEmptySlotsForOneVolume(topo *topology.Topology, preferredDataCenter string, rp *storage.ReplicaPlacement) (servers []*topology.DataNode, err error) {
|
|
|
|
//find main datacenter and other data centers
|
|
|
|
mainDataCenter, otherDataCenters, dc_err := topo.RandomlyPickNodes(rp.DiffDataCenterCount+1, func(node topology.Node) bool {
|
|
|
|
if preferredDataCenter != "" && node.IsDataCenter() && node.Id() != topology.NodeId(preferredDataCenter) {
|
|
|
|
return false
|
2012-09-03 08:50:04 +00:00
|
|
|
}
|
2014-03-03 06:16:54 +00:00
|
|
|
return node.FreeSpace() > rp.DiffRackCount+rp.SameRackCount+1
|
|
|
|
})
|
|
|
|
if dc_err != nil {
|
|
|
|
return nil, dc_err
|
|
|
|
}
|
|
|
|
|
|
|
|
//find main rack and other racks
|
|
|
|
mainRack, otherRacks, rack_err := mainDataCenter.(*topology.DataCenter).RandomlyPickNodes(rp.DiffRackCount+1, func(node topology.Node) bool {
|
|
|
|
return node.FreeSpace() > rp.SameRackCount+1
|
|
|
|
})
|
|
|
|
if rack_err != nil {
|
|
|
|
return nil, rack_err
|
|
|
|
}
|
|
|
|
|
|
|
|
//find main rack and other racks
|
|
|
|
mainServer, otherServers, server_err := mainRack.(*topology.Rack).RandomlyPickNodes(rp.SameRackCount+1, func(node topology.Node) bool {
|
|
|
|
return node.FreeSpace() > 1
|
|
|
|
})
|
|
|
|
if server_err != nil {
|
|
|
|
return nil, server_err
|
|
|
|
}
|
|
|
|
|
|
|
|
servers = append(servers, mainServer.(*topology.DataNode))
|
|
|
|
for _, server := range otherServers {
|
|
|
|
servers = append(servers, server.(*topology.DataNode))
|
|
|
|
}
|
|
|
|
for _, rack := range otherRacks {
|
|
|
|
r := rand.Intn(rack.FreeSpace())
|
|
|
|
if server, e := rack.ReserveOneVolume(r); e == nil {
|
|
|
|
servers = append(servers, server)
|
|
|
|
} else {
|
|
|
|
return servers, e
|
2012-09-03 08:50:04 +00:00
|
|
|
}
|
2014-03-03 06:16:54 +00:00
|
|
|
}
|
|
|
|
for _, datacenter := range otherDataCenters {
|
|
|
|
r := rand.Intn(datacenter.FreeSpace())
|
|
|
|
if server, e := datacenter.ReserveOneVolume(r); e == nil {
|
|
|
|
servers = append(servers, server)
|
|
|
|
} else {
|
|
|
|
return servers, e
|
2012-09-03 08:50:04 +00:00
|
|
|
}
|
2012-08-27 20:52:02 +00:00
|
|
|
}
|
2012-09-17 06:18:47 +00:00
|
|
|
return
|
2012-08-27 20:52:02 +00:00
|
|
|
}
|
2014-03-03 06:16:54 +00:00
|
|
|
|
|
|
|
func (vg *VolumeGrowth) grow(topo *topology.Topology, vid storage.VolumeId, collection string, rp *storage.ReplicaPlacement, servers ...*topology.DataNode) error {
|
2012-09-03 08:50:04 +00:00
|
|
|
for _, server := range servers {
|
2014-03-03 06:16:54 +00:00
|
|
|
if err := AllocateVolume(server, vid, collection, rp); err == nil {
|
|
|
|
vi := storage.VolumeInfo{Id: vid, Size: 0, Collection: collection, ReplicaPlacement: rp, Version: storage.CurrentVersion}
|
2012-09-17 00:31:15 +00:00
|
|
|
server.AddOrUpdateVolume(vi)
|
2012-09-19 08:45:30 +00:00
|
|
|
topo.RegisterVolumeLayout(&vi, server)
|
2013-08-09 06:57:22 +00:00
|
|
|
glog.V(0).Infoln("Created Volume", vid, "on", server)
|
2012-09-17 00:31:15 +00:00
|
|
|
} else {
|
2013-08-09 06:57:22 +00:00
|
|
|
glog.V(0).Infoln("Failed to assign", vid, "to", servers, "error", err)
|
2013-08-13 06:48:10 +00:00
|
|
|
return errors.New("Failed to assign " + vid.String() + ", " + err.Error())
|
2012-09-17 00:31:15 +00:00
|
|
|
}
|
2012-09-03 08:50:04 +00:00
|
|
|
}
|
2012-09-17 06:18:47 +00:00
|
|
|
return nil
|
2012-09-03 08:50:04 +00:00
|
|
|
}
|