adding etcd storage support for cluster meta data. Currently just

sequence. More to come...
This commit is contained in:
Chris Lu
2013-11-10 01:31:50 -08:00
parent 5cb6590eae
commit 1888d01fa0
12 changed files with 149 additions and 56 deletions

View File

@@ -0,0 +1,19 @@
package sequence
import ()
// just for testing
type MemorySequencer struct {
counter uint64
}
func NewMemorySequencer() (m *MemorySequencer) {
m = &MemorySequencer{counter: 1}
return
}
func (m *MemorySequencer) NextFileId(count int) (uint64, int) {
ret := m.counter
m.counter += uint64(count)
return ret, count
}

View File

@@ -5,7 +5,6 @@ import (
"code.google.com/p/weed-fs/go/glog"
"code.google.com/p/weed-fs/go/metastore"
"encoding/gob"
"path"
"sync"
)
@@ -17,8 +16,7 @@ type Sequencer interface {
NextFileId(count int) (uint64, int)
}
type SequencerImpl struct {
dir string
fileName string
fileFullPath string
volumeLock sync.Mutex
sequenceLock sync.Mutex
@@ -29,19 +27,30 @@ type SequencerImpl struct {
metaStore *metastore.MetaStore
}
func NewSequencer(dirname string, filename string) (m *SequencerImpl) {
m = &SequencerImpl{dir: dirname, fileName: filename}
func NewFileSequencer(filepath string) (m *SequencerImpl) {
m = &SequencerImpl{fileFullPath: filepath}
m.metaStore = &metastore.MetaStore{metastore.NewMetaStoreFileBacking()}
m.initilize()
return
}
if !m.metaStore.Has(m.dir, m.fileName+".seq") {
func NewEtcdSequencer(etcdCluster string) (m *SequencerImpl) {
m = &SequencerImpl{fileFullPath: "/weedfs/default/sequence"}
m.metaStore = &metastore.MetaStore{metastore.NewMetaStoreEtcdBacking(etcdCluster)}
m.initilize()
return
}
func (m *SequencerImpl) initilize() {
if !m.metaStore.Has(m.fileFullPath) {
m.FileIdSequence = FileIdSaveInterval
glog.V(0).Infoln("Setting file id sequence", m.FileIdSequence)
} else {
var err error
if m.FileIdSequence, err = m.metaStore.GetUint64(m.dir, m.fileName+".seq"); err != nil {
if data, err := m.metaStore.Get(m.dir, m.fileName+".seq"); err == nil {
if m.FileIdSequence, err = m.metaStore.GetUint64(m.fileFullPath); err != nil {
if data, err := m.metaStore.Get(m.fileFullPath); err == nil {
m.FileIdSequence = decode(data)
glog.V(0).Infoln("Decoding old version of FileIdSequence", m.FileIdSequence)
glog.V(0).Infoln("Decoding old version of FileIdSequence", m.FileIdSequence)
} else {
glog.V(0).Infof("No existing FileIdSequence: %s", err)
}
@@ -69,16 +78,16 @@ func (m *SequencerImpl) NextFileId(count int) (uint64, int) {
return m.FileIdSequence - m.fileIdCounter - uint64(count), count
}
func (m *SequencerImpl) saveSequence() {
glog.V(0).Infoln("Saving file id sequence", m.FileIdSequence, "to", path.Join(m.dir, m.fileName+".seq"))
if e := m.metaStore.SetUint64(m.FileIdSequence, m.dir, m.fileName+".seq"); e != nil {
glog.V(0).Infoln("Saving file id sequence", m.FileIdSequence, "to", m.fileFullPath)
if e := m.metaStore.SetUint64(m.fileFullPath, m.FileIdSequence); e != nil {
glog.Fatalf("Sequence id Save [ERROR] %s", e)
}
}
//decode are for backward compatible purpose
func decode(input []byte) uint64 {
func decode(input string) uint64 {
var x uint64
b := bytes.NewReader(input)
b := bytes.NewReader([]byte(input))
decoder := gob.NewDecoder(b)
if e := decoder.Decode(&x); e == nil {
return x