Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove premature protection for Start/Stop, trust Service to start/stop once #342

Merged
merged 1 commit into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Remove premature protection for Start/Stop, trust Service to start/st…
…op once

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu committed Apr 27, 2021
commit dc08c67239968a75d8742c9b5cdd61a192bd6619
22 changes: 5 additions & 17 deletions internal/receiver/smartagentreceiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/signalfx/signalfx-agent/pkg/utils/hostfs"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenterror"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/consumer"
"go.uber.org/zap"
Expand All @@ -49,8 +48,6 @@ type Receiver struct {
nextTracesConsumer consumer.Traces
logger *zap.Logger
config *Config
startOnce sync.Once
stopOnce sync.Once
sync.Mutex
}

Expand Down Expand Up @@ -118,12 +115,7 @@ func (r *Receiver) Start(_ context.Context, host component.Host) error {

configCore.ProcPath = saConfigProvider.SmartAgentConfig().ProcPath

err = componenterror.ErrAlreadyStarted
r.startOnce.Do(func() {
// starts the monitor
err = saconfig.CallConfigure(r.monitor, r.config.monitorConfig)
})
return err
return saconfig.CallConfigure(r.monitor, r.config.monitorConfig)
}

func (r *Receiver) Shutdown(context.Context) error {
Expand All @@ -132,18 +124,14 @@ func (r *Receiver) Shutdown(context.Context) error {
monitorType: r.config.monitorConfig.MonitorConfigCore().Type,
}, r.logger)

err := componenterror.ErrAlreadyStopped
if r.monitor == nil {
err = fmt.Errorf("smartagentreceiver's Shutdown() called before Start() or with invalid monitor state")
return fmt.Errorf("smartagentreceiver's Shutdown() called before Start() or with invalid monitor state")
} else if shutdownable, ok := (r.monitor).(monitors.Shutdownable); !ok {
err = fmt.Errorf("invalid monitor state at Shutdown(): %#v", r.monitor)
return fmt.Errorf("invalid monitor state at Shutdown(): %#v", r.monitor)
} else {
r.stopOnce.Do(func() {
shutdownable.Shutdown()
err = nil
})
shutdownable.Shutdown()
}
return err
return nil
}

func (r *Receiver) createMonitor(monitorType string, host component.Host) (monitor interface{}, err error) {
Expand Down
11 changes: 1 addition & 10 deletions internal/receiver/smartagentreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenterror"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configtest"
Expand Down Expand Up @@ -197,23 +196,15 @@ func TestStartReceiverWithUnknownMonitorType(t *testing.T) {
)
}

func TestMultipleStartAndShutdownInvocations(t *testing.T) {
func TestStartAndShutdown(t *testing.T) {
t.Cleanup(cleanUp)
cfg := newConfig("valid", "cpu", 1)
receiver := NewReceiver(zap.NewNop(), cfg)
err := receiver.Start(context.Background(), componenttest.NewNopHost())
require.NoError(t, err)

err = receiver.Start(context.Background(), componenttest.NewNopHost())
require.Error(t, err)
assert.Equal(t, err, componenterror.ErrAlreadyStarted)

err = receiver.Shutdown(context.Background())
require.NoError(t, err)

err = receiver.Shutdown(context.Background())
require.Error(t, err)
assert.Equal(t, err, componenterror.ErrAlreadyStopped)
}

func TestOutOfOrderShutdownInvocations(t *testing.T) {
Expand Down