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.

323 lines
7.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package storage
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "sync"
  9. "time"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/stats"
  12. "github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
  13. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  14. )
  15. type DiskLocation struct {
  16. Directory string
  17. MaxVolumeCount int
  18. MinFreeSpacePercent float32
  19. volumes map[needle.VolumeId]*Volume
  20. volumesLock sync.RWMutex
  21. // erasure coding
  22. ecVolumes map[needle.VolumeId]*erasure_coding.EcVolume
  23. ecVolumesLock sync.RWMutex
  24. isDiskSpaceLow bool
  25. }
  26. func NewDiskLocation(dir string, maxVolumeCount int, minFreeSpacePercent float32) *DiskLocation {
  27. location := &DiskLocation{Directory: dir, MaxVolumeCount: maxVolumeCount, MinFreeSpacePercent: minFreeSpacePercent}
  28. location.volumes = make(map[needle.VolumeId]*Volume)
  29. location.ecVolumes = make(map[needle.VolumeId]*erasure_coding.EcVolume)
  30. go location.CheckDiskSpace()
  31. return location
  32. }
  33. func (l *DiskLocation) volumeIdFromPath(dir os.FileInfo) (needle.VolumeId, string, error) {
  34. name := dir.Name()
  35. if !dir.IsDir() && strings.HasSuffix(name, ".idx") {
  36. base := name[:len(name)-len(".idx")]
  37. collection, volumeId, err := parseCollectionVolumeId(base)
  38. return volumeId, collection, err
  39. }
  40. return 0, "", fmt.Errorf("Path is not a volume: %s", name)
  41. }
  42. func parseCollectionVolumeId(base string) (collection string, vid needle.VolumeId, err error) {
  43. i := strings.LastIndex(base, "_")
  44. if i > 0 {
  45. collection, base = base[0:i], base[i+1:]
  46. }
  47. vol, err := needle.NewVolumeId(base)
  48. return collection, vol, err
  49. }
  50. func (l *DiskLocation) loadExistingVolume(fileInfo os.FileInfo, needleMapKind NeedleMapType) bool {
  51. name := fileInfo.Name()
  52. if !fileInfo.IsDir() && strings.HasSuffix(name, ".idx") {
  53. vid, collection, err := l.volumeIdFromPath(fileInfo)
  54. if err != nil {
  55. glog.Warningf("get volume id failed, %s, err : %s", name, err)
  56. return false
  57. }
  58. // void loading one volume more than once
  59. l.volumesLock.RLock()
  60. _, found := l.volumes[vid]
  61. l.volumesLock.RUnlock()
  62. if found {
  63. glog.V(1).Infof("loaded volume, %v", vid)
  64. return true
  65. }
  66. v, e := NewVolume(l.Directory, collection, vid, needleMapKind, nil, nil, 0, 0)
  67. if e != nil {
  68. glog.V(0).Infof("new volume %s error %s", name, e)
  69. return false
  70. }
  71. l.SetVolume(vid, v)
  72. size, _, _ := v.FileStat()
  73. glog.V(0).Infof("data file %s, replicaPlacement=%s v=%d size=%d ttl=%s",
  74. l.Directory+"/"+name, v.ReplicaPlacement, v.Version(), size, v.Ttl.String())
  75. return true
  76. }
  77. return false
  78. }
  79. func (l *DiskLocation) concurrentLoadingVolumes(needleMapKind NeedleMapType, concurrency int) {
  80. task_queue := make(chan os.FileInfo, 10*concurrency)
  81. go func() {
  82. if dirs, err := ioutil.ReadDir(l.Directory); err == nil {
  83. for _, dir := range dirs {
  84. task_queue <- dir
  85. }
  86. }
  87. close(task_queue)
  88. }()
  89. var wg sync.WaitGroup
  90. for workerNum := 0; workerNum < concurrency; workerNum++ {
  91. wg.Add(1)
  92. go func() {
  93. defer wg.Done()
  94. for dir := range task_queue {
  95. _ = l.loadExistingVolume(dir, needleMapKind)
  96. }
  97. }()
  98. }
  99. wg.Wait()
  100. }
  101. func (l *DiskLocation) loadExistingVolumes(needleMapKind NeedleMapType) {
  102. l.concurrentLoadingVolumes(needleMapKind, 10)
  103. glog.V(0).Infof("Store started on dir: %s with %d volumes max %d", l.Directory, len(l.volumes), l.MaxVolumeCount)
  104. l.loadAllEcShards()
  105. glog.V(0).Infof("Store started on dir: %s with %d ec shards", l.Directory, len(l.ecVolumes))
  106. }
  107. func (l *DiskLocation) DeleteCollectionFromDiskLocation(collection string) (e error) {
  108. l.volumesLock.Lock()
  109. delVolsMap := l.unmountVolumeByCollection(collection)
  110. l.volumesLock.Unlock()
  111. l.ecVolumesLock.Lock()
  112. delEcVolsMap := l.unmountEcVolumeByCollection(collection)
  113. l.ecVolumesLock.Unlock()
  114. errChain := make(chan error, 2)
  115. var wg sync.WaitGroup
  116. wg.Add(2)
  117. go func() {
  118. for _, v := range delVolsMap {
  119. if err := v.Destroy(); err != nil {
  120. errChain <- err
  121. }
  122. }
  123. wg.Done()
  124. }()
  125. go func() {
  126. for _, v := range delEcVolsMap {
  127. v.Destroy()
  128. }
  129. wg.Done()
  130. }()
  131. go func() {
  132. wg.Wait()
  133. close(errChain)
  134. }()
  135. errBuilder := strings.Builder{}
  136. for err := range errChain {
  137. errBuilder.WriteString(err.Error())
  138. errBuilder.WriteString("; ")
  139. }
  140. if errBuilder.Len() > 0 {
  141. e = fmt.Errorf(errBuilder.String())
  142. }
  143. return
  144. }
  145. func (l *DiskLocation) deleteVolumeById(vid needle.VolumeId) (found bool, e error) {
  146. l.volumesLock.Lock()
  147. defer l.volumesLock.Unlock()
  148. v, ok := l.volumes[vid]
  149. if !ok {
  150. return
  151. }
  152. e = v.Destroy()
  153. if e != nil {
  154. return
  155. }
  156. found = true
  157. delete(l.volumes, vid)
  158. return
  159. }
  160. func (l *DiskLocation) LoadVolume(vid needle.VolumeId, needleMapKind NeedleMapType) bool {
  161. if fileInfo, found := l.LocateVolume(vid); found {
  162. return l.loadExistingVolume(fileInfo, needleMapKind)
  163. }
  164. return false
  165. }
  166. func (l *DiskLocation) DeleteVolume(vid needle.VolumeId) error {
  167. l.volumesLock.Lock()
  168. defer l.volumesLock.Unlock()
  169. _, ok := l.volumes[vid]
  170. if !ok {
  171. return fmt.Errorf("Volume not found, VolumeId: %d", vid)
  172. }
  173. _, err := l.deleteVolumeById(vid)
  174. return err
  175. }
  176. func (l *DiskLocation) UnloadVolume(vid needle.VolumeId) error {
  177. l.volumesLock.Lock()
  178. defer l.volumesLock.Unlock()
  179. v, ok := l.volumes[vid]
  180. if !ok {
  181. return fmt.Errorf("Volume not loaded, VolumeId: %d", vid)
  182. }
  183. v.Close()
  184. delete(l.volumes, vid)
  185. return nil
  186. }
  187. func (l *DiskLocation) unmountVolumeByCollection(collectionName string) map[needle.VolumeId]*Volume {
  188. deltaVols := make(map[needle.VolumeId]*Volume, 0)
  189. for k, v := range l.volumes {
  190. if v.Collection == collectionName && !v.isCompacting {
  191. deltaVols[k] = v
  192. }
  193. }
  194. for k := range deltaVols {
  195. delete(l.volumes, k)
  196. }
  197. return deltaVols
  198. }
  199. func (l *DiskLocation) SetVolume(vid needle.VolumeId, volume *Volume) {
  200. l.volumesLock.Lock()
  201. defer l.volumesLock.Unlock()
  202. l.volumes[vid] = volume
  203. volume.location = l
  204. }
  205. func (l *DiskLocation) FindVolume(vid needle.VolumeId) (*Volume, bool) {
  206. l.volumesLock.RLock()
  207. defer l.volumesLock.RUnlock()
  208. v, ok := l.volumes[vid]
  209. return v, ok
  210. }
  211. func (l *DiskLocation) VolumesLen() int {
  212. l.volumesLock.RLock()
  213. defer l.volumesLock.RUnlock()
  214. return len(l.volumes)
  215. }
  216. func (l *DiskLocation) Close() {
  217. l.volumesLock.Lock()
  218. for _, v := range l.volumes {
  219. v.Close()
  220. }
  221. l.volumesLock.Unlock()
  222. l.ecVolumesLock.Lock()
  223. for _, ecVolume := range l.ecVolumes {
  224. ecVolume.Close()
  225. }
  226. l.ecVolumesLock.Unlock()
  227. return
  228. }
  229. func (l *DiskLocation) LocateVolume(vid needle.VolumeId) (os.FileInfo, bool) {
  230. if fileInfos, err := ioutil.ReadDir(l.Directory); err == nil {
  231. for _, fileInfo := range fileInfos {
  232. volId, _, err := l.volumeIdFromPath(fileInfo)
  233. if vid == volId && err == nil {
  234. return fileInfo, true
  235. }
  236. }
  237. }
  238. return nil, false
  239. }
  240. func (l *DiskLocation) UnUsedSpace(volumeSizeLimit uint64) (unUsedSpace uint64) {
  241. l.volumesLock.RLock()
  242. defer l.volumesLock.RUnlock()
  243. for _, vol := range l.volumes {
  244. if vol.IsReadOnly() {
  245. continue
  246. }
  247. datSize, idxSize, _ := vol.FileStat()
  248. unUsedSpace += volumeSizeLimit - (datSize + idxSize)
  249. }
  250. return
  251. }
  252. func (l *DiskLocation) CheckDiskSpace() {
  253. for {
  254. if dir, e := filepath.Abs(l.Directory); e == nil {
  255. s := stats.NewDiskStatus(dir)
  256. if (s.PercentFree < l.MinFreeSpacePercent) != l.isDiskSpaceLow {
  257. l.isDiskSpaceLow = !l.isDiskSpaceLow
  258. }
  259. if l.isDiskSpaceLow {
  260. glog.V(0).Infof("dir %s freePercent %.2f%% < min %.2f%%, isLowDiskSpace: %v", dir, s.PercentFree, l.MinFreeSpacePercent, l.isDiskSpaceLow)
  261. } else {
  262. glog.V(4).Infof("dir %s freePercent %.2f%% < min %.2f%%, isLowDiskSpace: %v", dir, s.PercentFree, l.MinFreeSpacePercent, l.isDiskSpaceLow)
  263. }
  264. }
  265. time.Sleep(time.Minute)
  266. }
  267. }