mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2025-10-14 15:54:53 +08:00
adding etcd storage support for cluster meta data. Currently just
sequence. More to come...
This commit is contained in:
19
go/sequence/memory_sequencer.go
Normal file
19
go/sequence/memory_sequencer.go
Normal 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
|
||||
}
|
@@ -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
|
||||
|
Reference in New Issue
Block a user