Browse Source

Revert "add "freeze" subcommand to volume"

This reverts commit f262fed197.
pull/2/head
Chris Lu 12 years ago
parent
commit
3b21317863
  1. 27
      weed-fs/src/cmd/weed/fix.go
  2. 1
      weed-fs/src/cmd/weed/weed.go
  3. 7
      weed-fs/src/pkg/storage/cdb_map.go
  4. 35
      weed-fs/src/pkg/storage/needle_map.go
  5. 9
      weed-fs/src/pkg/storage/store.go
  6. 6
      weed-fs/src/pkg/storage/volume.go
  7. 63
      weed-fs/src/pkg/util/file.go

27
weed-fs/src/cmd/weed/fix.go

@ -1,7 +1,6 @@
package main
import (
"errors"
"log"
"os"
"path"
@ -34,36 +33,24 @@ func runFix(cmd *Command, args []string) bool {
}
fileName := strconv.Itoa(*volumeId)
if err := createIndexFile(path.Join(*dir, fileName+".dat")); err != nil {
log.Fatalf("[ERROR] " + err.Error())
}
return true
}
func createIndexFile(datafn string) error {
dataFile, e := os.OpenFile(datafn, os.O_RDONLY, 0644)
dataFile, e := os.OpenFile(path.Join(*dir, fileName+".dat"), os.O_RDONLY, 0644)
if e != nil {
return errors.New("Read Volume " + e.Error())
log.Fatalf("Read Volume [ERROR] %s\n", e)
}
defer dataFile.Close()
// log.Printf("dataFile=%s", dataFile)
indexFile, ie := os.OpenFile(datafn[:len(datafn)-4]+".idx", os.O_WRONLY|os.O_CREATE, 0644)
indexFile, ie := os.OpenFile(path.Join(*dir, fileName+".idx"), os.O_WRONLY|os.O_CREATE, 0644)
if ie != nil {
return errors.New("Create Volume Index " + ie.Error())
log.Fatalf("Create Volume Index [ERROR] %s\n", ie)
}
defer indexFile.Close()
dataFile.Seek(0, 0)
header := make([]byte, storage.SuperBlockSize)
if _, e := dataFile.Read(header); e != nil {
return errors.New("cannot read superblock: " + e.Error())
log.Fatalf("cannot read superblock: %s", e)
}
ver, _, e := storage.ParseSuperBlock(header)
if e != nil {
return errors.New("cannot parse superblock: " + e.Error())
}
ver, _, _ := storage.ParseSuperBlock(header)
n, rest := storage.ReadNeedleHeader(dataFile, ver)
dataFile.Seek(int64(rest), 1)
@ -79,5 +66,5 @@ func createIndexFile(datafn string) error {
n, rest = storage.ReadNeedleHeader(dataFile, ver)
dataFile.Seek(int64(rest), 1)
}
return nil
return true
}

1
weed-fs/src/cmd/weed/weed.go

@ -21,7 +21,6 @@ var server *string
var commands = []*Command{
cmdFix,
cmdFreeze,
cmdMaster,
cmdUpload,
cmdShell,

7
weed-fs/src/pkg/storage/cdb_map.go

@ -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,14 +52,12 @@ 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])
}
@ -71,9 +69,6 @@ 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)
}

35
weed-fs/src/pkg/storage/needle_map.go

@ -6,7 +6,6 @@ import (
"log"
"os"
"pkg/util"
"strings"
)
type NeedleMap struct {
@ -51,41 +50,11 @@ func NewNeedleMap(file *os.File) *NeedleMap {
}
// Nes frozen (on-disk, not modifiable(!)) needle map
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)
func NewFrozenNeedleMap(file *os.File) (*NeedleMap, error) {
fm, err := NewCdbMapFromIndex(file)
if err != nil {
return nil, err
}
if indexExists {
os.Remove(fileName + ".idx")
}
}
return &NeedleMap{
fm: fm,
bytes: make([]byte, 16),

9
weed-fs/src/pkg/storage/store.go

@ -33,8 +33,6 @@ 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 {
@ -76,7 +74,6 @@ 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 {
@ -88,8 +85,6 @@ 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 {
@ -97,8 +92,6 @@ 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 {
@ -106,8 +99,6 @@ 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
weed-fs/src/pkg/storage/volume.go

@ -6,7 +6,6 @@ import (
"log"
"os"
"path"
"pkg/util"
"sync"
)
@ -46,18 +45,13 @@ func (v *Volume) load() error {
v.maybeWriteSuperBlock()
}
// TODO: if .idx not exists, but .cdb exists, then use (but don't load!) that
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)
}
return e
}
func (v *Volume) Version() Version {
return v.version
}

63
weed-fs/src/pkg/util/file.go

@ -1,63 +0,0 @@
package util
import (
"errors"
"log"
"os"
)
// sets file (fh if not nil, otherwise fileName) permission to mask
// it will
// AND with the permission iff direction < 0
// OR with the permission iff direction > 0
// otherwise it will SET the permission to the mask
func SetFilePerm(fh *os.File, fileName string, mask os.FileMode, direction int8) (err error) {
var stat os.FileInfo
if fh == nil {
stat, err = os.Stat(fileName)
} else {
stat, err = fh.Stat()
}
if err != nil {
return err
}
mode := stat.Mode() & ^os.ModePerm
// log.Printf("mode1=%d mask=%d", mode, mask)
if direction == 0 {
mode |= mask
} else if direction > 0 {
mode |= stat.Mode().Perm() | mask
} else {
mode |= stat.Mode().Perm() & mask
}
log.Printf("pmode=%d operm=%d => nmode=%d nperm=%d",
stat.Mode(), stat.Mode()&os.ModePerm,
mode, mode&os.ModePerm)
if mode == 0 {
return errors.New("Zero FileMode")
}
if fh == nil {
err = os.Chmod(fileName, mode)
} else {
err = fh.Chmod(mode)
}
return err
}
// returns whether the filename exists - errors doesn't mean not exists!
func FileExists(fileName string) bool {
if _, e := os.Stat(fileName); e != nil && os.IsNotExist(e) {
return false
}
return true
}
// returns whether the filename is POSSIBLY writable
//- whether it has some kind of writable bit set
func FileIsWritable(fileName string) bool {
if stat, e := os.Stat(fileName); e == nil {
return stat.Mode().Perm()&0222 > 0
}
return false
}
Loading…
Cancel
Save