I was working on collecting stats about our API servers, and needed to connect them to some kind of a visualization system. The idea, of course, is that measurement drives all future optimizations and improvements, so we need to be able to quickly see what was going on in our processes.
We settled on using clojure-metrics to do the actual data collection from within our code, and then sending it all to the excellent Librato service for monitoring.
One thing I wanted was to send a snapshot of all collected metrics every 30 seconds. For this, I had a function called report-all-metrics that I essentially needed to run every 30 seconds. It would collect everything from the metrics registry, and then connect to the Librato API, and send everything over. It would be trivial to write this in a custom way in Clojure, by wrapping it in another function that recursively calls itself after sleeping for the desired duration.
However, I figured I’d wrap ScheduledThreadPoolExecutor from the java.util.concurrent package and get the benefits of the runtime managing this for me instead. I ended up with a function called run-thunk-periodically which does essentially what I described earlier. Here’s the code:
Here it is in action:
And the output looks like this:
The idea is that while it works as expected, when there is an exception thrown, it tells you what is going on in the logs. Also, the thread-pool name is set appropriately, so you can identify the threads in a profiler.
Hope this is useful to someone!
Logging is an obvious requirement when it comes to being able to debug non-trivial systems. We’ve been thinking a lot about logging, thanks to the large-scale, distributed nature of the Zolodeck architecture. Unfortunately, when logging larger Clojure data-structures, I often find some kinds of log statements a bit hard to decipher. For instance, consider a map m that looked like this:
When you log things like m (shown here with println for simplicity), you may end up needing to understand this:
Aaugh, look at that second line! Where does the data-structure begin and end? What is nested, and what’s top-level? And this problem gets progressively worse as the size and nested-ness of such data-structures grow. I wrote this following function to help alleviate some of the pain:
Remember to include clojure.pprint. And here’s how you use it:
That’s it, really. Not a big deal, not a particularly clever function. But it’s much better to see this structured and formatted log statement when you’re poring over log files in the middle of the night.
Just note that you want to use this sparingly. I first modified things to make ALL log statements automatically wrap everything being logged with pp-str: it immediately halved the performance of everything. pp-str isn’t cheap (actually, pprint isn’t cheap). So use with caution, where you really need it!
Now go sign-up for Zolodeck!
There is a spectrum of productivity when it comes to programming languages. I don’t really care to argue how much more productive dynamic languages are… but for those who buy that premise and want to learn a hyper-productive language, Clojure is a good choice. And for someone who has a Java background, the choice Clojure becomes the best one. Here’s why:
- Knowing Java – obviously useful: class-paths, class loaders, constructors, methods, static methods, standard libraries, jar files, etc. etc.
- Understanding of the JVM – heap, garbage collection, perm-gen space, debugging, profiling, performance tuning, etc.
- The Java library ecosystem – what logging framework to use? what web-server? database drivers? And on and on….
- The Maven situation – sometimes you have to know what’s going on underneath lein
- Understanding of how to structure large code-bases – Clojure codebases also grow
- OO Analysis and Design – similar to figuring out what functions go where
I’m sure there’s a lot more here, and I’ll elaborate on a few of these in future blog posts.
I’ve not used Java itself in a fairly long time (we’re using Clojure for Zolodeck). Even so, I’m getting a bit tired of some folks looking down on Java devs, when I’ve seen so many Clojure programmers struggle from not understanding the Java landscape.
So, hey Java Devs! Given that there are so many good reasons to learn Clojure – it’s a modern LISP with a full macro system, it’s a functional programming language, it has concurrency semantics, it sits on the JVM and has access to all those libraries, it makes a lot of sense for you to look at it. And if you’re already looking at something more dynamic than Java itself (say Groovy, or JRuby, or something similar), why not just take that extra step to something truly amazing? Especially when you have such an incredible advantage (your knowledge of the Java ecosystem) on your side already?
My talk at The Strange Loop conference this year was recorded and is now available at InfoQ. I talk about why we’re using Datomic, Storm, and Clojure for our backend on the Zolodeck project.
Let me know what you think!
Here’s another useful function I keep around:
Everyone knows what map does, and what concat does. And what mapcat does.
The function definition for pmapcat above, does what mapcat does, except that by using pmap underneath, it does so in parallel. The semantics are a bit different:
First off, the first parameter is called batches (and not, say, coll, for collection). This means that instead of passing in a simple collection of items, you have to pass in a collection of collections, where each is a batch of items.
Correspondingly, the parameter f is the function that will be applied not to each item, but to each batch of items.
Usage of this might look something like this:
One thing to remember is that pmap uses the Clojure send-off pool to do it’s thing, so the usual caveats will apply wrt to how f should behave.
I kept using an extra line of code for this, so I decided to create the following function:
Another extra line of code can similarly be removed using this function:
Obviously, the raw forms (i.e. using doseq or map) can be far more powerful when used with more arguments. Still, these simple versions cover 99.9% of my use-cases.
I keep both these (and a few more) in a handy utils.clojure namespace I created for just such functions.
Alan Perlis once said: A Lisp programmer knows the value of everything, but the cost of nothing.
I re-discovered this maxim this past week.
As many of you may know, we’re using Clojure, Datomic, and Storm to build Zolodeck. (I’ve described my ideal tech stack here). I’m quite excited about the leverage these technologies can provide. And I’m a big believer in getting something to work whichever way I can, as fast as I can, and then worrying about performance and so on. I never want to fall under the evil of premature optimization and all that… In fact, on this project, I keep telling my colleague (and everyone else who listens) how awesome (and fast) Datomic is, and how its built-in cache will make us stop worrying about database calls.
A function I wrote (that does some fairly involved computation involving relationship graphs and so on) was taking 910 seconds to complete. Yes, more than 15 minutes. Of course, I immediately suspected the database calls, thinking my enthusiasm was somehow misplaced or that I didn’t really understand the costs. As it turned out, Datomic is plenty fast. And my algorithm was naive and basically sucked… I had knowingly glossed over a lot of functions that weren’t exactly performant, and when called within an intensive set of tight loops, they added up fast.
After profiling with Yourkit, I was able to bring down the time to about 900 ms. At nearly a second, this is still quite an expensive call, but certainly less so than when it was ~ 1000x slower earlier.
I relearnt that tools are great and can help in many ways, just not in making up for my stupidity