aboutsummaryrefslogtreecommitdiff
path: root/weed/storage/volume_super_block.go
diff options
context:
space:
mode:
Diffstat (limited to 'weed/storage/volume_super_block.go')
-rw-r--r--weed/storage/volume_super_block.go75
1 files changed, 50 insertions, 25 deletions
diff --git a/weed/storage/volume_super_block.go b/weed/storage/volume_super_block.go
index 164c887e1..20a223f7d 100644
--- a/weed/storage/volume_super_block.go
+++ b/weed/storage/volume_super_block.go
@@ -4,11 +4,13 @@ import (
"fmt"
"os"
- "github.com/chrislusf/seaweedfs/weed/glog"
- "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
- "github.com/chrislusf/seaweedfs/weed/storage/needle"
- "github.com/chrislusf/seaweedfs/weed/util"
+ "github.com/joeslay/seaweedfs/weed/storage/memory_map"
+
"github.com/golang/protobuf/proto"
+ "github.com/joeslay/seaweedfs/weed/glog"
+ "github.com/joeslay/seaweedfs/weed/pb/master_pb"
+ "github.com/joeslay/seaweedfs/weed/storage/needle"
+ "github.com/joeslay/seaweedfs/weed/util"
)
const (
@@ -70,24 +72,34 @@ func (s *SuperBlock) Bytes() []byte {
}
func (v *Volume) maybeWriteSuperBlock() error {
- stat, e := v.dataFile.Stat()
- if e != nil {
- glog.V(0).Infof("failed to stat datafile %s: %v", v.dataFile.Name(), e)
- return e
- }
- if stat.Size() == 0 {
- v.SuperBlock.version = needle.CurrentVersion
- _, e = v.dataFile.Write(v.SuperBlock.Bytes())
- if e != nil && os.IsPermission(e) {
- //read-only, but zero length - recreate it!
- if v.dataFile, e = os.Create(v.dataFile.Name()); e == nil {
- if _, e = v.dataFile.Write(v.SuperBlock.Bytes()); e == nil {
- v.readOnly = false
+
+ mMap, exists := memory_map.FileMemoryMap[v.dataFile.Name()]
+ if exists {
+ if mMap.End_of_file == -1 {
+ v.SuperBlock.version = needle.CurrentVersion
+ mMap.WriteMemory(0, uint64(len(v.SuperBlock.Bytes())), v.SuperBlock.Bytes())
+ }
+ return nil
+ } else {
+ stat, e := v.dataFile.Stat()
+ if e != nil {
+ glog.V(0).Infof("failed to stat datafile %s: %v", v.dataFile.Name(), e)
+ return e
+ }
+ if stat.Size() == 0 {
+ v.SuperBlock.version = needle.CurrentVersion
+ _, e = v.dataFile.Write(v.SuperBlock.Bytes())
+ if e != nil && os.IsPermission(e) {
+ //read-only, but zero length - recreate it!
+ if v.dataFile, e = os.Create(v.dataFile.Name()); e == nil {
+ if _, e = v.dataFile.Write(v.SuperBlock.Bytes()); e == nil {
+ v.readOnly = false
+ }
}
}
}
+ return e
}
- return e
}
func (v *Volume) readSuperBlock() (err error) {
@@ -97,15 +109,28 @@ func (v *Volume) readSuperBlock() (err error) {
// ReadSuperBlock reads from data file and load it into volume's super block
func ReadSuperBlock(dataFile *os.File) (superBlock SuperBlock, err error) {
- if _, err = dataFile.Seek(0, 0); err != nil {
- err = fmt.Errorf("cannot seek to the beginning of %s: %v", dataFile.Name(), err)
- return
- }
+
header := make([]byte, _SuperBlockSize)
- if _, e := dataFile.Read(header); e != nil {
- err = fmt.Errorf("cannot read volume %s super block: %v", dataFile.Name(), e)
- return
+ mMap, exists := memory_map.FileMemoryMap[dataFile.Name()]
+ if exists {
+ mBuffer, e := mMap.ReadMemory(0, _SuperBlockSize)
+ if err != nil {
+ err = fmt.Errorf("cannot read volume %s super block: %v", dataFile.Name(), e)
+ return
+ }
+ copy(header, mBuffer.Buffer)
+ mBuffer.ReleaseMemory()
+ } else {
+ if _, err = dataFile.Seek(0, 0); err != nil {
+ err = fmt.Errorf("cannot seek to the beginning of %s: %v", dataFile.Name(), err)
+ return
+ }
+ if _, e := dataFile.Read(header); e != nil {
+ err = fmt.Errorf("cannot read volume %s super block: %v", dataFile.Name(), e)
+ return
+ }
}
+
superBlock.version = needle.Version(header[0])
if superBlock.ReplicaPlacement, err = NewReplicaPlacementFromByte(header[1]); err != nil {
err = fmt.Errorf("cannot read replica type: %s", err.Error())