ec_shard.go 2.4 KB

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