Rework the converged detection and provide a clean interface

The old converged detection was hacked in code, instead of something
with a nice interface. This cleans it up, splits it into a separate
file, and removes a race condition that happened with the old code.

We also take the time to get rid of the ugly Set* methods and replace
them all with a single AssociateData method. This might be unnecessary
if we can pass in the Converger method at Resource construction.

Lastly, and most interesting, we suspend the individual timeout callers
when they've already converged, thus reducing unnecessary traffic, and
avoiding fast (eg: < 5 second) timers triggering more than once if they
stay converged!

A quick note on theory for any future readers... What happens if we have
--converged-timeout=0 ? Well, for this and any other positive value,
it's important to realize that deciding if something is converged is
actually a race between if the converged timer will fire and if some
random new event will get triggered. This is because there is nothing
that can actually predict if or when a new event will happen (eg the
user modifying a file). As a result, a race is always inherent, and
actually not a negative or "incorrect" algorithm.

A future improvement could be to add a global lock to each resource, and
to lock all resources when computing if we are converged or not. In
practice, this hasn't been necessary. The worst case scenario would be
(in theory, because this hasn't been tested) if an event happens
*during* the converged calculation, and starts running, the exit command
then runs, and the event finishes, but it doesn't get a chance to notify
some service to restart. A lock could probably fix this theoretical
case.
This commit is contained in:
James Shubin
2016-03-29 07:06:56 -04:00
parent a6dc81a38e
commit 6f3ac4bf2a
10 changed files with 342 additions and 151 deletions

View File

@@ -122,13 +122,6 @@ func (g *Graph) SetState(state graphState) graphState {
return prev
}
// store a pointer in the resource to it's parent vertex
func (g *Graph) SetVertex() {
for v := range g.GetVerticesChan() {
v.Res.SetVertex(v)
}
}
// AddVertex uses variadic input to add all listed vertices to the graph
func (g *Graph) AddVertex(xv ...*Vertex) {
for _, v := range xv {
@@ -855,9 +848,10 @@ func (g *Graph) Exit() {
}
}
func (g *Graph) SetConvergedCallback(ctimeout int, converged chan bool) {
// AssociateData associates some data with the object in the graph in question
func (g *Graph) AssociateData(converger Converger) {
for v := range g.GetVerticesChan() {
v.Res.SetConvergedCallback(ctimeout, converged)
v.Res.AssociateData(converger)
}
}