From a45f4bb475f4eee6c121bd5a9d290f6d14b0c580 Mon Sep 17 00:00:00 2001 From: Craig Peterson Date: Wed, 22 Jul 2015 12:24:12 -0600 Subject: [PATCH 1/2] making save rely on fewer goroutines needing to acquire the lock. --- cmd/bosun/sched/bolt.go | 23 +++++++++++------------ cmd/bosun/sched/sched.go | 6 ++++++ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/cmd/bosun/sched/bolt.go b/cmd/bosun/sched/bolt.go index d5fd8725c4..ed8cb256e0 100644 --- a/cmd/bosun/sched/bolt.go +++ b/cmd/bosun/sched/bolt.go @@ -20,18 +20,20 @@ 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. s.Lock("Save") defer s.Unlock() - if savePending { - return - } - savePending = true - time.AfterFunc(time.Second*5, s.save) - }() + s.save() + } } type counterWriter struct { @@ -60,9 +62,6 @@ const ( ) func (s *Schedule) save() { - defer func() { - savePending = false - }() if s.db == nil { return } diff --git a/cmd/bosun/sched/sched.go b/cmd/bosun/sched/sched.go index f02db2ffa1..591053a15b 100644 --- a/cmd/bosun/sched/sched.go +++ b/cmd/bosun/sched/sched.go @@ -50,6 +50,7 @@ type Schedule struct { maxIncidentId uint64 incidentLock sync.Mutex db *bolt.DB + saveNeeded chan struct{} } func init() { @@ -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 { @@ -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() @@ -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) From e1c8d3953386b2e8ba0af7d0e5fa3f9613945e69 Mon Sep 17 00:00:00 2001 From: Craig Peterson Date: Wed, 22 Jul 2015 12:59:22 -0600 Subject: [PATCH 2/2] pinning travis to 1.4.2 for the moment --- .travis.yml | 2 +- cmd/bosun/sched/bolt.go | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index aa57e496db..4a1cf3e2d1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: go sudo: false go: - - tip + - 1.4.2 notifications: email: false diff --git a/cmd/bosun/sched/bolt.go b/cmd/bosun/sched/bolt.go index ed8cb256e0..d8b8d6b899 100644 --- a/cmd/bosun/sched/bolt.go +++ b/cmd/bosun/sched/bolt.go @@ -30,6 +30,13 @@ func (s *Schedule) Save() { 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() s.save()