Skip to content

Commit 351f855

Browse files
committed
Revert "Try a workaround to reduce cpu usage"
This reverts commit 2eebadb.
1 parent f6022fa commit 351f855

File tree

2 files changed

+44
-39
lines changed

2 files changed

+44
-39
lines changed

internal/cmd/api/api.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,8 @@ func run(ctx context.Context, cfg *Config, log logrus.FieldLogger) error {
204204
}
205205
}
206206

207-
log := log.WithField("subsystem", "event-watcher")
208-
log.Level = logrus.DebugLevel
209207
log.WithField("envs", len(k8sClients)).Info("Start event watcher")
210-
eventWatcher := event.NewWatcher(pool, k8sClients, log.WithField("subsystem", "event-watcher"))
208+
eventWatcher := event.NewWatcher(pool, k8sClients, log)
211209
go eventWatcher.Run(ctx)
212210
}
213211

internal/kubernetes/event/watcher.go

+43-36
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package event
33
import (
44
"context"
55
"encoding/json"
6-
"errors"
76
"fmt"
87
"regexp"
98
"strings"
@@ -29,7 +28,8 @@ type Watcher struct {
2928
log logrus.FieldLogger
3029
wg *pool.ContextPool
3130

32-
cancel context.CancelFunc
31+
// State returns true when the watcher should be started/continue running and false when it should stop.
32+
state []chan bool
3333
}
3434

3535
func NewWatcher(pool *pgxpool.Pool, clients map[string]kubernetes.Interface, log logrus.FieldLogger) *Watcher {
@@ -42,75 +42,78 @@ func NewWatcher(pool *pgxpool.Pool, clients map[string]kubernetes.Interface, log
4242
events: make(chan eventsql.UpsertParams, 20),
4343
queries: eventsql.New(pool),
4444
log: log,
45+
state: chs,
4546
}
4647
}
4748

4849
func (w *Watcher) Run(ctx context.Context) {
49-
w.wg = pool.New().WithContext(ctx)
50+
w.wg = pool.New().WithErrors().WithContext(ctx)
5051

5152
leaderelection.RegisterOnStartedLeading(w.onStartedLeading)
5253
leaderelection.RegisterOnStoppedLeading(w.onStoppedLeading)
5354
if leaderelection.IsLeader() {
54-
w.log.Debug("Is already leader, force start")
5555
w.onStartedLeading(ctx)
5656
}
5757

5858
w.wg.Go(func(ctx context.Context) error {
5959
return w.batchInsert(ctx)
6060
})
6161

62+
i := 0
63+
for env, client := range w.clients {
64+
ch := w.state[i]
65+
i++
66+
w.wg.Go(func(ctx context.Context) error {
67+
return w.run(ctx, env, client, ch)
68+
})
69+
}
70+
6271
if err := w.wg.Wait(); err != nil {
6372
w.log.WithError(err).Error("error running events watcher")
6473
}
6574
}
6675

6776
func (w *Watcher) onStoppedLeading() {
68-
w.log.Debug("onStoppedLeading...")
69-
if w.cancel != nil {
70-
w.cancel()
71-
w.cancel = nil
72-
73-
w.log.Debug("cancelling")
77+
for _, ch := range w.state {
78+
select {
79+
case ch <- false:
80+
default:
81+
w.log.WithField("state", "stopped").Error("failed to send state")
82+
}
7483
}
7584
}
7685

77-
func (w *Watcher) onStartedLeading(ctx context.Context) {
78-
if w.cancel != nil {
79-
w.cancel()
80-
}
81-
82-
go func() {
83-
time.Sleep(5 * time.Second)
84-
w.onStoppedLeading()
85-
}()
86-
87-
ctx, cancel := context.WithCancel(ctx)
88-
w.cancel = cancel
89-
90-
for env, client := range w.clients {
91-
w.wg.Go(func(_ context.Context) error {
92-
w.log.WithField("env", env).Debug("starting watcher")
93-
return w.run(ctx, env, client)
94-
})
86+
func (w *Watcher) onStartedLeading(_ context.Context) {
87+
for _, ch := range w.state {
88+
select {
89+
case ch <- true:
90+
default:
91+
w.log.WithField("state", "started").Error("failed to send state")
92+
}
9593
}
9694
}
9795

9896
var regHorizontalPodAutoscaler = regexp.MustCompile(`New size: (\d+); reason: (\w+).*(below|above) target`)
9997

100-
func (w *Watcher) run(ctx context.Context, env string, client kubernetes.Interface) error {
98+
func (w *Watcher) run(ctx context.Context, env string, client kubernetes.Interface, state chan bool) error {
10199
for {
102-
if err := w.watch(ctx, env, client); err != nil {
103-
if errors.Is(err, context.Canceled) {
104-
return nil
100+
select {
101+
case <-ctx.Done():
102+
return nil
103+
case s := <-state:
104+
w.log.WithField("env", env).WithField("state", s).Info("state change")
105+
if s {
106+
if err := w.watch(ctx, env, client, state); err != nil {
107+
w.log.WithError(err).Error("failed to watch events")
108+
}
109+
w.log.WithField("env", env).Info("stopped watching")
110+
105111
}
106-
w.log.WithError(err).Error("failed to watch events")
107112
}
108-
w.log.WithField("env", env).Info("stopped watching")
109-
time.Sleep(2 * time.Second)
110113
}
111114
}
112115

113-
func (w *Watcher) watch(ctx context.Context, env string, client kubernetes.Interface) error {
116+
func (w *Watcher) watch(ctx context.Context, env string, client kubernetes.Interface, state chan bool) error {
114117
// Events we want to watch for
115118
// SuccessfulRescale - Check for successful rescale events
116119
// Killing - Check for liveness failures
@@ -166,6 +169,10 @@ func (w *Watcher) watch(ctx context.Context, env string, client kubernetes.Inter
166169
select {
167170
case <-ctx.Done():
168171
return nil
172+
case s := <-state:
173+
if !s {
174+
return nil
175+
}
169176
case event := <-rescale.ResultChan():
170177
handleEvent(event, func(e *eventv1.Event) (eventsql.UpsertParams, bool) {
171178
if !strings.HasPrefix(e.Note, "New size") {

0 commit comments

Comments
 (0)