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.

84 lines
2.0 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) loadEcShards(baseName string, shards []string, collection string, vid needle.VolumeId) (err error){
  16. for _, shard := range shards{
  17. shardId, err := strconv.ParseInt(path.Ext(shard)[3:], 10, 64)
  18. if err != nil {
  19. return fmt.Errorf("failed to parse ec shard name %v: %v", shard, err)
  20. }
  21. ecVolumeShard, err := erasure_coding.NewEcVolumeShard(l.Directory, collection, vid, int(shardId))
  22. if err != nil {
  23. return fmt.Errorf("failed to create ec shard %v: %v", shard, err)
  24. }
  25. l.ecShardsLock.Lock()
  26. l.ecShards[vid] = append(l.ecShards[vid], ecVolumeShard)
  27. l.ecShardsLock.Unlock()
  28. }
  29. return nil
  30. }
  31. func (l *DiskLocation) loadAllEcShards() (err error){
  32. fileInfos, err := ioutil.ReadDir(l.Directory)
  33. if err != nil {
  34. return fmt.Errorf("load all ec shards in dir %s: %v", l.Directory, err)
  35. }
  36. sort.Slice(fileInfos, func(i, j int) bool {
  37. return fileInfos[i].Name() < fileInfos[j].Name()
  38. })
  39. var sameVolumeShards []string
  40. var prevVolumeId needle.VolumeId
  41. for _, fileInfo := range fileInfos{
  42. if fileInfo.IsDir(){
  43. continue
  44. }
  45. ext := path.Ext(fileInfo.Name())
  46. name := fileInfo.Name()
  47. baseName := name[:len(name)-len(ext)]
  48. collection, volumeId, err := parseCollectionVolumeId(baseName)
  49. if err != nil {
  50. continue
  51. }
  52. if re.MatchString(ext){
  53. if prevVolumeId == 0 || volumeId == prevVolumeId {
  54. sameVolumeShards = append(sameVolumeShards, fileInfo.Name())
  55. }else{
  56. sameVolumeShards = []string{fileInfo.Name()}
  57. }
  58. prevVolumeId = volumeId
  59. continue
  60. }
  61. if ext == ".ecx" && volumeId == prevVolumeId{
  62. if err = l.loadEcShards(baseName, sameVolumeShards, collection, volumeId);err!=nil{
  63. return fmt.Errorf("loadEcShards collection:%v volumeId:%d : %v", collection, volumeId, err)
  64. }
  65. prevVolumeId = volumeId
  66. continue
  67. }
  68. }
  69. return nil
  70. }