Measure

Log In

Topics

How to Avoid Graph Loops in Puppet So That You Can Avoid Graph Loops in Puppet

We recently ran into a slightly tricky problem with our Puppet configuration (a loop in the directed acyclic graph in the catalog), and I’d like to talk a little bit about what the problem
was and how we fixed it.

Quick Overview: Puppet and Graph Theory

Graph Theory?!

When Puppet compiles a manifest file (or files), it builds a data structure that relates each resource to every other resource it either depends on or is needed by. This kind of data structure is called a “directed acyclic graph” (DAG). Puppet uses this structure to decide where to begin applying changes and in what order to apply them; this is how it can explicitly order even complex dependency trees (install a package, edit these three files, then create this directory, and once all of those things are done, start this service).

The “Acyclic” Part of “Directed Acyclic Graph”

One of the primary reasons Puppet goes to all this trouble is to make sure that there are no loops (or “cycles”) in the DAG. A loop is created when a resource both is depended on and depends on some other resource (no matter how indirectly), and it means Puppet has no idea where to start applying changes. If resource A depends on resource B, which depends on resource A, then neither ordering is correct (doing either A or B first violates the DAG), and in those cases Puppet will throw up its hands and refuse to take any action at all.

Graph Loops and You

For a simple example of what happens when Puppet encounters a graph loop, the following manifest:

Be Vewwy Vewwy Quiet, We’re Hunting Graph Loops

In the simple example above, it’s pretty easy to see where the graph loop is and how to resolve it. However, once you start adding custom resources, virtual resources, classes included based on Facter facts, etc., it can get tricky real fast. For instance, here’s a real error message we ran into recently after a change:

Looking through this log message, it definitely wasn’t immediately obvious to me where the problem was. It seemed like all of the services were depending on and also required by their log directories, but a close inspection of the manifests where these definitions are yielded no fruit.

The --graph Option, You Say?

Puppet supports creating GraphViz-format files that detail the DAG it builds while compiling the manifests. These files can be rendered in a variety of graphing tools to get a visual picture of the relationships between various resources. For example, here’s a rendering of this very configuration:

It turns out that for complicated configurations, looking at a rendering of the DAG is not necessarily helpful.

Actual Analysis (as opposed to squinting)

While researching this problem, I found a blog post that addressed it specifically. It includes a simple Python script that will analyze a GraphViz file and display just the graph loops to make them easier to find.

Running this script against our configuration gave the following output:

This showed me immediately that the problem was with the resource that sets up log directories; somehow, the creation of the root logdir had become dependent on the creation of one of its children.

Armed with that information, I was able to quickly track down a recent change to our custom service setup resource where I’d added a subscribe metaparameter that wound up eventually watching for itself and thus creating a loop. I reverted that change and everything was happy once more.

Lessons Learned

In this specific case, of course I learned that this custom resource didn’t work exactly as I thought it did. However, I also learned some valuable troubleshooting techniques for the next time we run into this problem. I’m going to try to figure out a way to incorporate the loop-finding script with our continuous integration system; whenever a change is made to the Puppet configuration, one of the tests it runs should be to check for this sort of loop and to reject the change if one is found.

Also, I learned exactly how long it takes my laptop to open a 19862×21525 pixel image (82 seconds).

—

Did this post make your heart race at the thought of spelunking through a maze of twisty little Puppet manifests (all alike)? Come work with us and see how it’s done!