Omnipresent, Infallible, Omnipotent and Instantaneous Build Technologies
Join the DZone community and get the full member experience.Join For Free
claim: in 2014, build technologies should be omnipresent, infallible, omnipotent and instantaneous.
context: this article is about enterprise development teams who collectively issue many hundreds of builds a day, as they work on features or bugs. the third-party stuff they pull in (tomcat, apache. mysql) can use whatever build technologies they like – this isn’t about those. it is about multi-module, multi-application source-code organized in “one big trunk” with different teams focussed on different things from different or overlapping sets or directories of that source tree. it is about making the shortest build times from potentially very large build trees.
a hypothetical two-app example
say myapp1 and myapp2 are maintained by different project teams, and go into production on different release cadences. they both depend on the same libraries: log4j (third party), and “common” (something made in-house). both are in the same branch (trunk) of the same repo.
what my headline claim means
running a second build straight after a first, should be as close to instantaneous if nothing else has changed between the two builds.
with respect to the well-known test-pyramid (a martin fowler blog entry about a mike cohn publication), the thinking is tests are one of the following:
- unit (each test is < 1ms)
- integration/service (each is < 1 second)
- acceptance/functional (selenium etc, and perhaps greater than 1 second each)
google wrote about this too with test sizes in 2010, and they had this baked into their build-infrastructure from at least 2007 (predating the mike cohn publication).
in modern cd-centric builds, as developers (or ci daemons), we would regularly do one of these by choice:
- compile only builds
- compile followed by tests that don’t require application/service deployment
- compile followed by tests including functional/acceptance tests that require application/service deployment
it is possible to do full selenium (acceptance/functional) tests on components outside of the larger application deployment, but most companies don’t do that yet.
for our contrived two-app example, we should analyze a graph of additional triggers for stages of a build:
additional to the source for that node changing, of course.
as it happens, 3 and 5 could or should happen in parallel. if that can happen, then 4 & 6 could also potentially be in parallel as a consequence.
skipping compile when it is not needed
if you just did a build, and nothing else has changed, then you don’t need to recompile object code. in order to do that you need to track what source code made what object code. in order to do that, you need to match timestamps between the former that created the latter. alternatively, given that is naive, it would be smarter to rely on hash-style functions of source and other inputs that created the object code. in this instance it might be better to work at the directory level rather than the source-file level, though there are some speed tradeoffs there. you could make a case that the per-source-file and timestamp tracking version isn’t infallible (at least for some languages).
compile artifacts being idempotent is key here. the infallible aspect here means that compile should not be skipped if compile would fail – you have to be 100% sure that it would not fail, and the previously build binary is what would have been produced by it.
skipping tests when they are not needed
skipping tests is a more difficult decision to make. a naive implementation could skip tests if the prod-source, test-source and all relevant inputs are unchanged since the last run. oh and the last run yielded 100% passing tests. dependencies (including transitive ones) might change that though. whether binary or source (from outside the module in question), if they have changed then the fact that the previous compile/build had 100% passing tests is irrelevant. the infallible aspect here means that the build technology should not skip tests (indicating they should all pass), when some would fail.
further complicating matters is that tests can be subsetted. you may have run only unit tests in the previous run (skipping integration/service and acceptance/functional). if you’re not similarly running the same suite in this run, the build system should know how to optimize which tests have to be run or can/should be skipped in the following build.
leveraging co-workers recent builds.
say you and “jimmy” both came back from vacation, but jimmy is in work 30 minutes ahead of you. everything he does, you’re going to do too, but 30 mins later. he catches up in source control (
). he issues a build, which because he’s been away for a while, takes 15 mins – a lot is new the last build he did before his vacation.
when you come in, you do the same catch-up in source control, but your build takes 2 mins. why? your build should be able to pull compiled artifacts from jimmy’s system (assuming the same compiler version). sure he might have cleared his build artifacts already, but the build technology should have pushed them into a shared cache for nearby people or daemons to leverage. obviously may be some lru aspects to the cache, which is why i made this very contrived case in terms of timings. as it happens jimmy’s worst case scenario (15 mins) was likely not realized either if there were more people in the team that have been building head recently.
ci has needs too.
per build pipeline, ci slaves nodes will need to leverage each others build artifacts and test results. in between assigned builds, they should make an effort to catch up to the latest known passing build’s revision and the artifacts and test results (all passing obviously).
buck by facebookers (and ex-googlers)
buck is an attempt to target a multi-language source reality for larger companies, and reduce build times to the absolute minimum. simon stewart (mr webdriver) is one of the facebook techies leading the effort. he’s ex-google and ex-thoughtworks too. he remembers google’s fabled blaze build system very well, having previously been googler. he also made crazyfun for the selenium2 build in its image (while a googler). i blogged about buck when it was launched . the same day thomas broyer went deeper .
ahead of the actual compile and tests phases, a controlling process walks a graph of source to check:
1. whether all dependent modules are available in the checkout
2. whether there are any circular dependencies in the above
3. what production source sets need rebuilding
4. what test source sets need rebuilding
5. whether there is previously built object code available in the shared cache
6. whether there is previous relevant test results in the shared cache
simon tells me that cache eviction need not be lru, and that advanced systems might compute the cost of the artifact and keep the most expensive. he also notes that each target is treated as a pure function. specifically if the inputs have not changed, the output are expected to be identical.
maven: recent enhancements
maven is the java ecosystem’s dominant build system. it has been for ten years or more. jason van zyl is its creator, and though it is now ordinarily hosted at apache, it’s plugin architecture is very open, and it is easy to contribute modified or alternate plugins from afar. maven used to be purely iterative in it’s progression through trees of modules to build.
jason’s new boutique consultancy and services company around maven is takari.io . of many skills jason has, tenacity is the one you should note. maven is intermittently criticized over the years, but it keeps pushing forward – jason keeps pushing it – never underestimate his willingness to keep pushing forwards.
maven smart builder is the newest initiative that jason is front and center of, driven by his client’s needs. it skips stages if it can because of idempotent build artifacts. the newer maven is much more effectively multi-threaded now too. it is largely a migration path for existing maven teams, and pushed into the “repeatable build” territory. with this initiative comes a focussed incremental build api, the “smart builder” piece, and perhaps even a new ci daemon that’s tuned to enterprise needs for maven builds.
differences between the maven and buck way.
buck is attempting to be much more language neutral. python (including test invocation and binary/library creation) is covered. java too (android and j2se), as well as c and c++. no ruby or dotnet yet, but contributions are welcome. buck is also unix only for now – no windows capability yet – though the team wants to get there.
maven is pitched at the java ecosystem, which includes the groovy, clojure and scala languages. that said, it also handles c++ projects. it also has first class support for pulling dependencies from ‘maven central’ and publishing released artifacts there too. maven works on windows, linux and the mac.
buck has a daemon that’s deployed on your workstation – “buckd” – that watches for what’s happening with source files, and feeds into the build in order to correctly optimize things. a similar “mavend” is forthcoming.
consider source trees for a hypothetical example:
maven’s source layout (pom.xml is its build file):
buck’s source layout (buck files indicate it’s build grammar):
scm support for sparse checkouts
subversion naturally allows you to checkout from a sub-directory if that makes sense to you. git and mercurial only allow checkouts from the root directory. perforce makes you define a client-spec which could effectively hide the root node (meaning it is similar to subversion).
subversion allows you to do a “sparse checkout” from root in a non-recursive way, and the go on to specify a subset of the directories therein to further checkout (and track going forwards). git also has “sparse checkout”, but the coupled clone is not sparse. mercurial also has something coming in this design. perforce, via the same client-spec, allows the directories at any place deeper than root to be included or excluded. buck totally embraces that way of working, as did blaze before it. that’s how you manage a large head revision set for a “one big trunk” model down to just the bits you’re interested in.
buck and the newer maven will work with a partial checkout, but only if a critical module is not missing. a developer chooses to checkout the common and myapp1 directories (but not myapp2) … the buck and maven will still build correct artifacts for that from root, and run tests for them. only if that’s possible though, as i said, you couldn’t checkout myapp1 without common (for my contrived example).
maven similarly allows subsetting. you can checkout from any of the subdirectories with a pom.xml in them, and build from there. in that case maven will pull dependent artifacts from the maven repo it is configured to use. if the parent module suggests to recurse into myapp2, but it is not in the sparse checkout then the latest maven will not fail with an error.
maven’s natural inclination is to pull third party dependencies from a local maven repo (which in turn might have come from “central”). before maven, even the java community was used to checking in binary dependencies. the maven ecosystem is so dominant for java build technologies that even the competing build technologies (gradle etc) pull down deps from “maven central”. maven naturally places them in a .m2/ folder in your home directory. an example is “test” dependency “junit-4.12.jar”. note that it is in the repo with 4.12 in its file name, meaning some modules could depend on different versions. that last means lock-step upgrades are not necessary, and clashes have to be detected by maven itself at build time, before they cause tricky-to-solve problems at run-time.
buck, like blaze before it, pulls dependencies relatively from the checked out directory. classically, that’s a third_party/ directory in root. it is not wild-card included in build paths. instead each dependency is listed in the buck build grammar as a dependency. with reference to the test dependency on junit – it would be in third_party/ without 4.12 in its file name, meaning no modules at all could depend on different versions. that last means lock-step upgrades is normal.
hypothetically for maven, you could pull dependencies relatively (that third_party/ directory) from the same checkout, but nobody does. i would like to see that explored more. especially when git solves its binary files problems.
both maven and buck will build modules in order. as they go, they work out which build to step into and when the pre-requisites facilitate that. if myapp1 and myapp2 were in the checkout, it would build common first, the myapp1 and myapp2 (possibly in parallel given the takari enhancements).
buck will chose to fast fail if dependencies are missing at the outset. maven may fail part way through if dependencies are missing, because it is recursing methodically. maven will fast fail if there’s a circular dependency amongst the actual modules themselves, though.
maven also quickly builds a tiny parent module first – just as a reusable memo of the parent/child relationships and any data sharable at runtime that was common to all.
maven isn’t quite omnipresent with respect to skipping test-execution yet, but it is coming jason tells me.
stages 4 and 6 (and 5 & 7) are parallelized by both maven and buck.
one big trunk.
we’re encountering lots of companies that have “many trunks” these days. perhaps they are sipping from the soa or microservices cool-aid, and have a lot of subversion repos – one for each service. they’re self-reporting that they are in fact doing trunk based development. that’s really never what we meant when we advocated trunk based development – especially for enterprises. what we meant was “one big trunk”. one, not many. we might have to really focus on the “one big trunk” title going forward to differentiate.
minimalist test execution, via hacks
i’m not providing software here, just a what you need, recipe:
- in a ci build-job that’s perhaps always running against head, run one test in isolation such that you can work out which source files were utilized during it’s execution. write a “prodsourcefilename” line to a file that ends in testsourcefilename.sourcesused text file is close to the actual test source file. maybe that’s a hidden file. maybe that’s a hidden folder (preserve the paths in question too). check that file in to the repo. yes, the same repo. yes, check it in. yes, people will get it if they “svn up” (or equivalent).
- take all of those files (perhaps daily) and flip the information and write a new text file (or json or xml). the lines in it would be "sourcepathandfilename (space) testfilepathandname (carriage return). check that in too – perhaps in the root – try to make sure that the file is sorted consistently (to aid diffs).
- have a mode of operation for your test runner that uses the new meta file to reduce your whole test-base down to just the ones impacted by the modifications you are dealing with (via “svn status”, or equivalent).
it would be nice if that was impacted methods rather than impacted sources, but that’s much harder to collect. note that code coverage is not the same for every language, nor is the relationship between source file and object-code.
Published at DZone with permission of Paul Hammant, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.