ec_shard.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package erasure_coding
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "path"
  7. "strconv"
  8. "strings"
  9. "github.com/seaweedfs/seaweedfs/weed/stats"
  10. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  11. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  12. )
  13. type ShardId uint8
  14. type EcVolumeShard struct {
  15. VolumeId needle.VolumeId
  16. ShardId ShardId
  17. Collection string
  18. dir string
  19. ecdFile *os.File
  20. ecdFileSize int64
  21. DiskType types.DiskType
  22. }
  23. func NewEcVolumeShard(diskType types.DiskType, dirname string, collection string, id needle.VolumeId, shardId ShardId) (v *EcVolumeShard, e error) {
  24. v = &EcVolumeShard{dir: dirname, Collection: collection, VolumeId: id, ShardId: shardId, DiskType: diskType}
  25. baseFileName := v.FileName()
  26. // open ecd file
  27. if v.ecdFile, e = os.OpenFile(baseFileName+ToExt(int(shardId)), os.O_RDONLY, 0644); e != nil {
  28. if e == os.ErrNotExist || strings.Contains(e.Error(), "no such file or directory") {
  29. return nil, os.ErrNotExist
  30. }
  31. return nil, fmt.Errorf("cannot read ec volume shard %s%s: %v", baseFileName, ToExt(int(shardId)), e)
  32. }
  33. ecdFi, statErr := v.ecdFile.Stat()
  34. if statErr != nil {
  35. _ = v.ecdFile.Close()
  36. return nil, fmt.Errorf("can not stat ec volume shard %s%s: %v", baseFileName, ToExt(int(shardId)), statErr)
  37. }
  38. v.ecdFileSize = ecdFi.Size()
  39. v.Mount()
  40. return
  41. }
  42. func (shard *EcVolumeShard) Mount() {
  43. stats.VolumeServerVolumeGauge.WithLabelValues(shard.Collection, "ec_shards").Inc()
  44. }
  45. func (shard *EcVolumeShard) Unmount() {
  46. stats.VolumeServerVolumeGauge.WithLabelValues(shard.Collection, "ec_shards").Dec()
  47. }
  48. func (shard *EcVolumeShard) Size() int64 {
  49. return shard.ecdFileSize
  50. }
  51. func (shard *EcVolumeShard) String() string {
  52. return fmt.Sprintf("ec shard %v:%v, dir:%s, Collection:%s", shard.VolumeId, shard.ShardId, shard.dir, shard.Collection)
  53. }
  54. func (shard *EcVolumeShard) FileName() (fileName string) {
  55. return EcShardFileName(shard.Collection, shard.dir, int(shard.VolumeId))
  56. }
  57. func EcShardFileName(collection string, dir string, id int) (fileName string) {
  58. idString := strconv.Itoa(id)
  59. if collection == "" {
  60. fileName = path.Join(dir, idString)
  61. } else {
  62. fileName = path.Join(dir, collection+"_"+idString)
  63. }
  64. return
  65. }
  66. func EcShardBaseFileName(collection string, id int) (baseFileName string) {
  67. baseFileName = strconv.Itoa(id)
  68. if collection != "" {
  69. baseFileName = collection + "_" + baseFileName
  70. }
  71. return
  72. }
  73. func (shard *EcVolumeShard) Close() {
  74. if shard.ecdFile != nil {
  75. _ = shard.ecdFile.Close()
  76. shard.ecdFile = nil
  77. }
  78. }
  79. func (shard *EcVolumeShard) Destroy() {
  80. shard.Unmount()
  81. os.Remove(shard.FileName() + ToExt(int(shard.ShardId)))
  82. }
  83. func (shard *EcVolumeShard) ReadAt(buf []byte, offset int64) (int, error) {
  84. n, err := shard.ecdFile.ReadAt(buf, offset)
  85. if err == io.EOF && n == len(buf) {
  86. err = nil
  87. }
  88. return n, err
  89. }