Skip to content

Commit 4287027

Browse files
committed
Allow machineid.AutoUpdateVersionReporter to shut down correctly (#60219)
1 parent d66718e commit 4287027

File tree

4 files changed

+38
-34
lines changed

4 files changed

+38
-34
lines changed

lib/auth/auth.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ func NewServer(cfg *InitConfig, opts ...ServerOption) (as *Server, err error) {
808808
return nil, trace.Wrap(err)
809809
}
810810

811-
as.botVersionReporter, err = machineidv1.NewAutoUpdateVersionReporter(machineidv1.AutoUpdateVersionReporterConfig{
811+
as.BotInstanceVersionReporter, err = machineidv1.NewAutoUpdateVersionReporter(machineidv1.AutoUpdateVersionReporterConfig{
812812
Clock: cfg.Clock,
813813
Logger: as.logger.With(
814814
teleport.ComponentKey,
@@ -822,9 +822,6 @@ func NewServer(cfg *InitConfig, opts ...ServerOption) (as *Server, err error) {
822822
if err != nil {
823823
return nil, trace.Wrap(err)
824824
}
825-
if err := as.botVersionReporter.Run(as.CloseContext()); err != nil {
826-
return nil, trace.Wrap(err)
827-
}
828825

829826
if _, ok := as.getCache(); !ok {
830827
as.logger.WarnContext(closeCtx, "Auth server starting without cache (may have negative performance implications)")
@@ -1334,9 +1331,9 @@ type Server struct {
13341331
// plugin. The summarizer itself summarizes session recordings.
13351332
sessionSummarizerProvider *summarizer.SessionSummarizerProvider
13361333

1337-
// botVersionReporter is called periodically to generate a report of the
1338-
// number of bot instances by version and update group.
1339-
botVersionReporter *machineidv1.AutoUpdateVersionReporter
1334+
// BotInstanceVersionReporter is called periodically to generate a report of
1335+
// the number of bot instances by version and update group.
1336+
BotInstanceVersionReporter *machineidv1.AutoUpdateVersionReporter
13401337
}
13411338

13421339
// SetSAMLService registers svc as the SAMLService that provides the SAML
@@ -1846,7 +1843,7 @@ func (a *Server) runPeriodicOperations() {
18461843
case autoUpdateAgentReportKey:
18471844
go a.reportAgentVersions(a.closeCtx)
18481845
case autoUpdateBotInstanceReportKey:
1849-
go a.botVersionReporter.Report(a.closeCtx)
1846+
go a.BotInstanceVersionReporter.Report(a.closeCtx)
18501847
case autoUpdateBotInstanceMetricsKey:
18511848
go a.updateBotInstanceMetrics()
18521849
}

lib/auth/machineid/machineidv1/auto_update_version_reporter.go

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -130,36 +130,33 @@ func (r *AutoUpdateVersionReporter) Run(ctx context.Context) error {
130130
return trace.Wrap(err)
131131
}
132132

133-
go func() {
134-
defer r.logger.DebugContext(ctx, "Shutting down")
133+
defer r.logger.DebugContext(ctx, "Shutting down")
135134

136-
for {
137-
started := r.clock.Now()
138-
r.runLeader(ctx)
139-
leaderFor := r.clock.Now().Sub(started)
135+
for {
136+
started := r.clock.Now()
137+
r.runLeader(ctx)
138+
leaderFor := r.clock.Since(started)
140139

141-
// Context is done, exit immediately.
142-
if ctx.Err() != nil {
143-
return
144-
}
140+
// Context is done, exit immediately.
141+
if ctx.Err() != nil {
142+
return nil
143+
}
145144

146-
// If we were leader for a decent amount of time, any previous
147-
// backoff likely doesn't apply anymore.
148-
if leaderFor > 5*time.Minute {
149-
retry.Reset()
150-
}
145+
// If we were leader for a decent amount of time, any previous
146+
// backoff likely doesn't apply anymore.
147+
if leaderFor > 5*time.Minute {
148+
retry.Reset()
149+
}
151150

152-
// Wait for the next retry interval.
153-
retry.Inc()
151+
// Wait for the next retry interval.
152+
retry.Inc()
154153

155-
select {
156-
case <-retry.After():
157-
case <-ctx.Done():
158-
return
159-
}
154+
select {
155+
case <-retry.After():
156+
case <-ctx.Done():
157+
return nil
160158
}
161-
}()
162-
return nil
159+
}
163160
}
164161

165162
func (r *AutoUpdateVersionReporter) runLeader(ctx context.Context) error {

lib/auth/machineid/machineidv1/auto_update_version_reporter_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ import (
4848
func TestAutoUpdateVersionReporter(t *testing.T) {
4949
t.Parallel()
5050

51-
ctx := t.Context()
51+
ctx, cancel := context.WithCancel(t.Context())
52+
t.Cleanup(cancel)
53+
5254
clock := clockwork.NewFakeClockAt(time.Now().UTC())
5355

5456
backend, err := memory.New(memory.Config{
@@ -105,7 +107,9 @@ func TestAutoUpdateVersionReporter(t *testing.T) {
105107
require.NoError(t, err)
106108

107109
// Run the leader election process. Wait for the semaphore to be acquired.
108-
require.NoError(t, reporter.Run(ctx))
110+
errCh := make(chan error, 1)
111+
go func() { errCh <- reporter.Run(ctx) }()
112+
109113
select {
110114
case <-reporter.LeaderCh():
111115
case <-time.After(1 * time.Second):
@@ -149,6 +153,9 @@ func TestAutoUpdateVersionReporter(t *testing.T) {
149153
if diff != "" {
150154
t.Fatal(diff)
151155
}
156+
157+
cancel()
158+
require.NoError(t, <-errCh)
152159
}
153160

154161
func TestEmitInstancesMetric(t *testing.T) {

lib/service/service.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2725,6 +2725,9 @@ func (process *TeleportProcess) initAuthService() error {
27252725
process.RegisterFunc("auth.autoupdate_agent_rollout_controller", func() error {
27262726
return trace.Wrap(agentRolloutController.Run(process.GracefulExitContext()), "running autoupdate_agent_rollout controller")
27272727
})
2728+
process.RegisterFunc("auth.autoupdate_bot_instance_version_reporter", func() error {
2729+
return trace.Wrap(authServer.BotInstanceVersionReporter.Run(process.GracefulExitContext()))
2730+
})
27282731

27292732
process.RegisterFunc("auth.server_info", func() error {
27302733
return trace.Wrap(auth.ReconcileServerInfos(process.GracefulExitContext(), authServer))

0 commit comments

Comments
 (0)