2012-09-10 07:18:07 +00:00
|
|
|
package topology
|
|
|
|
|
2015-01-08 07:54:50 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2018-11-23 08:24:51 +00:00
|
|
|
|
2019-09-12 13:18:21 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
2015-01-08 07:54:50 +00:00
|
|
|
)
|
|
|
|
|
2012-09-19 23:48:04 +00:00
|
|
|
type VolumeLocationList struct {
|
2012-09-11 00:08:52 +00:00
|
|
|
list []*DataNode
|
2012-09-10 07:18:07 +00:00
|
|
|
}
|
|
|
|
|
2012-09-19 23:48:04 +00:00
|
|
|
func NewVolumeLocationList() *VolumeLocationList {
|
|
|
|
return &VolumeLocationList{}
|
2012-09-10 07:18:07 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 07:54:50 +00:00
|
|
|
func (dnll *VolumeLocationList) String() string {
|
|
|
|
return fmt.Sprintf("%v", dnll.list)
|
|
|
|
}
|
|
|
|
|
2012-09-19 23:48:04 +00:00
|
|
|
func (dnll *VolumeLocationList) Head() *DataNode {
|
2015-11-29 15:49:41 +00:00
|
|
|
//mark first node as master volume
|
2012-09-17 00:31:15 +00:00
|
|
|
return dnll.list[0]
|
|
|
|
}
|
|
|
|
|
2012-09-19 23:48:04 +00:00
|
|
|
func (dnll *VolumeLocationList) Length() int {
|
2013-01-17 08:56:56 +00:00
|
|
|
return len(dnll.list)
|
2012-09-19 23:48:04 +00:00
|
|
|
}
|
|
|
|
|
2014-03-19 11:48:13 +00:00
|
|
|
func (dnll *VolumeLocationList) Set(loc *DataNode) {
|
|
|
|
for i := 0; i < len(dnll.list); i++ {
|
|
|
|
if loc.Ip == dnll.list[i].Ip && loc.Port == dnll.list[i].Port {
|
|
|
|
dnll.list[i] = loc
|
|
|
|
return
|
2012-09-11 00:08:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
dnll.list = append(dnll.list, loc)
|
|
|
|
}
|
2012-11-07 09:51:43 +00:00
|
|
|
|
2012-09-19 23:48:04 +00:00
|
|
|
func (dnll *VolumeLocationList) Remove(loc *DataNode) bool {
|
2013-01-17 08:56:56 +00:00
|
|
|
for i, dnl := range dnll.list {
|
|
|
|
if loc.Ip == dnl.Ip && loc.Port == dnl.Port {
|
|
|
|
dnll.list = append(dnll.list[:i], dnll.list[i+1:]...)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2012-09-19 23:48:04 +00:00
|
|
|
}
|
2012-09-11 00:08:52 +00:00
|
|
|
|
2012-09-19 23:48:04 +00:00
|
|
|
func (dnll *VolumeLocationList) Refresh(freshThreshHold int64) {
|
2012-09-11 00:08:52 +00:00
|
|
|
var changed bool
|
|
|
|
for _, dnl := range dnll.list {
|
2012-09-17 08:48:09 +00:00
|
|
|
if dnl.LastSeen < freshThreshHold {
|
2012-09-11 00:08:52 +00:00
|
|
|
changed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if changed {
|
|
|
|
var l []*DataNode
|
|
|
|
for _, dnl := range dnll.list {
|
2012-09-17 08:48:09 +00:00
|
|
|
if dnl.LastSeen >= freshThreshHold {
|
2012-09-11 00:08:52 +00:00
|
|
|
l = append(l, dnl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dnll.list = l
|
|
|
|
}
|
2012-09-10 07:18:07 +00:00
|
|
|
}
|
2018-11-23 08:24:51 +00:00
|
|
|
|
2019-04-19 04:43:36 +00:00
|
|
|
func (dnll *VolumeLocationList) Stats(vid needle.VolumeId, freshThreshHold int64) (size uint64, fileCount int) {
|
2018-11-23 08:24:51 +00:00
|
|
|
for _, dnl := range dnll.list {
|
|
|
|
if dnl.LastSeen < freshThreshHold {
|
|
|
|
vinfo, err := dnl.GetVolumesById(vid)
|
|
|
|
if err == nil {
|
|
|
|
return vinfo.Size - vinfo.DeletedByteCount, vinfo.FileCount - vinfo.DeleteCount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, 0
|
|
|
|
}
|