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.

295 lines
6.7 KiB

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