Some Bits: Nelson's log

4 posts in December 2011

Sublime Text 2 and Python

I’m working on replacing my existing IDE (WingIDE) with Sublime Text 2. I prefer Sublime as an editor, particularly since WingIDE runs under X11 on MacOS (yuck). Also I don’t use most IDE features of Wing; just a bit of smart completion, unit test integration, pdb integration, .. OK, several things.

Anyway, here’s a list of Sublime Text modifications I’m finding useful for Python. I’ll update this post over time. (As an aside it’s hard to search for plugins for Python developers, because the plugins themselves are written in Python and so you get lots of false matches.)

  • sublimetext_python_checker: one of several linter plugins. I like this one because I can set it up to run PyFlakes in an unobtrusive way.
  • PdbSublimeTextSupport 0.2: a tiny shim to have Sublime show you what line of your source code it currently is in. I’ve never been good with PDB, I immediately miss all the graphical support of a full IDE like Wing. So this may not be enough.

I’ve also set up my own build command to use to run unit tests. It’s not complete, I need to figure out Sublime’s support for showing where errors occurred. But it’s a start.

{
 "cmd": ["python", "-m", "unittest", "discover", "-p", "*.py"]
}

Things that look promising that I haven’t tried:

Python linters

I was playing around with Python lint tools. There’s a lot of terrific info in this StackOverflow question, so all I really have to offer is some very simple knee jerk opinions.

  • pyflakes is very brief, but did a good job finding actual errors
  • pep8 is a nice way to check specifically for code style issues and nothing else.
  • pychecker is useless, since it executes the code
  • pylint is amazingly detailed and anal. Also useless because it prints way way too much. I really don’t care that my test functions don’t have docstrings or that my comments are more than 80 characters. Maybe if I were working on more team projects I’d build up a set of “ignore this message” rules and turn it into a useful tool to me.
Conclusion: pyflakes. If I cared more, I’d use pylint. If I were contributing code to libraries, pep8.

Trying out Riak on MacOS with Homebrew

Homebrew install notes

I decided to try out Riak on MacOS, using Homebrew to install. Here’s some rough notes.

“brew install riak” worked. However, it didn’t leave you the Makefile you need to do “make devrel” in order to set up the 3 node test cluster. So I interrupted Homebrew during the install, copied the patched sources, did “make all rel devrel” in my own source tree, then copied the “dev/dev?” directories off. Still using the Homebrew binaries. Lot of work for 4 lousy config files :-P

The Python client you want is riak-python from Basho. Unfortunately “pip install riak” doesn’t quite work, because Pip first tries to install protobuf and there’s a three year old bug open on Google’s protobuf client about how it doesn’t install right in Python. Some helpful soul forked Google’s library and his version can be installed witih pip install “git+http://github.com/rem/python-protobuf.git@v2.4.1#egg=protobuf”

With that you have a working Riak and Python environment that you can use to work with the tutorials online.

Insert notes

Trying to insert my weather data: millions of rows of 4 tuples: (station name, timestamp, speed, direction). Testing with 3 Riak nodes running on my single fast iMac. This data is not a good match for document oriented stores: I already learned that Mongo was not good and expect Riak not to be, either. Still, it’s real data I have and I want to try it.

Wow, is Riak slow if you just use it as a naive user. 3 node cluster running on a fast iMac, inserting 45,000 records one at a time with a store() between each took 120 seconds.

Some write optimization.. there appears to be no batch insert in the Python library I’m using (although see this). Riak’s exposure of consistency via the W parameter is really neat, but what I need is to commit 10,000 records at once. I’ve tried optimizing my writes (ie: windData.store(w = 0, dw = 0, return_body = False)). Switching from HTTP to Protocol Buffers gives about a 4x increase. Still, no matter what I do, I can’t do better than 1900 records / second. Compare Mongo’s 30,000 records / second or Postgres’ 10,000 rows a second. Am I doing something terribly wrong?

Sadly, I get a 10% speed increase (from 1900 to 2100 records / second) if I only have one node in the cluster. Not useful in the real world, but interesting.

With just a single node running Riak seems to use about 1 kilobyte / document (650,000 rows, 720 megs on disk). Not good, since the docs themselves are like 20 bytes of data. Compare about 50 bytes / row for both Postgres and Mongo. With 3 nodes the combined disk usage is about the same size; I’m surprised, I thought the default config was three copies of all data.

I sure miss the ability to drop a whole bucket. I don’t really have a way to reset my database while testing.

Query Notes

Once again I petulantly note that raw map/reduce is awfully low level if you just want a damn SQL function. Of course it gets wonderful when you want to query using 100 machines and a distributed data store.

Trying to list 50,000 keys in a bucket (via /keys?keys=true) kills both Chrome and curl: they complain the HTTP header returned is too big. Also the docs warn you to never do this in production. In other words, queries over the whole dataset are difficult. I think you need to use links or some extra indexing system to do queries. Riak has a search capability, btw, but it looks more full text oriented than simple data iteration.

I wrote some simple mapreduce code to do “select count(*) from bucket” and “select count(*) from bucket where parameter > 30”. It takes 16 seconds to run over 60,000 rows, that’s just unacceptable. Also the answer it gives is wrong; 43561 instead of 59072. Huh. While it’s cool I could run this query while nodes were dropping out, it’s uncool that the answer it gave varied as data was moved around.

I don’t think I have a bug in my query code, but you never know... Here it is.

import riak

client = riak.RiakClient(port=8081, transport_class=riak.RiakPbcTransport)

# select count(*) from KSFO
query= client.add('KSFO')
query.map("function(v) { return [1] }")
query.reduce("Riak.reduceSum")
for result in query.run(): print result

query = client.add('KSFO')
query.map('''function(v) {
    var data = JSON.parse(v.values[0].data);
    if (data[1] > 20) {
        return [1];
    } else {
        return [];
    }}''')
query.reduce("Riak.reduceSum")
for result in query.run(): print result

Evaluation

I knew this data was a poor match for Riak and now I’m doubly sure. The 1k / row is the real problem. I don’t think Riak is bad or anything, quite the opposite. Just curious how it feels to try it out on some data.

I really admire how Riak is distributed to the core. Most datastores design for a single server, then later add replication and partitioning. It’s a total mess. Riak started distributed, and if they did it right it should be significantly more robust for real network usage. Also for parallel data analysis. I also think it’s freaking amazing how you can do “riak-admin join” and “riak-admin leave” and the data transparently repartitions while the cluster is still up and serving write requests.

I wonder how robust Riak is in recovering if a node drops offline without a clean shutdown, then joins later with inconsistent data? The docs on CAP mention some voting and validation protocols, so they’ve thought about it. More a question of convenience than usability.

Postgres 9.0 to 9.1 upgrade with Mac / Homebrew

I just had a terrible time trying to upgrade PostgreSQL 9.0 to 9.1. I finally gave up and am still running the old one.

pg_upgrade is the recommended upgrade path. It’s pretty ugly, and among other things requires you have the old server binaries around. But it looks like it’d do the job. I had a few minor problems, like I ran “--check” and the check failed but left something around that made Postgres think a server was running. (Fortunately pg_ctl start just ignored that and started a server anyway, then pg_ctl stop fixed it.)

Then I hit a bigger problem: PostGIS I’ve got that installed in my 9.0. I *think* it’s installed in 9.1, too, but I can’t be sure with Homebrew. In any event pg_upgrade doesn’t think it’s there so I’m SOL for now.

An alternative is to do pg_dump on the old server, then pg_restore on the new. I suspect that’s what pg_upgrade does behind the scenes anyway. And I still have to resolve my PostGIS problem.