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
Fix instance mutator test #10605
Fix instance mutator test #10605
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ package instancemutater | |
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/juju/collections/set" | ||
"github.com/juju/errors" | ||
|
@@ -55,6 +56,7 @@ type MutaterContext interface { | |
type mutater struct { | ||
context MutaterContext | ||
logger Logger | ||
wg *sync.WaitGroup | ||
machines map[names.MachineTag]chan struct{} | ||
machineDead chan instancemutater.MutaterMachine | ||
} | ||
|
@@ -95,7 +97,8 @@ func (m *mutater) startMachines(tags []names.MachineTag) error { | |
id: id, | ||
} | ||
|
||
go runMachine(machine, c, m.machineDead) | ||
m.wg.Add(1) | ||
go runMachine(machine, c, m.machineDead, m.context.dying(), func() { m.wg.Done() }) | ||
} else { | ||
// We've received this tag before, therefore | ||
// the machine has been removed from the model | ||
|
@@ -106,13 +109,16 @@ func (m *mutater) startMachines(tags []names.MachineTag) error { | |
return nil | ||
} | ||
|
||
func runMachine(machine MutaterMachine, removed <-chan struct{}, died chan<- instancemutater.MutaterMachine) { | ||
func runMachine(machine MutaterMachine, removed <-chan struct{}, died chan<- instancemutater.MutaterMachine, dying <-chan struct{}, done func()) { | ||
defer done() | ||
defer func() { | ||
// We can't just send on the dead channel because the | ||
// central loop might be trying to write to us on the | ||
// removed channel. | ||
for { | ||
select { | ||
case <-dying: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggest: The machines and the worker share a context, no need to pass it in. It's buried down in mutaterWorker newMachineContext(). Ran the race stress 500+ successfully. |
||
return | ||
case died <- machine.machineApi: | ||
return | ||
case <-removed: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
package instancemutater | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/juju/errors" | ||
"gopkg.in/juju/names.v3" | ||
"gopkg.in/juju/worker.v1" | ||
|
@@ -174,9 +176,12 @@ type mutaterWorker struct { | |
} | ||
|
||
func (w *mutaterWorker) loop() error { | ||
var wg sync.WaitGroup | ||
defer wg.Wait() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only problem with wg.Wait is that it will hang if we are supposed to be dying if our code has bugs in it. But i think you've wired through dying already, so its probably ok. |
||
m := &mutater{ | ||
context: w.getRequiredContextFunc(w), | ||
logger: w.logger, | ||
wg: &wg, | ||
machines: make(map[names.MachineTag]chan struct{}), | ||
machineDead: make(chan instancemutater.MutaterMachine), | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
given 'died' 'dying' and 'done', maybe better names might be:
'processDying', 'machineDied', 'cleanup' ?
or maybe 'stop', 'apiDied', 'cleanup'?
I'm not sure on the names, it is just a little confusing as to what is signalling this code that it should stop, vs where is this code signaling others
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'removed' is modelCache.removeMachine has been called for the watched lxd machine or container. 'died' is the machine hosting the containers is going away. With my comment at 120, dying isn't necessary.
Perhaps:
died = hostMachineDied
removed = machineDying
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dying is necessary, otherwise the goroutine doesn't end.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, didn't see comment below, will use that.