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.

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