A little sugar with your Clojure Aspect Contracts
TL;DR: clojure-contracts-sugar - some sugar macros for clojure.core.contracts
Introduction
Back in November 2012, in one of my first toe-dippings into Clojure, I wrote a post on my initial experiences with Michael Fogus’s clojure.core.contracts (hereafter just CCC).
The rest of this post assumes you have a passing familiarity with CCC. If you aren’t, you may find my original post a digestible introduction.
Although using CCC for contracts programming is self-evident, it was the opportunity to use the Clojure’s :pre and :post assertions to support the concept and potential of aspects as defined originally by Gregor Kiczales in his work on Aspect Oriented Programming (AOP).
In this post I’m following through the experiments and ideas of the original post, and illustrating with some examples using a new library I’ve written recently - clojure-contracts-sugar (CHUGAR).
In the original post I made the point that CCC could do with some productivity aids - sugar - to ease the rich usage of CCC. CHUGAR attempts to supply that sugar and you can think of this post as partly a getting started tutorial for the library.
In the simplest cases, CHUGAR reduces to CCC albeit with a slightly more flexible syntax. If your contracts needs are straightforward, you’re likely better off using CCC or even :pre and :post assertions directly.
That said, I would encourage you though to have a look at the sections on mnemonics below which offer an easy way to use (and re-use) rich and flexible aspect contracts.
Note, the usual caveat, CHUGAR is a work in progress and really is intended to be only a starting point (foundation) for explorations of contracts from Clojure. The API is “settling” but may change.
A quick note on terminology: in the original post I used suck to identify the (input) arguments of a function and spit the result (return value). That “convention” is used extensively both here and in the library’s code.
Calling JRuby from Java and Clojure using Red Bridge
TL;DR: JRuby Red Bridge Embed Core is awesome!
A few months ago I needed to be able to call arbitrary methods in arbitrary JRuby scripts from an instance of a Java class.
Its turns out that JRuby has had a stable API called Red Bridge since version 1.4.
This post show how to use Red Bridge from Java, and also Clojure, to call arbitrary methods in arbitrary JRuby scripts.
The really nice thing about Red Bridge is that it will run “standard” Ruby without any fuss or bother e.g. you don’t need to compile the Ruby to JVM bytecode.
It also means the Java / Clojure is decoupled from the Ruby (source); you can change the Ruby very quickly and try again, making for a very quick development cycle.
Some trivial examples of using Clojure Reducers
TL;DR: Reducers are all about maximising performance.
The Clojure Reducers library (“reducers”) was announced by Clojure’s author Rich Hickey in May 2012 in this blog post. He followed up a week later with a second post.
These posts were very well written and densely packed with volumes of information and both really benefit from being pored over and multiple re-reading. Their approach was one of trying to convey an understanding of reducers together with some examples of how to use them.
Both posts covered a lot of ground, introducing the (new) reducers way of thinking about map, filter & reduce for collections, aided by new terminology to explain the new ideas.
Reducers also use heavily Clojure’s support for dynamically generating functions (in fact, a function generating a function that in turn generates a third function) so you need to have a clear grasp of how that works as well.
The second post also explained how reduce had been changed (in v1.4) to allow collections to support directly reduction using the CollReduce protocol.
Quite a lot to take in and the mental jigsaw puzzle quite hard to piece together at times.
There have been other posts explaining reducers (especially good is this slideshare from Leonardo Borges) but all have followed the approach of, and examples from, Rich’s original posts i.e. understand and then use.
Sometimes though is easier to consolidate your understanding after having used something successfully. As I’ve been poring over Rich’s and other posts, I been writing some examples for precisely that reason and thought I’d present a “narrative” of some trivial example to illustrate the use of reducers.
I’m will be deliberately avoiding as much “theory” and new terminology as I can and just concentrating on some practical, if trivial, examples, labouring points sometimes to ensure clarity.
A Ruby JMX Feed for Riemann
There are a plethora of monitoring tools available today, the range of choice catering for most every need and for all sorts of application and / or infrastructure complexity. And size of chequebook too although price is no real guide to quality, flexibility or ease of use.
Many, perhaps most, of the best solutions are open source and cross-platform (Linux/Unix and Windows). Some have great features, battle-hardened reliability, excellent support and documentation; and helpful and engaged communities underpinning them. The grand-daddy is undoubtedly Nagios. I’m no expert in Nagios but there is no doubt it is a magnificent achievement.
Do we really need another solution? Probably not. But Riemann is interesting and relevant because its not a monitoring tool per se but an event stream processor. Riemann is a more a scion of the same family as (the also hugely impressive) Esper.
Simply put Riemann is, in my words, a traffic cop for events coming in from multiple sources and going out to multiple sinks.
This post is about creating a Ruby source client for JMX data using Hadoop and HBase as the data sources. It does not go into how to build a Hadoop / HBase system (Apache BigTop 0.5.0 has just released and may be the easier route - I’ve not used it).
For completeness, I’ve included some background on Riemann, and simple guidance on how to run a Riemann server, and also how to configure Hadoop & HBase for JMX. If you are unfamiliar with either, you may find it beneficial to read them first, before the sections on the feed.
The below was tested on Ubuntu 12.04 using Ruby 1.9.3 (jRuby 1.7.2) and Cloudera’s CDH4 YARN distribution running in pseudo-distributed (i.e. one host) mode.
Gotcha: Constants in anonymous Ruby classes and mixins
I thought I had a reasonable grasp of Ruby and its constants, rarely these days having a wat? moment, but today I stumbled on something I find really quite surprising. Particularly so as I’ve never come across it before and I’ve read my fair share of Ruby books, posts and articles.
Have you ever used constants in anonymous mixins and classes? Well read on …