diff options
| author | Chris Lu <chris.lu@gmail.com> | 2014-04-16 23:43:27 -0700 |
|---|---|---|
| committer | Chris Lu <chris.lu@gmail.com> | 2014-04-16 23:43:27 -0700 |
| commit | 51939efeac635d0ba8b683cae6176aa60845b5f7 (patch) | |
| tree | 4b630c01d57cc1cc57e2ed58b25a275109039a30 /go/sequence/sequence.go | |
| parent | 9653a54766fbb7d9e7453c0df0eb0de016cfbce6 (diff) | |
| download | seaweedfs-51939efeac635d0ba8b683cae6176aa60845b5f7.tar.xz seaweedfs-51939efeac635d0ba8b683cae6176aa60845b5f7.zip | |
1. volume server now sends master server its max file key, so that
master server does not need to store the sequence on disk any more
2. fix raft server's failure to init cluster during bootstrapping
Diffstat (limited to 'go/sequence/sequence.go')
| -rw-r--r-- | go/sequence/sequence.go | 86 |
1 files changed, 3 insertions, 83 deletions
diff --git a/go/sequence/sequence.go b/go/sequence/sequence.go index 493804ec6..5a1bceaaf 100644 --- a/go/sequence/sequence.go +++ b/go/sequence/sequence.go @@ -1,89 +1,9 @@ package sequence -import ( - "bytes" - "code.google.com/p/weed-fs/go/glog" - "code.google.com/p/weed-fs/go/metastore" - "encoding/gob" - "sync" -) - -const ( - FileIdSaveInterval = 10000 -) +import () type Sequencer interface { NextFileId(count int) (uint64, int) -} -type SequencerImpl struct { - fileFullPath string - - volumeLock sync.Mutex - sequenceLock sync.Mutex - - FileIdSequence uint64 - fileIdCounter uint64 - - metaStore *metastore.MetaStore -} - -func NewFileSequencer(filepath string) (m *SequencerImpl) { - m = &SequencerImpl{fileFullPath: filepath} - m.metaStore = &metastore.MetaStore{metastore.NewMetaStoreFileBacking()} - 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.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) - } else { - glog.V(0).Infof("No existing FileIdSequence: %s", err) - } - } else { - glog.V(0).Infoln("Loading file id sequence", m.FileIdSequence) - } - //in case the server stops between intervals - } - return -} - -//count should be 1 or more -func (m *SequencerImpl) NextFileId(count int) (uint64, int) { - if count <= 0 { - return 0, 0 - } - m.sequenceLock.Lock() - defer m.sequenceLock.Unlock() - if m.fileIdCounter < uint64(count) { - m.fileIdCounter = FileIdSaveInterval - m.FileIdSequence += FileIdSaveInterval - m.saveSequence() - } - m.fileIdCounter = m.fileIdCounter - uint64(count) - return m.FileIdSequence - m.fileIdCounter - uint64(count), count -} -func (m *SequencerImpl) saveSequence() { - 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 string) uint64 { - var x uint64 - b := bytes.NewReader([]byte(input)) - decoder := gob.NewDecoder(b) - if e := decoder.Decode(&x); e == nil { - return x - } - return 0 + SetMax(uint64) + Peek() uint64 } |
