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 dead providers from coordinator #4010

Merged
merged 2 commits into from
May 16, 2024
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
8 changes: 8 additions & 0 deletions providers/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ func (c *coordinator) RemoveRuntime(runtime *Runtime) {
log.Warn().Err(err).Str("provider", p.Name).Msg("failed to shut down provider")
}
}
} else {
// Check for killed/crashed providers and remove them from the list of running providers
for _, p := range c.runningByID {
if p.isCloseOrShutdown() {
log.Warn().Str("provider", p.Name).Msg("removing closed provider")
delete(c.runningByID, p.ID)
}
}
}

// If all providers have been killed, reset the connection IDs back to 0
Expand Down
54 changes: 54 additions & 0 deletions providers/coordinator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,60 @@ func TestRemoveRuntime_StopUnusedProvider(t *testing.T) {
assert.Empty(t, c.runningByID)
}

func TestRemoveRuntime_RemoveDeadProvider(t *testing.T) {
ctrl := gomock.NewController(t)

// Setup 1 closed provider with 1 runtime
mockPlugin1 := NewMockProviderPlugin(ctrl)
p1 := &RunningProvider{
ID: "provider1",
Plugin: mockPlugin1,
isClosed: true,
}
r1 := &Runtime{
providers: map[string]*ConnectedProvider{
"provider1": {Instance: p1},
},
Provider: &ConnectedProvider{
Instance: p1,
Connection: &pp.ConnectRes{
Asset: &inventory.Asset{PlatformIds: []string{"platformId1"}},
},
},
}

// Setup another provider with another runtime
r2 := &Runtime{
providers: map[string]*ConnectedProvider{
"provider2": {Instance: p1},
},
Provider: &ConnectedProvider{
Instance: p1,
Connection: &pp.ConnectRes{
Asset: &inventory.Asset{PlatformIds: []string{"platformId2"}},
},
},
}

c := &coordinator{
runningByID: map[string]*RunningProvider{
"provider1": p1,
},
runtimes: map[string]*Runtime{
"platformId1": r1,
"platformId2": r2,
},
}

// Remove 1 runtime
c.RemoveRuntime(r1)

// Verify that the provider has been removed because it crashed
assert.Empty(t, c.runningByID)

c.RemoveRuntime(r2)
}

func TestRemoveRuntime_UsedProvider(t *testing.T) {
ctrl := gomock.NewController(t)

Expand Down
2 changes: 2 additions & 0 deletions providers/running_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type RunningProvider struct {
// initialize the heartbeat with the provider
func (p *RunningProvider) heartbeat() error {
if err := p.doOneHeartbeat(p.interval + p.gracePeriod); err != nil {
log.Error().Err(err).Str("plugin", p.Name).Msg("error in plugin heartbeat")
if err := p.Shutdown(); err != nil {
log.Error().Err(err).Str("plugin", p.Name).Msg("error in plugin shutdown")
}
Expand All @@ -47,6 +48,7 @@ func (p *RunningProvider) heartbeat() error {
go func() {
for !p.isCloseOrShutdown() {
if err := p.doOneHeartbeat(p.interval + p.gracePeriod); err != nil {
log.Error().Err(err).Str("plugin", p.Name).Msg("error in plugin heartbeat")
if err := p.Shutdown(); err != nil {
log.Error().Err(err).Str("plugin", p.Name).Msg("error in plugin shutdown")
}
Expand Down
5 changes: 5 additions & 0 deletions providers/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ func (r *Runtime) Connect(req *plugin.ConnectReq) error {
runtime: r,
}

// TODO: we probably want to check here if the provider is dead and restart it
// if r.Provider.Instance.isCloseOrShutdown() {

// }

r.Provider.Connection, r.Provider.ConnectionError = r.Provider.Instance.Plugin.Connect(req, &callbacks)
if r.Provider.ConnectionError != nil {
return r.Provider.ConnectionError
Expand Down
Loading