Major Cleanup, Reporting for Duty – Configuration Service
This afternoon I took a look at a guy's bug report that wasn't really all that good, but it got me looking at the problem, which was enough, I suppose. The background is that The Broker is an erlang process that has several built-in services as well as a simple means by registering services to The Broker so that clients can get at these guys easily and automatically route the data around. It's pretty nice, and one of the built-in services is the configuration service.
Basically, we have a mongoDB back-end and a simple service that allows the client apps to save and load all kinds of key/value data for all kinds of purposes. Some of my apps (ticker plants, greek engine) use this to hold the start-up configuration and load up this data on start-up and do all kinds of stuff with it. It's pretty nice.
However, there was a problem with a particular data structure coming out of mongoDB:
[{"key" : {"name" : "Bob", "age" : 49}}, {"code" : {"name" : "Boo", "age" : 72}}]
basically, a list of maps where the maps each have only a single key/value pair. It was coming out as a list of four items: key, map, key, map - not at all right. So I started digging into it. The only really useful way to handle this is to log what's coming out of mongo and fix up the erlang matches based on what is as opposed to what you think should be.
With just a few test cases down, we had the matches fixed up, and the outgoing mongo conversion was far cleaner. We totally re-wrote the method because it was just too difficult to follow, and made it a series of function calls, allowing erlang to figure out what version to call based on the data.
Then I went to fix up the ingoing mongoDB function and wham! I hit a wall. I spent quite a while trying to figure out why the conversion wasn't working. In the end, I had to calm down, pull back, and take a slightly larger view of things. Turns out, the original author had sprinkled conversion code in a lot of different places in the module - and the conversion we'd done was complete and needed nothing else. So the "double-decoding" was really one too many.
Once I took those out, things started working like I had expected them to. I was just too close to see that other code in the module was effecting the data we're getting out of mongo. Lesson to self: look larger next time.