From 935c46319c5b9b9e5e796a0d4e78357d4a831c8c Mon Sep 17 00:00:00 2001 From: Sean Porter Date: Tue, 30 Aug 2022 11:50:39 -0700 Subject: [PATCH 1/4] SIGHUP configuration reload Signed-off-by: Sean Porter --- service/collector.go | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/service/collector.go b/service/collector.go index 1bde637760b..7113f6b7f80 100644 --- a/service/collector.go +++ b/service/collector.go @@ -154,6 +154,21 @@ func (col *Collector) setupConfigurationComponents(ctx context.Context) error { return nil } +func (col *Collector) reloadConfiguration(ctx context.Context) error { + col.service.telemetrySettings.Logger.Warn("Config updated, restart service") + col.setCollectorState(Closing) + + if err := col.service.Shutdown(ctx); err != nil { + return fmt.Errorf("failed to shutdown the retiring config: %w", err) + } + + if err := col.setupConfigurationComponents(ctx); err != nil { + return fmt.Errorf("failed to setup configuration components: %w", err) + } + + return nil +} + // Run starts the collector according to the given configuration, and waits for it to complete. // Consecutive calls to Run are not allowed, Run shouldn't be called once a collector is shut down. func (col *Collector) Run(ctx context.Context) error { @@ -164,7 +179,7 @@ func (col *Collector) Run(ctx context.Context) error { // Only notify with SIGTERM and SIGINT if graceful shutdown is enabled. if !col.set.DisableGracefulShutdown { - signal.Notify(col.signalsChannel, os.Interrupt, syscall.SIGTERM) + signal.Notify(col.signalsChannel, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP) } LOOP: @@ -176,21 +191,22 @@ LOOP: break LOOP } - col.service.telemetrySettings.Logger.Warn("Config updated, restart service") - col.setCollectorState(Closing) - - if err = col.service.Shutdown(ctx); err != nil { - return fmt.Errorf("failed to shutdown the retiring config: %w", err) - } - if err = col.setupConfigurationComponents(ctx); err != nil { - return fmt.Errorf("failed to setup configuration components: %w", err) + if err = col.reloadConfiguration(ctx); err != nil { + return err } case err := <-col.asyncErrorChannel: col.service.telemetrySettings.Logger.Error("Asynchronous error received, terminating process", zap.Error(err)) break LOOP case s := <-col.signalsChannel: col.service.telemetrySettings.Logger.Info("Received signal from OS", zap.String("signal", s.String())) - break LOOP + switch s { + case syscall.SIGHUP: + if err := col.reloadConfiguration(ctx); err != nil { + return err + } + default: + break LOOP + } case <-col.shutdownChan: col.service.telemetrySettings.Logger.Info("Received shutdown request") break LOOP From b49c7aed67532e1f45cb6bc63924360a058ec7ed Mon Sep 17 00:00:00 2001 From: Sean Porter Date: Tue, 30 Aug 2022 12:49:05 -0700 Subject: [PATCH 2/4] basic SIGHUP reload signal test Signed-off-by: Sean Porter --- service/collector_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/service/collector_test.go b/service/collector_test.go index 5063f95e72e..0bd9d4984cf 100644 --- a/service/collector_test.go +++ b/service/collector_test.go @@ -196,6 +196,12 @@ func TestCollectorSendSignal(t *testing.T) { return Running == col.GetState() }, 2*time.Second, 200*time.Millisecond) + col.signalsChannel <- syscall.SIGHUP + + assert.Eventually(t, func() bool { + return Running == col.GetState() + }, 2*time.Second, 200*time.Millisecond) + col.signalsChannel <- syscall.SIGTERM wg.Wait() From 36e2632d01f6889fcf720e5e91d7ab68c60e7817 Mon Sep 17 00:00:00 2001 From: Sean Porter Date: Tue, 30 Aug 2022 13:28:49 -0700 Subject: [PATCH 3/4] always notify with SIGHUP for configuration reloading Signed-off-by: Sean Porter --- service/collector.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/service/collector.go b/service/collector.go index 7113f6b7f80..2339e805d54 100644 --- a/service/collector.go +++ b/service/collector.go @@ -177,9 +177,12 @@ func (col *Collector) Run(ctx context.Context) error { return err } + // Always notify with SIGHUP for configuration reloading. + signal.Notify(col.signalsChannel, syscall.SIGHUP) + // Only notify with SIGTERM and SIGINT if graceful shutdown is enabled. if !col.set.DisableGracefulShutdown { - signal.Notify(col.signalsChannel, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP) + signal.Notify(col.signalsChannel, os.Interrupt, syscall.SIGTERM) } LOOP: From d9205d752cc5552008bb3ff5781c704d104a38f6 Mon Sep 17 00:00:00 2001 From: Sean Porter Date: Mon, 17 Oct 2022 12:33:10 -0700 Subject: [PATCH 4/4] sighup reload changelog entry Signed-off-by: Sean Porter --- .chloggen/sighup-configuration-reloading.yaml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 .chloggen/sighup-configuration-reloading.yaml diff --git a/.chloggen/sighup-configuration-reloading.yaml b/.chloggen/sighup-configuration-reloading.yaml new file mode 100644 index 00000000000..48f1a8ccb9c --- /dev/null +++ b/.chloggen/sighup-configuration-reloading.yaml @@ -0,0 +1,16 @@ +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: service/collector + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: "Support SIGHUP configuration reloading" + +# One or more tracking issues or pull requests related to the change +issues: [5966] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: