volume_location_list.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package topology
  2. import (
  3. "fmt"
  4. )
  5. type VolumeLocationList struct {
  6. list []*DataNode
  7. }
  8. func NewVolumeLocationList() *VolumeLocationList {
  9. return &VolumeLocationList{}
  10. }
  11. func (dnll *VolumeLocationList) String() string {
  12. return fmt.Sprintf("%v", dnll.list)
  13. }
  14. func (dnll *VolumeLocationList) Head() *DataNode {
  15. //mark first node as master volume
  16. return dnll.list[0]
  17. }
  18. func (dnll *VolumeLocationList) Length() int {
  19. return len(dnll.list)
  20. }
  21. func (dnll *VolumeLocationList) Set(loc *DataNode) {
  22. for i := 0; i < len(dnll.list); i++ {
  23. if loc.Ip == dnll.list[i].Ip && loc.Port == dnll.list[i].Port {
  24. dnll.list[i] = loc
  25. return
  26. }
  27. }
  28. dnll.list = append(dnll.list, loc)
  29. }
  30. func (dnll *VolumeLocationList) Remove(loc *DataNode) bool {
  31. for i, dnl := range dnll.list {
  32. if loc.Ip == dnl.Ip && loc.Port == dnl.Port {
  33. dnll.list = append(dnll.list[:i], dnll.list[i+1:]...)
  34. return true
  35. }
  36. }
  37. return false
  38. }
  39. func (dnll *VolumeLocationList) Refresh(freshThreshHold int64) {
  40. var changed bool
  41. for _, dnl := range dnll.list {
  42. if dnl.LastSeen < freshThreshHold {
  43. changed = true
  44. break
  45. }
  46. }
  47. if changed {
  48. var l []*DataNode
  49. for _, dnl := range dnll.list {
  50. if dnl.LastSeen >= freshThreshHold {
  51. l = append(l, dnl)
  52. }
  53. }
  54. dnll.list = l
  55. }
  56. }