2012-08-24 11:56:09 +08:00
|
|
|
package topology
|
|
|
|
|
2012-08-24 13:56:14 +08:00
|
|
|
import (
|
2014-03-03 14:16:54 +08:00
|
|
|
"errors"
|
|
|
|
"math/rand"
|
2014-03-14 03:13:39 +08:00
|
|
|
"strings"
|
2014-10-27 02:34:55 +08:00
|
|
|
|
2015-04-17 03:18:06 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/go/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/go/storage"
|
2012-08-24 13:56:14 +08:00
|
|
|
)
|
2012-08-24 11:56:09 +08:00
|
|
|
|
2012-08-28 16:04:39 +08:00
|
|
|
type NodeId string
|
2012-09-03 05:33:48 +08:00
|
|
|
type Node interface {
|
|
|
|
Id() NodeId
|
|
|
|
String() string
|
|
|
|
FreeSpace() int
|
2014-03-03 14:16:54 +08:00
|
|
|
ReserveOneVolume(r int) (*DataNode, error)
|
2012-09-03 05:33:48 +08:00
|
|
|
UpAdjustMaxVolumeCountDelta(maxVolumeCountDelta int)
|
2012-12-04 13:27:57 +08:00
|
|
|
UpAdjustVolumeCountDelta(volumeCountDelta int)
|
2012-09-03 05:33:48 +08:00
|
|
|
UpAdjustActiveVolumeCountDelta(activeVolumeCountDelta int)
|
|
|
|
UpAdjustMaxVolumeId(vid storage.VolumeId)
|
2012-10-10 11:53:31 +08:00
|
|
|
|
2012-12-04 13:27:57 +08:00
|
|
|
GetVolumeCount() int
|
2012-09-03 05:33:48 +08:00
|
|
|
GetActiveVolumeCount() int
|
|
|
|
GetMaxVolumeCount() int
|
|
|
|
GetMaxVolumeId() storage.VolumeId
|
2012-09-19 16:45:30 +08:00
|
|
|
SetParent(Node)
|
2012-09-03 05:33:48 +08:00
|
|
|
LinkChildNode(node Node)
|
|
|
|
UnlinkChildNode(nodeId NodeId)
|
2012-09-19 05:05:12 +08:00
|
|
|
CollectDeadNodeAndFullVolumes(freshThreshHold int64, volumeSizeLimit uint64)
|
2012-09-03 16:50:04 +08:00
|
|
|
|
2012-09-09 07:25:44 +08:00
|
|
|
IsDataNode() bool
|
2013-06-20 09:10:38 +08:00
|
|
|
IsRack() bool
|
|
|
|
IsDataCenter() bool
|
2012-09-03 16:50:04 +08:00
|
|
|
Children() map[NodeId]Node
|
2012-09-10 15:18:07 +08:00
|
|
|
Parent() Node
|
2012-10-10 11:53:31 +08:00
|
|
|
|
|
|
|
GetValue() interface{} //get reference to the topology,dc,rack,datanode
|
2012-08-24 11:56:09 +08:00
|
|
|
}
|
2012-09-03 05:33:48 +08:00
|
|
|
type NodeImpl struct {
|
|
|
|
id NodeId
|
2012-12-04 13:27:57 +08:00
|
|
|
volumeCount int
|
2012-09-03 05:33:48 +08:00
|
|
|
activeVolumeCount int
|
|
|
|
maxVolumeCount int
|
|
|
|
parent Node
|
|
|
|
children map[NodeId]Node
|
|
|
|
maxVolumeId storage.VolumeId
|
2012-08-29 15:58:03 +08:00
|
|
|
|
2012-09-03 05:33:48 +08:00
|
|
|
//for rack, data center, topology
|
|
|
|
nodeType string
|
2012-10-10 11:53:31 +08:00
|
|
|
value interface{}
|
2012-09-01 17:20:59 +08:00
|
|
|
}
|
2012-08-31 16:35:11 +08:00
|
|
|
|
2014-03-03 14:16:54 +08:00
|
|
|
// the first node must satisfy filterFirstNodeFn(), the rest nodes must have one free slot
|
2014-03-14 03:13:39 +08:00
|
|
|
func (n *NodeImpl) RandomlyPickNodes(numberOfNodes int, filterFirstNodeFn func(dn Node) error) (firstNode Node, restNodes []Node, err error) {
|
2014-03-03 14:16:54 +08:00
|
|
|
candidates := make([]Node, 0, len(n.children))
|
2015-03-10 15:20:31 +08:00
|
|
|
var errs []string
|
2014-03-03 14:16:54 +08:00
|
|
|
for _, node := range n.children {
|
2014-03-14 03:13:39 +08:00
|
|
|
if err := filterFirstNodeFn(node); err == nil {
|
2014-03-03 14:16:54 +08:00
|
|
|
candidates = append(candidates, node)
|
2014-03-14 03:13:39 +08:00
|
|
|
} else {
|
|
|
|
errs = append(errs, string(node.Id())+":"+err.Error())
|
2014-03-03 14:16:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(candidates) == 0 {
|
2014-03-14 03:13:39 +08:00
|
|
|
return nil, nil, errors.New("No matching data node found! \n" + strings.Join(errs, "\n"))
|
2014-03-03 14:16:54 +08:00
|
|
|
}
|
|
|
|
firstNode = candidates[rand.Intn(len(candidates))]
|
|
|
|
glog.V(2).Infoln(n.Id(), "picked main node:", firstNode.Id())
|
|
|
|
|
|
|
|
restNodes = make([]Node, numberOfNodes-1)
|
|
|
|
candidates = candidates[:0]
|
|
|
|
for _, node := range n.children {
|
|
|
|
if node.Id() == firstNode.Id() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if node.FreeSpace() <= 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
glog.V(2).Infoln("select rest node candidate:", node.Id())
|
|
|
|
candidates = append(candidates, node)
|
|
|
|
}
|
|
|
|
glog.V(2).Infoln(n.Id(), "picking", numberOfNodes-1, "from rest", len(candidates), "node candidates")
|
|
|
|
ret := len(restNodes) == 0
|
|
|
|
for k, node := range candidates {
|
|
|
|
if k < len(restNodes) {
|
|
|
|
restNodes[k] = node
|
|
|
|
if k == len(restNodes)-1 {
|
|
|
|
ret = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
r := rand.Intn(k + 1)
|
|
|
|
if r < len(restNodes) {
|
|
|
|
restNodes[r] = node
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ret {
|
|
|
|
glog.V(2).Infoln(n.Id(), "failed to pick", numberOfNodes-1, "from rest", len(candidates), "node candidates")
|
|
|
|
err = errors.New("Not enough data node found!")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-09-09 07:25:44 +08:00
|
|
|
func (n *NodeImpl) IsDataNode() bool {
|
|
|
|
return n.nodeType == "DataNode"
|
2012-09-03 05:33:48 +08:00
|
|
|
}
|
|
|
|
func (n *NodeImpl) IsRack() bool {
|
|
|
|
return n.nodeType == "Rack"
|
|
|
|
}
|
|
|
|
func (n *NodeImpl) IsDataCenter() bool {
|
|
|
|
return n.nodeType == "DataCenter"
|
|
|
|
}
|
|
|
|
func (n *NodeImpl) String() string {
|
|
|
|
if n.parent != nil {
|
|
|
|
return n.parent.String() + ":" + string(n.id)
|
|
|
|
}
|
|
|
|
return string(n.id)
|
|
|
|
}
|
|
|
|
func (n *NodeImpl) Id() NodeId {
|
|
|
|
return n.id
|
|
|
|
}
|
|
|
|
func (n *NodeImpl) FreeSpace() int {
|
2012-12-04 13:27:57 +08:00
|
|
|
return n.maxVolumeCount - n.volumeCount
|
2012-09-03 05:33:48 +08:00
|
|
|
}
|
2012-09-19 16:45:30 +08:00
|
|
|
func (n *NodeImpl) SetParent(node Node) {
|
2012-09-03 05:33:48 +08:00
|
|
|
n.parent = node
|
|
|
|
}
|
2012-09-03 16:50:04 +08:00
|
|
|
func (n *NodeImpl) Children() map[NodeId]Node {
|
|
|
|
return n.children
|
|
|
|
}
|
|
|
|
func (n *NodeImpl) Parent() Node {
|
2012-09-10 15:18:07 +08:00
|
|
|
return n.parent
|
2012-09-03 16:50:04 +08:00
|
|
|
}
|
2012-10-10 11:53:31 +08:00
|
|
|
func (n *NodeImpl) GetValue() interface{} {
|
|
|
|
return n.value
|
2012-09-19 16:45:30 +08:00
|
|
|
}
|
2014-03-03 14:16:54 +08:00
|
|
|
func (n *NodeImpl) ReserveOneVolume(r int) (assignedNode *DataNode, err error) {
|
2012-08-29 15:58:03 +08:00
|
|
|
for _, node := range n.children {
|
2012-09-03 05:33:48 +08:00
|
|
|
freeSpace := node.FreeSpace()
|
2013-06-20 09:10:38 +08:00
|
|
|
// fmt.Println("r =", r, ", node =", node, ", freeSpace =", freeSpace)
|
2012-09-03 05:33:48 +08:00
|
|
|
if freeSpace <= 0 {
|
|
|
|
continue
|
|
|
|
}
|
2012-09-01 17:20:59 +08:00
|
|
|
if r >= freeSpace {
|
2012-08-29 15:58:03 +08:00
|
|
|
r -= freeSpace
|
|
|
|
} else {
|
2012-09-09 07:25:44 +08:00
|
|
|
if node.IsDataNode() && node.FreeSpace() > 0 {
|
2013-06-20 09:10:38 +08:00
|
|
|
// fmt.Println("vid =", vid, " assigned to node =", node, ", freeSpace =", node.FreeSpace())
|
2014-03-03 14:16:54 +08:00
|
|
|
return node.(*DataNode), nil
|
2012-09-03 16:50:04 +08:00
|
|
|
}
|
2014-03-03 14:16:54 +08:00
|
|
|
assignedNode, err = node.ReserveOneVolume(r)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2012-09-03 05:33:48 +08:00
|
|
|
}
|
2012-08-29 15:58:03 +08:00
|
|
|
}
|
|
|
|
}
|
2014-03-03 14:16:54 +08:00
|
|
|
return
|
2012-08-29 15:58:03 +08:00
|
|
|
}
|
|
|
|
|
2012-09-03 05:33:48 +08:00
|
|
|
func (n *NodeImpl) UpAdjustMaxVolumeCountDelta(maxVolumeCountDelta int) { //can be negative
|
|
|
|
n.maxVolumeCount += maxVolumeCountDelta
|
|
|
|
if n.parent != nil {
|
|
|
|
n.parent.UpAdjustMaxVolumeCountDelta(maxVolumeCountDelta)
|
2012-08-31 16:35:11 +08:00
|
|
|
}
|
2012-09-03 05:33:48 +08:00
|
|
|
}
|
2012-12-04 13:27:57 +08:00
|
|
|
func (n *NodeImpl) UpAdjustVolumeCountDelta(volumeCountDelta int) { //can be negative
|
|
|
|
n.volumeCount += volumeCountDelta
|
|
|
|
if n.parent != nil {
|
|
|
|
n.parent.UpAdjustVolumeCountDelta(volumeCountDelta)
|
|
|
|
}
|
|
|
|
}
|
2012-09-03 05:33:48 +08:00
|
|
|
func (n *NodeImpl) UpAdjustActiveVolumeCountDelta(activeVolumeCountDelta int) { //can be negative
|
|
|
|
n.activeVolumeCount += activeVolumeCountDelta
|
2012-08-29 15:58:03 +08:00
|
|
|
if n.parent != nil {
|
2012-09-03 05:33:48 +08:00
|
|
|
n.parent.UpAdjustActiveVolumeCountDelta(activeVolumeCountDelta)
|
2012-08-29 15:58:03 +08:00
|
|
|
}
|
2012-08-28 04:52:02 +08:00
|
|
|
}
|
2012-09-03 05:33:48 +08:00
|
|
|
func (n *NodeImpl) UpAdjustMaxVolumeId(vid storage.VolumeId) { //can be negative
|
|
|
|
if n.maxVolumeId < vid {
|
|
|
|
n.maxVolumeId = vid
|
|
|
|
if n.parent != nil {
|
|
|
|
n.parent.UpAdjustMaxVolumeId(vid)
|
|
|
|
}
|
|
|
|
}
|
2012-09-01 17:20:59 +08:00
|
|
|
}
|
2012-09-03 05:33:48 +08:00
|
|
|
func (n *NodeImpl) GetMaxVolumeId() storage.VolumeId {
|
2012-08-31 16:35:11 +08:00
|
|
|
return n.maxVolumeId
|
2012-08-29 16:37:40 +08:00
|
|
|
}
|
2012-12-04 13:27:57 +08:00
|
|
|
func (n *NodeImpl) GetVolumeCount() int {
|
|
|
|
return n.volumeCount
|
|
|
|
}
|
2012-09-03 05:33:48 +08:00
|
|
|
func (n *NodeImpl) GetActiveVolumeCount() int {
|
|
|
|
return n.activeVolumeCount
|
|
|
|
}
|
|
|
|
func (n *NodeImpl) GetMaxVolumeCount() int {
|
|
|
|
return n.maxVolumeCount
|
|
|
|
}
|
2012-08-29 16:37:40 +08:00
|
|
|
|
2012-09-03 05:33:48 +08:00
|
|
|
func (n *NodeImpl) LinkChildNode(node Node) {
|
|
|
|
if n.children[node.Id()] == nil {
|
|
|
|
n.children[node.Id()] = node
|
2012-09-17 08:31:15 +08:00
|
|
|
n.UpAdjustMaxVolumeCountDelta(node.GetMaxVolumeCount())
|
|
|
|
n.UpAdjustMaxVolumeId(node.GetMaxVolumeId())
|
2012-12-04 13:27:57 +08:00
|
|
|
n.UpAdjustVolumeCountDelta(node.GetVolumeCount())
|
2012-09-17 08:31:15 +08:00
|
|
|
n.UpAdjustActiveVolumeCountDelta(node.GetActiveVolumeCount())
|
2012-09-19 16:45:30 +08:00
|
|
|
node.SetParent(n)
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.V(0).Infoln(n, "adds child", node.Id())
|
2012-08-29 15:58:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-03 05:33:48 +08:00
|
|
|
func (n *NodeImpl) UnlinkChildNode(nodeId NodeId) {
|
2012-09-01 17:20:59 +08:00
|
|
|
node := n.children[nodeId]
|
|
|
|
if node != nil {
|
2016-04-11 18:17:22 +08:00
|
|
|
node.SetParent(nil)
|
2012-09-03 05:33:48 +08:00
|
|
|
delete(n.children, node.Id())
|
2012-12-04 13:27:57 +08:00
|
|
|
n.UpAdjustVolumeCountDelta(-node.GetVolumeCount())
|
2012-09-03 05:33:48 +08:00
|
|
|
n.UpAdjustActiveVolumeCountDelta(-node.GetActiveVolumeCount())
|
|
|
|
n.UpAdjustMaxVolumeCountDelta(-node.GetMaxVolumeCount())
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.V(0).Infoln(n, "removes", node, "volumeCount =", n.activeVolumeCount)
|
2012-08-29 15:58:03 +08:00
|
|
|
}
|
2012-08-28 04:52:02 +08:00
|
|
|
}
|
2012-09-10 15:18:07 +08:00
|
|
|
|
2012-09-19 05:05:12 +08:00
|
|
|
func (n *NodeImpl) CollectDeadNodeAndFullVolumes(freshThreshHold int64, volumeSizeLimit uint64) {
|
2012-09-10 15:18:07 +08:00
|
|
|
if n.IsRack() {
|
|
|
|
for _, c := range n.Children() {
|
|
|
|
dn := c.(*DataNode) //can not cast n to DataNode
|
2012-09-19 16:45:30 +08:00
|
|
|
if dn.LastSeen < freshThreshHold {
|
2012-09-19 05:05:12 +08:00
|
|
|
if !dn.Dead {
|
|
|
|
dn.Dead = true
|
|
|
|
n.GetTopology().chanDeadDataNodes <- dn
|
|
|
|
}
|
2012-09-10 15:18:07 +08:00
|
|
|
}
|
|
|
|
for _, v := range dn.volumes {
|
2012-09-19 16:45:30 +08:00
|
|
|
if uint64(v.Size) >= volumeSizeLimit {
|
2012-12-04 13:27:57 +08:00
|
|
|
//fmt.Println("volume",v.Id,"size",v.Size,">",volumeSizeLimit)
|
2012-12-04 12:28:12 +08:00
|
|
|
n.GetTopology().chanFullVolumes <- v
|
2012-09-10 15:18:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, c := range n.Children() {
|
2012-09-19 05:05:12 +08:00
|
|
|
c.CollectDeadNodeAndFullVolumes(freshThreshHold, volumeSizeLimit)
|
2012-09-10 15:18:07 +08:00
|
|
|
}
|
|
|
|
}
|
2012-09-19 05:05:12 +08:00
|
|
|
}
|
2012-09-10 15:18:07 +08:00
|
|
|
|
2012-10-10 11:53:31 +08:00
|
|
|
func (n *NodeImpl) GetTopology() *Topology {
|
|
|
|
var p Node
|
|
|
|
p = n
|
|
|
|
for p.Parent() != nil {
|
|
|
|
p = p.Parent()
|
|
|
|
}
|
|
|
|
return p.GetValue().(*Topology)
|
2012-09-10 15:18:07 +08:00
|
|
|
}
|