Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nsqd: Make sure data directory is writeable on startup #185

Merged
merged 1 commit into from
Apr 29, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions nsqd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ func main() {
nsqd.lookupdTCPAddrs = lookupdTCPAddrs

nsqd.LoadMetadata()
err = nsqd.PersistMetadata()
if err != nil {
log.Fatalf("ERROR: failed to persist metadata - %s", err.Error())
}
nsqd.Main()
<-exitChan
nsqd.Exit()
Expand Down
20 changes: 11 additions & 9 deletions nsqd/nsqd.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (n *NSQd) LoadMetadata() {
}
}

func (n *NSQd) PersistMetadata() {
func (n *NSQd) PersistMetadata() error {
// persist metadata about what topics/channels we have
// so that upon restart we can get back to the same state
fileName := fmt.Sprintf(path.Join(n.options.dataPath, "nsqd.%d.dat"), n.workerId)
Expand Down Expand Up @@ -200,30 +200,29 @@ func (n *NSQd) PersistMetadata() {

data, err := json.Marshal(&js)
if err != nil {
log.Printf("ERROR: failed to marshal JSON metadata - %s", err.Error())
return
return err
}

tmpFileName := fileName + ".tmp"
f, err := os.OpenFile(tmpFileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Printf("ERROR: failed to open temporary metadata file %s - %s", tmpFileName, err.Error())
return
return err
}

_, err = f.Write(data)
if err != nil {
log.Printf("ERROR: failed to write to temporary metadata file %s - %s", tmpFileName, err.Error())
f.Close()
return
return err
}
f.Sync()
f.Close()

err = os.Rename(tmpFileName, fileName)
if err != nil {
log.Printf("ERROR: failed to rename temporary metadata file %s to %s - %s", tmpFileName, fileName, err.Error())
return err
}

return nil
}

func (n *NSQd) Exit() {
Expand All @@ -236,7 +235,10 @@ func (n *NSQd) Exit() {
}

n.Lock()
n.PersistMetadata()
err := n.PersistMetadata()
if err != nil {
log.Printf("ERROR: failed to persist metadata - %s", err.Error())
}
log.Printf("NSQ: closing topics")
for _, topic := range n.topicMap {
topic.Close()
Expand Down