volume_location_list.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package topology
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  5. )
  6. type VolumeLocationList struct {
  7. list []*DataNode
  8. }
  9. func NewVolumeLocationList() *VolumeLocationList {
  10. return &VolumeLocationList{}
  11. }
  12. func (dnll *VolumeLocationList) String() string {
  13. return fmt.Sprintf("%v", dnll.list)
  14. }
  15. func (dnll *VolumeLocationList) Head() *DataNode {
  16. //mark first node as master volume
  17. return dnll.list[0]
  18. }
  19. func (dnll *VolumeLocationList) Length() int {
  20. return len(dnll.list)
  21. }
  22. func (dnll *VolumeLocationList) Set(loc *DataNode) {
  23. for i := 0; i < len(dnll.list); i++ {
  24. if loc.Ip == dnll.list[i].Ip && loc.Port == dnll.list[i].Port {
  25. dnll.list[i] = loc
  26. return
  27. }
  28. }
  29. dnll.list = append(dnll.list, loc)
  30. }
  31. func (dnll *VolumeLocationList) Remove(loc *DataNode) bool {
  32. for i, dnl := range dnll.list {
  33. if loc.Ip == dnl.Ip && loc.Port == dnl.Port {
  34. dnll.list = append(dnll.list[:i], dnll.list[i+1:]...)
  35. return true
  36. }
  37. }
  38. return false
  39. }
  40. func (dnll *VolumeLocationList) Refresh(freshThreshHold int64) {
  41. var changed bool
  42. for _, dnl := range dnll.list {
  43. if dnl.LastSeen < freshThreshHold {
  44. changed = true
  45. break
  46. }
  47. }
  48. if changed {
  49. var l []*DataNode
  50. for _, dnl := range dnll.list {
  51. if dnl.LastSeen >= freshThreshHold {
  52. l = append(l, dnl)
  53. }
  54. }
  55. dnll.list = l
  56. }
  57. }
  58. func (dnll *VolumeLocationList) Stats(vid needle.VolumeId, freshThreshHold int64) (size uint64, fileCount int) {
  59. for _, dnl := range dnll.list {
  60. if dnl.LastSeen < freshThreshHold {
  61. vinfo, err := dnl.GetVolumesById(vid)
  62. if err == nil {
  63. return vinfo.Size - vinfo.DeletedByteCount, vinfo.FileCount - vinfo.DeleteCount
  64. }
  65. }
  66. }
  67. return 0, 0
  68. }