disk_location.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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) (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. delete(l.volumes, vid)
  151. return
  152. }
  153. func (l *DiskLocation) LoadVolume(vid needle.VolumeId, needleMapKind NeedleMapType) bool {
  154. if fileInfo, found := l.LocateVolume(vid); found {
  155. return l.loadExistingVolume(fileInfo, needleMapKind)
  156. }
  157. return false
  158. }
  159. func (l *DiskLocation) DeleteVolume(vid needle.VolumeId) error {
  160. l.volumesLock.Lock()
  161. defer l.volumesLock.Unlock()
  162. _, ok := l.volumes[vid]
  163. if !ok {
  164. return fmt.Errorf("Volume not found, VolumeId: %d", vid)
  165. }
  166. return l.deleteVolumeById(vid)
  167. }
  168. func (l *DiskLocation) UnloadVolume(vid needle.VolumeId) error {
  169. l.volumesLock.Lock()
  170. defer l.volumesLock.Unlock()
  171. v, ok := l.volumes[vid]
  172. if !ok {
  173. return fmt.Errorf("Volume not loaded, VolumeId: %d", vid)
  174. }
  175. v.Close()
  176. delete(l.volumes, vid)
  177. return nil
  178. }
  179. func (l *DiskLocation) unmountVolumeByCollection(collectionName string) map[needle.VolumeId]*Volume {
  180. deltaVols := make(map[needle.VolumeId]*Volume, 0)
  181. for k, v := range l.volumes {
  182. if v.Collection == collectionName && !v.isCompacting {
  183. deltaVols[k] = v
  184. }
  185. }
  186. for k := range deltaVols {
  187. delete(l.volumes, k)
  188. }
  189. return deltaVols
  190. }
  191. func (l *DiskLocation) SetVolume(vid needle.VolumeId, volume *Volume) {
  192. l.volumesLock.Lock()
  193. defer l.volumesLock.Unlock()
  194. l.volumes[vid] = volume
  195. }
  196. func (l *DiskLocation) FindVolume(vid needle.VolumeId) (*Volume, bool) {
  197. l.volumesLock.RLock()
  198. defer l.volumesLock.RUnlock()
  199. v, ok := l.volumes[vid]
  200. return v, ok
  201. }
  202. func (l *DiskLocation) VolumesLen() int {
  203. l.volumesLock.RLock()
  204. defer l.volumesLock.RUnlock()
  205. return len(l.volumes)
  206. }
  207. func (l *DiskLocation) Close() {
  208. l.volumesLock.Lock()
  209. for _, v := range l.volumes {
  210. v.Close()
  211. }
  212. l.volumesLock.Unlock()
  213. l.ecVolumesLock.Lock()
  214. for _, ecVolume := range l.ecVolumes {
  215. ecVolume.Close()
  216. }
  217. l.ecVolumesLock.Unlock()
  218. return
  219. }
  220. func (l *DiskLocation) LocateVolume(vid needle.VolumeId) (os.FileInfo, bool) {
  221. if fileInfos, err := ioutil.ReadDir(l.Directory); err == nil {
  222. for _, fileInfo := range fileInfos {
  223. volId, _, err := l.volumeIdFromPath(fileInfo)
  224. if vid == volId && err == nil {
  225. return fileInfo, true
  226. }
  227. }
  228. }
  229. return nil, false
  230. }