Some Bits: Nelson's log

9 posts in September 2011

Comparing Mongo and Postgres for wind calculation

I’m playing around with my weather data, CSV files containing things like “KSQL 1136073600 270 10”. (Station name, timestamp, wind direction, wind speed). Each row is 10 bytes of honest data and I have oh, 300 million rows.

I would like a sophisticated query language for doing aggregate stats, like “show me a histogram of directions for winds > 5kts on Tuesday mornings”. I have been hacking reports up by hand in Python by loading CSV files into lists and doing lots of dict and slices, which is just awful.

Insert

I tried Mongo first because it was new and fun, but I think its query facilities are too limited to be useful to me. More on that in a bit. But it’s sure fast for inserts: 30,000 rows a second, 47 bytes / row on disk. It’s also fun to work with a schema-free database, it always takes me 20 minutes to get a SQL create table right.

Postgres is slower on inserts, about 10,000 rows a second and 52 bytes / row. (The Postgres FAQ says there’s about 28 bytes of overhead per row.) This is with naive Python executemany() insert; a bulk CSV import would be faster.

But I don’t much care about speed, and less about scalability. This is all one off calculations I’m running by myself. And I’m not equipped to do proper benchmarking anyway.

One caveat: I’m doing all of this measurement without indices. Well, not entirely, Mongo gets a sort of implicit index because I’m storing each station in a separate collection. In the real world of course you’d want an index on station, maybe also on aspects of the timestamp.

Queries

Mongo has very limited query support. It has a basic query predicate language that’s awkward and verbose, no date and time calculations, and precious little aggregation capability like SQL’s AVERAGE() operator. You can write all your queries in Javascript to run on the server. And Mongo has a nifty mapreduce capability for big processing. Only mapreduce doesn’t actually run in parallel in a single Mongo instance, so there’s not much benefit to that computing paradigm unless you have a sharded database.

The big drawback with Mongo is a lot of code to do something that’d be a one liner in SQL. For instance, here’s getting the average wind speed at a station.

# select average(s) from KSFO
# This map reduce version is slow, about 1s on 50,000 rows. It's single threaded :-(
# http://www.mongodb.org/display/DOCS/MapReduce#MapReduce-Parallelism

# Map function: turn a document into the value (1, speed)
# The key for this map is a constant, the number 1: we're summing over the whole collection
mapFunction = bson.Code("""
function() {
  emit(1, { count: 1, sum: this.s });
}""")

# Reduce function: sum up the counts and sums
# Note this can be called on its own output, so have to match types from map
reduceFunction = bson.Code("""
function(key, values) {
    var result = {count: 0, sum: 0};

    values.forEach(function(value) {
      result.count += value.count;
      result.sum += value.sum;
    });

    return result;
}""")

# Execute the query and grab the results. Up to us to do the average calculation.
result = coll.map_reduce(mapFunction, reduceFunction, "myresults")
total = result.find_one()["value"]["sum"]
count = result.find_one()["value"]["count"]

print "Average speed: %.1fkts (%d samples)" % ((total / count), count)

That’s a lot of code just to do an average! Code I have to test, and maintain. It’s not particularly fast, either, 1000ms.

By contrast Postgres really is just one line of code for averages select avg(speed) from winds where station = 'KPAO'. And SQL has great complex date math, stuff I’d have to write myself for Mongo. Postgres is faster on this query, too: 600ms to do the average and that’s on a full table scan of millions of rows without even an index on the station. (It goes to 5ms with the index).

Conclusion

I’m not trying to say Postgres good Mongo bad, not at all. But Mongo’s not a good match for what I’m doing. I don’t have terabytes of data I need to shard across multiple servers. I don’t need sophisticated schema agility. I don’t really need relations, either. All I really need is efficient storage and a fast, convenient query language. Postgres and SQL are pretty good at that.

NFS notes

I set up some NFS mounts from my Linux (Debian) box to my Mac. Some notes.

Servers: install nfs-kernel-server. Configure it along these lines. NFSv4 seems to want to serve all its fileshares from a single directory, which seems weird, so you do this “mount --bind” thing to collect them on the server. Options on the server are ro,nohide,insecure,no_subtree_check,async

Client: Use MacOS Disk Utility (File / NFS Mounts..) to add static mount points. They seem to mount automatically once you exit the tool, but I’m not sure. I set the following advanced options: ro,intr,soft,nfsvers=4. Here’s some Mac docs.

NFSv4 is the new hotness. You can verify you’re using it with “nfsstat -4 -s” on the server and count the numbers.

These notes are all for a read-only export of world readable data. Next step is to export read-write, which requires synchronizing users. Unless you go some authenticated route (ie: Kerberos) the best bet is to be sure numeric user IDs are the same on the client and the server. On MacOS you can change your uid by right clicking on your name in the Users & Groups preferences pane. I’m guessing you’ll have to manually chown the files afterwards.

MongoDB size notes

I’m playing with using MongoDB to store a bunch of wind time series data for my wind map. First time trying it out. Some quick notes:

I have a bunch of data that’s 4-tuples like this:

“KSQL”, 2011-09-23 11:47:00, 200, 7

That says that at KSQL at a specific time the wind was 7kts from 200 degrees. In an ideal encoding each row would take 10 bytes. In a comfortable encoding it’ll take 20. I’ll settle for 50 bytes per row in the unindexed database. MongoDB is taking 67 bytes per object. Or more!

Sample input: KDTW and KMDS. 116,000 rows total, 2 megs of CSV, 400k of csv.gz. After import, Mono is reporting a 7.8 meg data size, or 68 bytes / object. That’s with very short element names (“d”, “s”, etc). If I use long element names (“direction”, “speed”) it balloons to 87 bytes / object. And yes, the extra 19 bytes is exactly the # of characters I added to my element name.

Lame! Apparently Mongo compresses nothing, it doesn’t even try to use a lookup table to avoid storing a zillion copies of element names? (See this user request to tokenize field names.) That seems very bizarre; not only would a simple table save space, wouldn’t it save time by making the memory working set smaller?

I don’t have any need for queries across multiple station names. So instead of having one giant table (er, “collection”) for all my data, I’m going to make one collection per station. That’ll give me some 5000 collections. I’m still at 47 bytes per row, which I said was acceptable but seems awfully wasteful. I’m beginning to wonder if MongoDB is a good match for this data. All those ObjectIds aren’t doing me any good, that’s for sure.

Notes on getting TileStache renders working on MacOS

Personal work notes for this project I’m working on. Mac DLL hell included. Unlikely to be of interest to any unfortunate reader who found this post.

I’m trying to run some tile generation software that generates odd TIFFs. The TIFFs have two separate images in one single file. This is a legitimate TIFF but it’s unusual and I’m having a hard time producing it on my Mac. The exact same end code works fine on Linux.

There’s a bunch of software involved here. Python, gdal, boost, MapNik, Cairo, swig, TileStache, ... When it’s all mashed up the code I’m running is basically a Python program with a lot of bindings to geo and image C libraries, using boost and swig. Ie: a big fucking hairball.

If I use MacOS Lion’s version of libtiff, the code all runs fine but the images it produces are corrupted. Same error as this discussion. gdalinfo can’t load them:

gdalinfo out/10/000/163/000/395.tiff
ERROR 1: out/10/000/163/000/395.tiff:Failed to allocate memory for to read TIFF directory (0 elements of 12 bytes each)
ERROR 1: TIFFReadDirectory:Failed to read directory at offset 52744
gdalinfo failed - unable to open 'out/10/000/163/000/395.tiff'.

If I use homebrew’s default libtiff (3.9.5) the code hangs. Poking at it with gdb I see the program is spinning at 100% CPU deep inside some error binding between libtiff, Python, and either Boost or Swig. Yuck!

If I build my own tiff-4.0.0beta7 in homebrew and use it, it runs and produces the same corrupted TIFFs as Lion. (To build in Homebrew, you have to edit the libtiff.rb formula and add an option to configure, “--x-includes=/usr/X11/include”. Otherwise, it can’t find some of the GLheader files when compiling.). tiff-4.0.0beta5 is no better.

A big confounding issue has been getting the Python environment right in Homebrew. More notes on that here. I think everything’s working anyway, various Python and gdal programs do, but you never know.

Another confounding issue has been compiling things correctly. The transition in C compilers on Lion makes a mess. Homebrew gamely tries to use LLVM but some stuff only works with gcc-4.2 (including Numpy). I was worried that mixing the two compilers might have been causing problems, so finally rebuilt everything with just gcc using this environment:

export HOMEBREW_USE_GCC=true
export CC=gcc-4.2
export CXX=g++-4.2
export FFLAGS=-ff2c
export HOMEBREW_BUILD_FROM_SOURCE=true

Extra notes: “man dyld” is very useful for manipulating shared libraries. In particular the DYLD_INSERT_LIBRARIES and DYLD_PRINT_LIBRARIES environment variables let you see and control which versions of libraries are being used.

Serving mbtiles tiles for slippy maps

I’m playing with TileMill (see my blog post here). TileMill publishes to an mbtiles file, a sqlite3 database that contains the rendered image tiles. One simple file, so nice! Now, how to serve it? The simplest solution is TileStream hosting, but I’m too cheap and hackish to pay the very reasonable $50 / month.

So I set up a tile server on TileStache instead, a Python system. You can see the setup on GitHub. It took about four hours to get working, even though none of it is too complex. Here’s my notes.

  1. Install TileStache via Python easy_install and configure. It’s pretty simple, the only subtle thing is that TileStache wants a cache. This may not be necessary with pre-rendered mbtiles bundles but it was easy enough to set up memcached and configure it.
  2. Set up TileStache to render via CGI, with a CGI script and a bit of Apache config. CGI is a bad way to serve things, but I’d hoped it was sufficient and it was easy to get going. Turns out to be unacceptably slow.
  3. I tried a couple of failed approaches for WSGI. First I tried TileStache’s built in WerkZeug server. This works and is faster than CGI, but lacks administrative scripts to wrap it. Then I tried Apache WSGI and ran into an error with no useful debug output. Gave up on that.
  4. Set up gunicorn, the excellent Python asynchronous server for WSGI. It’s easy to set up on Debian if you get version 0.13 from Backports, it comes with nice administration scripts. Just needs a little config file. I also installed python-gevent for an async worker.
Doing all this, my crappy old single CPU server with 1 gig of RAM can handle serving a map to a single user at acceptable speed. It can probably do more than that, I haven’t load tested it.
I left some things not done, this was intended as a quick hack:
  • gunicorn really wants a proxy HTTP server like nginx in front of it. Good idea, but I didn’t want to bother. The bummer is gunicorn doesn’t set any caching headers at all; really need those for a real deployment.
  • The cool kids building TileMill are doing everything with Node.js these days. Their TileStream server would be another option, but getting Node running on Debian was too much work for a quick hack.
It’s a little surprising that MBTiles is so complex to serve. It’s basically just a big archive of JPG images, it’d be nice to have a little C program to serve it blindingly fast. “mbserv foo.mbtiles --port 8989”. TileStache is a big program that does a lot of stuff, but even TileStream is 900+ lines of code and all it does is serve MBTiles.

svg:text - baseline considered harmful

SVG has a bunch of properties for laying out text. Two really handy ones are “dominant-baseline” and “baseline-shift”, which together let you move text up and down a bit. “dominant-baseline: central” seems particularly useful because it lets you lay out text where 0, 0 is the middle of the text, not the baseline underneath the letters. Ie: visually center things vertically.

Unfortunately those properties don’t work in many SVG implementations. They work in Safari and Chrome, but not in Mobile Safari, IE, or Firefox.

A workaround is to use the dy parameter on the text nodes to move them around. dy: .35em is about like centering. I don’t think this works via CSS, though, only in the DOM.

Before and After

I’ve added some new code to my wind history project to show winds based on month of year. With all 12 months selected the new code should produce similar results to the old code. I don’t really have any proper tests for this stuff, though, so I’m just kinda looking at the pictures and saying “yeah, looks about right”. Here’s a before and after: do the wind roses look the same to you? (Note that I moved the location of one wind rose, San Jose. That’s a bug fix).

 

 

 

Update: Seth was nice enough to register the images and do a difference calculation for me. Which is awesome and sad all at once :-P

Geolocation API tests

I’m adding geolocation to my windhistory project. It’s a very easy API. Some quick notes based on a call to getCurrentPosition() with a few options: { maximumAge: 1000*60*60, timeout: 1000*2, enableHighAccuracy: false}

Chrome on MacOS seems to use IP address to identify what city I’m in. It puts me at 37.775/-122.419, the middle of San Francisco, whether my WiFi is on or not.

Safari on MacOS seems to require WiFi location. It won’t geolocate at all if wifi is turned off; the Javascript API returns error -2 (no position available). With WiFi on it seems to use the same airhook style location as the iPhone. It puts me at 37.749/-122.428, which is just about where I’m sitting. MacOS has privacy System Preferences for location services. The geolocation API stops working if you disable location services entirely, but it’s not necessary to have “Safari” checked in order for Safari to geolocate you. That seems like a bug, even if Safari itself is doing some privacy protection.

Firefox 6 on MacOS also seems to use WiFi location, 37.749/-122.428. Didn’t test what it did without wifi.

MSIE 9 on Windows 7, running in a VirtualBox VM on my Mac, gives me a slightly different location of 37.748/-122.416. I’ve got no idea where it got that idea, but it’s a pretty good fix. Certainly not wifi! Maybe Microsoft has a geolocation service guessing based on external IP?

Firefox 4 on Windows 7 (in the VM) puts me at 37.775/-122.419, just like Chrome.

Bottom line: Chrome seems to use an IP database, Safari and Firefox use the Mac API, and who knows what IE is doing.