The Swallows Return To Capistrano
- Posted in:
- automation
- services
- cloud
- environment
- migration
A while back (god almost a full year ago), I posted about the way in which we handle environment migrations, and to be honest, it hasn’t changed all that much. We have made some improvements to way we handle our environments (for example, we’ve improved our newest environments to be built into tested, versioned packages, rather than running directly from source), which is good, but the general migration process of clone temp, tear down old, clone back to active, tear down temp hasn’t really changed all that much.
Over time, we’ve come to realise that they are a number of weaknesses in that strategy though. Its slow (double clone!), its not overly clean and it can rarely lead to all of the data for the environment under migration being destroyed.
Yes, destroyed, i.e. lost forever.
This post is about that last weakness (the others will have to continue existing…for now).
Explosions!
In the original cloning scripts, there was an ominous comment, which simply said “# compare environment data here?”, which was a pretty big red flag in retrospect. You can’t always do everything though, and the various pressures applied to the development team meant that that step became somewhat manual.
That was a mistake.
After running a number of migrations across a few different environments (using basically the same concepts), we finally triggered that particular tripwire.
An otherwise uninteresting environment upgrade for one of our production services completely annihilated the underlying database (an EC2 instance running RavenDB), but the script gave no indication that anything went wrong.
Luckily, this particular service more of a temporary waystation, acting as a holding area facilitating the connection of two applications through a common web interface. This meant that while the loss of the data was bad (very bad), it wasn’t a problem for all of our customers. Only those people who had items sitting in the holding area waiting to be picked up were affected.
Obviously, the affected customers were quite unhappy, and rightfully so.
To this day I actually have no idea what went wrong with the actual migration. I had literally run the exact same scripts on a staging environment earlier that day, and verified that the same data was present before and after. After extensive investigation, we agreed that we would probably not get to the root of the issue in a timely fashion and that it might have just been an AWS thing (for a platform based on computers, sometimes AWS is amazingly non-deterministic). Instead, we agreed to attack the code that made it possible for the data loss to occur at all.
The migration scripts themselves.
Give Me More Statistics…Stat!
Returning to that ominous comment in the migration scripts, we realised that we needed an easy way to compare the data in two environments, at least at a high level. Using a basic comparison like that would enable us to make a decision about whether to proceed with the migration (specifically the part that destroys the old environment).
The solution is to implement a statistics endpoint.
The idea is pretty simple. We provide a set of information from the endpoint that summarises the content of the service (at least as best we can summarise it). Things like how many of a certain type of entity are present are basically all we have to deal with for now (simple services), but the concept could easily be extended to include information about any piece of data in the environment.
Something as simple as the example below fills our needs:
{ data: { customers: { count: 57 }, databases: { count: 129 } } }
A side effect of having an endpoint like this is that we can easily (at least using the http_poller input in Logstash) extract this information on a regular basis and put it into our log aggregation so that we can chart its change over time.
Making It Work
With the statistics endpoint written and deployed (after all it must be present in the environment being migrated before we can use it), all that’s left to do is incorporate it into the migration script.
I won’t rewrite the entirety of the migration script here, but I’ve included a skeleton below to provide an idea of how we use the comparison to make sure we haven’t lost anything important on the way through.
function Migrate { params ( #bunch of params here, mostly relating to credentials ) } try { # make current environment unavailable to normal traffic # clone current to temporary if (-not(Compare-Environments $current $temp)) { # delete the temporary environment and exit with an error } # delete current environment # clone temporary environment into the place where the current environment used to be if (-not(Compare-Environments $current $temp)) { # delete the new environment # keep the temporary environment because its the only one with the data } } catch { # if the current environment still exists, delete the temporary environment # if the current environment still exists, restore its availability } function Compare-Environments { params ( $a, $b ) $aEndpoint = "some logic for URL creation based off environment" $bEndpoint = "some logic for URL creation based off environment" $aStatistics = Invoke-RestMethod $aEndpoint #credentials, accept header, methods etc $bStatistics = Invoke-RestMethod $aEndpoint #credentials, accept header, methods etc if ((ConvertTo-Json $aStatistics.data) -eq (ConvertTo-Json $bStatistics.data)) { return true; } return false; }
Summary
The unfortunate truth of this whole saga, is that the person who originally implemented the migration scripts (I’m pretty sure it was me, so I take responsibility) was aware of the fact that the migration could potentially lead to loss of data. At the time, the protection against that was to ensure that we never deleted the old environment until we were absolutely sure that the new environment had been successfully created, making the assumption that the data had come over okay.
In the end, that assumption proved to be our undoing, because while everything appeared peachy, it actually failed spectacularly.
The introduction of a statistics endpoint (almost an environment data hash) is an elegant solution to the problem of potential data loss, which also has some nice side effects for tracking metrics that might not have been easily accessible outside of direct database access.
A double victory is a rare occurrence, so I think I’ll try to savour this one for a little while, even if I was the root cause of the problem.