mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-11 06:18:27 +01:00
add "freeze" subcommand to volume
This commit is contained in:
1 parent
dd685fdd8d
commit
f262fed197
7 files changed
+144
-16
No files matched your search
@@ -28,7 +28,7 @@ func NewCdbMap(filename string) (*CdbMap, error) {
|
||||
// writes the content of the index file to a CDB and returns that
|
||||
func NewCdbMapFromIndex(indexFile *os.File) (*CdbMap, error) {
|
||||
nm := indexFile.Name()
|
||||
nm = nm[strings.LastIndex(nm, ".")+1:] + "cdb"
|
||||
nm = nm[:strings.LastIndex(nm, ".")+1] + "cdb"
|
||||
|
||||
var (
|
||||
key uint64
|
||||
@@ -52,12 +52,14 @@ func NewCdbMapFromIndex(indexFile *os.File) (*CdbMap, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Printf("deleted: %s\nnm=%s", deleted, nm)
|
||||
w, err := cdb.NewWriter(nm)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
iterFun := func(buf []byte) error {
|
||||
key = util.BytesToUint64(buf[:8])
|
||||
log.Printf("iter key=%d", key)
|
||||
if _, ok = deleted[key]; !ok {
|
||||
w.PutPair(buf[:8], buf[8:16])
|
||||
}
|
||||
@@ -69,6 +71,9 @@ func NewCdbMapFromIndex(indexFile *os.File) (*CdbMap, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = util.SetFilePerm(nil, nm, 0444, -1); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewCdbMap(nm)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"pkg/util"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type NeedleMap struct {
|
||||
@@ -50,10 +51,40 @@ func NewNeedleMap(file *os.File) *NeedleMap {
|
||||
}
|
||||
|
||||
// Nes frozen (on-disk, not modifiable(!)) needle map
|
||||
func NewFrozenNeedleMap(file *os.File) (*NeedleMap, error) {
|
||||
fm, err := NewCdbMapFromIndex(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
func NewFrozenNeedleMap(fileName string) (*NeedleMap, error) {
|
||||
if strings.HasSuffix(fileName, ".dat") {
|
||||
fileName = fileName[:4]
|
||||
}
|
||||
var (
|
||||
fm *CdbMap
|
||||
indexExists bool
|
||||
)
|
||||
file, err := os.Open(fileName + ".idx")
|
||||
if err != nil && os.IsNotExist(err) {
|
||||
if fm, err = NewCdbMap(fileName + ".cdb"); err != nil {
|
||||
log.Printf("error opening %s.cdb: %s", fileName, err)
|
||||
fm = nil
|
||||
} else {
|
||||
if dstat, e := os.Stat(fileName + ".dat"); e == nil {
|
||||
if cstat, e := os.Stat(fileName + ".cdb"); e == nil {
|
||||
if cstat.ModTime().Before(dstat.ModTime()) {
|
||||
return nil, errors.New("CDB file " + fileName +
|
||||
".cdb is older than data file " + fileName + ".dat!")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
indexExists = true
|
||||
}
|
||||
if fm == nil {
|
||||
fm, err = NewCdbMapFromIndex(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if indexExists {
|
||||
os.Remove(fileName + ".idx")
|
||||
}
|
||||
}
|
||||
return &NeedleMap{
|
||||
fm: fm,
|
||||
|
||||
@@ -33,6 +33,8 @@ func NewStore(port int, ip, publicUrl, dirname string, maxVolumeCount int) (s *S
|
||||
log.Println("Store started on dir:", dirname, "with", len(s.volumes), "volumes")
|
||||
return
|
||||
}
|
||||
|
||||
// adds a volume to the store
|
||||
func (s *Store) AddVolume(volumeListString string, replicationType string) error {
|
||||
rt, e := NewReplicationTypeFromString(replicationType)
|
||||
if e != nil {
|
||||
@@ -74,6 +76,7 @@ func (s *Store) addVolume(vid VolumeId, replicationType ReplicationType) (err er
|
||||
return err
|
||||
}
|
||||
|
||||
// checks whether compaction is needed
|
||||
func (s *Store) CheckCompactVolume(volumeIdString string, garbageThresholdString string) (error, bool) {
|
||||
vid, err := NewVolumeId(volumeIdString)
|
||||
if err != nil {
|
||||
@@ -85,6 +88,8 @@ func (s *Store) CheckCompactVolume(volumeIdString string, garbageThresholdString
|
||||
}
|
||||
return nil, garbageThreshold < s.volumes[vid].garbageLevel()
|
||||
}
|
||||
|
||||
// compacts the volume
|
||||
func (s *Store) CompactVolume(volumeIdString string) error {
|
||||
vid, err := NewVolumeId(volumeIdString)
|
||||
if err != nil {
|
||||
@@ -92,6 +97,8 @@ func (s *Store) CompactVolume(volumeIdString string) error {
|
||||
}
|
||||
return s.volumes[vid].compact()
|
||||
}
|
||||
|
||||
// commits the compaction
|
||||
func (s *Store) CommitCompactVolume(volumeIdString string) error {
|
||||
vid, err := NewVolumeId(volumeIdString)
|
||||
if err != nil {
|
||||
@@ -99,6 +106,8 @@ func (s *Store) CommitCompactVolume(volumeIdString string) error {
|
||||
}
|
||||
return s.volumes[vid].commitCompact()
|
||||
}
|
||||
|
||||
// reads directory and loads volumes
|
||||
func (s *Store) loadExistingVolumes() {
|
||||
if dirs, err := ioutil.ReadDir(s.dir); err == nil {
|
||||
for _, dir := range dirs {
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"pkg/util"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -45,13 +46,18 @@ func (v *Volume) load() error {
|
||||
v.maybeWriteSuperBlock()
|
||||
}
|
||||
// TODO: if .idx not exists, but .cdb exists, then use (but don't load!) that
|
||||
indexFile, ie := os.OpenFile(fileName+".idx", os.O_RDWR|os.O_CREATE, 0644)
|
||||
if ie != nil {
|
||||
return fmt.Errorf("cannot create Volume Data %s.dat: %s", fileName, e)
|
||||
if !util.FileIsWritable(v.dataFile.Name()) { //Read-Only
|
||||
v.nm, e = NewFrozenNeedleMap(fileName)
|
||||
} else {
|
||||
indexFile, ie := os.OpenFile(fileName+".idx", os.O_RDWR|os.O_CREATE, 0644)
|
||||
if ie != nil {
|
||||
return fmt.Errorf("cannot create Volume Data %s.dat: %s", fileName, e)
|
||||
}
|
||||
v.nm, e = LoadNeedleMap(indexFile)
|
||||
}
|
||||
v.nm, e = LoadNeedleMap(indexFile)
|
||||
return e
|
||||
}
|
||||
|
||||
func (v *Volume) Version() Version {
|
||||
return v.version
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user