2012-08-24 03:56:09 +00:00
|
|
|
package topology
|
|
|
|
|
2012-08-24 05:56:14 +00:00
|
|
|
import (
|
2013-02-10 11:49:51 +00:00
|
|
|
"code.google.com/p/weed-fs/go/storage"
|
2013-02-27 06:54:22 +00:00
|
|
|
"fmt"
|
2012-08-24 05:56:14 +00:00
|
|
|
)
|
2012-08-24 03:56:09 +00:00
|
|
|
|
2012-08-28 08:04:39 +00:00
|
|
|
type NodeId string
|
2012-09-02 21:33:48 +00:00
|
|
|
type Node interface {
|
|
|
|
Id() NodeId
|
|
|
|
String() string
|
|
|
|
FreeSpace() int
|
2013-06-20 01:10:38 +00:00
|
|
|
ReserveOneVolume(r int, vid storage.VolumeId, dataCenter string) (bool, *DataNode)
|
2012-09-02 21:33:48 +00:00
|
|
|
UpAdjustMaxVolumeCountDelta(maxVolumeCountDelta int)
|
2012-12-04 05:27:57 +00:00
|
|
|
UpAdjustVolumeCountDelta(volumeCountDelta int)
|
2012-09-02 21:33:48 +00:00
|
|
|
UpAdjustActiveVolumeCountDelta(activeVolumeCountDelta int)
|
|
|
|
UpAdjustMaxVolumeId(vid storage.VolumeId)
|
2012-10-10 03:53:31 +00:00
|
|
|
|
2012-12-04 05:27:57 +00:00
|
|
|
GetVolumeCount() int
|
2012-09-02 21:33:48 +00:00
|
|
|
GetActiveVolumeCount() int
|
|
|
|
GetMaxVolumeCount() int
|
|
|
|
GetMaxVolumeId() storage.VolumeId
|
2012-09-19 08:45:30 +00:00
|
|
|
SetParent(Node)
|
2012-09-02 21:33:48 +00:00
|
|
|
LinkChildNode(node Node)
|
|
|
|
UnlinkChildNode(nodeId NodeId)
|
2012-09-18 21:05:12 +00:00
|
|
|
CollectDeadNodeAndFullVolumes(freshThreshHold int64, volumeSizeLimit uint64)
|
2012-09-03 08:50:04 +00:00
|
|
|
|
2012-09-08 23:25:44 +00:00
|
|
|
IsDataNode() bool
|
2013-06-20 01:10:38 +00:00
|
|
|
IsRack() bool
|
|
|
|
IsDataCenter() bool
|
2012-09-03 08:50:04 +00:00
|
|
|
Children() map[NodeId]Node
|
2012-09-10 07:18:07 +00:00
|
|
|
Parent() Node
|
2012-10-10 03:53:31 +00:00
|
|
|
|
|
|
|
GetValue() interface{} //get reference to the topology,dc,rack,datanode
|
2012-08-24 03:56:09 +00:00
|
|
|
}
|
2012-09-02 21:33:48 +00:00
|
|
|
type NodeImpl struct {
|
|
|
|
id NodeId
|
2012-12-04 05:27:57 +00:00
|
|
|
volumeCount int
|
2012-09-02 21:33:48 +00:00
|
|
|
activeVolumeCount int
|
|
|
|
maxVolumeCount int
|
|
|
|
parent Node
|
|
|
|
children map[NodeId]Node
|
|
|
|
maxVolumeId storage.VolumeId
|
2012-08-29 07:58:03 +00:00
|
|
|
|
2012-09-02 21:33:48 +00:00
|
|
|
//for rack, data center, topology
|
|
|
|
nodeType string
|
2012-10-10 03:53:31 +00:00
|
|
|
value interface{}
|
2012-09-01 09:20:59 +00:00
|
|
|
}
|
2012-08-31 08:35:11 +00:00
|
|
|
|
2012-09-08 23:25:44 +00:00
|
|
|
func (n *NodeImpl) IsDataNode() bool {
|
|
|
|
return n.nodeType == "DataNode"
|
2012-09-02 21:33:48 +00: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 05:27:57 +00:00
|
|
|
return n.maxVolumeCount - n.volumeCount
|
2012-09-02 21:33:48 +00:00
|
|
|
}
|
2012-09-19 08:45:30 +00:00
|
|
|
func (n *NodeImpl) SetParent(node Node) {
|
2012-09-02 21:33:48 +00:00
|
|
|
n.parent = node
|
|
|
|
}
|
2012-09-03 08:50:04 +00:00
|
|
|
func (n *NodeImpl) Children() map[NodeId]Node {
|
|
|
|
return n.children
|
|
|
|
}
|
|
|
|
func (n *NodeImpl) Parent() Node {
|
2012-09-10 07:18:07 +00:00
|
|
|
return n.parent
|
2012-09-03 08:50:04 +00:00
|
|
|
}
|
2012-10-10 03:53:31 +00:00
|
|
|
func (n *NodeImpl) GetValue() interface{} {
|
|
|
|
return n.value
|
2012-09-19 08:45:30 +00:00
|
|
|
}
|
2013-06-20 01:10:38 +00:00
|
|
|
func (n *NodeImpl) ReserveOneVolume(r int, vid storage.VolumeId, dataCenter string) (bool, *DataNode) {
|
2012-09-02 21:33:48 +00:00
|
|
|
ret := false
|
2012-09-08 23:25:44 +00:00
|
|
|
var assignedNode *DataNode
|
2012-08-29 07:58:03 +00:00
|
|
|
for _, node := range n.children {
|
2012-09-02 21:33:48 +00:00
|
|
|
freeSpace := node.FreeSpace()
|
2013-06-20 01:10:38 +00:00
|
|
|
// fmt.Println("r =", r, ", node =", node, ", freeSpace =", freeSpace)
|
2012-09-02 21:33:48 +00:00
|
|
|
if freeSpace <= 0 {
|
|
|
|
continue
|
|
|
|
}
|
2013-06-20 01:10:38 +00:00
|
|
|
if dataCenter != "" && node.IsDataCenter() && node.Id() != NodeId(dataCenter) {
|
|
|
|
continue
|
|
|
|
}
|
2012-09-01 09:20:59 +00:00
|
|
|
if r >= freeSpace {
|
2012-08-29 07:58:03 +00:00
|
|
|
r -= freeSpace
|
|
|
|
} else {
|
2012-09-08 23:25:44 +00:00
|
|
|
if node.IsDataNode() && node.FreeSpace() > 0 {
|
2013-06-20 01:10:38 +00:00
|
|
|
// fmt.Println("vid =", vid, " assigned to node =", node, ", freeSpace =", node.FreeSpace())
|
2012-09-08 23:25:44 +00:00
|
|
|
return true, node.(*DataNode)
|
2012-09-03 08:50:04 +00:00
|
|
|
}
|
2013-06-20 01:10:38 +00:00
|
|
|
ret, assignedNode = node.ReserveOneVolume(r, vid, dataCenter)
|
2012-09-02 21:33:48 +00:00
|
|
|
if ret {
|
|
|
|
break
|
|
|
|
}
|
2012-08-29 07:58:03 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-01 09:43:43 +00:00
|
|
|
return ret, assignedNode
|
2012-08-29 07:58:03 +00:00
|
|
|
}
|
|
|
|
|
2012-09-02 21:33:48 +00:00
|
|
|
func (n *NodeImpl) UpAdjustMaxVolumeCountDelta(maxVolumeCountDelta int) { //can be negative
|
|
|
|
n.maxVolumeCount += maxVolumeCountDelta
|
|
|
|
if n.parent != nil {
|
|
|
|
n.parent.UpAdjustMaxVolumeCountDelta(maxVolumeCountDelta)
|
2012-08-31 08:35:11 +00:00
|
|
|
}
|
2012-09-02 21:33:48 +00:00
|
|
|
}
|
2012-12-04 05:27:57 +00:00
|
|
|
func (n *NodeImpl) UpAdjustVolumeCountDelta(volumeCountDelta int) { //can be negative
|
|
|
|
n.volumeCount += volumeCountDelta
|
|
|
|
if n.parent != nil {
|
|
|
|
n.parent.UpAdjustVolumeCountDelta(volumeCountDelta)
|
|
|
|
}
|
|
|
|
}
|
2012-09-02 21:33:48 +00:00
|
|
|
func (n *NodeImpl) UpAdjustActiveVolumeCountDelta(activeVolumeCountDelta int) { //can be negative
|
|
|
|
n.activeVolumeCount += activeVolumeCountDelta
|
2012-08-29 07:58:03 +00:00
|
|
|
if n.parent != nil {
|
2012-09-02 21:33:48 +00:00
|
|
|
n.parent.UpAdjustActiveVolumeCountDelta(activeVolumeCountDelta)
|
2012-08-29 07:58:03 +00:00
|
|
|
}
|
2012-08-27 20:52:02 +00:00
|
|
|
}
|
2012-09-02 21:33:48 +00: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 09:20:59 +00:00
|
|
|
}
|
2012-09-02 21:33:48 +00:00
|
|
|
func (n *NodeImpl) GetMaxVolumeId() storage.VolumeId {
|
2012-08-31 08:35:11 +00:00
|
|
|
return n.maxVolumeId
|
2012-08-29 08:37:40 +00:00
|
|
|
}
|
2012-12-04 05:27:57 +00:00
|
|
|
func (n *NodeImpl) GetVolumeCount() int {
|
|
|
|
return n.volumeCount
|
|
|
|
}
|
2012-09-02 21:33:48 +00:00
|
|
|
func (n *NodeImpl) GetActiveVolumeCount() int {
|
|
|
|
return n.activeVolumeCount
|
|
|
|
}
|
|
|
|
func (n *NodeImpl) GetMaxVolumeCount() int {
|
|
|
|
return n.maxVolumeCount
|
|
|
|
}
|
2012-08-29 08:37:40 +00:00
|
|
|
|
2012-09-02 21:33:48 +00:00
|
|
|
func (n *NodeImpl) LinkChildNode(node Node) {
|
|
|
|
if n.children[node.Id()] == nil {
|
|
|
|
n.children[node.Id()] = node
|
2012-09-17 00:31:15 +00:00
|
|
|
n.UpAdjustMaxVolumeCountDelta(node.GetMaxVolumeCount())
|
|
|
|
n.UpAdjustMaxVolumeId(node.GetMaxVolumeId())
|
2012-12-04 05:27:57 +00:00
|
|
|
n.UpAdjustVolumeCountDelta(node.GetVolumeCount())
|
2012-09-17 00:31:15 +00:00
|
|
|
n.UpAdjustActiveVolumeCountDelta(node.GetActiveVolumeCount())
|
2012-09-19 08:45:30 +00:00
|
|
|
node.SetParent(n)
|
2012-09-23 03:46:31 +00:00
|
|
|
fmt.Println(n, "adds child", node.Id())
|
2012-08-29 07:58:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-02 21:33:48 +00:00
|
|
|
func (n *NodeImpl) UnlinkChildNode(nodeId NodeId) {
|
2012-09-01 09:20:59 +00:00
|
|
|
node := n.children[nodeId]
|
2012-09-19 08:45:30 +00:00
|
|
|
node.SetParent(nil)
|
2012-09-01 09:20:59 +00:00
|
|
|
if node != nil {
|
2012-09-02 21:33:48 +00:00
|
|
|
delete(n.children, node.Id())
|
2012-12-04 05:27:57 +00:00
|
|
|
n.UpAdjustVolumeCountDelta(-node.GetVolumeCount())
|
2012-09-02 21:33:48 +00:00
|
|
|
n.UpAdjustActiveVolumeCountDelta(-node.GetActiveVolumeCount())
|
|
|
|
n.UpAdjustMaxVolumeCountDelta(-node.GetMaxVolumeCount())
|
|
|
|
fmt.Println(n, "removes", node, "volumeCount =", n.activeVolumeCount)
|
2012-08-29 07:58:03 +00:00
|
|
|
}
|
2012-08-27 20:52:02 +00:00
|
|
|
}
|
2012-09-10 07:18:07 +00:00
|
|
|
|
2012-09-18 21:05:12 +00:00
|
|
|
func (n *NodeImpl) CollectDeadNodeAndFullVolumes(freshThreshHold int64, volumeSizeLimit uint64) {
|
2012-09-10 07:18:07 +00:00
|
|
|
if n.IsRack() {
|
|
|
|
for _, c := range n.Children() {
|
|
|
|
dn := c.(*DataNode) //can not cast n to DataNode
|
2012-09-19 08:45:30 +00:00
|
|
|
if dn.LastSeen < freshThreshHold {
|
2012-09-18 21:05:12 +00:00
|
|
|
if !dn.Dead {
|
|
|
|
dn.Dead = true
|
|
|
|
n.GetTopology().chanDeadDataNodes <- dn
|
|
|
|
}
|
2012-09-10 07:18:07 +00:00
|
|
|
}
|
|
|
|
for _, v := range dn.volumes {
|
2012-09-19 08:45:30 +00:00
|
|
|
if uint64(v.Size) >= volumeSizeLimit {
|
2012-12-04 05:27:57 +00:00
|
|
|
//fmt.Println("volume",v.Id,"size",v.Size,">",volumeSizeLimit)
|
2012-12-04 04:28:12 +00:00
|
|
|
n.GetTopology().chanFullVolumes <- v
|
2012-09-10 07:18:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, c := range n.Children() {
|
2012-09-18 21:05:12 +00:00
|
|
|
c.CollectDeadNodeAndFullVolumes(freshThreshHold, volumeSizeLimit)
|
2012-09-10 07:18:07 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-18 21:05:12 +00:00
|
|
|
}
|
2012-09-10 07:18:07 +00:00
|
|
|
|
2012-10-10 03:53:31 +00: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 07:18:07 +00:00
|
|
|
}
|