Richard Bucker

Mojolicious and MojoX::Redis

Posted at — Oct 29, 2011

I’ve been looking at the code for MojoX::Redis for a couple of days now and I’m impressed and depressed at the same time.First the good news. Like many projects it’s open source. The better news is that it looks cool. The code is nicely formatted and if anyone was following the PEP equivalent for perl then you’d say it was adhered to.On the sad news side of things. While there is some POD doc at the end of the main project file that’s it. The code is not documented at all. And the worst of it is that the code is the exact reason why people hate this crap. This person clearly knows the ins and outs or perl and he demonstrated that aptitude well. But if you asked me to reverse engineer it… it’s going to take a while and a few cases of wine or beer.The best feature is that it implements non-blocking in a way that complements Mojolicious, however, the first side effect is that the main thread continues to run while the first request is processing. When really the benefit of this sort of functionality is to let peer events run not the current main thread. Since it was not documented in any meaningful way this had to be experienced first hand… and after reviewing the test code I’m not sure that my conclusions from my code are correct.Anyway, here is an explanation as I see it in pseudo code.1) do some redis function like INCR expecting a response2) do a get on the same key3) compare the results and they will always fail becausethe results from #1 have not completed by the time #2 completes.Some code that demonstrates thismy $retval1 = undef;$redis->execute(“incr” => [$mykey] => sub{my ($redis,$res)=@; $retval1=$res;});my $retval2 = undef;$redis->execute(“get” => [$mykey] => sub{my ($redis,$res)=@; $retval2=$res;});die “they do not match” if $retval1 != $retval2;The side effect here is that it simply does not work. The only way to make this work is something like this:my $retval1 = undef;my $retval2 = undef;$redis->execute(“incr” => [$mykey] => sub{ my ($redis,$res)=@; $retval1=$res;} $redis->execute(“get” => [$mykey] => sub{my ($redis,$res)=@; $retval2=$res;}); );die “they do not match” if $retval1 != $retval2;The effect in the above code is that since the sub() that is called upon completion of the incr() is called when the incr() is completed. The same for the subsequent call to the get(). The last die() will still have the same effect of getting control before the redis calls have completed execution. So fo for this to be effective the die() needs to be inside the sub() of the get(). Phew!I looked at the test cases in MojoX::Redis and there were some interesting examples. There was an implementation of the redis pipeline in the form of a multi() transaction. This could be interesting since one could do an incr() and a get() in the same pipeline, however, if you needed the result in order to perform future calls then you’d have the same timing problems with the response not being ready or available in local memory for future calls.An async lib of the redis tools seems novel but it makes certain use-cases very difficult and verbose. For example I was playing with the sinatra example of RestMQ. Sinatra being ruby has many of the same warts, and certainly the ruby version of the lib was not evented like MojoX::Redis so I do not expect that it’s going to get much work done. (I really like their demo version because it is so little code and it so accurately depicts the mission that it’s hard not to like the elegance. Even though it’s ruby.) But the reality is that it is still constrained.In summary, while Mojolicious is nice and simple to use(I still like it). The simple use-cases are simple but as soon as you advance to the next step this will get tricky. If you track after the “get it to work correctly” and then think about performance you could end up rewriting the project to make it performant. So be ‘/q/:queue’ => sub { my $self = shift; my $result = undef; my $queue = $self->param(‘queue’); my $value = $self->param(‘value’); if (! defined $queue) { $self->app->log->debug(‘queue was not in the URL (’.$queue.')'); $self->render_not_found; } else { my $uuid = undef; my $lkey = undef; my $q1 = $queue . $QUEUE_SUFFIX; my $q2 = $queue . $UUID_SUFFIX; $redis->execute(“incr” => [$q2] => sub{ my ($redis, $res) = @_; $uuid = $res->[0]; $self->app->log->debug(‘the uuid is (’.($uuid||‘undefined’).')'); }); $lkey = $queue . ‘:’ . $uuid; $redis->execute(“sadd” => [$QUEUESET, $q1]); $redis->execute(“set” => [$lkey, $value]); $redis->execute(“lpush” => [$q1, $lkey]); $self->app->log->debug(‘the uuid q is (’.($q2||‘undefined’).')'); $self->render(text => ‘{ok, ' . $lkey . ‘}'); }};When this code executes… the following output is on the console:rbucker@mvgw:~/hg/metaventures/gwtwo$ ./alt/ daemon[Sun Oct 30 00:59:50 2011] [info] Server listening (http://*:3000)Server available at[Sun Oct 30 00:59:51 2011] [debug] Your secret passphrase needs to be changed!!![Sun Oct 30 00:59:51 2011] [debug] POST /q/myqueue/ (Wget/1.12 (linux-gnu)).[Sun Oct 30 00:59:51 2011] [debug] Dispatching callback.Use of uninitialized value $uuid in concatenation (.) or string at ./alt/ line 47.[Sun Oct 30 00:59:51 2011] [debug] the uuid q is (myqueue:UUID)[Sun Oct 30 00:59:51 2011] [debug] 200 OK (0.003678s, 271.887/s).[Sun Oct 30 00:59:51 2011] [debug] the uuid is (19)My observation is that the output from the sub() is in the log after the rest of the output. Also the error complaining about line 47 is because $uuid is currently undefined when the line executes. Therefore the callback is not merging the execution and therefore any sensible use requires that the code be nested. And that sucks.