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.

268 lines
6.2 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
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) {
  45. name := fileInfo.Name()
  46. if !fileInfo.IsDir() && strings.HasSuffix(name, ".idx") {
  47. vid, collection, err := l.volumeIdFromPath(fileInfo)
  48. if err == nil {
  49. l.volumesLock.RLock()
  50. _, found := l.volumes[vid]
  51. l.volumesLock.RUnlock()
  52. if !found {
  53. if v, e := NewVolume(l.Directory, collection, vid, needleMapKind, nil, nil, 0, 0); e == nil {
  54. l.volumesLock.Lock()
  55. l.volumes[vid] = v
  56. l.volumesLock.Unlock()
  57. size, _, _ := v.FileStat()
  58. glog.V(0).Infof("data file %s, replicaPlacement=%s v=%d size=%d ttl=%s",
  59. l.Directory+"/"+name, v.ReplicaPlacement, v.Version(), size, v.Ttl.String())
  60. // println("volume", vid, "last append at", v.lastAppendAtNs)
  61. } else {
  62. glog.V(0).Infof("new volume %s error %s", name, e)
  63. }
  64. }
  65. }
  66. }
  67. }
  68. func (l *DiskLocation) concurrentLoadingVolumes(needleMapKind NeedleMapType, concurrency int) {
  69. task_queue := make(chan os.FileInfo, 10*concurrency)
  70. go func() {
  71. if dirs, err := ioutil.ReadDir(l.Directory); err == nil {
  72. for _, dir := range dirs {
  73. task_queue <- dir
  74. }
  75. }
  76. close(task_queue)
  77. }()
  78. var wg sync.WaitGroup
  79. for workerNum := 0; workerNum < concurrency; workerNum++ {
  80. wg.Add(1)
  81. go func() {
  82. defer wg.Done()
  83. for dir := range task_queue {
  84. l.loadExistingVolume(dir, needleMapKind)
  85. }
  86. }()
  87. }
  88. wg.Wait()
  89. }
  90. func (l *DiskLocation) loadExistingVolumes(needleMapKind NeedleMapType) {
  91. l.concurrentLoadingVolumes(needleMapKind, 10)
  92. glog.V(0).Infof("Store started on dir: %s with %d volumes max %d", l.Directory, len(l.volumes), l.MaxVolumeCount)
  93. l.loadAllEcShards()
  94. glog.V(0).Infof("Store started on dir: %s with %d ec shards", l.Directory, len(l.ecVolumes))
  95. }
  96. func (l *DiskLocation) DeleteCollectionFromDiskLocation(collection string) (e error) {
  97. l.volumesLock.Lock()
  98. delVolsMap := l.unmountVolumeByCollection(collection)
  99. l.volumesLock.Unlock()
  100. l.ecVolumesLock.Lock()
  101. delEcVolsMap := l.unmountEcVolumeByCollection(collection)
  102. l.ecVolumesLock.Unlock()
  103. errChain := make(chan error, 2)
  104. var wg sync.WaitGroup
  105. wg.Add(2)
  106. go func() {
  107. for _, v := range delVolsMap {
  108. if err := v.Destroy(); err != nil {
  109. errChain <- err
  110. }
  111. }
  112. wg.Done()
  113. }()
  114. go func() {
  115. for _, v := range delEcVolsMap {
  116. v.Destroy()
  117. }
  118. wg.Done()
  119. }()
  120. go func() {
  121. wg.Wait()
  122. close(errChain)
  123. }()
  124. errBuilder := strings.Builder{}
  125. for err := range errChain {
  126. errBuilder.WriteString(err.Error())
  127. errBuilder.WriteString("; ")
  128. }
  129. if errBuilder.Len() > 0 {
  130. e = fmt.Errorf(errBuilder.String())
  131. }
  132. return
  133. }
  134. func (l *DiskLocation) deleteVolumeById(vid needle.VolumeId) (e error) {
  135. v, ok := l.volumes[vid]
  136. if !ok {
  137. return
  138. }
  139. e = v.Destroy()
  140. if e != nil {
  141. return
  142. }
  143. delete(l.volumes, vid)
  144. return
  145. }
  146. func (l *DiskLocation) LoadVolume(vid needle.VolumeId, needleMapKind NeedleMapType) bool {
  147. if fileInfo, found := l.LocateVolume(vid); found {
  148. l.loadExistingVolume(fileInfo, needleMapKind)
  149. return true
  150. }
  151. return false
  152. }
  153. func (l *DiskLocation) DeleteVolume(vid needle.VolumeId) error {
  154. l.volumesLock.Lock()
  155. defer l.volumesLock.Unlock()
  156. _, ok := l.volumes[vid]
  157. if !ok {
  158. return fmt.Errorf("Volume not found, VolumeId: %d", vid)
  159. }
  160. return l.deleteVolumeById(vid)
  161. }
  162. func (l *DiskLocation) UnloadVolume(vid needle.VolumeId) error {
  163. l.volumesLock.Lock()
  164. defer l.volumesLock.Unlock()
  165. v, ok := l.volumes[vid]
  166. if !ok {
  167. return fmt.Errorf("Volume not loaded, VolumeId: %d", vid)
  168. }
  169. v.Close()
  170. delete(l.volumes, vid)
  171. return nil
  172. }
  173. func (l *DiskLocation) unmountVolumeByCollection(collectionName string) map[needle.VolumeId]*Volume {
  174. deltaVols := make(map[needle.VolumeId]*Volume, 0)
  175. for k, v := range l.volumes {
  176. if v.Collection == collectionName && !v.isCompacting {
  177. deltaVols[k] = v
  178. }
  179. }
  180. for k := range deltaVols {
  181. delete(l.volumes, k)
  182. }
  183. return deltaVols
  184. }
  185. func (l *DiskLocation) SetVolume(vid needle.VolumeId, volume *Volume) {
  186. l.volumesLock.Lock()
  187. defer l.volumesLock.Unlock()
  188. l.volumes[vid] = volume
  189. }
  190. func (l *DiskLocation) FindVolume(vid needle.VolumeId) (*Volume, bool) {
  191. l.volumesLock.RLock()
  192. defer l.volumesLock.RUnlock()
  193. v, ok := l.volumes[vid]
  194. return v, ok
  195. }
  196. func (l *DiskLocation) VolumesLen() int {
  197. l.volumesLock.RLock()
  198. defer l.volumesLock.RUnlock()
  199. return len(l.volumes)
  200. }
  201. func (l *DiskLocation) Close() {
  202. l.volumesLock.Lock()
  203. for _, v := range l.volumes {
  204. v.Close()
  205. }
  206. l.volumesLock.Unlock()
  207. l.ecVolumesLock.Lock()
  208. for _, ecVolume := range l.ecVolumes {
  209. ecVolume.Close()
  210. }
  211. l.ecVolumesLock.Unlock()
  212. return
  213. }
  214. func (l *DiskLocation) LocateVolume(vid needle.VolumeId) (os.FileInfo, bool) {
  215. if fileInfos, err := ioutil.ReadDir(l.Directory); err == nil {
  216. for _, fileInfo := range fileInfos {
  217. volId, _, err := l.volumeIdFromPath(fileInfo)
  218. if vid == volId && err == nil {
  219. return fileInfo, true
  220. }
  221. }
  222. }
  223. return nil, false
  224. }