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.

103 lines
2.7 KiB

4 years ago
4 years ago
6 years ago
  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. stats.VolumeServerVolumeGauge.WithLabelValues(v.Collection, "ec_shards").Inc()
  40. return
  41. }
  42. func (shard *EcVolumeShard) Size() int64 {
  43. return shard.ecdFileSize
  44. }
  45. func (shard *EcVolumeShard) String() string {
  46. return fmt.Sprintf("ec shard %v:%v, dir:%s, Collection:%s", shard.VolumeId, shard.ShardId, shard.dir, shard.Collection)
  47. }
  48. func (shard *EcVolumeShard) FileName() (fileName string) {
  49. return EcShardFileName(shard.Collection, shard.dir, int(shard.VolumeId))
  50. }
  51. func EcShardFileName(collection string, dir string, id int) (fileName string) {
  52. idString := strconv.Itoa(id)
  53. if collection == "" {
  54. fileName = path.Join(dir, idString)
  55. } else {
  56. fileName = path.Join(dir, collection+"_"+idString)
  57. }
  58. return
  59. }
  60. func EcShardBaseFileName(collection string, id int) (baseFileName string) {
  61. baseFileName = strconv.Itoa(id)
  62. if collection != "" {
  63. baseFileName = collection + "_" + baseFileName
  64. }
  65. return
  66. }
  67. func (shard *EcVolumeShard) Close() {
  68. if shard.ecdFile != nil {
  69. _ = shard.ecdFile.Close()
  70. shard.ecdFile = nil
  71. }
  72. }
  73. func (shard *EcVolumeShard) Destroy() {
  74. os.Remove(shard.FileName() + ToExt(int(shard.ShardId)))
  75. stats.VolumeServerVolumeGauge.WithLabelValues(shard.Collection, "ec_shards").Dec()
  76. }
  77. func (shard *EcVolumeShard) ReadAt(buf []byte, offset int64) (int, error) {
  78. n, err := shard.ecdFile.ReadAt(buf, offset)
  79. if err == io.EOF && n == len(buf) {
  80. err = nil
  81. }
  82. return n, err
  83. }