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.

87 lines
1.8 KiB

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