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.

202 lines
4.4 KiB

  1. package storage
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "strings"
  6. "sync"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "fmt"
  9. )
  10. type DiskLocation struct {
  11. Directory string
  12. MaxVolumeCount int
  13. volumes map[VolumeId]*Volume
  14. sync.RWMutex
  15. }
  16. func NewDiskLocation(dir string, maxVolumeCount int) *DiskLocation {
  17. location := &DiskLocation{Directory: dir, MaxVolumeCount: maxVolumeCount}
  18. location.volumes = make(map[VolumeId]*Volume)
  19. return location
  20. }
  21. func (l *DiskLocation) volumeIdFromPath(dir os.FileInfo) (VolumeId, string, error) {
  22. name := dir.Name()
  23. if !dir.IsDir() && strings.HasSuffix(name, ".dat") {
  24. collection := ""
  25. base := name[:len(name) - len(".dat")]
  26. i := strings.LastIndex(base, "_")
  27. if i > 0 {
  28. collection, base = base[0:i], base[i + 1:]
  29. }
  30. vol, err := NewVolumeId(base);
  31. return vol, collection, err
  32. }
  33. return 0, "", fmt.Errorf("Path is not a volume: %s", name)
  34. }
  35. func (l *DiskLocation) loadExistingVolume(dir os.FileInfo, needleMapKind NeedleMapType, mutex *sync.RWMutex) {
  36. name := dir.Name()
  37. if !dir.IsDir() && strings.HasSuffix(name, ".dat") {
  38. vid, collection, err := l.volumeIdFromPath(dir)
  39. if err == nil {
  40. mutex.RLock()
  41. _, found := l.volumes[vid]
  42. mutex.RUnlock()
  43. if !found {
  44. if v, e := NewVolume(l.Directory, collection, vid, needleMapKind, nil, nil, 0); e == nil {
  45. mutex.Lock()
  46. l.volumes[vid] = v
  47. mutex.Unlock()
  48. glog.V(0).Infof("data file %s, replicaPlacement=%s v=%d size=%d ttl=%s",
  49. l.Directory+"/"+name, v.ReplicaPlacement, v.Version(), v.Size(), v.Ttl.String())
  50. if v.Size() != v.dataFileSize {
  51. glog.V(0).Infof("data file %s, size=%d expected=%d",
  52. l.Directory+"/"+name, v.Size(), v.dataFileSize)
  53. }
  54. } else {
  55. glog.V(0).Infof("new volume %s error %s", name, e)
  56. }
  57. }
  58. }
  59. }
  60. }
  61. func (l *DiskLocation) concurrentLoadingVolumes(needleMapKind NeedleMapType, concurrentFlag bool) {
  62. var concurrency int
  63. if concurrentFlag {
  64. //You could choose a better optimized concurency value after testing at your environment
  65. concurrency = 10
  66. } else {
  67. concurrency = 1
  68. }
  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. var mutex sync.RWMutex
  80. for workerNum := 0; workerNum < concurrency; workerNum++ {
  81. wg.Add(1)
  82. go func() {
  83. defer wg.Done()
  84. for dir := range task_queue {
  85. l.loadExistingVolume(dir, needleMapKind, &mutex)
  86. }
  87. }()
  88. }
  89. wg.Wait()
  90. }
  91. func (l *DiskLocation) loadExistingVolumes(needleMapKind NeedleMapType) {
  92. l.Lock()
  93. defer l.Unlock()
  94. l.concurrentLoadingVolumes(needleMapKind, true)
  95. glog.V(0).Infoln("Store started on dir:", l.Directory, "with", len(l.volumes), "volumes", "max", l.MaxVolumeCount)
  96. }
  97. func (l *DiskLocation) DeleteCollectionFromDiskLocation(collection string) (e error) {
  98. l.Lock()
  99. defer l.Unlock()
  100. for k, v := range l.volumes {
  101. if v.Collection == collection {
  102. e = l.deleteVolumeById(k)
  103. if e != nil {
  104. return
  105. }
  106. }
  107. }
  108. return
  109. }
  110. func (l *DiskLocation) deleteVolumeById(vid VolumeId) (e error) {
  111. v, ok := l.volumes[vid]
  112. if !ok {
  113. return
  114. }
  115. e = v.Destroy()
  116. if e != nil {
  117. return
  118. }
  119. delete(l.volumes, vid)
  120. return
  121. }
  122. func (l *DiskLocation) LoadVolume(vid VolumeId, needleMapKind NeedleMapType) bool {
  123. if dirs, err := ioutil.ReadDir(l.Directory); err == nil {
  124. for _, dir := range dirs {
  125. volId, _, err := l.volumeIdFromPath(dir)
  126. if vid == volId && err == nil {
  127. var mutex sync.RWMutex
  128. l.loadExistingVolume(dir, needleMapKind, &mutex)
  129. return true
  130. }
  131. }
  132. }
  133. return false
  134. }
  135. func (l *DiskLocation) DeleteVolume(vid VolumeId) (error) {
  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 VolumeId) (error) {
  143. _, ok := l.volumes[vid]
  144. if !ok {
  145. return fmt.Errorf("Volume not loaded, VolumeId: %d", vid)
  146. }
  147. delete(l.volumes, vid)
  148. return nil
  149. }
  150. func (l *DiskLocation) SetVolume(vid VolumeId, volume *Volume) {
  151. l.Lock()
  152. defer l.Unlock()
  153. l.volumes[vid] = volume
  154. }
  155. func (l *DiskLocation) FindVolume(vid VolumeId) (*Volume, bool) {
  156. l.RLock()
  157. defer l.RUnlock()
  158. v, ok := l.volumes[vid]
  159. return v, ok
  160. }
  161. func (l *DiskLocation) VolumesLen() int {
  162. l.RLock()
  163. defer l.RUnlock()
  164. return len(l.volumes)
  165. }
  166. func (l *DiskLocation) Close() {
  167. l.Lock()
  168. defer l.Unlock()
  169. for _, v := range l.volumes {
  170. v.Close()
  171. }
  172. return
  173. }