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.

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