2019-05-22 13:41:20 +08:00
|
|
|
package erasure_coding
|
|
|
|
|
|
|
|
import (
|
2019-06-20 13:57:14 +08:00
|
|
|
"errors"
|
2019-05-28 13:00:36 +08:00
|
|
|
"fmt"
|
2019-05-25 04:28:44 +08:00
|
|
|
"math"
|
2019-05-28 13:00:36 +08:00
|
|
|
"os"
|
2019-05-25 04:28:44 +08:00
|
|
|
"sort"
|
2019-05-28 13:54:58 +08:00
|
|
|
"sync"
|
|
|
|
"time"
|
2019-05-22 13:41:20 +08:00
|
|
|
|
2019-12-29 04:44:59 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb"
|
2019-05-22 13:41:20 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
|
2019-12-29 13:52:06 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
|
2019-05-28 13:00:36 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/idx"
|
2019-05-22 13:41:20 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
2019-05-28 02:59:03 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/types"
|
2019-05-22 13:41:20 +08:00
|
|
|
)
|
|
|
|
|
2019-06-20 13:57:14 +08:00
|
|
|
var (
|
|
|
|
NotFoundError = errors.New("needle not found")
|
|
|
|
)
|
|
|
|
|
2019-05-28 12:40:51 +08:00
|
|
|
type EcVolume struct {
|
2019-05-28 13:54:58 +08:00
|
|
|
VolumeId needle.VolumeId
|
|
|
|
Collection string
|
|
|
|
dir string
|
|
|
|
ecxFile *os.File
|
|
|
|
ecxFileSize int64
|
2019-06-05 12:52:37 +08:00
|
|
|
ecxCreatedAt time.Time
|
2019-05-28 13:54:58 +08:00
|
|
|
Shards []*EcVolumeShard
|
|
|
|
ShardLocations map[ShardId][]string
|
|
|
|
ShardLocationsRefreshTime time.Time
|
|
|
|
ShardLocationsLock sync.RWMutex
|
2019-05-31 15:58:51 +08:00
|
|
|
Version needle.Version
|
2019-06-20 13:57:14 +08:00
|
|
|
ecjFile *os.File
|
|
|
|
ecjFileAccessLock sync.Mutex
|
2019-05-28 13:00:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewEcVolume(dir string, collection string, vid needle.VolumeId) (ev *EcVolume, err error) {
|
|
|
|
ev = &EcVolume{dir: dir, Collection: collection, VolumeId: vid}
|
|
|
|
|
|
|
|
baseFileName := EcShardFileName(collection, dir, int(vid))
|
|
|
|
|
|
|
|
// open ecx file
|
2019-06-20 13:57:14 +08:00
|
|
|
if ev.ecxFile, err = os.OpenFile(baseFileName+".ecx", os.O_RDWR, 0644); err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot open ec volume index %s.ecx: %v", baseFileName, err)
|
2019-05-28 13:00:36 +08:00
|
|
|
}
|
|
|
|
ecxFi, statErr := ev.ecxFile.Stat()
|
|
|
|
if statErr != nil {
|
|
|
|
return nil, fmt.Errorf("can not stat ec volume index %s.ecx: %v", baseFileName, statErr)
|
|
|
|
}
|
|
|
|
ev.ecxFileSize = ecxFi.Size()
|
2019-06-05 12:52:37 +08:00
|
|
|
ev.ecxCreatedAt = ecxFi.ModTime()
|
2019-05-28 13:00:36 +08:00
|
|
|
|
2019-06-20 13:57:14 +08:00
|
|
|
// open ecj file
|
|
|
|
if ev.ecjFile, err = os.OpenFile(baseFileName+".ecj", os.O_RDWR|os.O_CREATE, 0644); err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot open ec volume journal %s.ecj: %v", baseFileName, err)
|
|
|
|
}
|
|
|
|
|
2019-12-29 04:44:59 +08:00
|
|
|
// read volume info
|
|
|
|
ev.Version = needle.Version3
|
|
|
|
if volumeInfo, found := pb.MaybeLoadVolumeInfo(baseFileName + ".vif"); found {
|
|
|
|
ev.Version = needle.Version(volumeInfo.Version)
|
2019-12-29 13:52:06 +08:00
|
|
|
} else {
|
|
|
|
pb.SaveVolumeInfo(baseFileName+".vif", &volume_server_pb.VolumeInfo{Version: uint32(ev.Version)})
|
2019-12-29 04:44:59 +08:00
|
|
|
}
|
|
|
|
|
2019-05-28 15:51:01 +08:00
|
|
|
ev.ShardLocations = make(map[ShardId][]string)
|
|
|
|
|
2019-05-28 13:00:36 +08:00
|
|
|
return
|
2019-05-28 12:40:51 +08:00
|
|
|
}
|
2019-05-22 13:41:20 +08:00
|
|
|
|
2019-05-28 12:40:51 +08:00
|
|
|
func (ev *EcVolume) AddEcVolumeShard(ecVolumeShard *EcVolumeShard) bool {
|
|
|
|
for _, s := range ev.Shards {
|
2019-05-22 13:41:20 +08:00
|
|
|
if s.ShardId == ecVolumeShard.ShardId {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 12:40:51 +08:00
|
|
|
ev.Shards = append(ev.Shards, ecVolumeShard)
|
2019-05-28 15:51:01 +08:00
|
|
|
sort.Slice(ev.Shards, func(i, j int) bool {
|
2019-05-28 12:40:51 +08:00
|
|
|
return ev.Shards[i].VolumeId < ev.Shards[j].VolumeId ||
|
|
|
|
ev.Shards[i].VolumeId == ev.Shards[j].VolumeId && ev.Shards[i].ShardId < ev.Shards[j].ShardId
|
2019-05-25 04:28:44 +08:00
|
|
|
})
|
2019-05-22 13:41:20 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-06-01 16:41:22 +08:00
|
|
|
func (ev *EcVolume) DeleteEcVolumeShard(shardId ShardId) (ecVolumeShard *EcVolumeShard, deleted bool) {
|
2019-05-22 13:41:20 +08:00
|
|
|
foundPosition := -1
|
2019-05-28 12:40:51 +08:00
|
|
|
for i, s := range ev.Shards {
|
|
|
|
if s.ShardId == shardId {
|
2019-05-22 13:41:20 +08:00
|
|
|
foundPosition = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if foundPosition < 0 {
|
2019-06-01 16:41:22 +08:00
|
|
|
return nil, false
|
2019-05-22 13:41:20 +08:00
|
|
|
}
|
|
|
|
|
2019-06-01 16:41:22 +08:00
|
|
|
ecVolumeShard = ev.Shards[foundPosition]
|
|
|
|
|
2019-05-28 12:40:51 +08:00
|
|
|
ev.Shards = append(ev.Shards[:foundPosition], ev.Shards[foundPosition+1:]...)
|
2019-06-01 16:41:22 +08:00
|
|
|
return ecVolumeShard, true
|
2019-05-22 13:41:20 +08:00
|
|
|
}
|
|
|
|
|
2019-05-28 12:40:51 +08:00
|
|
|
func (ev *EcVolume) FindEcVolumeShard(shardId ShardId) (ecVolumeShard *EcVolumeShard, found bool) {
|
|
|
|
for _, s := range ev.Shards {
|
2019-05-27 16:29:46 +08:00
|
|
|
if s.ShardId == shardId {
|
|
|
|
return s, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2019-05-28 12:40:51 +08:00
|
|
|
func (ev *EcVolume) Close() {
|
|
|
|
for _, s := range ev.Shards {
|
2019-05-22 13:41:20 +08:00
|
|
|
s.Close()
|
|
|
|
}
|
2019-06-20 13:57:14 +08:00
|
|
|
if ev.ecjFile != nil {
|
|
|
|
ev.ecjFileAccessLock.Lock()
|
|
|
|
_ = ev.ecjFile.Close()
|
|
|
|
ev.ecjFile = nil
|
|
|
|
ev.ecjFileAccessLock.Unlock()
|
|
|
|
}
|
2019-05-28 13:00:36 +08:00
|
|
|
if ev.ecxFile != nil {
|
|
|
|
_ = ev.ecxFile.Close()
|
|
|
|
ev.ecxFile = nil
|
|
|
|
}
|
2019-05-22 13:41:20 +08:00
|
|
|
}
|
|
|
|
|
2019-05-31 00:47:54 +08:00
|
|
|
func (ev *EcVolume) Destroy() {
|
|
|
|
|
|
|
|
ev.Close()
|
|
|
|
|
|
|
|
for _, s := range ev.Shards {
|
|
|
|
s.Destroy()
|
|
|
|
}
|
2019-06-01 16:41:22 +08:00
|
|
|
os.Remove(ev.FileName() + ".ecx")
|
2019-06-20 13:57:14 +08:00
|
|
|
os.Remove(ev.FileName() + ".ecj")
|
2019-12-29 04:28:58 +08:00
|
|
|
os.Remove(ev.FileName() + ".vif")
|
2019-06-01 16:41:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EcVolume) FileName() string {
|
|
|
|
|
|
|
|
return EcShardFileName(ev.Collection, ev.dir, int(ev.VolumeId))
|
|
|
|
|
2019-05-31 00:47:54 +08:00
|
|
|
}
|
|
|
|
|
2019-06-05 12:52:37 +08:00
|
|
|
func (ev *EcVolume) ShardSize() int64 {
|
|
|
|
if len(ev.Shards) > 0 {
|
|
|
|
return ev.Shards[0].Size()
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EcVolume) CreatedAt() time.Time {
|
|
|
|
return ev.ecxCreatedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *EcVolume) ShardIdList() (shardIds []ShardId) {
|
|
|
|
for _, s := range ev.Shards {
|
|
|
|
shardIds = append(shardIds, s.ShardId)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-28 12:40:51 +08:00
|
|
|
func (ev *EcVolume) ToVolumeEcShardInformationMessage() (messages []*master_pb.VolumeEcShardInformationMessage) {
|
2019-05-25 04:28:44 +08:00
|
|
|
prevVolumeId := needle.VolumeId(math.MaxUint32)
|
|
|
|
var m *master_pb.VolumeEcShardInformationMessage
|
2019-05-28 12:40:51 +08:00
|
|
|
for _, s := range ev.Shards {
|
2019-05-25 04:28:44 +08:00
|
|
|
if s.VolumeId != prevVolumeId {
|
|
|
|
m = &master_pb.VolumeEcShardInformationMessage{
|
|
|
|
Id: uint32(s.VolumeId),
|
|
|
|
Collection: s.Collection,
|
|
|
|
}
|
|
|
|
messages = append(messages, m)
|
2019-05-22 13:41:20 +08:00
|
|
|
}
|
2019-05-25 04:28:44 +08:00
|
|
|
prevVolumeId = s.VolumeId
|
|
|
|
m.EcIndexBits = uint32(ShardBits(m.EcIndexBits).AddShardId(s.ShardId))
|
2019-05-22 13:41:20 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-20 15:17:11 +08:00
|
|
|
func (ev *EcVolume) LocateEcShardNeedle(needleId types.NeedleId, version needle.Version) (offset types.Offset, size uint32, intervals []Interval, err error) {
|
2019-05-22 13:41:20 +08:00
|
|
|
|
2019-05-27 16:29:46 +08:00
|
|
|
// find the needle from ecx file
|
2019-06-21 16:14:10 +08:00
|
|
|
offset, size, err = ev.FindNeedleFromEcx(needleId)
|
2019-05-27 16:29:46 +08:00
|
|
|
if err != nil {
|
2019-06-21 16:14:10 +08:00
|
|
|
return types.Offset{}, 0, nil, fmt.Errorf("FindNeedleFromEcx: %v", err)
|
2019-05-22 13:41:20 +08:00
|
|
|
}
|
|
|
|
|
2019-05-28 13:00:36 +08:00
|
|
|
shard := ev.Shards[0]
|
|
|
|
|
2019-05-27 16:29:46 +08:00
|
|
|
// calculate the locations in the ec shards
|
2019-05-29 12:29:07 +08:00
|
|
|
intervals = LocateData(ErasureCodingLargeBlockSize, ErasureCodingSmallBlockSize, DataShardsCount*shard.ecdFileSize, offset.ToAcutalOffset(), uint32(needle.GetActualSize(size, version)))
|
2019-05-28 13:00:36 +08:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-21 16:14:10 +08:00
|
|
|
func (ev *EcVolume) FindNeedleFromEcx(needleId types.NeedleId) (offset types.Offset, size uint32, err error) {
|
2019-12-18 17:21:21 +08:00
|
|
|
return SearchNeedleFromSortedIndex(ev.ecxFile, ev.ecxFileSize, needleId, nil)
|
2019-06-20 13:57:14 +08:00
|
|
|
}
|
|
|
|
|
2019-12-18 17:21:21 +08:00
|
|
|
func SearchNeedleFromSortedIndex(ecxFile *os.File, ecxFileSize int64, needleId types.NeedleId, processNeedleFn func(file *os.File, offset int64) error) (offset types.Offset, size uint32, err error) {
|
2019-05-28 13:00:36 +08:00
|
|
|
var key types.NeedleId
|
|
|
|
buf := make([]byte, types.NeedleMapEntrySize)
|
2019-06-20 13:57:14 +08:00
|
|
|
l, h := int64(0), ecxFileSize/types.NeedleMapEntrySize
|
2019-05-28 13:00:36 +08:00
|
|
|
for l < h {
|
|
|
|
m := (l + h) / 2
|
2019-06-20 13:57:14 +08:00
|
|
|
if _, err := ecxFile.ReadAt(buf, m*types.NeedleMapEntrySize); err != nil {
|
|
|
|
return types.Offset{}, types.TombstoneFileSize, fmt.Errorf("ecx file %d read at %d: %v", ecxFileSize, m*types.NeedleMapEntrySize, err)
|
2019-05-28 13:00:36 +08:00
|
|
|
}
|
|
|
|
key, offset, size = idx.IdxFileEntry(buf)
|
|
|
|
if key == needleId {
|
2019-06-20 13:57:14 +08:00
|
|
|
if processNeedleFn != nil {
|
|
|
|
err = processNeedleFn(ecxFile, m*types.NeedleHeaderSize)
|
|
|
|
}
|
2019-05-28 13:00:36 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if key < needleId {
|
|
|
|
l = m + 1
|
|
|
|
} else {
|
|
|
|
h = m
|
|
|
|
}
|
|
|
|
}
|
2019-05-27 16:29:46 +08:00
|
|
|
|
2019-06-20 13:57:14 +08:00
|
|
|
err = NotFoundError
|
2019-05-28 02:59:03 +08:00
|
|
|
return
|
2019-05-22 13:41:20 +08:00
|
|
|
}
|