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.

218 lines
4.9 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
6 years ago
  1. package storage
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "strings"
  6. "sync"
  7. "fmt"
  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. 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, ".dat") {
  30. base := name[:len(name)-len(".dat")]
  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, ".dat") {
  47. vid, collection, err := l.volumeIdFromPath(fileInfo)
  48. if err == nil {
  49. l.RLock()
  50. _, found := l.volumes[vid]
  51. l.RUnlock()
  52. if !found {
  53. if v, e := NewVolume(l.Directory, collection, vid, needleMapKind, nil, nil, 0); e == nil {
  54. l.Lock()
  55. l.volumes[vid] = v
  56. l.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.Lock()
  98. defer l.Unlock()
  99. for k, v := range l.volumes {
  100. if v.Collection == collection {
  101. e = l.deleteVolumeById(k)
  102. if e != nil {
  103. return
  104. }
  105. }
  106. }
  107. return
  108. }
  109. func (l *DiskLocation) deleteVolumeById(vid needle.VolumeId) (e error) {
  110. v, ok := l.volumes[vid]
  111. if !ok {
  112. return
  113. }
  114. e = v.Destroy()
  115. if e != nil {
  116. return
  117. }
  118. delete(l.volumes, vid)
  119. return
  120. }
  121. func (l *DiskLocation) LoadVolume(vid needle.VolumeId, needleMapKind NeedleMapType) bool {
  122. if fileInfos, err := ioutil.ReadDir(l.Directory); err == nil {
  123. for _, fileInfo := range fileInfos {
  124. volId, _, err := l.volumeIdFromPath(fileInfo)
  125. if vid == volId && err == nil {
  126. l.loadExistingVolume(fileInfo, needleMapKind)
  127. return true
  128. }
  129. }
  130. }
  131. return false
  132. }
  133. func (l *DiskLocation) DeleteVolume(vid needle.VolumeId) error {
  134. l.Lock()
  135. defer l.Unlock()
  136. _, ok := l.volumes[vid]
  137. if !ok {
  138. return fmt.Errorf("Volume not found, VolumeId: %d", vid)
  139. }
  140. return l.deleteVolumeById(vid)
  141. }
  142. func (l *DiskLocation) UnloadVolume(vid needle.VolumeId) error {
  143. l.Lock()
  144. defer l.Unlock()
  145. v, ok := l.volumes[vid]
  146. if !ok {
  147. return fmt.Errorf("Volume not loaded, VolumeId: %d", vid)
  148. }
  149. v.Close()
  150. delete(l.volumes, vid)
  151. return nil
  152. }
  153. func (l *DiskLocation) SetVolume(vid needle.VolumeId, volume *Volume) {
  154. l.Lock()
  155. defer l.Unlock()
  156. l.volumes[vid] = volume
  157. }
  158. func (l *DiskLocation) FindVolume(vid needle.VolumeId) (*Volume, bool) {
  159. l.RLock()
  160. defer l.RUnlock()
  161. v, ok := l.volumes[vid]
  162. return v, ok
  163. }
  164. func (l *DiskLocation) VolumesLen() int {
  165. l.RLock()
  166. defer l.RUnlock()
  167. return len(l.volumes)
  168. }
  169. func (l *DiskLocation) Close() {
  170. l.Lock()
  171. for _, v := range l.volumes {
  172. v.Close()
  173. }
  174. l.Unlock()
  175. l.ecVolumesLock.Lock()
  176. for _, shards := range l.ecVolumes {
  177. shards.Close()
  178. }
  179. l.ecVolumesLock.Unlock()
  180. return
  181. }