seaweedfs/weed/topology/volume_growth.go

233 lines
7.9 KiB
Go
Raw Normal View History

package topology
import (
"fmt"
2013-02-27 06:54:22 +00:00
"math/rand"
2013-01-17 08:56:56 +00:00
"sync"
"github.com/chrislusf/seaweedfs/weed/storage/needle"
2019-12-23 20:48:20 +00:00
"github.com/chrislusf/seaweedfs/weed/storage/super_block"
"github.com/chrislusf/seaweedfs/weed/util"
2019-12-23 20:48:20 +00:00
2019-04-19 04:43:36 +00:00
"google.golang.org/grpc"
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/storage"
)
/*
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 VolumeGrowOption struct {
Collection string
2019-12-23 20:48:20 +00:00
ReplicaPlacement *super_block.ReplicaPlacement
Ttl *needle.TTL
2020-12-16 17:14:05 +00:00
DiskType storage.DiskType
Prealloacte int64
DataCenter string
Rack string
DataNode string
2019-10-22 05:57:01 +00:00
MemoryMapMaxSizeMb uint32
}
type VolumeGrowth struct {
2013-01-17 08:56:56 +00:00
accessLock sync.Mutex
}
func (o *VolumeGrowOption) String() string {
return fmt.Sprintf("Collection:%s, ReplicaPlacement:%v, Ttl:%v, DataCenter:%s, Rack:%s, DataNode:%s", o.Collection, o.ReplicaPlacement, o.Ttl, o.DataCenter, o.Rack, o.DataNode)
}
func NewDefaultVolumeGrowth() *VolumeGrowth {
return &VolumeGrowth{}
}
// 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) {
v := util.GetViper()
v.SetDefault("master.volume_growth.copy_1", 7)
v.SetDefault("master.volume_growth.copy_2", 6)
v.SetDefault("master.volume_growth.copy_3", 3)
v.SetDefault("master.volume_growth.copy_other", 1)
switch copyCount {
case 1:
count = v.GetInt("master.volume_growth.copy_1")
case 2:
count = v.GetInt("master.volume_growth.copy_2")
case 3:
count = v.GetInt("master.volume_growth.copy_3")
default:
count = v.GetInt("master.volume_growth.copy_other")
}
return
}
func (vg *VolumeGrowth) AutomaticGrowByType(option *VolumeGrowOption, grpcDialOption grpc.DialOption, topo *Topology, targetCount int) (count int, err error) {
if targetCount == 0 {
targetCount = vg.findVolumeCount(option.ReplicaPlacement.GetCopyCount())
}
count, err = vg.GrowByCountAndType(grpcDialOption, targetCount, option, topo)
if count > 0 && count%option.ReplicaPlacement.GetCopyCount() == 0 {
return count, nil
}
return count, err
}
2019-02-18 20:11:52 +00:00
func (vg *VolumeGrowth) GrowByCountAndType(grpcDialOption grpc.DialOption, targetCount int, option *VolumeGrowOption, topo *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
for i := 0; i < targetCount; i++ {
2019-02-18 20:11:52 +00:00
if c, e := vg.findAndGrow(grpcDialOption, topo, option); e == nil {
counter += c
} else {
2020-03-23 01:32:49 +00:00
glog.V(0).Infof("create %d volume, created %d: %v", targetCount, counter, e)
return counter, e
2012-09-30 09:20:33 +00:00
}
}
return
}
2019-02-18 20:11:52 +00:00
func (vg *VolumeGrowth) findAndGrow(grpcDialOption grpc.DialOption, topo *Topology, option *VolumeGrowOption) (int, error) {
servers, e := vg.findEmptySlotsForOneVolume(topo, option)
if e != nil {
return 0, e
}
vid, raftErr := topo.NextVolumeId()
if raftErr != nil {
return 0, raftErr
}
2019-02-18 20:11:52 +00:00
err := vg.grow(grpcDialOption, topo, vid, option, servers...)
return len(servers), err
}
2014-04-13 10:06:58 +00:00
// 1. find the main data node
// 1.1 collect all data nodes that have 1 slots
// 2.2 collect all racks that have rp.SameRackCount+1
// 2.2 collect all data centers that have DiffRackCount+rp.SameRackCount+1
// 2. find rest data nodes
func (vg *VolumeGrowth) findEmptySlotsForOneVolume(topo *Topology, option *VolumeGrowOption) (servers []*DataNode, err error) {
//find main datacenter and other data centers
rp := option.ReplicaPlacement
2020-12-17 21:25:05 +00:00
mainDataCenter, otherDataCenters, dc_err := topo.PickNodesByWeight(rp.DiffDataCenterCount+1, option, func(node Node) error {
if option.DataCenter != "" && node.IsDataCenter() && node.Id() != NodeId(option.DataCenter) {
return fmt.Errorf("Not matching preferred data center:%s", option.DataCenter)
2012-09-03 08:50:04 +00:00
}
if len(node.Children()) < rp.DiffRackCount+1 {
return fmt.Errorf("Only has %d racks, not enough for %d.", len(node.Children()), rp.DiffRackCount+1)
}
2020-12-17 21:25:05 +00:00
if node.AvailableSpaceFor(option) < int64(rp.DiffRackCount+rp.SameRackCount+1) {
return fmt.Errorf("Free:%d < Expected:%d", node.AvailableSpaceFor(option), rp.DiffRackCount+rp.SameRackCount+1)
}
2014-04-13 10:06:58 +00:00
possibleRacksCount := 0
for _, rack := range node.Children() {
possibleDataNodesCount := 0
for _, n := range rack.Children() {
2020-12-17 21:25:05 +00:00
if n.AvailableSpaceFor(option) >= 1 {
2014-04-13 10:06:58 +00:00
possibleDataNodesCount++
}
}
if possibleDataNodesCount >= rp.SameRackCount+1 {
possibleRacksCount++
}
}
if possibleRacksCount < rp.DiffRackCount+1 {
return fmt.Errorf("Only has %d racks with more than %d free data nodes, not enough for %d.", possibleRacksCount, rp.SameRackCount+1, rp.DiffRackCount+1)
}
return nil
})
if dc_err != nil {
return nil, dc_err
}
//find main rack and other racks
2020-12-17 21:25:05 +00:00
mainRack, otherRacks, rackErr := mainDataCenter.(*DataCenter).PickNodesByWeight(rp.DiffRackCount+1, option, func(node Node) error {
if option.Rack != "" && node.IsRack() && node.Id() != NodeId(option.Rack) {
return fmt.Errorf("Not matching preferred rack:%s", option.Rack)
}
2020-12-17 21:25:05 +00:00
if node.AvailableSpaceFor(option) < int64(rp.SameRackCount+1) {
return fmt.Errorf("Free:%d < Expected:%d", node.AvailableSpaceFor(option), rp.SameRackCount+1)
}
if len(node.Children()) < rp.SameRackCount+1 {
2014-04-13 10:06:58 +00:00
// a bit faster way to test free racks
return fmt.Errorf("Only has %d data nodes, not enough for %d.", len(node.Children()), rp.SameRackCount+1)
}
2014-04-13 10:06:58 +00:00
possibleDataNodesCount := 0
for _, n := range node.Children() {
2020-12-17 21:25:05 +00:00
if n.AvailableSpaceFor(option) >= 1 {
2014-04-13 10:06:58 +00:00
possibleDataNodesCount++
}
}
2014-04-13 10:06:58 +00:00
if possibleDataNodesCount < rp.SameRackCount+1 {
return fmt.Errorf("Only has %d data nodes with a slot, not enough for %d.", possibleDataNodesCount, rp.SameRackCount+1)
}
return nil
})
2019-01-17 01:17:19 +00:00
if rackErr != nil {
return nil, rackErr
}
//find main rack and other racks
2020-12-17 21:25:05 +00:00
mainServer, otherServers, serverErr := mainRack.(*Rack).PickNodesByWeight(rp.SameRackCount+1, option, func(node Node) error {
if option.DataNode != "" && node.IsDataNode() && node.Id() != NodeId(option.DataNode) {
return fmt.Errorf("Not matching preferred data node:%s", option.DataNode)
}
2020-12-17 21:25:05 +00:00
if node.AvailableSpaceFor(option) < 1 {
return fmt.Errorf("Free:%d < Expected:%d", node.AvailableSpaceFor(option), 1)
}
return nil
})
2019-01-17 01:17:19 +00:00
if serverErr != nil {
return nil, serverErr
}
servers = append(servers, mainServer.(*DataNode))
for _, server := range otherServers {
servers = append(servers, server.(*DataNode))
}
for _, rack := range otherRacks {
2020-12-17 21:25:05 +00:00
r := rand.Int63n(rack.AvailableSpaceFor(option))
if server, e := rack.ReserveOneVolume(r, option); e == nil {
servers = append(servers, server)
} else {
return servers, e
2012-09-03 08:50:04 +00:00
}
}
for _, datacenter := range otherDataCenters {
2020-12-17 21:25:05 +00:00
r := rand.Int63n(datacenter.AvailableSpaceFor(option))
if server, e := datacenter.ReserveOneVolume(r, option); e == nil {
servers = append(servers, server)
} else {
return servers, e
2012-09-03 08:50:04 +00:00
}
}
return
}
2019-04-19 04:43:36 +00:00
func (vg *VolumeGrowth) grow(grpcDialOption grpc.DialOption, topo *Topology, vid needle.VolumeId, option *VolumeGrowOption, servers ...*DataNode) error {
2012-09-03 08:50:04 +00:00
for _, server := range servers {
2019-02-18 20:11:52 +00:00
if err := AllocateVolume(server, grpcDialOption, vid, option); err == nil {
vi := storage.VolumeInfo{
Id: vid,
Size: 0,
Collection: option.Collection,
ReplicaPlacement: option.ReplicaPlacement,
Ttl: option.Ttl,
2019-04-19 04:43:36 +00:00
Version: needle.CurrentVersion,
}
server.AddOrUpdateVolume(vi)
topo.RegisterVolumeLayout(vi, server)
glog.V(0).Infoln("Created Volume", vid, "on", server.NodeImpl.String())
} else {
2015-02-26 05:29:12 +00:00
glog.V(0).Infoln("Failed to assign volume", vid, "to", servers, "error", err)
2015-01-14 01:04:41 +00:00
return fmt.Errorf("Failed to assign %d: %v", vid, err)
}
2012-09-03 08:50:04 +00:00
}
return nil
2012-09-03 08:50:04 +00:00
}