coen-hyde / Shanty-Mongo

Shanty Mongo is a mongodb library for the Zend Framework. Its intention is to make working with mongodb documents as natural and as simple as possible. In particular allowing embedded documents to also have custom document classes.
Other
200 stars 52 forks source link

Shanty & replica set connection strings #75

Closed tonymillion closed 4 years ago

tonymillion commented 12 years ago

Hi, I'm connecting to my replica set using 'new style' connection strings as such:

$connectionString = "mongodb://USER:PASS@master.server.com:16575,secondary-1.server.com:18612,secondary-2.server.com:18614"

$connection = new Shanty_Mongo_Connection($connectionString, 
             array("replicaSet" => "myReplicaSet"));

It may already be the case, but I thought I'd raise it here to check:

Will Shanty use the connection string correctly and distribute reads across the secondaries or will I need to split the connection string and use addSlave() etc?

Is there any special work necessary to enable slave reading, as you would do in "raw" mongo

rs.slaveOk(); // enable querying a secondary
coen-hyde commented 12 years ago

Hi Tony,

You have the right idea. The php driver its self takes care of deciding who is master and who is slave in a replicaset. So provide a connection string as you have done above.

I don't believe there is anything else you need to do to read from slaves.

tonymillion commented 12 years ago

The second part was how to you provide the slaveOk() part of the query to reads to offload them to the secondaries in the replica set?

gwagner commented 12 years ago

You would want to do add

<?php
MongoCursor::$slaveOkay = true;

Somewhere before your first query. In 9d0654445f0ad89e56bbd61b0c7f1996d8364eb3 i added a way to set slaveOkay as one of the options for connections which is much much cleaner.

tonymillion commented 12 years ago

Just to reopen this issue - I have a replica set, and about once every few days (they seem to have elections) I see this:

Fatal error: Uncaught exception 'MongoCursorException' with message 'not master and slaveok=false' in /var/www/API/api/Shanty/Mongo/Collection.php:350
Stack trace:
#0 /var/www/API/api/Shanty/Mongo/Collection.php(350): MongoCollection->findOne(Array, Array)
#1 /var/www/API/api/classes/Stats.php(26): Shanty_Mongo_Collection::one(Array)

even though I have:

MongoCursor::$slaveOkay = true;

In my main file before shanty is even included. I think its a case that shanty isn't using the slaves properly (probably because of all the crazy stuff its doing in the setup)….

coen-hyde commented 12 years ago

Hi Tony,

Sorry you're having trouble with this. Unfortunately I've never used replica sets and wrote this functionality because someone asked for it. If you can find the issue i'd very much welcome a patch.

Cheers, Coen

tonymillion commented 12 years ago

My initial thought was to completely remove all the code that deals with setting up masters & slaves (since the PHP native driver already handles all of this, so basically a connection group would contain a Mongo object (which is the connection) the connect string that was used to create it, and the group name.

Its going to take quite a bit of work for me to unravel it though.

coen-hyde commented 12 years ago

Yeah, Shanty Mongo's own Master/Slave stuff certainly does confuse people, i'd be happy to see it gone.

tonymillion commented 12 years ago

FYI: I have started working on this, the basic idea is to create a Shanty_Mongo_Connection as such:

    $connection = new Shanty_Mongo_Connection($connectString, array("replicaSet" => "yoursetName.db"));

Then to call a new function:

Shanty_Mongo::addConnection($connection, $groupName);

(group name being optional)

This is basically the same as calling 'addMaster' and having getReadConnection and getWriteConnection return the same object. In addition you can get the slaveOkay property on the $connection to have reads offloaded to the slaves.

settermjd commented 12 years ago

Hi @tonymillion,

is the code ready to go? I am keen to use it if it is, in a screencast I've got coming up, next week. If you need a hand with it, give me a shout.

tonymillion commented 12 years ago

Its not right now, but oddly enough, cleaning all that up was on my task list for this week, after checking in the find&modify code!

settermjd commented 12 years ago

Tony,

no worries. I'll go with the PHP mongo code for now.

Matt

Kind regards,

Matthew http://www.matthewsetter.com | http://www.maltblue.com | @maltblue

On 13 Jun 2012, at 13:04, Tony Million wrote:

Its not right now, but oddly enough, cleaning all that up was on my task list for this week, after checking in the find&modify code!


Reply to this email directly or view it on GitHub: https://github.com/coen-hyde/Shanty-Mongo/issues/75#issuecomment-6298112

sbarrax commented 12 years ago

Excuse me, how can I port that connection string (I have a replica set) from php to ini file?

t.i.a.

marco

tholder commented 10 years ago

@tonymillion - did your last 2 commits fix this? I know shanty is retired, but this current implementation is so badly borked and inconsistent with the native driver that I think it would make sense to implement a fix?

Thanks

tonymillion commented 10 years ago

Yep, my branch worked and should support connection strings properly (as of the date I committed it).

I don't use PHP or Shanty (or actually Mongo) any more so I don't know if anything significant has changed in the 2 years since I committed it, but it was working back then!

coen-hyde commented 10 years ago

@tholder I think you're one of the last people using this library, feel free to update the library as you see fit.

tholder commented 10 years ago

@tonymillion thanks! I might merge it when I get a chance.

@coen-hyde I'm not sure if that makes me feel special or just sad :)