Sunday, May 15, 2016

Relational Operations on Inverted Indexes using ES-Hadoop

I've been interested in how to apply relational algebra to flat collections like inverted indexes.  This coincided well with the need to do a research project for a Big Data Algorithms course (my last at CU) with @JoseVieitez.  These are the assumptions we started with:
  • We want to persist data in ElasticSearch because it's fast, scalable, and generally awesome.
  • Doing complex queries similar to a SQL JOIN are difficult on flat collections like this.  People often denormalize or squeeze their ontology into partial solutions like Parent-Child docs.
  • Many paths lead to unpleasantries like client side joins.  This headache is to be avoided for many reasons - we want to push this responsibility into the persistence layer.
  • ElasticSearch publishes an interesting connector called ES-Hadoop.  The beauty of this application is its a two way street - you can process data in Hadoop-based systems (like Spark) on the way into ElasticSearch or you can grab data out of ElasticSearch for Hadoop processing.

Proposal

Store normalized data in ElasticSearch and impose relational operations in Hadoop with Spark.  Our main goal was to compare query processing times between PostgreSQL and this system.

Here are the technologies we used:
  • ElasticSearch (2.2.0)
  • ES-Hadoop (2.2.0)
  • Apache Spark (1.6.0 for Hadoop 2.6+)
  • PostgreSQL (9.4.5)

Test Data

For test data, we grabbed a bunch of IMDB data (approx 15M "acted in" tuples linking 3.7M movies to 2.3M actors) which is conceptually linked but it still required some munging.  We teased the data into the following relational schema.
  • Movies
    • id (BigInt, PK)
    • name (Text, GIN FTS)
  • Actors
    • id (BigInt, PK)
    • name (Text)
  • Acted_In
    • movie_id (BigInt, btree)
    • actor_id (BigInt, btree)
    • role (Text)
Our canonical SQL query is as follows:

SELECT a.name AS Actor, m.name AS Movie, j.role AS Role 
FROM actors a 
LEFT JOIN acted_in j ON a.id = j.actor_id
INNER JOIN movies m 
ON j.movie_id = m.id AND to_tsvector('english', m.name) @@ to_tsquery('english', 'bourne');

This query is saying "Show me the actor name, the name of the movie, and the role he played for every movie with the word 'Bourne' in the title". We liked it because it's a 3 table join and touches on full text search. It feels like a pretty common query pattern that relational databases often field so this is the query we sought to implement in our architecture.

Our desired output are rows like: ("Alfieri Victor", "The Bourne Supremacy", "Renzi").

Implementation

Our goal wasn't to write a SQL-to-ES-Hadoop query processing engine - we're evaluating performance. So our program follows a static path which simulates a complex system which translates SQL (the 'bourne' query above) to interactions between ElasticSearch and Spark which ultimately produces structured results like the 'Renzi' row.

There are 3 ES collections - Movies, Actors, and Acted_In. To accomplish the 'bourne' query, there will be 3 ES interactions:
  1. Get all movies with 'Bourne' in the title. 
  2. Get all acted_in tuples for actors in these movies. 
  3. Get all actors for these tuples.
In our system, Spark has the responsibility of performing relational algebra which collates these separate datasets into a single response (the "Renzi" row mentioned above). We looked to Spark SQL to, somewhat ironically, abstract this away. Spark SQL is a way to interact with structured data which will ultimately allow us to run the same SQL query in Spark that we ran in PostgreSQL.

So our final workflow became:
  1. Get all movies with 'Bourne' in the title and feed them into a temporary Spark SQL table. 
  2. Get all acted_in tuples for actors in these movies and feed them into a temporary Spark SQL table. 
  3. Get all actors for these tuples and feed them into a temporary Spark SQL table.
  4. RUN THE SAME SQL QUERY THAT WAS RUN AGAINST POSTGRES (minus any thinning constraints like the 'Bourne' requirement which is now performed in ElasticSearch).
Our final source is not production quality - step 1 (getting 'bourne' movies from ES into Spark) is pretty simple and demonstrates the nature of our program:

PostgreSQL Performance

We expected PostgreSQL to do well with this dataset and we were not disappointed. Quantifying performance is mix of art and science - we wanted to stay away from cached results but didn't run against entirely cold indexes.

Here are query running times in ms for the 3 volume profiles (1M, 15M, 150M, 150M paged tuples) vs query result set sizes (5, 500, 5,000 rows).

L (1M)
M (15M)*
H (150M)
H/paged (150M)
L (5)
50
1,600
100
1,450
M (500)
55
1,700
670
2,900
H (5,000)
55
1,800
1,400
5,500

* We consider the Medium volume times to be anomalies since PostgreSQL did not properly utilize indexes - it did at the High volume stage. We didn't spend an extraordinary amount of time trying to resolve this since it's outside the focus of this project. Previous experience dictates these queries can probably be run in no more than a couple hundred ms.

The far right column (H/paged) explores a profile we feel PostgreSQL sometimes has difficultly with - paging. So using the large dataset (150M tuples), we attempted some queries which paged and did find it lagged somewhat as we expected.

ES-based System Performance

L (1M)
M (15M)
H (150M)
H/paged (150M)
L (5)
5,400
5,300
5,400
np
M (500)
6,000
5,900
6,100
np
H (5,000)
8,800
9,600
10,000
np

Observations

  • There was minimal correlation between corpus size and response time for the ES-Hadoop program - As expected, ElasticSearch was blazingly fast so the larger volumes didn't matter much. 
  • ES-Hadoop suffered greatly as result set sizes increased - Spark is billed as a faster MapReduce but we had difficultly getting good performance, at least compared against PostgreSQL. This could partially be due to our pedestrian knowledge of Spark. Spark SQL in particular struggled to keep up. We wrote a Spark script that used native Python data containers instead of Spark SQL and observed 20-30% improvements in time. Worthy of further investigation but it wouldn't make a huge difference in our evaluation. 
  • ES-Hadoop always incurred a non-trivial amount of startup costs - We always suffered an approximately 1,000 ms penalty firing up Spark's infrastructure. Switching from local mode to distributed mode did not help, perhaps due to the relative small amount of data we were playing with. 
  • PostgreSQL won every category but there is a caveat - It’s clear that PostgreSQL is much faster, however, recall our observation that corpus size does not impact ES-Hadoop execution times. PostgreSQL will eventually break down in ways ElasticSearch will not. So we feel there is a potential inflection point that was not exposed in our research particularly when you consider paging (PostgreSQL can struggle counting) which is a very common requirement for data repos.

Other Work

  • Solr Streaming Expressions push parallel relational operation work into the Solr nodes themselves (rather than extract the data like we do).  I feel like this is a very promising concept.  I've played with this feature for a day or two and was very impressed.
  • ZomboDB can be thought of ElasticSearch-backed PostgreSQL indexes.  A great option if you need the robustness of a solid RDMBS and want access to the power of ES searching/aggregation/etc.  There's also a concept of linking indexes similar to a join.  I've used this product and think it's a really cool merging of technologies.
  • I haven't tried the SIREn Join Plugin for Elasticsearch but this appears to offer a left join option between ES indexes.

Saturday, January 2, 2016

Basic recursive Postgres updates

I recently came across a need to update a large amount of Postgres data (many millions of rows) laid out in a hierarchical format with potentially inherited attributes. To make the use case generic, consider the following structure that a bookstore might employ:
If it's not clear, this is the structure being modeled. Locations with asterisks indicate those locations are not being inherited - ie, they are direct. Note 'ww2' books just happen to have the same location as its logical parent ('history' books).  This is intentional just to show a potential condition.
Let's say we want to update the 'book' location from 'main floor' to 'new floor'. This should cascade to all object_types which inherit their location from 'book'. The query (pardon the bizarre cast to bigint) utilizes a WITH RECURSIVE CTE to process through an adjacency tree:
Perhaps more readable as a JPA named query. Note the inner query is traversing the hierarchy looking for candidates to give to the outer UPDATE.
The result shows the 4 relevant rows ('books' and those which descend from 'books' AND inherit their location) are updated:

Sunday, May 31, 2015

Inverted Index Text Mining of Twitter Data

A year ago, I got tired of wading through my timeline to find tweets of interest.  It was beginning to feel like work or another chore I had to complete every day.  OnlyWorthy was born over a weekend and it has served very admirably.  A little Python script wakes up every night, grabs all the day's tweets from a handful of accounts, and retweets those with the highest scores (a combination of favorites and retweets).  I follow @OnlyWorthy instead of those handful of accounts so I only get these 'good' tweets in my timeline.

This is obviously a crude way to solve my problem.  So I saw an opportunity to iterate OnlyWorthy in conjunction with my Data Mining class project with a more sophisticated mechanism.

The idea is as follows: put tweets into the Vector Space Model.  If a collection of tweets which a user enjoys is known, they could be indexed as individual documents.  New tweets could also be indexed.  At this point, similarity scores can be generated and relevant tweets can be identified.

This is the first time I've worked with PyLucene and I was very impressed.  Common Lucene analyzers and the Similarity API were utilized.

Here's an example workflow:

- A user identifies 100 tweets which they have enjoyed.  This can be done a variety of ways (examination of Twitter activity, an app with swipe left/right feedback, etc) but for this project their text was simply fed in or randomly selected.  All these tweets are placed in an index.
- A user identifies 50 twitter usernames they like.  This is to scope the project but if bulk-level API access was provided, perhaps more of the Twitter firehose can be ingested.
- At some interval, the application grabs recent tweets from those 50 accounts.  All these tweets are placed in an index and a score is calculated for each new tweet against each liked tweet.
- A handful of the top scoring new tweets are considered worthy and displayed to the user.  This could be done by retweet or presentation in a special app.  In my project, they were simply printed to stdout.
- I did not implement a continual feedback loop here but that's what should be done to keep refining the performance.

I went with an ensemble system which calculated TF-IDF cosine similarity and BM25 values.

Results were pretty good.  For example, given these tweets:
the program identified these tweets which seem like pretty good matches (one about monetary policy and one about new gadgets):


Difficulties:

- It sometimes told the user what they already knew.  If the user indicated they liked a tweet accouncing a new version of Elasticsearch, for example, other tweets with similiar content would be flagged.  I added some penaltization based on time thinking that these tweets would be clustered but it didn't work well enough.
- Tuning is necessary to get the performance needed to evaluate millions of tweets.
- Lots of scaffolding was mocked and needs to be implemented.  While it's not extremely difficult to build a system to enable people to flag tweets that they enjoy, it also can't be written quickly.

If interested, the source code is available here.  I haven't deployed this to OpenShift because there is still a lot of work required to make it run continuously.  Feel free to borrow whatever ideas are expressed here if you're interested in this concept!

Sunday, August 24, 2014

CU-Boulder's CAETE program - my experience

When I left the military in 2011, I intended to make use of my GI Bill benefits.  They are generous enough to pay for a graduate degree and I felt like I really needed that to make up for lost time spent outside the software development industry.

I wasn't keen on having to physically attend a classroom, having unsatisfactorily tried that before.  So even though there are a few options in San Diego, an Internet-based program was my preference.  With a job and a family, time efficiency is critical.

University of Colorado Boulder has a solid engineering program with a department called Center for Advanced Engineering and Technology Education (CAETE).  I believe it's being re-branded as Engineering Anywhere - can't tell what's going on with the name.

In any event, I wanted to pursue the Masters of Engineering, Computer Science degree.  You're given 6 years to complete 30 credits (basically 10 courses).  I've gone with the coursework-only plan which just requires some breadth in my classes.

I've taken 1 class per Fall/Spring semester for the last 3 years.  I'm about to start my 4th year with a plan to take 1 class this Fall, 2 in the Spring, and 1 in the Summer of 2015 to complete the program.

Overall, I've been extremely satisfied.  The instructors have been great and the course content has been interesting and relevant to me.

Lectures conducted during the day in Boulder are almost always posted that night in time for me to watch them.  My strategy is to watch the lectures and do any required reading during the weekdays and leave the weekends open to homework, project work, and studying for tests.  I would guess I've been spending approximately 12 hours a week doing schoolwork.

The infrastructure needed to stream the lectures has steadily improved since I started the program.  When I had DSL with 1 Mbps download speeds, things didn't always work so well but it's better now with 10 Mbps.  It's usually a split screen with the instructor on one side and slides on the other.  Sometimes it gets difficult to see what the instructor is writing on a classroom whiteboard but I think I've had only 1 course with the problem more than a couple times.  They usually stick with writing on the slide deck which comes through fine.

Tests are performed with a proctor and you're given a time window to get it done.  Never had a problem with this.  I recall having to travel for work a couple years ago and needing to move my test around by a day or so and it wasn't an issue.

I get the most out of hand's on experience so the projects have been the most interesting to me.  Sometimes the project gets demo'd to the instructor or TA via Skype or Hangouts which gives us a chance to talk about it.  Everything has been in Java or Python with the exception of the networking class which was all in C.

So I'd recommend this program to anyone thinking about getting their CS graduate degree from an Internet-based program.  The coursework has been appropriate and engaging.  I definitely think it's helped me improve my software development knowledge.  Since the GI Bill is paying my way I don't feel like I know enough about the financial costs to comment on the tuition level.

Here's a list of the classes I've already taken with a couple notes:

Saturday, June 7, 2014

A SolrCloud backup scheme

SolrCloud needs a real backup mechanism - there's an open ticket for this but until then users will have to make due with ReplicationHandler.  The HTTP API is really simple:
http://master_host:port/solr/replication?command=backup&location=/path/to/backup/dir
It takes a snapshot of the whole index at that path.  This is obviously targeted for legacy Solr, not SolrCloud.  So let's shoehorn this into SolrCloud by practicing with the following configuration in which a single collection has 4 shards spread over 4 hosts and each shard has a replica (so 2 total copies of each shard):

  • host1: mycollection_shard1_replica1, mycollection_shard4_replica2
  • host2: mycollection_shard2_replica1, mycollection_shard3_replica2
  • host3: mycollection_shard3_replica1, mycollection_shard2_replica2
  • host4: mycollection_shard4_replica1, mycollection_shard1_replica2
If each host is running a single Solr instance at port 8983, both replicas are being served from the same Solr process.  Replication commands need to declare what data it wants backed up.  More on that below.

4 calls to ReplicationHandler's HTTP API are required to snapshot all of mycollection.  It doesn't matter which replica gets hit but perhaps it's a good idea to distribute the work and to stagger the commands so the cloud isn't too bogged down.  That is, instead of pummeling host1 and host2, perhaps each host should have to backup 1 shard.  So 4 GETs:
http://host1:8983/solr/mycollection_shard1_replica1/replication?command=backup&location=/backups/060714/shard1
http://host2:8983/solr/mycollection_shard2_replica1/replication?command=backup&location=/backups/060714/shard2 
http://host3:8983/solr/mycollection_shard3_replica1/replication?command=backup&location=/backups/060714/shard3
http://host4:8983/solr/mycollection_shard4_replica1/replication?command=backup&location=/backups/060714/shard4
Note the location path includes a date attribute.  The HTTP call to /replication will return immediately but it might take several seconds or many minutes for the replication action to complete.  You can write some fancy daemon to figure out when it actually completes but why not write to some shared slab of disk and forget about it?

The data files will be uncompressed so you'll probably want to compress it at some point.  Rotation will be important because this isn't rsync - ReplicationHandler will write the whole thing each time.  In case you have a compatible configuration, note there is a "numberToKeep" param available.

Ultimately, your /backups/060714/ dir is left with 4 directories, each with the index files for a particular shard.  Recovery is really easy - just drop those files into the appropriate data/index/ directory.

Saturday, May 17, 2014

Metering tweets (a weekend project)

This project will probably take me more than a weekend but Twitter's new mute feature got me thinking - I don't need to mute people I follow but I would like to eliminate a lot of their fluff.  I can recall at least 10 people I had to unfollow because even though I thought they sent out good stuff, for every good tweet there were 5 worthless ones.

The idea is this: I'd like only a user's best x tweets every day to show up in my feed.  Let's define quality by quantifying what happens with a tweet (replies, retweets, favorites, etc).  Lots of hand waving here obviously and this part isn't fully baked.  Let's ignore this problem for now :-)

So every day an app combs through a user's tweets and picks out the best few tweets.  How do I get those tweets into my feed?  I'm sure as heck not going to build a new client, though that might be the smoothest implementation.  I created a "shadow account" which I can send tweets from and if I follow it, content will show up in my timeline without having to build a separate client.

Everything I've described is fairly easy.  Adding new users for the app to monitor, adjusting when the app evaluates tweets, how many to select, etc all require a web or mobile app.  I'm not too keen on building that part.  I'm hoping I'll know if this is a good idea before I get to this point - the app can be hard coded for now.

I'm hosting this on Openshift and writing it in Python.  I've loaded a cron cartridge and envision adding a script to the daily bin.  Twitter's API is pretty serious, especially auth, so I'm taking time to learn that.  I found a nice little Python library that abstracts the application-only auth mechanism so I'll roll with that for now (Edit: switched to tweepy).  My repo is public so feel free to take a look!

Monday, March 17, 2014

"Illegal instant due to time zone offset transition" with Joda-Time

Thanks to Daylight Saving Time, you might run into this error while using the excellent Joda-Time library:
java.lang.IllegalArgumentException: Cannot parse "2014-03-09T02:01:47Z": Illegal instant due to time zone offset transition (America/Los_Angeles)

I'm getting this pulling UTC timestamps out of Solr.  My app's input and output will always be UTC.  The offending code:

So Joda is trying to turn this into a time that never existed in Los Angeles (we sprung forward from 01:59:59 to 03:00:00).

Most answers on the web related to this error correctly indicate you should probably use LocalDateTime.  But what if you want to remain UTC?  Just tell the formatter to not revert to the local time zone.  This makes the rest of the app safer as well because it's not handling time values which were translated from UTC to local.

Monday, January 20, 2014

Modifying the Solr QueryParser

If you're doing development in Solr trunk and want to adjust the QueryParser, take a look at the JavaCC grammar file at lucene/solr/core/src/java/org/apache/solr/parser/QueryParser.jj.  This isn't a tutorial about JavaCC - there are plenty of those out there.

Once your changes are complete, you'll need to generate the underlying classes again.  ant builds from lucene/ or lucene/solr/ don't accomplish this.  So to do this, run 'ant javacc' from lucene/solr/core/.

That's it.  Continue on with your normal build patterns - I often run 'ant example' from lucene/solr/ when debugging locally.  It might make sense for parent builds to always run the javacc target in lucene/solr/core/ but I assume things are set up this way for a reason.

Saturday, January 4, 2014

Setting up a Solr dev environment

While I'm waiting for my next class to begin, I wanted to contribute some Solr code.  I take so much from that project so it's only fair...

I use IntelliJ, SVN, and Maven for my non-Android development - Lucene/Solr has an SVN repo and it looks like some people use IntelliJ but the project seems to favor Ant over Maven but when looking through the Ant commands, there are hooks into Maven.  Haven't had a chance to explore that much yet.

I run Mountain Lion so this is Mac OS X specific.

  1. Checked out source:

    svn co http://svn.apache.org/repos/asf/lucene/dev/branches/branch_4x branch_4x

    Edit: Most development should occur in trunk.  It'll get backported to the 4x branch as required.
  2. Ran "ant idea" to generate the IntelliJ artifacts from branch_4x/ but got an error:

    "branch_4x/lucene/common-build.xml:411: Ivy is not available"

    No worries, downloaded Apache Ivy and put the jar where the docs said to:

    sudo cp ~/Downloads/apache-ivy-2.3.0/ivy-2.3.0.jar /usr/share/ant/lib/
  3. "ant idea" works now - took almost 8 mins probably due to my slow connection.
  4. In IntelliJ 12 I ran "Open Project" on branch_4x/.  Included at the end of "ant idea" are instructions to set up the project.  I elected to create branch_4x/lucene/build.properties and included:

    idea.jdk = project-jdk-name="1.6" project-jdk-type="JavaSDK"

    Though it looks like we should be running Java 7?  We'll see.

    Edit: trunk is Java 7, branch_4x is Java 6.
  5. So here's where it got a little tricky and it's mostly my fault for not reading carefully.  Ran "ant example" from branch_4x/solr/.
  6. Setup remote debugging in IntelliJ by creating a remote configuration.  I took the docs recommendation of localhost:5900.  Take the command line string and from branch_4x/solr/example ran:

    java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5900 -jar start.jar

    I resisted using example/start.jar because I didn't want to use the example, I wanted to use my stuff!  That wasn't the case, duh.
  7. From IntelliJ, debug the remote configuration that was just made.  Browsed to localhost:8983 in a browser and it looked good.

    Edit: You'll need to install Python3 as well to get "ant precommit" to complete successfully.

Monday, December 9, 2013

Pushing artifacts to your local repo using Maven's Ant plugin

If you have an Ant module that you need to integrate into an existing Maven project, you'll likely face a few challenges.  Let's just pretend you don't want to mess with Ant too much.

Despite the existence of the maven-antrun-plugin, they don't mesh perfectly together.  One issue is getting the built artifact into your local Maven repository.

Here's one technique: once the Ant build is complete, reach into the target directory and copy the artifact into your repo while taking care to roll the path and version.
You might want to adjust this according to your environment.  Perhaps adding more phases and breaking up the operations would be wise.

Saturday, November 30, 2013

Publishing an app for fun, experience, and no profit

The lure of passive income is well documented but for many developers the costs outweigh the benefits.  Once you charge money for a web or mobile app, a responsibility to customers is formed - you have to fix bugs, keep backend services highly available, stand up a customer service process, etc.

I like tinkering in mobile apps but don't want that pressure.  I created an app to scratch an itch.  It's not great but it works.  It was published a couple years ago when not many apps with comparable functionality existed and has built up a group of steady users.  I recognize many Android apps are undesirable but put mine one level above that category (the UI is not pretty).

So why should developers self-publish apps that don't make any money?
  • It's fun!

    This is my primary reason.  Don't you like seeing your software being used?  My users' data gets stored remotely so the more rows I see added, the more I know people are using it.  Also, I love getting emails from people who just say "thanks".
  • Great technical experience

    I fancy myself as a full-stack engineer so this has been a great way to see something through from beginning to end.  I get to touch lots of different technologies - client code, security layers, database endpoints, etc.
  • Do something different

    I don't cook up mobile apps in my day job.  At least now I can talk reasonably intelligently about Android, AWS, and Google App Engine.
  • Get the first one out of the way

    Maybe one day I'll want to build something independently for profit.  Ever get the feeling you don't know what you don't know?  I feel like this experience will help me avoid that problem, at least partially.

Monday, November 11, 2013

Temporally faceting through event data

I've been struggling to scale a use case I have for a large corpus of event data in Solr.  It's kinda faceting but not really.  facet.range, while awesome, doesn't get me there because I need specificity, not bins.  Scale precludes simple sort.  Consider the following events of people visiting locations:

docID, person, location, date
1, "John", "StoreA", 2013-01-01
2, "John", "StoreB", 2013-06-30
3, "John", "StoreA", 2013-07-11
4, "Frank", "StoreC", 2013-02-01
5, "Kim", "StoreA", 2013-08-01

The first row could read as "John visited StoreA on 1/1/13".  I want to know data about each person's most recent trip to a particular location.

So let's say we facet on location "StoreA".  Here's the HTTP request (shown instead of SolrJ code for the purpose of this blog post):

http://localhost:8983/solr/events/select?
q=location:StoreA
&rows=0
&facet=true
&facet.limit=-1
&facet.field=person
&facet.mincount=1

This executes a facet search for person which only returns facet_counts for events with location = "StoreA".  For the above corpus, the applicable results are:

{ "John": 2, "Kim": 1 }

This is similar to what you'd expect on Amazon if you search on "television": you'll see a dozen or so Brands on which to drill into.  But my request isn't just "Show me everyone who has been to StoreA", it's "For everyone who has been to StoreA, show me the most recent visit with the document ID".  So what I'm really asking for is:

{ "John": ["2013-07-11", 3], "Kim": ["2013-08-01", 5] }

The scale (billions of records) and composition (people are usually seen at a small subset of locations) of my data makes it optimal to figure out who has been to StoreA before attempting to find their most recent visit.  Most people haven't been to StoreA so eliminating them as quickly as possible is key.

It's been most performant to process the facet results then fetch the document with the most recent date for each hit (2 in this example).  It would look something like this:

  1. Issue the facet query - this will give you a list of the people who have been to "StoreA".
  2. Concurrently fetch the info about each of these "StoreA" visitors.  I use ExecutorService pools quite a bit:

    http://localhost:8983/solr/events/select?
    q=person:John+AND+location:StoreA
    &sort=date+desc
    &rows=1
    &fl=docID,date
Even if you tune the run time of the facet query down to a couple seconds and each of the fetches in Step 2 take 10 ms, if you need to crunch through 10,000 of these matches, the response time isn't exactly instantaneous (over 5 seconds and probably closer to 10 seconds in this example).  Plus, that's a ton of queries to send down range.  You quickly get into a need to stage results or at least cache them.  In fact, I've considered pre-processing schemes to focus my queries to people who have been seen at particular locations.

So I continue to experiment, trying to find the optimal solution.  There has been lots of facet work in Solr of late so I hope that iteration leads to more tools at my disposal.

Saturday, August 24, 2013

Manually editing Solr's clusterstate.json on Zookeeper

There will probably come a time when you want to do something not readily covered in Solr's APIs.  Manually editing the clusterstate.json is easy to do but should be approached with caution.

I wanted to drop all shards from a single host from my Solr cluster (didn't care about losing the data) and do a little spring cleaning.  To open a command-line interface with ZK:

/path/to/zookeeper/bin/zkCli.sh -server 127.0.0.1:2181

Then simply:

get /clusterstate.json

Place that content into a local file.  After backing up the original content, make your edits.  For instance, to drop the dead nodes, delete JSON elements with "down" states:


To upload your new clusterstate.json (no need to halt services):

/path/to/zookeeper/bin/zkCli.sh -server 127.0.0.1:2181 set /clusterstate.json "`cat /local/path/to/clusterstate.json`"

Tuesday, July 9, 2013

Mismatched Solr 4.x versions with SolrCloud

I wanted to run a Solr 4.0 client against a more modern Solr server (like Solr 4.3.1 - shard splitting!).  While we should match client and server versions, it's not unheard of to float a little bit.  4.0 to 4.3.1 is more than a little bit but I wanted to get Lily to work against 4.3.1 - notsomuch.

You'll likely get an error like this in the Solr ClusterState code:

org.apache.solr.common.cloud.ZkStateReader: Updating cluster state from ZooKeeper... 
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map 

So something changed with the way the cluster is recorded in ZK/Solr.  I encountered this again by accident when I forgot to update the Solrj version in my app's POM (again, trying to run a 4.0 client against a 4.3+ server).

This is probably only an issue with SolrCloud so remaining Solr classic users likely aren't affected.  But if you're not using SolrCloud yet, you should take a look.

Sunday, May 5, 2013

My Weekend Project: trellminder (Trello email notifications)

Love trello!  My wife and I use it to coordinate what needs to be done around the house.  And I agree with the folks at trello that email notifications (and email in general) are lousy.  I don't want to see each time a card changes - the only thing I want to be alerted when a task is about to be due or is past due.  Mainly so I don't get in trouble at home ;-)  So I thought it would be a fun weekend project.

I use CentOS at work so I'm most comfortable with Red Hat and I've played with OpenShift, their PaaS option, before.  This feels like a cron job, which OpenShift supports.  Trello has a nice API so this wasn't too hard to code in Python.  I didn't want to make my Trello board public of course but you can generate a token which allows read-only access to a board, once approved.

Once you get an account on OpenShift, create a python-2.6 app and add the cron cartridge:

rhc app create trellminder python-2.6
rhc cartridge add cron-1.4 -a trellminder

I use requests whenever possible - urllib2 should be avoided at all costs.  To do so, add "install_requires=['requests']" to setup.py in your root dir.

There was a very helpful question on the OpenShift forum which clued me into how this script should run.  Mainly, I should use a shell script to activate the Python 2.6 virtenv then call the python script.  The use of a 'jobs.deny' file would force cron to call the .sh and ignore the .py.

The actual source code is posted on my github repo.

Wednesday, May 1, 2013

Getting replicated Ehcache and iptables to play nice

Struggled with this a bit and thought others might find this useful.  If you're using RMI Replicated caching with Ehcache, you need to put a little thought into port security/strategy.  The sample ehcache.xml includes:


Using this config means you're going to have to poke holes in iptables for ports 40001 and 40002.  All that is pretty simple - the gotcha is if you're using automatic peer discovery.  It needs multicasting to work.  The docs call this out but it took me awhile to realize I had to specifically allow it in iptables, as it is likely prohibited by default in most environments.

IBM has a nice post about how to do this.  So in this example (replicated Ehcache with automatic peer discovery), you'll need this in iptables on each host you expect to participate:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 40001 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 40002 -j ACCEPT
-A INPUT -m pkttype --pkt-type multicast -j ACCEPT

Saturday, April 20, 2013

Using Python and paramiko to move bits around

Automating the movement of files around a stack is one of those unglamorous jobs that just has to get done.  It's certainly possible to do this in bash but Python makes it a little more easy and fun.

paramiko is an actively maintained Python module that assists with secure connections to remote hosts.  This use cases favors the SFTPClient class which easily allows an app to connect to and put files on a remote machine.  paramiko is LGPL and only requires Python 2.5+ and PyCrypto 2.1+.

It's probably best to create an account on the remote host with a very limited scope.  That is, it only has permission to modify the directory which you intend to write to.  Those creds will go in the Python script.  Don't run this as root or your normal user account!

In this simple example, all files in a particular local directory will be recursively copied to a remote host.  Enjoy!

Monday, February 18, 2013

Assembling ordered Lists using HashMap and Collections.sort()

LinkedHashMap won't work if that's what you were thinking....

I was presented with a use case having to assemble an ordered List of objects out of tens/hundreds of thousands of ordered elements.  Here's the List<EventObject> I need to do something with:

  • Keys are not unique.
  • The List<EventObject> needs to become a List<SummarizedObject>.
  • 1 or more EventObjects make up a SummarizedObject.
  • The order of List<EventObject> must be matched in the resulting List<SummarizedObject>.  Meaning, the first EventObject will definitely be in the first SummarizedObject and the last EventObject will definitely be in the last SummarizedObject.
  • Typically, for every 10k EventObject values 100 unique keys and 200 unique SummarizedObjects are created (give or take 1 order of magnitude).
  • I'm not concerned about concurrency at this time.
LinkedHashMap<String, List<SummarizedObject>> wouldn't work for me even though it maintains order because a single key could have several SummarizedObjects (hence the List of SummarizedObjects) whose order could interweave with values in different keys.  Bummer.

I kept coming back to the HashMap.  Being able to quickly determine a key's existence in the Map and (if it does exist) going straight to that key's List<SummarizedObject> was ideal.  So I dropped an order field in SummarizedObject, which looked like:


The pseudo-code for getting the EventObjects into a HashMap<String, List<SummarizedObject>> is:


So my last problem is to assemble all SummarizedObjects into a single, properly ordered List.  Enter Collections.sort(). Here's real code:


Great performance and not that much code. And it can be optimized further by finding a better way to find the proper SummarizedObject in Step 5 of the above pseudo-code. Hope this helps someone!

Saturday, January 26, 2013

Marshaling POJOs with Jersey

When a web service has multiple consumers, you obviously don't want to duplicate a lot of code.  So what if you want to push data to/from a Java API and a web site like jQuery?  Jersey can help you do both with JSON.  A lot of what I'll cover is in the Jersey docs.
  • Turn JSON support on in your servlet's web.xml:
<init-param>
  <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
  <param-value>true</param-value>
</init-param>
  • This example also uses compression so add the gzip filters too.
  • Annotate your POJO with JAXB.  Make sure you have an empty constructor.  I like to make the member variables private and force use of public getters.  I also elect to ignore unknown properties.
@XmlRootElement(name = "foo")
@XmlAccessorType(XmlAccessType.PUBLIC_METHOD)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Foo {
    private String blah;

    Foo() {}
    
    @XmlElement()
    public String getBlah() { return blah; }
}
  • Set up your web service.  Since you're returning JSON, jQuery will be happy with basically no further consideration.  Note I didn't define a setBlah() above but I hope you get the picture.  
@GET @Path("/")
@Produces({MediaType.APPLICATION_JSON})
public Foo getFoo() {
  Foo foo = new Foo();
  foo.setBlah("Hello!");
  return foo;
}
  • Connect your WebResource properly.  Note the addition of the JacksonJsonProvider class.  The gzip filter is not required but you should probably use it.

ClientConfig config = new DefaultClientConfig();
config.getClasses().add(JacksonJsonProvider.class);
Client client = Client.create(config);
client.addFilter(new GZIPContentEncodingFilter(true)); 
WebResource webResource = client.resource(hostname);
  • Now request your POJO response (note the use of gzip here):

Foo foo = webResource.path("/")
               .header("Accept-Encoding", "gzip")
               .get(new GenericType<Foo>() {});
  • You can also pass POJOs to the web service using POSTs.  This snippet assumes you annotated class Bar is a similar manner to Foo and the "/bar" POST endpoint accepts a Bar object.

Bar bar = new Bar("stuff");

Foo foo = webResource.path("/bar").post(new GenericType<Foo>() {}, bar);

Edited on 2/10/13 to include compression in example.

Sunday, January 6, 2013

"Multiple dex files" error with Eclipse + ADT

I haven't done much Android development in the last year but this week wanted to update my app a bit.  So I installed Eclipse Juno (4.2), grabbed the latest Android Developer Tools (r21), and the latest AWS SDK for Android (1.4.4) since my app persists data on AWS.

These are all pretty big jumps from what I used to have - Eclipse Indigo, probably a single digit ADT, and AWS SDK 0.2.X IIRC.  I've only been using my System76 laptop for grad school work of late, not "real" coding obviously.  Almost everything worked fine.  Thanks go out to these teams for honoring backward compatibility!  The one problem I had was "Unable to execute dex: Multiple dex files define X" (X being something in the AWS package).

So off to StackOverflow.  I tried all the suggestions:
  • Open/close Eclipse a few times, doing project cleans
  • Reinstalling ADT
  • Deleting my /bin and /gen directories, then cleaning
  • Made sure my build path was legit - several people traced it back to having /bin in the build path
The AWS JARs I imported were core, debug, and sdb (my app uses SimpleDB - don't ask).  Since my testing only consisted of moving the .apk to dropbox and making sure it worked on my Galaxy S3, I didn't need the debug JAR.  Once I removed it, everything worked okay.

Kinda perturbed this is still a problem - many SO posts and blogs I've seen are by people who appear to know what they're talking about so I don't think it's always a silly oversight by junior developers.  I'm thinking it's a dependency problem - maybe AWS SDK 1.4.4 wasn't developed with ADT r21?  If that was the case, Eclipse and SDK providers make it really hard to grab older versions like you would with a POM file.  If I needed to debug in this instance, I'd be in real trouble.