topology_event_handling.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package topology
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/stats"
  4. "github.com/seaweedfs/seaweedfs/weed/storage/erasure_coding"
  5. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  6. "google.golang.org/grpc"
  7. "math/rand"
  8. "time"
  9. "github.com/seaweedfs/seaweedfs/weed/glog"
  10. "github.com/seaweedfs/seaweedfs/weed/storage"
  11. )
  12. func (t *Topology) StartRefreshWritableVolumes(grpcDialOption grpc.DialOption, garbageThreshold float64, growThreshold float64, preallocate int64) {
  13. go func() {
  14. for {
  15. if t.IsLeader() {
  16. freshThreshHold := time.Now().Unix() - 3*t.pulse //3 times of sleep interval
  17. t.CollectDeadNodeAndFullVolumes(freshThreshHold, t.volumeSizeLimit, growThreshold)
  18. }
  19. time.Sleep(time.Duration(float32(t.pulse*1e3)*(1+rand.Float32())) * time.Millisecond)
  20. }
  21. }()
  22. go func(garbageThreshold float64) {
  23. for {
  24. if t.IsLeader() {
  25. t.Vacuum(grpcDialOption, garbageThreshold, 0, "", preallocate)
  26. } else {
  27. stats.MasterReplicaPlacementMismatch.Reset()
  28. }
  29. time.Sleep(14*time.Minute + time.Duration(120*rand.Float32())*time.Second)
  30. }
  31. }(garbageThreshold)
  32. go func() {
  33. for {
  34. select {
  35. case fv := <-t.chanFullVolumes:
  36. t.SetVolumeCapacityFull(fv)
  37. case cv := <-t.chanCrowdedVolumes:
  38. t.SetVolumeCrowded(cv)
  39. }
  40. }
  41. }()
  42. }
  43. func (t *Topology) SetVolumeCapacityFull(volumeInfo storage.VolumeInfo) bool {
  44. diskType := types.ToDiskType(volumeInfo.DiskType)
  45. vl := t.GetVolumeLayout(volumeInfo.Collection, volumeInfo.ReplicaPlacement, volumeInfo.Ttl, diskType)
  46. if !vl.SetVolumeCapacityFull(volumeInfo.Id) {
  47. return false
  48. }
  49. vl.accessLock.RLock()
  50. defer vl.accessLock.RUnlock()
  51. vidLocations, found := vl.vid2location[volumeInfo.Id]
  52. if !found {
  53. return false
  54. }
  55. for _, dn := range vidLocations.list {
  56. if !volumeInfo.ReadOnly {
  57. disk := dn.getOrCreateDisk(volumeInfo.DiskType)
  58. deltaDiskUsages := newDiskUsages()
  59. deltaDiskUsage := deltaDiskUsages.getOrCreateDisk(types.ToDiskType(volumeInfo.DiskType))
  60. deltaDiskUsage.activeVolumeCount = -1
  61. disk.UpAdjustDiskUsageDelta(deltaDiskUsages)
  62. }
  63. }
  64. return true
  65. }
  66. func (t *Topology) SetVolumeCrowded(volumeInfo storage.VolumeInfo) {
  67. diskType := types.ToDiskType(volumeInfo.DiskType)
  68. vl := t.GetVolumeLayout(volumeInfo.Collection, volumeInfo.ReplicaPlacement, volumeInfo.Ttl, diskType)
  69. vl.SetVolumeCrowded(volumeInfo.Id)
  70. }
  71. func (t *Topology) UnRegisterDataNode(dn *DataNode) {
  72. for _, v := range dn.GetVolumes() {
  73. glog.V(0).Infoln("Removing Volume", v.Id, "from the dead volume server", dn.Id())
  74. diskType := types.ToDiskType(v.DiskType)
  75. vl := t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl, diskType)
  76. vl.SetVolumeUnavailable(dn, v.Id)
  77. }
  78. negativeUsages := dn.GetDiskUsages().negative()
  79. dn.UpAdjustDiskUsageDelta(negativeUsages)
  80. dn.DeltaUpdateVolumes([]storage.VolumeInfo{}, dn.GetVolumes())
  81. dn.DeltaUpdateEcShards([]*erasure_coding.EcVolumeInfo{}, dn.GetEcShards())
  82. if dn.Parent() != nil {
  83. dn.Parent().UnlinkChildNode(dn.Id())
  84. }
  85. }