disk_location.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. package storage
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "runtime"
  7. "strconv"
  8. "strings"
  9. "sync"
  10. "time"
  11. "github.com/google/uuid"
  12. "github.com/seaweedfs/seaweedfs/weed/glog"
  13. "github.com/seaweedfs/seaweedfs/weed/stats"
  14. "github.com/seaweedfs/seaweedfs/weed/storage/erasure_coding"
  15. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  16. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  17. "github.com/seaweedfs/seaweedfs/weed/util"
  18. )
  19. type DiskLocation struct {
  20. Directory string
  21. DirectoryUuid string
  22. IdxDirectory string
  23. DiskType types.DiskType
  24. MaxVolumeCount int32
  25. OriginalMaxVolumeCount int32
  26. MinFreeSpace util.MinFreeSpace
  27. volumes map[needle.VolumeId]*Volume
  28. volumesLock sync.RWMutex
  29. // erasure coding
  30. ecVolumes map[needle.VolumeId]*erasure_coding.EcVolume
  31. ecVolumesLock sync.RWMutex
  32. isDiskSpaceLow bool
  33. closeCh chan struct{}
  34. }
  35. func GenerateDirUuid(dir string) (dirUuidString string, err error) {
  36. glog.V(1).Infof("Getting uuid of volume directory:%s", dir)
  37. fileName := dir + "/vol_dir.uuid"
  38. if !util.FileExists(fileName) {
  39. dirUuidString, err = writeNewUuid(fileName)
  40. } else {
  41. uuidData, readErr := os.ReadFile(fileName)
  42. if readErr != nil {
  43. return "", fmt.Errorf("failed to read uuid from %s : %v", fileName, readErr)
  44. }
  45. if len(uuidData) > 0 {
  46. dirUuidString = string(uuidData)
  47. } else {
  48. dirUuidString, err = writeNewUuid(fileName)
  49. }
  50. }
  51. return dirUuidString, err
  52. }
  53. func writeNewUuid(fileName string) (string, error) {
  54. dirUuid, _ := uuid.NewRandom()
  55. dirUuidString := dirUuid.String()
  56. if err := util.WriteFile(fileName, []byte(dirUuidString), 0644); err != nil {
  57. return "", fmt.Errorf("failed to write uuid to %s : %v", fileName, err)
  58. }
  59. return dirUuidString, nil
  60. }
  61. func NewDiskLocation(dir string, maxVolumeCount int32, minFreeSpace util.MinFreeSpace, idxDir string, diskType types.DiskType) *DiskLocation {
  62. glog.V(4).Infof("Added new Disk %s: maxVolumes=%d", dir, maxVolumeCount)
  63. dir = util.ResolvePath(dir)
  64. if idxDir == "" {
  65. idxDir = dir
  66. } else {
  67. idxDir = util.ResolvePath(idxDir)
  68. }
  69. dirUuid, err := GenerateDirUuid(dir)
  70. if err != nil {
  71. glog.Fatalf("cannot generate uuid of dir %s: %v", dir, err)
  72. }
  73. location := &DiskLocation{
  74. Directory: dir,
  75. DirectoryUuid: dirUuid,
  76. IdxDirectory: idxDir,
  77. DiskType: diskType,
  78. MaxVolumeCount: maxVolumeCount,
  79. OriginalMaxVolumeCount: maxVolumeCount,
  80. MinFreeSpace: minFreeSpace,
  81. }
  82. location.volumes = make(map[needle.VolumeId]*Volume)
  83. location.ecVolumes = make(map[needle.VolumeId]*erasure_coding.EcVolume)
  84. location.closeCh = make(chan struct{})
  85. go func() {
  86. location.CheckDiskSpace()
  87. for {
  88. select {
  89. case <-location.closeCh:
  90. return
  91. case <-time.After(time.Minute):
  92. location.CheckDiskSpace()
  93. }
  94. }
  95. }()
  96. return location
  97. }
  98. func volumeIdFromFileName(filename string) (needle.VolumeId, string, error) {
  99. if isValidVolume(filename) {
  100. base := filename[:len(filename)-4]
  101. collection, volumeId, err := parseCollectionVolumeId(base)
  102. return volumeId, collection, err
  103. }
  104. return 0, "", fmt.Errorf("file is not a volume: %s", filename)
  105. }
  106. func parseCollectionVolumeId(base string) (collection string, vid needle.VolumeId, err error) {
  107. i := strings.LastIndex(base, "_")
  108. if i > 0 {
  109. collection, base = base[0:i], base[i+1:]
  110. }
  111. vol, err := needle.NewVolumeId(base)
  112. return collection, vol, err
  113. }
  114. func isValidVolume(basename string) bool {
  115. return strings.HasSuffix(basename, ".idx") || strings.HasSuffix(basename, ".vif")
  116. }
  117. func getValidVolumeName(basename string) string {
  118. if isValidVolume(basename) {
  119. return basename[:len(basename)-4]
  120. }
  121. return ""
  122. }
  123. func (l *DiskLocation) loadExistingVolume(dirEntry os.DirEntry, needleMapKind NeedleMapKind, skipIfEcVolumesExists bool, ldbTimeout int64, diskId uint32) bool {
  124. basename := dirEntry.Name()
  125. if dirEntry.IsDir() {
  126. return false
  127. }
  128. volumeName := getValidVolumeName(basename)
  129. if volumeName == "" {
  130. return false
  131. }
  132. // skip if ec volumes exists
  133. if skipIfEcVolumesExists {
  134. if util.FileExists(l.IdxDirectory + "/" + volumeName + ".ecx") {
  135. return false
  136. }
  137. }
  138. // check for incomplete volume
  139. noteFile := l.Directory + "/" + volumeName + ".note"
  140. if util.FileExists(noteFile) {
  141. note, _ := os.ReadFile(noteFile)
  142. glog.Warningf("volume %s was not completed: %s", volumeName, string(note))
  143. removeVolumeFiles(l.Directory + "/" + volumeName)
  144. removeVolumeFiles(l.IdxDirectory + "/" + volumeName)
  145. return false
  146. }
  147. // parse out collection, volume id
  148. vid, collection, err := volumeIdFromFileName(basename)
  149. if err != nil {
  150. glog.Warningf("get volume id failed, %s, err : %s", volumeName, err)
  151. return false
  152. }
  153. // avoid loading one volume more than once
  154. l.volumesLock.RLock()
  155. _, found := l.volumes[vid]
  156. l.volumesLock.RUnlock()
  157. if found {
  158. glog.V(1).Infof("loaded volume, %v", vid)
  159. return true
  160. }
  161. // load the volume
  162. v, e := NewVolume(l.Directory, l.IdxDirectory, collection, vid, needleMapKind, nil, nil, 0, needle.GetCurrentVersion(), 0, ldbTimeout)
  163. if e != nil {
  164. glog.V(0).Infof("new volume %s error %s", volumeName, e)
  165. return false
  166. }
  167. v.diskId = diskId // Set the disk ID for existing volumes
  168. l.SetVolume(vid, v)
  169. size, _, _ := v.FileStat()
  170. glog.V(0).Infof("data file %s, replication=%s v=%d size=%d ttl=%s disk_id=%d",
  171. l.Directory+"/"+volumeName+".dat", v.ReplicaPlacement, v.Version(), size, v.Ttl.String(), diskId)
  172. return true
  173. }
  174. func (l *DiskLocation) concurrentLoadingVolumes(needleMapKind NeedleMapKind, concurrency int, ldbTimeout int64, diskId uint32) {
  175. task_queue := make(chan os.DirEntry, 10*concurrency)
  176. go func() {
  177. foundVolumeNames := make(map[string]bool)
  178. if dirEntries, err := os.ReadDir(l.Directory); err == nil {
  179. for _, entry := range dirEntries {
  180. volumeName := getValidVolumeName(entry.Name())
  181. if volumeName == "" {
  182. continue
  183. }
  184. if _, found := foundVolumeNames[volumeName]; !found {
  185. foundVolumeNames[volumeName] = true
  186. task_queue <- entry
  187. }
  188. }
  189. }
  190. close(task_queue)
  191. }()
  192. var wg sync.WaitGroup
  193. for workerNum := 0; workerNum < concurrency; workerNum++ {
  194. wg.Add(1)
  195. go func() {
  196. defer wg.Done()
  197. for fi := range task_queue {
  198. _ = l.loadExistingVolume(fi, needleMapKind, true, ldbTimeout, diskId)
  199. }
  200. }()
  201. }
  202. wg.Wait()
  203. }
  204. func (l *DiskLocation) loadExistingVolumes(needleMapKind NeedleMapKind, ldbTimeout int64) {
  205. l.loadExistingVolumesWithId(needleMapKind, ldbTimeout, 0) // Default disk ID for backward compatibility
  206. }
  207. func (l *DiskLocation) loadExistingVolumesWithId(needleMapKind NeedleMapKind, ldbTimeout int64, diskId uint32) {
  208. workerNum := runtime.NumCPU()
  209. val, ok := os.LookupEnv("GOMAXPROCS")
  210. if ok {
  211. num, err := strconv.Atoi(val)
  212. if err != nil || num < 1 {
  213. num = 10
  214. glog.Warningf("failed to set worker number from GOMAXPROCS , set to default:10")
  215. }
  216. workerNum = num
  217. } else {
  218. if workerNum <= 10 {
  219. workerNum = 10
  220. }
  221. }
  222. l.concurrentLoadingVolumes(needleMapKind, workerNum, ldbTimeout, diskId)
  223. glog.V(0).Infof("Store started on dir: %s with %d volumes max %d (disk ID: %d)", l.Directory, len(l.volumes), l.MaxVolumeCount, diskId)
  224. l.loadAllEcShards()
  225. glog.V(0).Infof("Store started on dir: %s with %d ec shards (disk ID: %d)", l.Directory, len(l.ecVolumes), diskId)
  226. }
  227. func (l *DiskLocation) DeleteCollectionFromDiskLocation(collection string) (e error) {
  228. l.volumesLock.Lock()
  229. delVolsMap := l.unmountVolumeByCollection(collection)
  230. l.volumesLock.Unlock()
  231. l.ecVolumesLock.Lock()
  232. delEcVolsMap := l.unmountEcVolumeByCollection(collection)
  233. l.ecVolumesLock.Unlock()
  234. errChain := make(chan error, 2)
  235. var wg sync.WaitGroup
  236. wg.Add(2)
  237. go func() {
  238. for _, v := range delVolsMap {
  239. if err := v.Destroy(false); err != nil {
  240. errChain <- err
  241. }
  242. }
  243. wg.Done()
  244. }()
  245. go func() {
  246. for _, v := range delEcVolsMap {
  247. v.Destroy()
  248. }
  249. wg.Done()
  250. }()
  251. go func() {
  252. wg.Wait()
  253. close(errChain)
  254. }()
  255. errBuilder := strings.Builder{}
  256. for err := range errChain {
  257. errBuilder.WriteString(err.Error())
  258. errBuilder.WriteString("; ")
  259. }
  260. if errBuilder.Len() > 0 {
  261. e = fmt.Errorf("%s", errBuilder.String())
  262. }
  263. return
  264. }
  265. func (l *DiskLocation) deleteVolumeById(vid needle.VolumeId, onlyEmpty bool) (found bool, e error) {
  266. v, ok := l.volumes[vid]
  267. if !ok {
  268. return
  269. }
  270. e = v.Destroy(onlyEmpty)
  271. if e != nil {
  272. return
  273. }
  274. found = true
  275. delete(l.volumes, vid)
  276. return
  277. }
  278. func (l *DiskLocation) LoadVolume(diskId uint32, vid needle.VolumeId, needleMapKind NeedleMapKind) bool {
  279. if fileInfo, found := l.LocateVolume(vid); found {
  280. return l.loadExistingVolume(fileInfo, needleMapKind, false, 0, diskId)
  281. }
  282. return false
  283. }
  284. var ErrVolumeNotFound = fmt.Errorf("volume not found")
  285. func (l *DiskLocation) DeleteVolume(vid needle.VolumeId, onlyEmpty bool) error {
  286. l.volumesLock.Lock()
  287. defer l.volumesLock.Unlock()
  288. _, ok := l.volumes[vid]
  289. if !ok {
  290. return ErrVolumeNotFound
  291. }
  292. _, err := l.deleteVolumeById(vid, onlyEmpty)
  293. return err
  294. }
  295. func (l *DiskLocation) UnloadVolume(vid needle.VolumeId) error {
  296. l.volumesLock.Lock()
  297. defer l.volumesLock.Unlock()
  298. v, ok := l.volumes[vid]
  299. if !ok {
  300. return ErrVolumeNotFound
  301. }
  302. v.Close()
  303. delete(l.volumes, vid)
  304. return nil
  305. }
  306. func (l *DiskLocation) unmountVolumeByCollection(collectionName string) map[needle.VolumeId]*Volume {
  307. deltaVols := make(map[needle.VolumeId]*Volume, 0)
  308. for k, v := range l.volumes {
  309. if v.Collection == collectionName && !v.isCompacting && !v.isCommitCompacting {
  310. deltaVols[k] = v
  311. }
  312. }
  313. for k := range deltaVols {
  314. delete(l.volumes, k)
  315. }
  316. return deltaVols
  317. }
  318. func (l *DiskLocation) SetVolume(vid needle.VolumeId, volume *Volume) {
  319. l.volumesLock.Lock()
  320. defer l.volumesLock.Unlock()
  321. l.volumes[vid] = volume
  322. volume.location = l
  323. }
  324. func (l *DiskLocation) FindVolume(vid needle.VolumeId) (*Volume, bool) {
  325. l.volumesLock.RLock()
  326. defer l.volumesLock.RUnlock()
  327. v, ok := l.volumes[vid]
  328. return v, ok
  329. }
  330. func (l *DiskLocation) VolumesLen() int {
  331. l.volumesLock.RLock()
  332. defer l.volumesLock.RUnlock()
  333. return len(l.volumes)
  334. }
  335. func (l *DiskLocation) SetStopping() {
  336. l.volumesLock.Lock()
  337. for _, v := range l.volumes {
  338. v.SyncToDisk()
  339. }
  340. l.volumesLock.Unlock()
  341. return
  342. }
  343. func (l *DiskLocation) Close() {
  344. l.volumesLock.Lock()
  345. for _, v := range l.volumes {
  346. v.Close()
  347. }
  348. l.volumesLock.Unlock()
  349. l.ecVolumesLock.Lock()
  350. for _, ecVolume := range l.ecVolumes {
  351. ecVolume.Close()
  352. }
  353. l.ecVolumesLock.Unlock()
  354. close(l.closeCh)
  355. return
  356. }
  357. func (l *DiskLocation) LocateVolume(vid needle.VolumeId) (os.DirEntry, bool) {
  358. // println("LocateVolume", vid, "on", l.Directory)
  359. if dirEntries, err := os.ReadDir(l.Directory); err == nil {
  360. for _, entry := range dirEntries {
  361. // println("checking", entry.Name(), "...")
  362. volId, _, err := volumeIdFromFileName(entry.Name())
  363. // println("volId", volId, "err", err)
  364. if vid == volId && err == nil {
  365. return entry, true
  366. }
  367. }
  368. }
  369. return nil, false
  370. }
  371. func (l *DiskLocation) UnUsedSpace(volumeSizeLimit uint64) (unUsedSpace uint64) {
  372. l.volumesLock.RLock()
  373. defer l.volumesLock.RUnlock()
  374. for _, vol := range l.volumes {
  375. if vol.IsReadOnly() {
  376. continue
  377. }
  378. datSize, idxSize, _ := vol.FileStat()
  379. unUsedSpaceVolume := int64(volumeSizeLimit) - int64(datSize+idxSize)
  380. glog.V(4).Infof("Volume stats for %d: volumeSizeLimit=%d, datSize=%d idxSize=%d unused=%d", vol.Id, volumeSizeLimit, datSize, idxSize, unUsedSpaceVolume)
  381. if unUsedSpaceVolume >= 0 {
  382. unUsedSpace += uint64(unUsedSpaceVolume)
  383. }
  384. }
  385. return
  386. }
  387. func (l *DiskLocation) CheckDiskSpace() {
  388. if dir, e := filepath.Abs(l.Directory); e == nil {
  389. s := stats.NewDiskStatus(dir)
  390. stats.VolumeServerResourceGauge.WithLabelValues(l.Directory, "all").Set(float64(s.All))
  391. stats.VolumeServerResourceGauge.WithLabelValues(l.Directory, "used").Set(float64(s.Used))
  392. stats.VolumeServerResourceGauge.WithLabelValues(l.Directory, "free").Set(float64(s.Free))
  393. isLow, desc := l.MinFreeSpace.IsLow(s.Free, s.PercentFree)
  394. if isLow != l.isDiskSpaceLow {
  395. l.isDiskSpaceLow = !l.isDiskSpaceLow
  396. }
  397. logLevel := glog.Level(4)
  398. if l.isDiskSpaceLow {
  399. logLevel = glog.Level(0)
  400. }
  401. glog.V(logLevel).Infof("dir %s %s", dir, desc)
  402. }
  403. }