From 79cade52a62c1888b13adab4690456c358e58cf6 Mon Sep 17 00:00:00 2001 From: Brett Buddin Date: Mon, 23 Mar 2020 10:56:21 -0400 Subject: [PATCH 1/2] fix(task/scheduler): Reuse slices built by iterator to reduce allocations. --- task/backend/scheduler/treescheduler.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/task/backend/scheduler/treescheduler.go b/task/backend/scheduler/treescheduler.go index 79fe16d5d71..e0bf2eedc57 100644 --- a/task/backend/scheduler/treescheduler.go +++ b/task/backend/scheduler/treescheduler.go @@ -73,6 +73,7 @@ type TreeScheduler struct { workchans []chan Item wg sync.WaitGroup checkpointer SchedulableService + items *itemList sm *SchedulerMetrics } @@ -118,6 +119,7 @@ func NewScheduler(executor Executor, checkpointer SchedulableService, opts ...tr time: clock.New(), done: make(chan struct{}, 1), checkpointer: checkpointer, + items: &itemList{}, } // apply options @@ -215,7 +217,12 @@ type itemList struct { } func (s *TreeScheduler) process() { - iter, toReAdd := s.iterator(s.time.Now()) + // Reset the length of the slice in preparation of the next iterator. + s.items.toDelete = s.items.toDelete[:0] + s.items.toInsert = s.items.toInsert[:0] + + toReAdd := s.items + iter := s.iterator(s.time.Now()) s.priorityQueue.Ascend(iter) for i := range toReAdd.toDelete { delete(s.nextTime, toReAdd.toDelete[i].id) @@ -232,8 +239,7 @@ func (s *TreeScheduler) resetTimer(whenFromNow time.Duration) { s.timer.Reset(whenFromNow) } -func (s *TreeScheduler) iterator(ts time.Time) (btree.ItemIterator, *itemList) { - itemsToPlace := &itemList{} +func (s *TreeScheduler) iterator(ts time.Time) btree.ItemIterator { return func(i btree.Item) bool { if i == nil { return false @@ -249,13 +255,13 @@ func (s *TreeScheduler) iterator(ts time.Time) (btree.ItemIterator, *itemList) { wc := xxhash.Sum64(buf[:]) % uint64(len(s.workchans)) // we just hash so that the number is uniformly distributed select { case s.workchans[wc] <- it: - itemsToPlace.toDelete = append(itemsToPlace.toDelete, it) + s.items.toDelete = append(s.items.toDelete, it) if err := it.updateNext(); err != nil { // in this error case we can't schedule next, so we have to drop the task s.onErr(context.Background(), it.id, it.Next(), &ErrUnrecoverable{err}) return true } - itemsToPlace.toInsert = append(itemsToPlace.toInsert, it) + s.items.toInsert = append(s.items.toInsert, it) case <-s.done: return false @@ -264,7 +270,7 @@ func (s *TreeScheduler) iterator(ts time.Time) (btree.ItemIterator, *itemList) { } } return true - }, itemsToPlace + } } // When gives us the next time the scheduler will run a task. From d97775809f42ed1f9fac6457f73586d245ab0676 Mon Sep 17 00:00:00 2001 From: Brett Buddin Date: Mon, 23 Mar 2020 13:00:32 -0400 Subject: [PATCH 2/2] chore(changelog): Update changelog to reflect scheduler buffer reuse. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4dbe899f333..ea2579090fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ 1. [17240](https://github.com/influxdata/influxdb/pull/17240): NodeJS logo displays properly in Firefox 1. [17363](https://github.com/influxdata/influxdb/pull/17363): Fixed telegraf configuration bugs where system buckets were appearing in the buckets dropdown 1. [17391](https://github.com/influxdata/influxdb/pull/17391): Fixed threshold check bug where checks could not be created when a field had a space in the name +1. [17384](https://github.com/influxdata/influxdb/pull/17384): Reuse slices built by iterator to reduce allocations ### UI Improvements