topology_vacuum.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package topology
  2. import (
  3. "context"
  4. "io"
  5. "sync/atomic"
  6. "time"
  7. "github.com/seaweedfs/seaweedfs/weed/pb"
  8. "google.golang.org/grpc"
  9. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  10. "github.com/seaweedfs/seaweedfs/weed/glog"
  11. "github.com/seaweedfs/seaweedfs/weed/operation"
  12. "github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
  13. )
  14. func (t *Topology) batchVacuumVolumeCheck(grpcDialOption grpc.DialOption, vid needle.VolumeId,
  15. locationlist *VolumeLocationList, garbageThreshold float64) (*VolumeLocationList, bool) {
  16. ch := make(chan int, locationlist.Length())
  17. errCount := int32(0)
  18. for index, dn := range locationlist.list {
  19. go func(index int, url pb.ServerAddress, vid needle.VolumeId) {
  20. err := operation.WithVolumeServerClient(false, url, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  21. resp, err := volumeServerClient.VacuumVolumeCheck(context.Background(), &volume_server_pb.VacuumVolumeCheckRequest{
  22. VolumeId: uint32(vid),
  23. })
  24. if err != nil {
  25. atomic.AddInt32(&errCount, 1)
  26. ch <- -1
  27. return err
  28. }
  29. if resp.GarbageRatio >= garbageThreshold {
  30. ch <- index
  31. } else {
  32. ch <- -1
  33. }
  34. return nil
  35. })
  36. if err != nil {
  37. glog.V(0).Infof("Checking vacuuming %d on %s: %v", vid, url, err)
  38. }
  39. }(index, dn.ServerAddress(), vid)
  40. }
  41. vacuumLocationList := NewVolumeLocationList()
  42. waitTimeout := time.NewTimer(time.Minute * time.Duration(t.volumeSizeLimit/1024/1024/1000+1))
  43. defer waitTimeout.Stop()
  44. for range locationlist.list {
  45. select {
  46. case index := <-ch:
  47. if index != -1 {
  48. vacuumLocationList.list = append(vacuumLocationList.list, locationlist.list[index])
  49. }
  50. case <-waitTimeout.C:
  51. return vacuumLocationList, false
  52. }
  53. }
  54. return vacuumLocationList, errCount == 0 && len(vacuumLocationList.list) > 0
  55. }
  56. func (t *Topology) batchVacuumVolumeCompact(grpcDialOption grpc.DialOption, vl *VolumeLayout, vid needle.VolumeId,
  57. locationlist *VolumeLocationList, preallocate int64) bool {
  58. vl.accessLock.Lock()
  59. vl.removeFromWritable(vid)
  60. vl.accessLock.Unlock()
  61. ch := make(chan bool, locationlist.Length())
  62. for index, dn := range locationlist.list {
  63. go func(index int, url pb.ServerAddress, vid needle.VolumeId) {
  64. glog.V(0).Infoln(index, "Start vacuuming", vid, "on", url)
  65. err := operation.WithVolumeServerClient(true, url, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  66. stream, err := volumeServerClient.VacuumVolumeCompact(context.Background(), &volume_server_pb.VacuumVolumeCompactRequest{
  67. VolumeId: uint32(vid),
  68. Preallocate: preallocate,
  69. })
  70. if err != nil {
  71. return err
  72. }
  73. for {
  74. resp, recvErr := stream.Recv()
  75. if recvErr != nil {
  76. if recvErr == io.EOF {
  77. break
  78. } else {
  79. return recvErr
  80. }
  81. }
  82. glog.V(0).Infof("%d vacuum %d on %s processed %d bytes, loadAvg %.02f%%",
  83. index, vid, url, resp.ProcessedBytes, resp.LoadAvg_1M*100)
  84. }
  85. return nil
  86. })
  87. if err != nil {
  88. glog.Errorf("Error when vacuuming %d on %s: %v", vid, url, err)
  89. ch <- false
  90. } else {
  91. glog.V(0).Infof("Complete vacuuming %d on %s", vid, url)
  92. ch <- true
  93. }
  94. }(index, dn.ServerAddress(), vid)
  95. }
  96. isVacuumSuccess := true
  97. waitTimeout := time.NewTimer(3 * time.Minute * time.Duration(t.volumeSizeLimit/1024/1024/1000+1))
  98. defer waitTimeout.Stop()
  99. for range locationlist.list {
  100. select {
  101. case canCommit := <-ch:
  102. isVacuumSuccess = isVacuumSuccess && canCommit
  103. case <-waitTimeout.C:
  104. return false
  105. }
  106. }
  107. return isVacuumSuccess
  108. }
  109. func (t *Topology) batchVacuumVolumeCommit(grpcDialOption grpc.DialOption, vl *VolumeLayout, vid needle.VolumeId, vacuumLocationList, locationList *VolumeLocationList) bool {
  110. isCommitSuccess := true
  111. isReadOnly := false
  112. isFullCapacity := false
  113. for _, dn := range vacuumLocationList.list {
  114. glog.V(0).Infoln("Start Committing vacuum", vid, "on", dn.Url())
  115. err := operation.WithVolumeServerClient(false, dn.ServerAddress(), grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  116. resp, err := volumeServerClient.VacuumVolumeCommit(context.Background(), &volume_server_pb.VacuumVolumeCommitRequest{
  117. VolumeId: uint32(vid),
  118. })
  119. if resp != nil {
  120. if resp.IsReadOnly {
  121. isReadOnly = true
  122. }
  123. if resp.VolumeSize > t.volumeSizeLimit {
  124. isFullCapacity = true
  125. }
  126. }
  127. return err
  128. })
  129. if err != nil {
  130. glog.Errorf("Error when committing vacuum %d on %s: %v", vid, dn.Url(), err)
  131. isCommitSuccess = false
  132. } else {
  133. glog.V(0).Infof("Complete Committing vacuum %d on %s", vid, dn.Url())
  134. }
  135. }
  136. //we should check the status of all replicas
  137. if len(locationList.list) > len(vacuumLocationList.list) {
  138. for _, dn := range locationList.list {
  139. isFound := false
  140. for _, dnVaccum := range vacuumLocationList.list {
  141. if dn.id == dnVaccum.id {
  142. isFound = true
  143. break
  144. }
  145. }
  146. if !isFound {
  147. err := operation.WithVolumeServerClient(false, dn.ServerAddress(), grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  148. resp, err := volumeServerClient.VolumeStatus(context.Background(), &volume_server_pb.VolumeStatusRequest{
  149. VolumeId: uint32(vid),
  150. })
  151. if resp != nil {
  152. if resp.IsReadOnly {
  153. isReadOnly = true
  154. }
  155. if resp.VolumeSize > t.volumeSizeLimit {
  156. isFullCapacity = true
  157. }
  158. }
  159. return err
  160. })
  161. if err != nil {
  162. glog.Errorf("Error when checking volume %d status on %s: %v", vid, dn.Url(), err)
  163. //we mark volume read-only, since the volume state is unknown
  164. isReadOnly = true
  165. }
  166. }
  167. }
  168. }
  169. if isCommitSuccess {
  170. //record vacuum time of volume
  171. vl.accessLock.Lock()
  172. vl.vacuumedVolumes[vid] = time.Now()
  173. vl.accessLock.Unlock()
  174. for _, dn := range vacuumLocationList.list {
  175. vl.SetVolumeAvailable(dn, vid, isReadOnly, isFullCapacity)
  176. }
  177. }
  178. return isCommitSuccess
  179. }
  180. func (t *Topology) batchVacuumVolumeCleanup(grpcDialOption grpc.DialOption, vl *VolumeLayout, vid needle.VolumeId, locationlist *VolumeLocationList) {
  181. for _, dn := range locationlist.list {
  182. glog.V(0).Infoln("Start cleaning up", vid, "on", dn.Url())
  183. err := operation.WithVolumeServerClient(false, dn.ServerAddress(), grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  184. _, err := volumeServerClient.VacuumVolumeCleanup(context.Background(), &volume_server_pb.VacuumVolumeCleanupRequest{
  185. VolumeId: uint32(vid),
  186. })
  187. return err
  188. })
  189. if err != nil {
  190. glog.Errorf("Error when cleaning up vacuum %d on %s: %v", vid, dn.Url(), err)
  191. } else {
  192. glog.V(0).Infof("Complete cleaning up vacuum %d on %s", vid, dn.Url())
  193. }
  194. }
  195. }
  196. func (t *Topology) Vacuum(grpcDialOption grpc.DialOption, garbageThreshold float64, volumeId uint32, collection string, preallocate int64) {
  197. // if there is vacuum going on, return immediately
  198. swapped := atomic.CompareAndSwapInt64(&t.vacuumLockCounter, 0, 1)
  199. if !swapped {
  200. return
  201. }
  202. defer atomic.StoreInt64(&t.vacuumLockCounter, 0)
  203. // now only one vacuum process going on
  204. glog.V(1).Infof("Start vacuum on demand with threshold: %f collection: %s volumeId: %d",
  205. garbageThreshold, collection, volumeId)
  206. for _, col := range t.collectionMap.Items() {
  207. c := col.(*Collection)
  208. if collection != "" && collection != c.Name {
  209. continue
  210. }
  211. for _, vl := range c.storageType2VolumeLayout.Items() {
  212. if vl != nil {
  213. volumeLayout := vl.(*VolumeLayout)
  214. if volumeId > 0 {
  215. vid := needle.VolumeId(volumeId)
  216. volumeLayout.accessLock.RLock()
  217. locationList, ok := volumeLayout.vid2location[vid]
  218. volumeLayout.accessLock.RUnlock()
  219. if ok {
  220. t.vacuumOneVolumeId(grpcDialOption, volumeLayout, c, garbageThreshold, locationList, vid, preallocate)
  221. }
  222. } else {
  223. t.vacuumOneVolumeLayout(grpcDialOption, volumeLayout, c, garbageThreshold, preallocate)
  224. }
  225. }
  226. }
  227. }
  228. }
  229. func (t *Topology) vacuumOneVolumeLayout(grpcDialOption grpc.DialOption, volumeLayout *VolumeLayout, c *Collection, garbageThreshold float64, preallocate int64) {
  230. volumeLayout.accessLock.RLock()
  231. tmpMap := make(map[needle.VolumeId]*VolumeLocationList)
  232. for vid, locationList := range volumeLayout.vid2location {
  233. tmpMap[vid] = locationList.Copy()
  234. }
  235. volumeLayout.accessLock.RUnlock()
  236. for vid, locationList := range tmpMap {
  237. t.vacuumOneVolumeId(grpcDialOption, volumeLayout, c, garbageThreshold, locationList, vid, preallocate)
  238. }
  239. }
  240. func (t *Topology) vacuumOneVolumeId(grpcDialOption grpc.DialOption, volumeLayout *VolumeLayout, c *Collection, garbageThreshold float64, locationList *VolumeLocationList, vid needle.VolumeId, preallocate int64) {
  241. volumeLayout.accessLock.RLock()
  242. isReadOnly := volumeLayout.readonlyVolumes.IsTrue(vid)
  243. isEnoughCopies := volumeLayout.enoughCopies(vid)
  244. volumeLayout.accessLock.RUnlock()
  245. if isReadOnly || !isEnoughCopies {
  246. return
  247. }
  248. glog.V(1).Infof("check vacuum on collection:%s volume:%d", c.Name, vid)
  249. if vacuumLocationList, needVacuum := t.batchVacuumVolumeCheck(
  250. grpcDialOption, vid, locationList, garbageThreshold); needVacuum {
  251. if t.batchVacuumVolumeCompact(grpcDialOption, volumeLayout, vid, vacuumLocationList, preallocate) {
  252. t.batchVacuumVolumeCommit(grpcDialOption, volumeLayout, vid, vacuumLocationList, locationList)
  253. } else {
  254. t.batchVacuumVolumeCleanup(grpcDialOption, volumeLayout, vid, vacuumLocationList)
  255. }
  256. }
  257. }