On Jan 11, 2007, at 4:16 PM, Mark Stosberg wrote:
Henri Asseily wrote:
$DATABASE::conf{'test'} = {
load_balance => 1,
db_stack => [
[ 'dbi:Sybase:server=prod1;database=test','user1','pass1',
$attrib ],
[ 'dbi:Sybase:server=prod2;database=test','user2','pass2',
$attrib ],
], ...
The problem is that there are too many ways to load balance, which
gets
even more confusing when you use presistent connections such as
those in
mod_perl. Unless you have a large number of client processes compared
the the servers, when you use persistent connections you're rarely
going
to be happily load balancing in a way that is equitable. Certain
algorithms are better for certain situations such as a lower ratio of
clients to servers, for example.
And since the developer knows her systems better than I do, let her
write a proper balancing routine for her needs. The trivial ones are
unnecessary for me to include since they're trivial, and the more
advanced ones are too specific to a situation for the general public
(i.e. probably the 3 people using this module) to care about...
I see your point. I would refine my suggestion to be the following
then:
load_balance => 1
This would just do something "obvious" or "easy". I think this still
would be no so obvious to those just getting into the field.
As a second option, support this, which is also easy:
load_balance => \&code_ref,
I suspect it's clearer to you than me what the arguments and return
values of such a routine would be, but it solves the problem in a
useful
way. First, you don't have to choose, write, or maintain a specific
implementation. Second, because there is a hook here in the API for
this
with document input and return values, other people can publish and
share plugins.
Eventually, you could just recommend "oh, you need the load balancing
plugin that Ofer wrote", or "the simple one that Mark wrote, etc". :)
I have used callback plugin systems like this as part of
Data::FormValidator and CGI::Application, and they have been
helpful for
the reasons cited above.
(I actually have set up a similar callback in DBIx::HA when a
failover happens)
Well, Peter Holzer gave you the obvious solution:
use List::Util qw(shuffle);
$DATABASE::conf{'test'} = {
max_retries => 2,
db_stack => [
shuffle (
[ 'dbi:Sybase:server=prod1;database=test', 'user1', 'pass1',
$attrib ],
[ 'dbi:Sybase:server=prod2;database=test', 'user2', 'pass2',
$attrib ],
[ 'dbi:Sybase:server=prod3;database=test', 'user3', 'pass3',
$attrib ],
)
], ...
And for a callback, what's the point? You're writing the above, so
you can write whatever code you want. DBIx::HA doesn't specify what
the db_stack is about, nor what the load balancing should be. All it
wants is a stack with a certain format.
Here's a more generalized example of Peter's:
sub load_balance {
use List::Util qw(shuffle);
return shuffle(shift);
}
$DATABASE::conf{'test'} = {
max_retries => 2,
db_stack => [
load_balance (
[ 'dbi:Sybase:server=prod1;database=test', 'user1', 'pass1',
$attrib ],
[ 'dbi:Sybase:server=prod2;database=test', 'user2', 'pass2',
$attrib ],
[ 'dbi:Sybase:server=prod3;database=test', 'user3', 'pass3',
$attrib ],
)
], ...
Again, all this stuff is trivial and I don't see the point of
including it in the module. But if you do, submit a patch and I'll
add it in.
In the meantime, for the next version I'll put Peter's example in the
POD.
Thanks.
H