Chris Lu
12 years ago
9 changed files with 111 additions and 77 deletions
-
1weed-fs/bin/.gitignore
-
5weed-fs/pkg/linux_amd64/pkg/.gitignore
-
7weed-fs/src/cmd/weed/master.go
-
2weed-fs/src/cmd/weed/version.go
-
8weed-fs/src/cmd/weed/volume.go
-
3weed-fs/src/cmd/weed/weed.go
-
75weed-fs/src/pkg/directory/volume_mapping.go
-
71weed-fs/src/pkg/sequence/sequence.go
-
16weed-fs/src/pkg/util/parse.go
@ -1 +0,0 @@ |
|||
/weed |
@ -1,5 +0,0 @@ |
|||
/directory.a |
|||
/replication.a |
|||
/storage.a |
|||
/topology.a |
|||
/util.a |
@ -0,0 +1,71 @@ |
|||
package sequence |
|||
|
|||
import ( |
|||
"encoding/gob" |
|||
"os" |
|||
"path" |
|||
"sync" |
|||
"log" |
|||
) |
|||
|
|||
const ( |
|||
FileIdSaveInterval = 10000 |
|||
) |
|||
|
|||
type Sequencer interface { |
|||
NextFileId(count int) (uint64, int) |
|||
} |
|||
type SequencerImpl struct { |
|||
dir string |
|||
fileName string |
|||
|
|||
volumeLock sync.Mutex |
|||
sequenceLock sync.Mutex |
|||
|
|||
FileIdSequence uint64 |
|||
fileIdCounter uint64 |
|||
} |
|||
|
|||
func NewSequencer(dirname string, filename string) (m *SequencerImpl) { |
|||
m = &SequencerImpl{dir: dirname, fileName: filename} |
|||
|
|||
seqFile, se := os.OpenFile(path.Join(m.dir, m.fileName+".seq"), os.O_RDONLY, 0644) |
|||
if se != nil { |
|||
m.FileIdSequence = FileIdSaveInterval |
|||
log.Println("Setting file id sequence", m.FileIdSequence) |
|||
} else { |
|||
decoder := gob.NewDecoder(seqFile) |
|||
defer seqFile.Close() |
|||
decoder.Decode(&m.FileIdSequence) |
|||
log.Println("Loading file id sequence", m.FileIdSequence, "=>", m.FileIdSequence+FileIdSaveInterval) |
|||
//in case the server stops between intervals
|
|||
m.FileIdSequence += FileIdSaveInterval |
|||
} |
|||
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, count |
|||
} |
|||
func (m *SequencerImpl) saveSequence() { |
|||
log.Println("Saving file id sequence", m.FileIdSequence, "to", path.Join(m.dir, m.fileName+".seq")) |
|||
seqFile, e := os.OpenFile(path.Join(m.dir, m.fileName+".seq"), os.O_CREATE|os.O_WRONLY, 0644) |
|||
if e != nil { |
|||
log.Fatalf("Sequence File Save [ERROR] %s\n", e) |
|||
} |
|||
defer seqFile.Close() |
|||
encoder := gob.NewEncoder(seqFile) |
|||
encoder.Encode(m.FileIdSequence) |
|||
} |
@ -0,0 +1,16 @@ |
|||
package util |
|||
|
|||
import ( |
|||
"strconv" |
|||
) |
|||
|
|||
func ParseInt(text string, defaultValue int) int{ |
|||
count, parseError := strconv.ParseUint(text,10,64) |
|||
if parseError!=nil { |
|||
if len(text)>0{ |
|||
return 0 |
|||
} |
|||
return defaultValue |
|||
} |
|||
return int(count) |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue