seaweedfs源码阅读4-volume启动过程

根据文章使用delve调试Golang程序技巧
使用go install -gcflags “-N -l” weed.go 对程序进行重新编译,方便调试

通过weed.go 调用volume.go ,参数处理完成后,生成一个ServeMux实例,有关go http ServeMux介绍,

1
2
3
4
5
volumeMux := http.NewServeMux()
publicVolumeMux := volumeMux
if isSeperatedPublicPort {
truepublicVolumeMux = http.NewServeMux()
}

根据参数VolumeServerOptions 中的 indexType,选择volume Needle Map 的存储位置,默认使用内存

1
2
3
4
5
6
7
volumeNeedleMapKind := storage.NeedleMapInMemory
switch *v.indexType {
case "leveldb":
volumeNeedleMapKind = storage.NeedleMapLevelDb
case "boltdb":
volumeNeedleMapKind = storage.NeedleMapBoltDb
}

调试信息

1
2
(dlv) p *v.indexType
"memory"

根据参数生成volumeServer,使用volume_server.go 中的NewVolumeServer 生成volumeServer
在volume_server.go 中,设置masterNode ,store, guard

1
2
3
4
vs.SetMasterNode(masterNode)
vs.store = storage.NewStore(port, ip, publicUrl, folders, maxCounts, vs.needleMapKind)
vs.guard = security.NewGuard(whiteList, "")

生成store时会加载已有的volume

1
2
3
4
5
6
7
8
9
10
11
12
func (port int, ip, publicUrl string, dirnames []string, maxVolumeCounts []int, needleMapKind NeedleMapType) (s *Store) {
trues = &Store{Port: port, Ip: ip, PublicUrl: publicUrl}
trues.Locations = make([]*DiskLocation, 0)
truefor i := 0; i < len(dirnames); i++ {
truetruelocation := NewDiskLocation(dirnames[i], maxVolumeCounts[i])
truetruelocation.loadExistingVolumes(needleMapKind)
truetrues.Locations = append(s.Locations, location)
true}
truereturn
}