You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.1 KiB

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