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.

108 lines
2.6 KiB

  1. package erasure_coding
  2. import (
  3. "fmt"
  4. "os"
  5. "path"
  6. "strconv"
  7. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  8. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  9. )
  10. type EcVolumeShard struct {
  11. VolumeId needle.VolumeId
  12. ShardId uint8
  13. Collection string
  14. dir string
  15. ecdFile *os.File
  16. ecxFile *os.File
  17. }
  18. type EcVolumeShards []*EcVolumeShard
  19. func NewEcVolumeShard(dirname string, collection string, id needle.VolumeId, shardId int) (v *EcVolumeShard, e error) {
  20. v = &EcVolumeShard{dir: dirname, Collection: collection, VolumeId: id, ShardId: uint8(shardId)}
  21. baseFileName := v.FileName()
  22. if v.ecxFile, e = os.OpenFile(baseFileName+".ecx", os.O_RDONLY, 0644); e != nil {
  23. return nil, fmt.Errorf("cannot read ec volume index %s.ecx: %v", baseFileName, e)
  24. }
  25. if v.ecdFile, e = os.OpenFile(baseFileName+ToExt(shardId), os.O_RDONLY, 0644); e != nil {
  26. return nil, fmt.Errorf("cannot read ec volume shard %s.%s: %v", baseFileName, ToExt(shardId), e)
  27. }
  28. return
  29. }
  30. func (shards *EcVolumeShards) AddEcVolumeShard(ecVolumeShard *EcVolumeShard) bool {
  31. for _, s := range *shards {
  32. if s.ShardId == ecVolumeShard.ShardId {
  33. return false
  34. }
  35. }
  36. *shards = append(*shards, ecVolumeShard)
  37. return true
  38. }
  39. func (shards *EcVolumeShards) DeleteEcVolumeShard(ecVolumeShard *EcVolumeShard) bool {
  40. foundPosition := -1
  41. for i, s := range *shards {
  42. if s.ShardId == ecVolumeShard.ShardId {
  43. foundPosition = i
  44. }
  45. }
  46. if foundPosition < 0 {
  47. return false
  48. }
  49. *shards = append((*shards)[:foundPosition], (*shards)[foundPosition+1:]...)
  50. return true
  51. }
  52. func (shards *EcVolumeShards) Close() {
  53. for _, s := range *shards {
  54. s.Close()
  55. }
  56. }
  57. func (shards *EcVolumeShards) ToVolumeInformationMessage() (messages []*master_pb.VolumeEcShardInformationMessage) {
  58. for _, s := range *shards {
  59. m := &master_pb.VolumeEcShardInformationMessage{
  60. Id: uint32(s.VolumeId),
  61. Collection: s.Collection,
  62. EcIndex: uint32(s.ShardId),
  63. }
  64. messages = append(messages, m)
  65. }
  66. return
  67. }
  68. func (v *EcVolumeShard) String() string {
  69. return fmt.Sprintf("ec shard %v:%v, dir:%s, Collection:%s", v.VolumeId, v.ShardId, v.dir, v.Collection)
  70. }
  71. func (v *EcVolumeShard) FileName() (fileName string) {
  72. return EcShardFileName(v.Collection, v.dir, int(v.VolumeId))
  73. }
  74. func EcShardFileName(collection string, dir string, id int) (fileName string) {
  75. idString := strconv.Itoa(id)
  76. if collection == "" {
  77. fileName = path.Join(dir, idString)
  78. } else {
  79. fileName = path.Join(dir, collection+"_"+idString)
  80. }
  81. return
  82. }
  83. func (v *EcVolumeShard) Close() {
  84. if v.ecdFile != nil {
  85. _ = v.ecdFile.Close()
  86. v.ecdFile = nil
  87. }
  88. if v.ecxFile != nil {
  89. _ = v.ecxFile.Close()
  90. v.ecxFile = nil
  91. }
  92. }