volume_location_list.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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) Copy() *VolumeLocationList {
  16. list := make([]*DataNode, len(dnll.list))
  17. copy(list, dnll.list)
  18. return &VolumeLocationList{
  19. list: list,
  20. }
  21. }
  22. func (dnll *VolumeLocationList) Head() *DataNode {
  23. //mark first node as master volume
  24. return dnll.list[0]
  25. }
  26. func (dnll *VolumeLocationList) Rest() []*DataNode {
  27. //mark first node as master volume
  28. return dnll.list[1:]
  29. }
  30. func (dnll *VolumeLocationList) Length() int {
  31. if dnll == nil {
  32. return 0
  33. }
  34. return len(dnll.list)
  35. }
  36. func (dnll *VolumeLocationList) Set(loc *DataNode) {
  37. for i := 0; i < len(dnll.list); i++ {
  38. if loc.Ip == dnll.list[i].Ip && loc.Port == dnll.list[i].Port {
  39. dnll.list[i] = loc
  40. return
  41. }
  42. }
  43. dnll.list = append(dnll.list, loc)
  44. }
  45. func (dnll *VolumeLocationList) Remove(loc *DataNode) bool {
  46. for i, dnl := range dnll.list {
  47. if loc.Ip == dnl.Ip && loc.Port == dnl.Port {
  48. dnll.list = append(dnll.list[:i], dnll.list[i+1:]...)
  49. return true
  50. }
  51. }
  52. return false
  53. }
  54. func (dnll *VolumeLocationList) Refresh(freshThreshHold int64) {
  55. var changed bool
  56. for _, dnl := range dnll.list {
  57. if dnl.LastSeen < freshThreshHold {
  58. changed = true
  59. break
  60. }
  61. }
  62. if changed {
  63. var l []*DataNode
  64. for _, dnl := range dnll.list {
  65. if dnl.LastSeen >= freshThreshHold {
  66. l = append(l, dnl)
  67. }
  68. }
  69. dnll.list = l
  70. }
  71. }
  72. func (dnll *VolumeLocationList) Stats(vid needle.VolumeId, freshThreshHold int64) (size uint64, fileCount int) {
  73. for _, dnl := range dnll.list {
  74. if dnl.LastSeen < freshThreshHold {
  75. vinfo, err := dnl.GetVolumesById(vid)
  76. if err == nil {
  77. return (vinfo.Size - vinfo.DeletedByteCount) * uint64(len(dnll.list)), vinfo.FileCount - vinfo.DeleteCount
  78. }
  79. }
  80. }
  81. return 0, 0
  82. }