Getting a cursor timeout with php driver and mapreduce

2,085 views
Skip to first unread message

mlevy

unread,
Jul 2, 2010, 9:03:05 PM7/2/10
to mongodb-user
Hi all,

I hope you can help. I am getting a cursor timeout exception thrown
when I am attempting a mapreduce from a php script.

PHP Fatal error: Uncaught exception 'MongoCursorTimeoutException'
with message 'cursor timed out' in /home/matt/sandbox/moria/src.tl/
test_install.php:59
Stack trace:
#0 /home/matt/sandbox/moria/src.tl/test_install.php(59): MongoDB-
>command(Array)
#1 {main}
thrown in /home/matt/sandbox/moria/src.tl/test_install.php on line
59


The code is (a bit sloppy as I am flailing a bit here):

$map = new MongoCode("
function() {
emit( { event_id: this.event_id,
event_ts: this.event_ts,
event_name: this.event_name,
fb_id: this.fb_id
},
{count: 1}
);
}");


$reduce = new MongoCode("
function(k, v) {
var total = 0;
for (var i = 0; i < v.length; i++) {
total += v[i].count;
}
return {count: total};
} ");

$query = array('event_name' => 'STATE_FirstTimePlayOfGame',
'event_ts' => array('$gte' => '2010-07-01', '$lt' =>
'2010-07-02'));

$command = array("mapreduce" => "events",
"map" => $map,
"reduce" => $reduce,
"query" => $query);

$db_host = "localhost";
$db_name = "moria";
$collection_name = "events";

try {
$mdb_conn = new Mongo($db_host);
}
catch (MongoConnectionException $e) {
$msg = "Error connecting to MongoDB\n";
die($msg);
}
echo "Connected\n";

$moria = $mdb_conn->$db_name;

echo "got moria\n";

// set events collection
$events = $moria->selectCollection($collection_name);
echo "got events\n";

echo "setting timeout\n";
$cursor = $events->find()->timeout(300000);

echo "running command\n";
$installs = $moria->command($command);


-----
I don't get any farther than this. The collection is pretty big (42
million) and though I plan to shard, its not currently sharded.

Any ideas?

Thanks in advance for any help / insights you can provide,
Matt

Kristina Chodorow

unread,
Jul 2, 2010, 10:50:05 PM7/2/10
to mongod...@googlegroups.com
A command is a query, so you need to turn off the timeout on it. You
can turn off the timeout by setting MongoCursor::$timeout to -1:

echo "setting timeout\n";
MongoCursor::timeout = -1;

echo "running command\n";
$installs = $moria->command($command);

That should let the command run for as long as it needs.

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

mlevy

unread,
Jul 5, 2010, 2:37:39 PM7/5/10
to mongodb-user
Hi Kristina,

Thanks for the reply. I don 't understand your response, so please
forgive any basic questions. Still getting up to speed on mongo and
its usage.

Do I have to declare a new cursor, set the timer, then run my command
from the cursor? Or can I just globally declare 'MongoCursor::timeout
= -1'. When I just paste the line in you provided, I get a syntax
error.

I am running Mongo 1.4.4

Thanks for your help!

Cheers,
Matt

Kristina Chodorow

unread,
Jul 5, 2010, 2:51:59 PM7/5/10
to mongod...@googlegroups.com
Sorry, MongoCursor::$timeout. It's a static variable. You can just
set it, no need to create a cursor. The timeout will apply to any
request that waits for a database response.

mlevy

unread,
Jul 5, 2010, 5:18:21 PM7/5/10
to mongodb-user
Thanks! I think that worked!
Very much appreciated.

Cheers,
Matt
Reply all
Reply to author
Forward
0 new messages