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.

284 lines
9.2 KiB

6 years ago
6 years ago
4 years ago
6 years ago
3 years ago
3 years ago
6 years ago
  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. for _, dn := range vacuumLocationList.list {
  113. glog.V(0).Infoln("Start Committing vacuum", vid, "on", dn.Url())
  114. err := operation.WithVolumeServerClient(false, dn.ServerAddress(), grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  115. resp, err := volumeServerClient.VacuumVolumeCommit(context.Background(), &volume_server_pb.VacuumVolumeCommitRequest{
  116. VolumeId: uint32(vid),
  117. })
  118. if resp != nil && resp.IsReadOnly {
  119. isReadOnly = true
  120. }
  121. return err
  122. })
  123. if err != nil {
  124. glog.Errorf("Error when committing vacuum %d on %s: %v", vid, dn.Url(), err)
  125. isCommitSuccess = false
  126. } else {
  127. glog.V(0).Infof("Complete Committing vacuum %d on %s", vid, dn.Url())
  128. }
  129. }
  130. //we should check the status of all replicas
  131. if len(locationList.list) > len(vacuumLocationList.list) {
  132. for _, dn := range locationList.list {
  133. isFound := false
  134. for _, dnVaccum := range vacuumLocationList.list {
  135. if dn.id == dnVaccum.id {
  136. isFound = true
  137. break
  138. }
  139. }
  140. if !isFound {
  141. err := operation.WithVolumeServerClient(false, dn.ServerAddress(), grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  142. resp, err := volumeServerClient.VolumeStatus(context.Background(), &volume_server_pb.VolumeStatusRequest{
  143. VolumeId: uint32(vid),
  144. })
  145. if resp != nil && resp.IsReadOnly {
  146. isReadOnly = true
  147. }
  148. return err
  149. })
  150. if err != nil {
  151. glog.Errorf("Error when checking volume %d status on %s: %v", vid, dn.Url(), err)
  152. //we mark volume read-only, since the volume state is unknown
  153. isReadOnly = true
  154. }
  155. }
  156. }
  157. }
  158. if isCommitSuccess {
  159. //reset all vacuumed volumes size
  160. for _, dn := range vacuumLocationList.list {
  161. vInfo, err := dn.GetVolumesById(vid)
  162. if err != nil {
  163. glog.V(0).Infof("get volume info for volume: %d failed %v", vid, err)
  164. return false
  165. }
  166. dn.Lock()
  167. disk := dn.getOrCreateDisk(vInfo.DiskType)
  168. vInfo.Size = 0
  169. disk.doAddOrUpdateVolume(vInfo)
  170. dn.Unlock()
  171. }
  172. for _, dn := range vacuumLocationList.list {
  173. vl.SetVolumeAvailable(dn, vid, isReadOnly)
  174. }
  175. }
  176. return isCommitSuccess
  177. }
  178. func (t *Topology) batchVacuumVolumeCleanup(grpcDialOption grpc.DialOption, vl *VolumeLayout, vid needle.VolumeId, locationlist *VolumeLocationList) {
  179. for _, dn := range locationlist.list {
  180. glog.V(0).Infoln("Start cleaning up", vid, "on", dn.Url())
  181. err := operation.WithVolumeServerClient(false, dn.ServerAddress(), grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  182. _, err := volumeServerClient.VacuumVolumeCleanup(context.Background(), &volume_server_pb.VacuumVolumeCleanupRequest{
  183. VolumeId: uint32(vid),
  184. })
  185. return err
  186. })
  187. if err != nil {
  188. glog.Errorf("Error when cleaning up vacuum %d on %s: %v", vid, dn.Url(), err)
  189. } else {
  190. glog.V(0).Infof("Complete cleaning up vacuum %d on %s", vid, dn.Url())
  191. }
  192. }
  193. }
  194. func (t *Topology) Vacuum(grpcDialOption grpc.DialOption, garbageThreshold float64, volumeId uint32, collection string, preallocate int64) {
  195. // if there is vacuum going on, return immediately
  196. swapped := atomic.CompareAndSwapInt64(&t.vacuumLockCounter, 0, 1)
  197. if !swapped {
  198. return
  199. }
  200. defer atomic.StoreInt64(&t.vacuumLockCounter, 0)
  201. // now only one vacuum process going on
  202. glog.V(1).Infof("Start vacuum on demand with threshold: %f collection: %s volumeId: %d",
  203. garbageThreshold, collection, volumeId)
  204. for _, col := range t.collectionMap.Items() {
  205. c := col.(*Collection)
  206. if collection != "" && collection != c.Name {
  207. continue
  208. }
  209. for _, vl := range c.storageType2VolumeLayout.Items() {
  210. if vl != nil {
  211. volumeLayout := vl.(*VolumeLayout)
  212. if volumeId > 0 {
  213. vid := needle.VolumeId(volumeId)
  214. volumeLayout.accessLock.RLock()
  215. locationList, ok := volumeLayout.vid2location[vid]
  216. volumeLayout.accessLock.RUnlock()
  217. if ok {
  218. t.vacuumOneVolumeId(grpcDialOption, volumeLayout, c, garbageThreshold, locationList, vid, preallocate)
  219. }
  220. } else {
  221. t.vacuumOneVolumeLayout(grpcDialOption, volumeLayout, c, garbageThreshold, preallocate)
  222. }
  223. }
  224. }
  225. }
  226. }
  227. func (t *Topology) vacuumOneVolumeLayout(grpcDialOption grpc.DialOption, volumeLayout *VolumeLayout, c *Collection, garbageThreshold float64, preallocate int64) {
  228. volumeLayout.accessLock.RLock()
  229. tmpMap := make(map[needle.VolumeId]*VolumeLocationList)
  230. for vid, locationList := range volumeLayout.vid2location {
  231. tmpMap[vid] = locationList.Copy()
  232. }
  233. volumeLayout.accessLock.RUnlock()
  234. for vid, locationList := range tmpMap {
  235. t.vacuumOneVolumeId(grpcDialOption, volumeLayout, c, garbageThreshold, locationList, vid, preallocate)
  236. }
  237. }
  238. func (t *Topology) vacuumOneVolumeId(grpcDialOption grpc.DialOption, volumeLayout *VolumeLayout, c *Collection, garbageThreshold float64, locationList *VolumeLocationList, vid needle.VolumeId, preallocate int64) {
  239. volumeLayout.accessLock.RLock()
  240. isReadOnly := volumeLayout.readonlyVolumes.IsTrue(vid)
  241. isEnoughCopies := volumeLayout.enoughCopies(vid)
  242. volumeLayout.accessLock.RUnlock()
  243. if isReadOnly || !isEnoughCopies {
  244. return
  245. }
  246. glog.V(1).Infof("check vacuum on collection:%s volume:%d", c.Name, vid)
  247. if vacuumLocationList, needVacuum := t.batchVacuumVolumeCheck(
  248. grpcDialOption, vid, locationList, garbageThreshold); needVacuum {
  249. if t.batchVacuumVolumeCompact(grpcDialOption, volumeLayout, vid, vacuumLocationList, preallocate) {
  250. t.batchVacuumVolumeCommit(grpcDialOption, volumeLayout, vid, vacuumLocationList, locationList)
  251. } else {
  252. t.batchVacuumVolumeCleanup(grpcDialOption, volumeLayout, vid, vacuumLocationList)
  253. }
  254. }
  255. }