nsq源码分析(3):nsqd之数据持久化2
当nsqd进程退出时,将内存中的数据写入到磁盘
当nsqd进程启动时,将磁盘中的数据读入到内存
nsqd数据信息结构体
nsqd/nsqd.go
type meta struct {
Topics []struct {
Name string `json:"name"` // topic名称
Paused bool `json:"paused"` // topic状态
Channels []struct {
Name string `json:"name"` // channel名称
Paused bool `json:"paused"` // channel状态
} `json:"channels"`
} `json:"topics"`
}
nsqd退出时写入本地元数据
nsqd/nsqd.go
func (n *NSQD) Exit() {
...
n.Lock()
err := n.PersistMetadata()
if err != nil {
n.logf("ERROR: failed to persist metadata - %s", err)
}
n.logf("NSQ: closing topics")
for _, topic := range n.topicMap {
topic.Close()
}
n.Unlock()
...
}
nsqd/nsqd.go
// 将topic和channel的信息写入磁盘
func (n *NSQD) PersistMetadata() error {
// persist metadata about what topics/channels we have, across restarts
// fileName:nsqd数据文件名。保