volume_location_list.go 1.9 KB

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