volume_location_list.go 2.0 KB

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