topology_event_handling.go 2.6 KB

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