disk_location.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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, 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. for k, v := range l.volumes {
  99. if v.Collection == collection {
  100. e = l.deleteVolumeById(k)
  101. if e != nil {
  102. l.Unlock()
  103. return
  104. }
  105. }
  106. }
  107. l.Unlock()
  108. l.ecVolumesLock.Lock()
  109. for k, v := range l.ecVolumes {
  110. if v.Collection == collection {
  111. e = l.deleteEcVolumeById(k)
  112. if e != nil {
  113. l.ecVolumesLock.Unlock()
  114. return
  115. }
  116. }
  117. }
  118. l.ecVolumesLock.Unlock()
  119. return
  120. }
  121. func (l *DiskLocation) deleteVolumeById(vid needle.VolumeId) (e error) {
  122. v, ok := l.volumes[vid]
  123. if !ok {
  124. return
  125. }
  126. e = v.Destroy()
  127. if e != nil {
  128. return
  129. }
  130. delete(l.volumes, vid)
  131. return
  132. }
  133. func (l *DiskLocation) LoadVolume(vid needle.VolumeId, needleMapKind NeedleMapType) bool {
  134. if fileInfos, err := ioutil.ReadDir(l.Directory); err == nil {
  135. for _, fileInfo := range fileInfos {
  136. volId, _, err := l.volumeIdFromPath(fileInfo)
  137. if vid == volId && err == nil {
  138. l.loadExistingVolume(fileInfo, needleMapKind)
  139. return true
  140. }
  141. }
  142. }
  143. return false
  144. }
  145. func (l *DiskLocation) DeleteVolume(vid needle.VolumeId) error {
  146. l.Lock()
  147. defer l.Unlock()
  148. _, ok := l.volumes[vid]
  149. if !ok {
  150. return fmt.Errorf("Volume not found, VolumeId: %d", vid)
  151. }
  152. return l.deleteVolumeById(vid)
  153. }
  154. func (l *DiskLocation) UnloadVolume(vid needle.VolumeId) error {
  155. l.Lock()
  156. defer l.Unlock()
  157. v, ok := l.volumes[vid]
  158. if !ok {
  159. return fmt.Errorf("Volume not loaded, VolumeId: %d", vid)
  160. }
  161. v.Close()
  162. delete(l.volumes, vid)
  163. return nil
  164. }
  165. func (l *DiskLocation) SetVolume(vid needle.VolumeId, volume *Volume) {
  166. l.Lock()
  167. defer l.Unlock()
  168. l.volumes[vid] = volume
  169. }
  170. func (l *DiskLocation) FindVolume(vid needle.VolumeId) (*Volume, bool) {
  171. l.RLock()
  172. defer l.RUnlock()
  173. v, ok := l.volumes[vid]
  174. return v, ok
  175. }
  176. func (l *DiskLocation) VolumesLen() int {
  177. l.RLock()
  178. defer l.RUnlock()
  179. return len(l.volumes)
  180. }
  181. func (l *DiskLocation) Close() {
  182. l.Lock()
  183. for _, v := range l.volumes {
  184. v.Close()
  185. }
  186. l.Unlock()
  187. l.ecVolumesLock.Lock()
  188. for _, ecVolume := range l.ecVolumes {
  189. ecVolume.Close()
  190. }
  191. l.ecVolumesLock.Unlock()
  192. return
  193. }