2019-05-27 08:29:46 +00:00
|
|
|
package erasure_coding
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/types"
|
2019-05-27 08:29:46 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
2020-08-26 16:16:58 +00:00
|
|
|
"strings"
|
2019-05-27 08:29:46 +00:00
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/stats"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
2019-05-27 08:29:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ShardId uint8
|
|
|
|
|
|
|
|
type EcVolumeShard struct {
|
|
|
|
VolumeId needle.VolumeId
|
|
|
|
ShardId ShardId
|
|
|
|
Collection string
|
|
|
|
dir string
|
|
|
|
ecdFile *os.File
|
|
|
|
ecdFileSize int64
|
2021-02-16 10:47:02 +00:00
|
|
|
DiskType types.DiskType
|
2019-05-27 08:29:46 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 10:47:02 +00:00
|
|
|
func NewEcVolumeShard(diskType types.DiskType, dirname string, collection string, id needle.VolumeId, shardId ShardId) (v *EcVolumeShard, e error) {
|
2019-05-27 08:29:46 +00:00
|
|
|
|
2021-02-16 10:47:02 +00:00
|
|
|
v = &EcVolumeShard{dir: dirname, Collection: collection, VolumeId: id, ShardId: shardId, DiskType: diskType}
|
2019-05-27 08:29:46 +00:00
|
|
|
|
|
|
|
baseFileName := v.FileName()
|
|
|
|
|
|
|
|
// open ecd file
|
|
|
|
if v.ecdFile, e = os.OpenFile(baseFileName+ToExt(int(shardId)), os.O_RDONLY, 0644); e != nil {
|
2020-08-26 16:16:58 +00:00
|
|
|
if e == os.ErrNotExist || strings.Contains(e.Error(), "no such file or directory") {
|
|
|
|
return nil, os.ErrNotExist
|
|
|
|
}
|
2020-08-26 15:55:15 +00:00
|
|
|
return nil, fmt.Errorf("cannot read ec volume shard %s%s: %v", baseFileName, ToExt(int(shardId)), e)
|
2019-05-27 08:29:46 +00:00
|
|
|
}
|
|
|
|
ecdFi, statErr := v.ecdFile.Stat()
|
|
|
|
if statErr != nil {
|
2020-08-26 15:55:15 +00:00
|
|
|
return nil, fmt.Errorf("can not stat ec volume shard %s%s: %v", baseFileName, ToExt(int(shardId)), statErr)
|
2019-05-27 08:29:46 +00:00
|
|
|
}
|
|
|
|
v.ecdFileSize = ecdFi.Size()
|
|
|
|
|
2019-06-18 04:02:50 +00:00
|
|
|
stats.VolumeServerVolumeCounter.WithLabelValues(v.Collection, "ec_shards").Inc()
|
2019-06-16 09:24:15 +00:00
|
|
|
|
2019-05-27 08:29:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-05 04:52:37 +00:00
|
|
|
func (shard *EcVolumeShard) Size() int64 {
|
|
|
|
return shard.ecdFileSize
|
|
|
|
}
|
|
|
|
|
2019-05-27 08:29:46 +00:00
|
|
|
func (shard *EcVolumeShard) String() string {
|
|
|
|
return fmt.Sprintf("ec shard %v:%v, dir:%s, Collection:%s", shard.VolumeId, shard.ShardId, shard.dir, shard.Collection)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (shard *EcVolumeShard) FileName() (fileName string) {
|
|
|
|
return EcShardFileName(shard.Collection, shard.dir, int(shard.VolumeId))
|
|
|
|
}
|
|
|
|
|
|
|
|
func EcShardFileName(collection string, dir string, id int) (fileName string) {
|
|
|
|
idString := strconv.Itoa(id)
|
|
|
|
if collection == "" {
|
|
|
|
fileName = path.Join(dir, idString)
|
|
|
|
} else {
|
|
|
|
fileName = path.Join(dir, collection+"_"+idString)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-03 09:26:31 +00:00
|
|
|
func EcShardBaseFileName(collection string, id int) (baseFileName string) {
|
|
|
|
baseFileName = strconv.Itoa(id)
|
|
|
|
if collection != "" {
|
2019-06-05 05:04:10 +00:00
|
|
|
baseFileName = collection + "_" + baseFileName
|
2019-06-03 09:26:31 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-27 08:29:46 +00:00
|
|
|
func (shard *EcVolumeShard) Close() {
|
|
|
|
if shard.ecdFile != nil {
|
|
|
|
_ = shard.ecdFile.Close()
|
|
|
|
shard.ecdFile = nil
|
|
|
|
}
|
|
|
|
}
|
2019-05-27 18:59:03 +00:00
|
|
|
|
2019-05-30 16:47:54 +00:00
|
|
|
func (shard *EcVolumeShard) Destroy() {
|
|
|
|
os.Remove(shard.FileName() + ToExt(int(shard.ShardId)))
|
2019-07-17 07:03:05 +00:00
|
|
|
stats.VolumeServerVolumeCounter.WithLabelValues(shard.Collection, "ec_shards").Dec()
|
2019-05-30 16:47:54 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 18:59:03 +00:00
|
|
|
func (shard *EcVolumeShard) ReadAt(buf []byte, offset int64) (int, error) {
|
|
|
|
|
|
|
|
return shard.ecdFile.ReadAt(buf, offset)
|
|
|
|
|
|
|
|
}
|