Monthly Archives: August 2012

Hackers sit down too much

Being a hacker means one tends to sit down too much. The hunch-backed hacker image complete with screen glow isn’t too far from the truth for some of us.

I hate this! We sit so much!

So I’ve devised a simple plan for reducing the amount I sit on a daily basis.

I picked one thing from my day that I do frequently enough and decided that when I do it I will do so standing. Namely emails.

It’s a simple change but somewhat effective.

My commitment from hereon is this: if I receive or write an email, organise my inbox or set up a meeting I will do so standing.

Server Backup with Spider Oak

I’m a SpiderOak fan. It’s secure, easy and well documented. Some really good traits. Their support is fantastic too.

After using it on my Laptop, I thought I’d start using it for my Debian server’s backup. It makes sense to use something that bundles encryption and zero-knowledge to satisfy offsite backup.

The same client you would use on your Linux desktop can be used in headless or batch modes. Combined with the --backup command, you can target your essential backup areas quickly and easy.

Assuming you have signed up (you get 3GBs free with this referral link), all you need to do is follow these easy steps.

Step 1 – Get SpiderOak on your sever
wget https://spideroak.com/directdownload?platform=slackware&arch=i386

Extract it to a place of your choosing and cd there (for argument’s sake, we’ll call this extraction directory $SO_HOME).

Step 2 – Copy the shared libraries
SpiderOak’s client will expect shared libraries to be available on your system. These are bundled in the .tar.gz you’ve downloaded. Copy these to your /usr/lib/ to make life easy.

cp $SO_HOME/usr/lib/SpiderOak /usr/lib/ -R

Step 3 – Initialise SpiderOak
You need to give SpiderOak a username and password and then let it add the server as a new device. You can do this by executing the following command and completing the prompts:
SpiderOak --setup=-

Step 4 – test the client works
Running a simple $SO_HOME/usr/bin/SpiderOak --help should show no errors and present you with the help message.

Step 5 – run a backup
Now all you’ll have to do is execute SpiderOak with a backup target and let it run!

SpiderOak --batchmode --backup=/home

Voila!

Clojure’s Concurrency: easy atoms

Clojure’s atoms let one transactionally and atomically store values. When one uses an atom, Clojure manages the mutation of the value and guarantees atomicity. Such a feature is very useful in a highly concurrent application. Much like Java’s Atomic* classes, but somewhat more powerful.

This is a brief introduction.

Atomic Values
To define an atom, one simply invokes the atom function value with an initial value (just like agents; see also agents and futures):
(def a (atom 0)) ; create an atom and let me refer to it by the identifier a

Now a is our reference to an atom which is managed by Clojure.

To get the value held in a, we simply dereference it:
user=> @a ; or (deref a)
0

Applying state changes to atoms can be achieved in two ways.

swap!
Calling swap! on an atom and passing a function will synchronously and atomically compare-and-set the atom’s value. If we wanted to increase the value held in a we’d do the following:
(swap! a inc)

At which point a would now hold 1:
user=> @a
1

Note: most of the state-mutation functions in Clojure’s concurrency features return the new/current state of the target. For atoms, calling swap! will return the new value should it succeed.

The compare-and-set nature is very useful because agents have another powerful element: validators. When defining an agent, you can optionally pass the :validator key and a function. This function will be used for the compare-and-set “phase”.

Let’s redefine a with our new validator:
(def a (atom 0 :validator even?))

This basically says “let a reference an atom whose initial value is 0 and call the function even? before setting any new values”.

If we then called swap! with the inc function on an initial value of 0, we’d expect it to fail:
user=> (swap! a inc)
IllegalStateException Invalid reference state clojure.lang.ARef.validate (ARef.java:33)

which is awesome, as we can get the atomicity from using atoms but not have code dotted around the place performing pre-requisite checks on new values and all supporting high concurrency.

reset!
Another way of mutating an atom is by using the reset! function to assign the atom a new value:
(reset! a 4)

If an atom has a validator assigned, this will still execute when calling reset!:
user=> (reset! a 1)
IllegalStateException Invalid reference state clojure.lang.ARef.validate (ARef.java:33)