Re: Pear DB on mysql replication cluster
| From: | Alberto M | Date: | Fri, 28 Nov 2003 13:53:56 +0000 |
| Subject: | Re: Pear DB on mysql replication cluster | ||
| References: | 1 2 3 4 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-23954@lists.php.net to get a copy of this message | ||
Michael Wallner wrote:
Hi Alberto M, you wrote: [...] Hi, I don't think it would make much sense bloating the DB class itself... class ClusterDB { function ClusterDB($master) { $this->master = &DB::connect($master); $slave = $this->master->getOne(' SELECT slave FROM slaves ORDER BY connections ASC LIMIT 1' ); $slavedsn = "mysql://user:pass@$slave/db"; $this->slave = &DB::connect($slavedsn); $this->query(" UPDATE slaves SET connections=connections+1 WHERE slave='$slave'" ); } function query($query) { if (DB::isManip($query)) { return $this->master->query($query); } else { return $this->slave->query($query); } } } What about this? Regards,nice! i patched a little more class ClusterDB {
var $master;
var $slave;
var $slavename;
function ClusterDB($master)
{
$this->master = &DB::connect($master);
$this->slavename = $this->master->getOne('
SELECT slave
FROM slaves
WHERE connections<maxconnections
ORDER BY connections ASC
LIMIT 1'
);
$slavedsn = "mysql://user:pass@$this->slavename/db";
$this->slave = &DB::connect($slavedsn);
if (DB::isError($this->slave))
$this->slave = $this->master;
$this->query("
UPDATE slaves
SET connections=connections+1
WHERE slave='$this->slavename'"
);
}
function query($query)
{
if (DB::isManip($query)) {
return $this->master->query($query);
} else {
return $this->slave->query($query);
}
}
function close()
{
$this->query("
UPDATE slaves
SET connections=connections-1
WHERE slave='$this->slavename' and connections>0"
);
$this->master->disconnect();
$this->slave->disconnect();
}
}
let me try if works...
but I don't like too much depending on master to be able to choose and do a connection to a slave, this is why i liked an array solution.
on the other side, this solution has the connection number handling that is possible only with databases or other store methods.
as I said, we can use a weight system instead of connection limit and defer to mysql server max connections the choice to enable a new connection or not. however we need to check if there are connections available...
latest question: is it correct to do something like:
ClusterDB extends DB
or
ClusterDB extends DB_mysql?
since i would be able to combine the features of Pear DB, with a transparent master/slave handling.
should we go on patching this way? i mean... i'm not so sure it can be interesting for other people.
do you think could be interesting to put this one on PEAR package?
bye
--
"Imagination is more important than knowledge"