From 7cef280bdc77099e0048692893f440038c8cc0cb Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 11 Aug 2013 11:38:55 -0700 Subject: [PATCH] handle cases when .idx files are also readonly adjusting log level --- go/storage/cdb_map.go | 4 ++-- go/storage/compress.go | 6 +++--- go/storage/file_id.go | 2 +- go/storage/volume.go | 29 +++++++++++++++++++---------- go/weed/volume_test.go | 3 ++- 5 files changed, 27 insertions(+), 17 deletions(-) diff --git a/go/storage/cdb_map.go b/go/storage/cdb_map.go index ebb49d514..451d8727a 100644 --- a/go/storage/cdb_map.go +++ b/go/storage/cdb_map.go @@ -206,12 +206,12 @@ func DumpNeedleMapToCdb(cdbName string, nm *NeedleMap) error { func openTempCdb(fileName string) (cdb.AdderFunc, cdb.CloserFunc, error) { fh, err := os.Create(fileName) if err != nil { - return nil, nil, fmt.Errorf("cannot create cdb file %s: %s", fileName, err) + return nil, nil, fmt.Errorf("cannot create cdb file %s: %s", fileName, err.Error()) } adder, closer, err := cdb.MakeFactory(fh) if err != nil { fh.Close() - return nil, nil, fmt.Errorf("error creating factory: %s", err) + return nil, nil, fmt.Errorf("error creating factory: %s", err.Error()) } return adder, func() error { if e := closer(); e != nil { diff --git a/go/storage/compress.go b/go/storage/compress.go index add66792a..eaf3c9ad8 100644 --- a/go/storage/compress.go +++ b/go/storage/compress.go @@ -37,11 +37,11 @@ func GzipData(input []byte) ([]byte, error) { buf := new(bytes.Buffer) w, _ := gzip.NewWriterLevel(buf, flate.BestCompression) if _, err := w.Write(input); err != nil { - glog.V(4).Infoln("error compressing data:", err) + glog.V(2).Infoln("error compressing data:", err) return nil, err } if err := w.Close(); err != nil { - glog.V(4).Infoln("error closing compressed data:", err) + glog.V(2).Infoln("error closing compressed data:", err) return nil, err } return buf.Bytes(), nil @@ -52,7 +52,7 @@ func UnGzipData(input []byte) ([]byte, error) { defer r.Close() output, err := ioutil.ReadAll(r) if err != nil { - glog.V(4).Infoln("error uncompressing data:", err) + glog.V(2).Infoln("error uncompressing data:", err) } return output, err } diff --git a/go/storage/file_id.go b/go/storage/file_id.go index 20d508a8b..f482af029 100644 --- a/go/storage/file_id.go +++ b/go/storage/file_id.go @@ -19,7 +19,7 @@ func NewFileId(VolumeId VolumeId, Key uint64, Hashcode uint32) *FileId { func ParseFileId(fid string) *FileId { a := strings.Split(fid, ",") if len(a) != 2 { - glog.V(4).Infoln("Invalid fid", fid, ", split length", len(a)) + glog.V(1).Infoln("Invalid fid", fid, ", split length", len(a)) return nil } vid_string, key_hash_string := a[0], a[1] diff --git a/go/storage/volume.go b/go/storage/volume.go index c212e9b37..ef720ff75 100644 --- a/go/storage/volume.go +++ b/go/storage/volume.go @@ -73,12 +73,12 @@ func (v *Volume) load(alsoLoadIndex bool) error { if e == nil && alsoLoadIndex { var indexFile *os.File if v.readOnly { - glog.V(4).Infoln("opening file", fileName+".idx") + glog.V(2).Infoln("opening file", fileName+".idx") if indexFile, e = os.Open(fileName + ".idx"); e != nil && !os.IsNotExist(e) { return fmt.Errorf("cannot open index file %s.idx: %s", fileName, e.Error()) } if indexFile != nil { - glog.V(4).Infoln("check file", fileName+".cdb") + glog.V(2).Infoln("check file", fileName+".cdb") if _, err := os.Stat(fileName + ".cdb"); os.IsNotExist(err) { glog.V(0).Infof("converting %s.idx to %s.cdb", fileName, fileName) if e = ConvertIndexToCdb(fileName+".cdb", indexFile); e != nil { @@ -89,7 +89,7 @@ func (v *Volume) load(alsoLoadIndex bool) error { } } } - glog.V(4).Infoln("open file", fileName+".cdb") + glog.V(2).Infoln("open file", fileName+".cdb") if v.nm, e = OpenCdbMap(fileName + ".cdb"); e != nil { if os.IsNotExist(e) { glog.V(0).Infof("Failed to read cdb file :%s, fall back to normal readonly mode.", fileName) @@ -99,14 +99,23 @@ func (v *Volume) load(alsoLoadIndex bool) error { } } } - glog.V(4).Infoln("open to write file", fileName+".idx") - indexFile, e = os.OpenFile(fileName+".idx", os.O_RDWR|os.O_CREATE, 0644) - if e != nil { - return fmt.Errorf("cannot create Volume Data %s.dat: %s", fileName, e.Error()) + if v.readOnly { + glog.V(1).Infoln("open to read file", fileName+".idx") + indexFile, e = os.OpenFile(fileName+".idx", os.O_RDONLY, 0644) + if e != nil { + return fmt.Errorf("cannot read Volume Data %s.dat: %s", fileName, e.Error()) + } + } else { + glog.V(1).Infoln("open to write file", fileName+".idx") + indexFile, e = os.OpenFile(fileName+".idx", os.O_RDWR|os.O_CREATE, 0644) + if e != nil { + return fmt.Errorf("cannot create Volume Data %s.dat: %s", fileName, e.Error()) + } + } + glog.V(0).Infoln("loading file", fileName+".idx", "readonly", v.readOnly) + if v.nm, e = LoadNeedleMap(indexFile); e != nil { + glog.V(0).Infoln("loading error:", e) } - glog.V(4).Infoln("loading file", fileName+".idx") - v.nm, e = LoadNeedleMap(indexFile) - glog.V(4).Infoln("loading error:", e) } return e } diff --git a/go/weed/volume_test.go b/go/weed/volume_test.go index 601d8aaf5..2499d8543 100644 --- a/go/weed/volume_test.go +++ b/go/weed/volume_test.go @@ -1,11 +1,12 @@ package main import ( + "code.google.com/p/weed-fs/go/glog" "net/http" "testing" "time" ) func TestXYZ(t *testing.T) { - glog.V(4).Infoln("Last-Modified", time.Unix(int64(1373273596), 0).UTC().Format(http.TimeFormat)) + glog.V(0).Infoln("Last-Modified", time.Unix(int64(1373273596), 0).UTC().Format(http.TimeFormat)) }