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.

152 lines
3.4 KiB

  1. package storage
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "path"
  6. "regexp"
  7. "sort"
  8. "strconv"
  9. "github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
  10. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  11. )
  12. var (
  13. re = regexp.MustCompile("\\.ec[0-9][0-9]")
  14. )
  15. func (l *DiskLocation) HasEcShard(vid needle.VolumeId) (erasure_coding.EcVolumeShards, bool) {
  16. l.ecShardsLock.RLock()
  17. defer l.ecShardsLock.RUnlock()
  18. ecShards, ok := l.ecShards[vid]
  19. if ok {
  20. return ecShards, true
  21. }
  22. return nil, false
  23. }
  24. func (l *DiskLocation) FindEcShard(vid needle.VolumeId, shardId erasure_coding.ShardId) (*erasure_coding.EcVolumeShard, bool) {
  25. l.ecShardsLock.RLock()
  26. defer l.ecShardsLock.RUnlock()
  27. ecShards, ok := l.ecShards[vid]
  28. if !ok {
  29. return nil, false
  30. }
  31. for _, ecShard := range ecShards {
  32. if ecShard.ShardId == shardId {
  33. return ecShard, true
  34. }
  35. }
  36. return nil, false
  37. }
  38. func (l *DiskLocation) LoadEcShard(collection string, vid needle.VolumeId, shardId erasure_coding.ShardId) (err error) {
  39. ecVolumeShard, err := erasure_coding.NewEcVolumeShard(l.Directory, collection, vid, shardId)
  40. if err != nil {
  41. return fmt.Errorf("failed to create ec shard %d.%d: %v", vid, shardId, err)
  42. }
  43. l.ecShardsLock.Lock()
  44. l.ecShards[vid] = append(l.ecShards[vid], ecVolumeShard)
  45. l.ecShardsLock.Unlock()
  46. return nil
  47. }
  48. func (l *DiskLocation) UnloadEcShard(vid needle.VolumeId, shardId erasure_coding.ShardId) bool {
  49. l.ecShardsLock.Lock()
  50. defer l.ecShardsLock.Unlock()
  51. vidShards, found := l.ecShards[vid]
  52. if !found {
  53. return false
  54. }
  55. shardIndex := -1
  56. for i, shard := range vidShards {
  57. if shard.ShardId == shardId {
  58. shardIndex = i
  59. break
  60. }
  61. }
  62. if shardIndex < 0 {
  63. return false
  64. }
  65. if len(vidShards) == 1 {
  66. delete(l.ecShards, vid)
  67. return true
  68. }
  69. l.ecShards[vid] = append(vidShards[:shardIndex], vidShards[shardIndex+1:]...)
  70. return true
  71. }
  72. func (l *DiskLocation) loadEcShards(shards []string, collection string, vid needle.VolumeId) (err error) {
  73. for _, shard := range shards {
  74. shardId, err := strconv.ParseInt(path.Ext(shard)[3:], 10, 64)
  75. if err != nil {
  76. return fmt.Errorf("failed to parse ec shard name %v: %v", shard, err)
  77. }
  78. err = l.LoadEcShard(collection, vid, erasure_coding.ShardId(shardId))
  79. if err != nil {
  80. return fmt.Errorf("failed to load ec shard %v: %v", shard, err)
  81. }
  82. }
  83. return nil
  84. }
  85. func (l *DiskLocation) loadAllEcShards() (err error) {
  86. fileInfos, err := ioutil.ReadDir(l.Directory)
  87. if err != nil {
  88. return fmt.Errorf("load all ec shards in dir %s: %v", l.Directory, err)
  89. }
  90. sort.Slice(fileInfos, func(i, j int) bool {
  91. return fileInfos[i].Name() < fileInfos[j].Name()
  92. })
  93. var sameVolumeShards []string
  94. var prevVolumeId needle.VolumeId
  95. for _, fileInfo := range fileInfos {
  96. if fileInfo.IsDir() {
  97. continue
  98. }
  99. ext := path.Ext(fileInfo.Name())
  100. name := fileInfo.Name()
  101. baseName := name[:len(name)-len(ext)]
  102. collection, volumeId, err := parseCollectionVolumeId(baseName)
  103. if err != nil {
  104. continue
  105. }
  106. if re.MatchString(ext) {
  107. if prevVolumeId == 0 || volumeId == prevVolumeId {
  108. sameVolumeShards = append(sameVolumeShards, fileInfo.Name())
  109. } else {
  110. sameVolumeShards = []string{fileInfo.Name()}
  111. }
  112. prevVolumeId = volumeId
  113. continue
  114. }
  115. if ext == ".ecx" && volumeId == prevVolumeId {
  116. if err = l.loadEcShards(sameVolumeShards, collection, volumeId); err != nil {
  117. return fmt.Errorf("loadEcShards collection:%v volumeId:%d : %v", collection, volumeId, err)
  118. }
  119. prevVolumeId = volumeId
  120. continue
  121. }
  122. }
  123. return nil
  124. }