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

bosun: making save rely on fewer goroutines needing to acquire the lock. #1187

Merged
merged 2 commits into from
Jul 22, 2015
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go
sudo: false
go:
- tip
- 1.4.2

notifications:
email: false
Expand Down
30 changes: 18 additions & 12 deletions cmd/bosun/sched/bolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,27 @@ import (
"bosun.org/opentsdb"
)

var savePending bool

func (s *Schedule) Save() {
go func() {
select {
case s.saveNeeded <- struct{}{}:
default:
}
}

func (s *Schedule) performSave() {
for range s.saveNeeded {
time.Sleep(5 * time.Second) // wait 5 seconds to throttle.

// if channel has an item on it, pull it off now to avoid re-saving later.
select {
case <-s.saveNeeded:
default:
}

s.Lock("Save")
defer s.Unlock()
if savePending {
return
}
savePending = true
time.AfterFunc(time.Second*5, s.save)
}()
s.save()
}
}

type counterWriter struct {
Expand Down Expand Up @@ -60,9 +69,6 @@ const (
)

func (s *Schedule) save() {
defer func() {
savePending = false
}()
if s.db == nil {
return
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/bosun/sched/sched.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Schedule struct {
maxIncidentId uint64
incidentLock sync.Mutex
db *bolt.DB
saveNeeded chan struct{}
}

func init() {
Expand Down Expand Up @@ -412,6 +413,7 @@ func (s *Schedule) Init(c *conf.Conf) error {
s.Incidents = make(map[uint64]*Incident)
s.status = make(States)
s.Search = search.NewSearch()
s.saveNeeded = make(chan struct{}, 1)
if c.StateFile != "" {
s.db, err = bolt.Open(c.StateFile, 0600, nil)
if err != nil {
Expand All @@ -436,6 +438,9 @@ func Close() {
}

func (s *Schedule) Close() {
s.Lock("Close")
defer s.Unlock()
close(s.saveNeeded)
s.save()
if s.db != nil {
s.db.Close()
Expand All @@ -451,6 +456,7 @@ func (s *Schedule) Run() error {
go s.PingHosts()
}
go s.Poll()
go s.performSave()
interval := uint64(0)
for {
wait := time.After(s.Conf.CheckFrequency)
Expand Down