seaweedfs源码阅读记录9-生成fid过程

使用命令 : curl -X POST http://localhost:9333/dir/assign

在topology.go 中

1
2
3
4
5
6
7
8
func (t *Topology) (count uint64, option *VolumeGrowOption) (string, uint64, *DataNode, error) {
truevid, count, datanodes, err := t.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl).PickForWrite(count, option)
trueif err != nil || datanodes.Length() == 0 {
truetruereturn "", 0, nil, errors.New("No writable volumes available!")
true}
truefileId, count := t.Sequence.NextFileId(count)
truereturn storage.NewFileId(*vid, fileId, rand.Uint32()).String(), count, datanodes.Head(), nil //调用file_id.go中的NewFileId函数
}

在file_id.go 中

1
2
3
4
5
6
7
8
9
type FileId struct {
trueVolumeId VolumeId
trueKey uint64 // fileId = 1050682
trueHashcode uint32 // rand.Uint32()
}
func NewFileId(VolumeId VolumeId, Key uint64, Hashcode uint32) *FileId {
truereturn &FileId{VolumeId: VolumeId, Key: Key, Hashcode: Hashcode}
}

使用memory_sequencer.go中的函数

1
2
3
4
5
6
7
func (m *MemorySequencer) NextFileId(count uint64) (uint64, uint64) { // count = 1
truem.sequenceLock.Lock()
truedefer m.sequenceLock.Unlock()
trueret := m.counter
truem.counter += uint64(count)
truereturn ret, count
}

调试信息

1
2
3
4
(dlv) p m
*github.com/chrislusf/seaweedfs/weed/sequence.MemorySequencer {
truecounter: 1050683,
truesequenceLock: sync.Mutex {state: 1, sema: 0},}

在 master_server_handlers.go 中封装信息

1
2
3
4
5
6
fid, count, dn, err := ms.Topo.PickForWrite(requestedCount, option)
trueif err == nil {
truetruewriteJsonQuiet(w, r, http.StatusOK, operation.AssignResult{Fid: fid, Url: dn.Url(), PublicUrl: dn.PublicUrl, Count: count})
true} else {
truetruewriteJsonQuiet(w, r, http.StatusNotAcceptable, operation.AssignResult{Error: err.Error()})
true}