mirror of
https://github.com/chrislusf/seaweedfs
synced 2024-09-12 12:00:45 +02:00
* avoid data race of TraverseBfs * close is enough avoid panic I1014 12:29:59.207120 volume_loading.go:131 loading sorted db /tmp/sw/test2_19.sdx error: unexpected file /tmp/sw/test2_19.idx size: 255 I1014 12:29:59.207125 volume_loading.go:119 open to write file /tmp/sw/test4_26.idx panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x5260a4c] goroutine 287 [running]: github.com/seaweedfs/seaweedfs/weed/storage.(*SortedFileNeedleMap).Close(0x0) /Users/tochka/GolandProjects/seaweedfs/weed/storage/needle_map_sorted_file.go:97 +0x2c github.com/seaweedfs/seaweedfs/weed/storage.(*Volume).load.func1() /Users/tochka/GolandProjects/seaweedfs/weed/storage/volume_loading.go:32 +0x8e github.com/seaweedfs/seaweedfs/weed/storage.(*Volume).load(0xc001b36280, 0x1, 0x1, 0x0, 0x69228c0?) /Users/tochka/GolandProjects/seaweedfs/weed/storage/volume_loading.go:205 +0x256c github.com/seaweedfs/seaweedfs/weed/storage.NewVolume({0x7ffeefbff6e0, 0x7}, {0x7ffeefbff6e0, 0x7}, {0xc0009a9284, 0x5}, 0x13, 0x0, 0x0, 0x0, ...) /Users/tochka/GolandProjects/seaweedfs/weed/storage/volume.go:62 +0x30f github.com/seaweedfs/seaweedfs/weed/storage.(*DiskLocation).loadExistingVolume(0xc0006f40c0, {0x846c8d0, 0xc0009ce600}, 0x0?, 0x1) /Users/tochka/GolandProjects/seaweedfs/weed/storage/disk_location.go:161 +0x4da github.com/seaweedfs/seaweedfs/weed/storage.(*DiskLocation).concurrentLoadingVolumes.func2() /Users/tochka/GolandProjects/seaweedfs/weed/storage/disk_location.go:201 +0xf9 created by github.com/seaweedfs/seaweedfs/weed/storage.(*DiskLocation).concurrentLoadingVolumes /Users/tochka/GolandProjects/seaweedfs/weed/storage/disk_location.go:198 +0x150
113 lines
3 KiB
Go
113 lines
3 KiB
Go
package storage
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/erasure_coding"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle_map"
|
|
. "github.com/seaweedfs/seaweedfs/weed/storage/types"
|
|
)
|
|
|
|
type SortedFileNeedleMap struct {
|
|
baseNeedleMapper
|
|
baseFileName string
|
|
dbFile *os.File
|
|
dbFileSize int64
|
|
}
|
|
|
|
func NewSortedFileNeedleMap(indexBaseFileName string, indexFile *os.File) (m *SortedFileNeedleMap, err error) {
|
|
m = &SortedFileNeedleMap{baseFileName: indexBaseFileName}
|
|
m.indexFile = indexFile
|
|
fileName := indexBaseFileName + ".sdx"
|
|
if !isSortedFileFresh(fileName, indexFile) {
|
|
glog.V(0).Infof("Start to Generate %s from %s", fileName, indexFile.Name())
|
|
erasure_coding.WriteSortedFileFromIdx(indexBaseFileName, ".sdx")
|
|
glog.V(0).Infof("Finished Generating %s from %s", fileName, indexFile.Name())
|
|
}
|
|
glog.V(1).Infof("Opening %s...", fileName)
|
|
|
|
if m.dbFile, err = os.Open(indexBaseFileName + ".sdx"); err != nil {
|
|
return
|
|
}
|
|
dbStat, _ := m.dbFile.Stat()
|
|
m.dbFileSize = dbStat.Size()
|
|
glog.V(1).Infof("Loading %s...", indexFile.Name())
|
|
mm, indexLoadError := newNeedleMapMetricFromIndexFile(indexFile)
|
|
if indexLoadError != nil {
|
|
return nil, indexLoadError
|
|
}
|
|
m.mapMetric = *mm
|
|
return
|
|
}
|
|
|
|
func isSortedFileFresh(dbFileName string, indexFile *os.File) bool {
|
|
// normally we always write to index file first
|
|
dbFile, err := os.Open(dbFileName)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
defer dbFile.Close()
|
|
dbStat, dbStatErr := dbFile.Stat()
|
|
indexStat, indexStatErr := indexFile.Stat()
|
|
if dbStatErr != nil || indexStatErr != nil {
|
|
glog.V(0).Infof("Can not stat file: %v and %v", dbStatErr, indexStatErr)
|
|
return false
|
|
}
|
|
|
|
return dbStat.ModTime().After(indexStat.ModTime())
|
|
}
|
|
|
|
func (m *SortedFileNeedleMap) Get(key NeedleId) (element *needle_map.NeedleValue, ok bool) {
|
|
offset, size, err := erasure_coding.SearchNeedleFromSortedIndex(m.dbFile, m.dbFileSize, key, nil)
|
|
ok = err == nil
|
|
return &needle_map.NeedleValue{Key: key, Offset: offset, Size: size}, ok
|
|
|
|
}
|
|
|
|
func (m *SortedFileNeedleMap) Put(key NeedleId, offset Offset, size Size) error {
|
|
return os.ErrInvalid
|
|
}
|
|
|
|
func (m *SortedFileNeedleMap) Delete(key NeedleId, offset Offset) error {
|
|
|
|
_, size, err := erasure_coding.SearchNeedleFromSortedIndex(m.dbFile, m.dbFileSize, key, nil)
|
|
|
|
if err != nil {
|
|
if err == erasure_coding.NotFoundError {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
|
|
if size.IsDeleted() {
|
|
return nil
|
|
}
|
|
|
|
// write to index file first
|
|
if err := m.appendToIndexFile(key, offset, TombstoneFileSize); err != nil {
|
|
return err
|
|
}
|
|
_, _, err = erasure_coding.SearchNeedleFromSortedIndex(m.dbFile, m.dbFileSize, key, erasure_coding.MarkNeedleDeleted)
|
|
|
|
return err
|
|
}
|
|
|
|
func (m *SortedFileNeedleMap) Close() {
|
|
if m == nil {
|
|
return
|
|
}
|
|
if m.indexFile != nil {
|
|
m.indexFile.Close()
|
|
}
|
|
if m.dbFile != nil {
|
|
m.dbFile.Close()
|
|
}
|
|
}
|
|
|
|
func (m *SortedFileNeedleMap) Destroy() error {
|
|
m.Close()
|
|
os.Remove(m.indexFile.Name())
|
|
return os.Remove(m.baseFileName + ".sdx")
|
|
}
|