2019-05-22 05:41:20 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-04-18 02:35:43 +00:00
|
|
|
"golang.org/x/exp/slices"
|
2020-08-26 16:16:58 +00:00
|
|
|
"os"
|
2019-05-22 05:41:20 +00:00
|
|
|
"path"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2023-09-18 21:43:05 +00:00
|
|
|
"strings"
|
2019-05-22 05:41:20 +00:00
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/erasure_coding"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
2019-05-22 05:41:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-02-18 06:05:28 +00:00
|
|
|
re = regexp.MustCompile(`\.ec[0-9][0-9]`)
|
2019-05-22 05:41:20 +00:00
|
|
|
)
|
|
|
|
|
2019-05-28 04:40:51 +00:00
|
|
|
func (l *DiskLocation) FindEcVolume(vid needle.VolumeId) (*erasure_coding.EcVolume, bool) {
|
|
|
|
l.ecVolumesLock.RLock()
|
|
|
|
defer l.ecVolumesLock.RUnlock()
|
2019-05-27 08:29:46 +00:00
|
|
|
|
2019-05-28 04:40:51 +00:00
|
|
|
ecVolume, ok := l.ecVolumes[vid]
|
2019-05-27 08:29:46 +00:00
|
|
|
if ok {
|
2019-05-28 04:40:51 +00:00
|
|
|
return ecVolume, true
|
2019-05-27 08:29:46 +00:00
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2019-06-01 08:51:28 +00:00
|
|
|
func (l *DiskLocation) DestroyEcVolume(vid needle.VolumeId) {
|
|
|
|
l.ecVolumesLock.Lock()
|
|
|
|
defer l.ecVolumesLock.Unlock()
|
|
|
|
|
|
|
|
ecVolume, found := l.ecVolumes[vid]
|
|
|
|
if found {
|
|
|
|
ecVolume.Destroy()
|
2019-06-05 05:04:10 +00:00
|
|
|
delete(l.ecVolumes, vid)
|
2019-06-01 08:51:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-18 05:56:21 +00:00
|
|
|
func (l *DiskLocation) CollectEcShards(vid needle.VolumeId, shardFileNames []string) (ecVolume *erasure_coding.EcVolume, found bool) {
|
|
|
|
l.ecVolumesLock.RLock()
|
|
|
|
defer l.ecVolumesLock.RUnlock()
|
|
|
|
|
|
|
|
ecVolume, found = l.ecVolumes[vid]
|
|
|
|
if !found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, ecShard := range ecVolume.Shards {
|
|
|
|
if ecShard.ShardId < erasure_coding.ShardId(len(shardFileNames)) {
|
|
|
|
shardFileNames[ecShard.ShardId] = erasure_coding.EcShardFileName(ecVolume.Collection, l.Directory, int(ecVolume.VolumeId)) + erasure_coding.ToExt(int(ecShard.ShardId))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-26 06:23:19 +00:00
|
|
|
func (l *DiskLocation) FindEcShard(vid needle.VolumeId, shardId erasure_coding.ShardId) (*erasure_coding.EcVolumeShard, bool) {
|
2019-05-28 04:40:51 +00:00
|
|
|
l.ecVolumesLock.RLock()
|
|
|
|
defer l.ecVolumesLock.RUnlock()
|
2019-05-22 05:41:20 +00:00
|
|
|
|
2019-05-28 04:40:51 +00:00
|
|
|
ecVolume, ok := l.ecVolumes[vid]
|
2019-05-26 06:23:19 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
2019-05-28 04:40:51 +00:00
|
|
|
for _, ecShard := range ecVolume.Shards {
|
2019-05-26 06:23:19 +00:00
|
|
|
if ecShard.ShardId == shardId {
|
|
|
|
return ecShard, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *DiskLocation) LoadEcShard(collection string, vid needle.VolumeId, shardId erasure_coding.ShardId) (err error) {
|
|
|
|
|
2021-02-16 10:47:02 +00:00
|
|
|
ecVolumeShard, err := erasure_coding.NewEcVolumeShard(l.DiskType, l.Directory, collection, vid, shardId)
|
2019-05-26 06:23:19 +00:00
|
|
|
if err != nil {
|
2020-08-26 16:16:58 +00:00
|
|
|
if err == os.ErrNotExist {
|
|
|
|
return os.ErrNotExist
|
|
|
|
}
|
2019-05-26 06:23:19 +00:00
|
|
|
return fmt.Errorf("failed to create ec shard %d.%d: %v", vid, shardId, err)
|
|
|
|
}
|
2019-05-28 04:40:51 +00:00
|
|
|
l.ecVolumesLock.Lock()
|
2019-06-03 09:26:31 +00:00
|
|
|
defer l.ecVolumesLock.Unlock()
|
2019-05-28 05:00:36 +00:00
|
|
|
ecVolume, found := l.ecVolumes[vid]
|
|
|
|
if !found {
|
2021-02-16 10:47:02 +00:00
|
|
|
ecVolume, err = erasure_coding.NewEcVolume(l.DiskType, l.Directory, l.IdxDirectory, collection, vid)
|
2019-05-28 05:00:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create ec volume %d: %v", vid, err)
|
|
|
|
}
|
|
|
|
l.ecVolumes[vid] = ecVolume
|
|
|
|
}
|
|
|
|
ecVolume.AddEcVolumeShard(ecVolumeShard)
|
2019-05-26 06:23:19 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *DiskLocation) UnloadEcShard(vid needle.VolumeId, shardId erasure_coding.ShardId) bool {
|
|
|
|
|
2019-05-28 04:40:51 +00:00
|
|
|
l.ecVolumesLock.Lock()
|
|
|
|
defer l.ecVolumesLock.Unlock()
|
2019-05-26 06:23:19 +00:00
|
|
|
|
2019-05-28 04:40:51 +00:00
|
|
|
ecVolume, found := l.ecVolumes[vid]
|
2019-05-26 06:23:19 +00:00
|
|
|
if !found {
|
|
|
|
return false
|
|
|
|
}
|
2019-06-01 08:41:22 +00:00
|
|
|
if _, deleted := ecVolume.DeleteEcVolumeShard(shardId); deleted {
|
2019-05-28 04:40:51 +00:00
|
|
|
if len(ecVolume.Shards) == 0 {
|
|
|
|
delete(l.ecVolumes, vid)
|
2019-06-06 06:20:26 +00:00
|
|
|
ecVolume.Close()
|
2019-05-26 06:23:19 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *DiskLocation) loadEcShards(shards []string, collection string, vid needle.VolumeId) (err error) {
|
|
|
|
|
|
|
|
for _, shard := range shards {
|
2019-05-22 05:41:20 +00:00
|
|
|
shardId, err := strconv.ParseInt(path.Ext(shard)[3:], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse ec shard name %v: %v", shard, err)
|
|
|
|
}
|
2019-05-26 06:23:19 +00:00
|
|
|
|
|
|
|
err = l.LoadEcShard(collection, vid, erasure_coding.ShardId(shardId))
|
2019-05-22 05:41:20 +00:00
|
|
|
if err != nil {
|
2019-05-26 06:23:19 +00:00
|
|
|
return fmt.Errorf("failed to load ec shard %v: %v", shard, err)
|
2019-05-22 05:41:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-26 06:23:19 +00:00
|
|
|
func (l *DiskLocation) loadAllEcShards() (err error) {
|
2019-05-22 05:41:20 +00:00
|
|
|
|
2021-10-14 04:27:58 +00:00
|
|
|
dirEntries, err := os.ReadDir(l.Directory)
|
2019-05-22 05:41:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("load all ec shards in dir %s: %v", l.Directory, err)
|
|
|
|
}
|
2020-11-27 11:17:10 +00:00
|
|
|
if l.IdxDirectory != l.Directory {
|
2021-10-14 04:27:58 +00:00
|
|
|
indexDirEntries, err := os.ReadDir(l.IdxDirectory)
|
2020-11-27 11:17:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("load all ec shards in dir %s: %v", l.IdxDirectory, err)
|
|
|
|
}
|
2021-10-14 04:27:58 +00:00
|
|
|
dirEntries = append(dirEntries, indexDirEntries...)
|
2020-11-27 11:17:10 +00:00
|
|
|
}
|
2023-09-18 21:43:05 +00:00
|
|
|
slices.SortFunc(dirEntries, func(a, b os.DirEntry) int {
|
|
|
|
return strings.Compare(a.Name(), b.Name())
|
2019-05-22 05:41:20 +00:00
|
|
|
})
|
|
|
|
var sameVolumeShards []string
|
|
|
|
var prevVolumeId needle.VolumeId
|
2021-10-14 04:27:58 +00:00
|
|
|
for _, fileInfo := range dirEntries {
|
2019-05-26 06:23:19 +00:00
|
|
|
if fileInfo.IsDir() {
|
2019-05-22 05:41:20 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
ext := path.Ext(fileInfo.Name())
|
|
|
|
name := fileInfo.Name()
|
|
|
|
baseName := name[:len(name)-len(ext)]
|
|
|
|
|
|
|
|
collection, volumeId, err := parseCollectionVolumeId(baseName)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-05-26 06:23:19 +00:00
|
|
|
if re.MatchString(ext) {
|
2019-05-22 05:41:20 +00:00
|
|
|
if prevVolumeId == 0 || volumeId == prevVolumeId {
|
|
|
|
sameVolumeShards = append(sameVolumeShards, fileInfo.Name())
|
2019-05-26 06:23:19 +00:00
|
|
|
} else {
|
2019-05-22 05:41:20 +00:00
|
|
|
sameVolumeShards = []string{fileInfo.Name()}
|
|
|
|
}
|
|
|
|
prevVolumeId = volumeId
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-05-26 06:23:19 +00:00
|
|
|
if ext == ".ecx" && volumeId == prevVolumeId {
|
|
|
|
if err = l.loadEcShards(sameVolumeShards, collection, volumeId); err != nil {
|
2019-05-22 05:41:20 +00:00
|
|
|
return fmt.Errorf("loadEcShards collection:%v volumeId:%d : %v", collection, volumeId, err)
|
|
|
|
}
|
|
|
|
prevVolumeId = volumeId
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-05-30 16:47:54 +00:00
|
|
|
|
|
|
|
func (l *DiskLocation) deleteEcVolumeById(vid needle.VolumeId) (e error) {
|
|
|
|
ecVolume, ok := l.ecVolumes[vid]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ecVolume.Destroy()
|
|
|
|
delete(l.ecVolumes, vid)
|
|
|
|
return
|
|
|
|
}
|
2019-12-24 09:20:34 +00:00
|
|
|
|
|
|
|
func (l *DiskLocation) unmountEcVolumeByCollection(collectionName string) map[needle.VolumeId]*erasure_coding.EcVolume {
|
|
|
|
deltaVols := make(map[needle.VolumeId]*erasure_coding.EcVolume, 0)
|
|
|
|
for k, v := range l.ecVolumes {
|
|
|
|
if v.Collection == collectionName {
|
|
|
|
deltaVols[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, _ := range deltaVols {
|
|
|
|
delete(l.ecVolumes, k)
|
|
|
|
}
|
|
|
|
return deltaVols
|
|
|
|
}
|
2020-03-22 23:21:42 +00:00
|
|
|
|
|
|
|
func (l *DiskLocation) EcVolumesLen() int {
|
|
|
|
l.ecVolumesLock.RLock()
|
|
|
|
defer l.ecVolumesLock.RUnlock()
|
|
|
|
|
|
|
|
return len(l.ecVolumes)
|
|
|
|
}
|