2012-08-23 20:56:09 -07:00
|
|
|
package topology
|
|
|
|
|
|
2012-08-28 01:04:39 -07:00
|
|
|
import (
|
2019-03-17 20:27:08 -07:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
|
2012-09-16 19:18:37 -07:00
|
|
|
"strconv"
|
2012-09-17 01:48:09 -07:00
|
|
|
"time"
|
2012-08-28 01:04:39 -07:00
|
|
|
)
|
2012-08-23 20:56:09 -07:00
|
|
|
|
|
|
|
|
type Rack struct {
|
2012-09-02 14:33:48 -07:00
|
|
|
NodeImpl
|
2012-08-23 20:56:09 -07:00
|
|
|
}
|
2012-08-31 01:35:11 -07:00
|
|
|
|
2012-09-02 14:33:48 -07:00
|
|
|
func NewRack(id string) *Rack {
|
2012-08-31 01:35:11 -07:00
|
|
|
r := &Rack{}
|
2012-09-16 19:18:37 -07:00
|
|
|
r.id = NodeId(id)
|
|
|
|
|
r.nodeType = "Rack"
|
|
|
|
|
r.children = make(map[NodeId]Node)
|
2012-09-19 01:45:30 -07:00
|
|
|
r.NodeImpl.value = r
|
2012-08-31 01:35:11 -07:00
|
|
|
return r
|
|
|
|
|
}
|
2012-09-14 01:17:13 -07:00
|
|
|
|
2012-12-22 16:26:02 -08:00
|
|
|
func (r *Rack) FindDataNode(ip string, port int) *DataNode {
|
2013-01-17 00:56:56 -08:00
|
|
|
for _, c := range r.Children() {
|
|
|
|
|
dn := c.(*DataNode)
|
|
|
|
|
if dn.MatchLocation(ip, port) {
|
|
|
|
|
return dn
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2012-12-22 16:26:02 -08:00
|
|
|
}
|
2019-04-04 19:27:00 -07:00
|
|
|
func (r *Rack) GetOrCreateDataNode(ip string, port int, publicUrl string, maxVolumeCount int64) *DataNode {
|
2012-09-16 19:18:37 -07:00
|
|
|
for _, c := range r.Children() {
|
|
|
|
|
dn := c.(*DataNode)
|
|
|
|
|
if dn.MatchLocation(ip, port) {
|
2012-09-17 01:48:09 -07:00
|
|
|
dn.LastSeen = time.Now().Unix()
|
2012-09-16 19:18:37 -07:00
|
|
|
return dn
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-09-22 20:46:31 -07:00
|
|
|
dn := NewDataNode(ip + ":" + strconv.Itoa(port))
|
2012-09-16 19:18:37 -07:00
|
|
|
dn.Ip = ip
|
|
|
|
|
dn.Port = port
|
|
|
|
|
dn.PublicUrl = publicUrl
|
|
|
|
|
dn.maxVolumeCount = maxVolumeCount
|
2012-09-17 01:48:09 -07:00
|
|
|
dn.LastSeen = time.Now().Unix()
|
2012-09-16 19:18:37 -07:00
|
|
|
r.LinkChildNode(dn)
|
|
|
|
|
return dn
|
2012-09-14 01:17:13 -07:00
|
|
|
}
|
2012-09-16 17:31:15 -07:00
|
|
|
|
2015-03-10 00:20:31 -07:00
|
|
|
func (r *Rack) ToMap() interface{} {
|
2012-09-16 19:18:37 -07:00
|
|
|
m := make(map[string]interface{})
|
2015-03-10 00:20:31 -07:00
|
|
|
m["Id"] = r.Id()
|
|
|
|
|
m["Max"] = r.GetMaxVolumeCount()
|
|
|
|
|
m["Free"] = r.FreeSpace()
|
2012-09-16 19:18:37 -07:00
|
|
|
var dns []interface{}
|
2015-03-10 00:20:31 -07:00
|
|
|
for _, c := range r.Children() {
|
2012-09-16 19:18:37 -07:00
|
|
|
dn := c.(*DataNode)
|
|
|
|
|
dns = append(dns, dn.ToMap())
|
|
|
|
|
}
|
|
|
|
|
m["DataNodes"] = dns
|
|
|
|
|
return m
|
2012-09-16 17:31:15 -07:00
|
|
|
}
|
2019-03-17 20:27:08 -07:00
|
|
|
|
|
|
|
|
func (r *Rack) ToRackInfo() *master_pb.RackInfo {
|
|
|
|
|
m := &master_pb.RackInfo{
|
|
|
|
|
Id: string(r.Id()),
|
|
|
|
|
VolumeCount: uint64(r.GetVolumeCount()),
|
|
|
|
|
MaxVolumeCount: uint64(r.GetMaxVolumeCount()),
|
|
|
|
|
FreeVolumeCount: uint64(r.FreeSpace()),
|
|
|
|
|
ActiveVolumeCount: uint64(r.GetActiveVolumeCount()),
|
2019-12-03 21:36:42 -08:00
|
|
|
RemoteVolumeCount: uint64(r.GetRemoteVolumeCount()),
|
2019-03-17 20:27:08 -07:00
|
|
|
}
|
|
|
|
|
for _, c := range r.Children() {
|
|
|
|
|
dn := c.(*DataNode)
|
|
|
|
|
m.DataNodeInfos = append(m.DataNodeInfos, dn.ToDataNodeInfo())
|
|
|
|
|
}
|
|
|
|
|
return m
|
|
|
|
|
}
|