1 package DBIx::Class::Storage::DBI::Replicated::Balancer;
7 DBIx::Class::Storage::DBI::Replicated::Balancer; A Software Load Balancer
11 This class is used internally by L<DBIx::Class::Storage::DBI::Replicated>. You
12 shouldn't need to create instances of this class.
16 Given a pool (L<DBIx::Class::Storage::DBI::Replicated::Pool>) of replicated
17 database's (L<DBIx::Class::Storage::DBI::Replicated::Replicant>), defines a
18 method by which query load can be spread out across each replicant in the pool.
22 This class defines the following attributes.
26 The L<DBIx::Class::Storage::DBI> object that is the master database all the
27 replicants are trying to follow. The balancer needs to know it since it's the
34 isa=>'DBIx::Class::Storage::DBI',
40 The L<DBIx::Class::Storage::DBI::Replicated::Pool> object that we are trying to
47 isa=>'DBIx::Class::Storage::DBI::Replicated::Pool',
51 =head2 current_replicant
53 Replicant storages (slaves) handle all read only traffic. The assumption is
54 that your database will become readbound well before it becomes write bound
55 and that being able to spread your read only traffic around to multiple
56 databases is going to help you to scale traffic.
58 This attribute returns the next slave to handle a read request. Your L</pool>
59 attribute has methods to help you shuffle through all the available replicants
60 via it's balancer object.
64 has 'current_replicant' => (
66 isa=>'DBIx::Class::Storage::DBI',
77 This class defines the following methods.
79 =head2 _build_current_replicant
81 Lazy builder for the L</current_replicant_storage> attribute.
85 sub _build_current_replicant {
92 Given a pool object, return the next replicant that will serve queries. The
93 default behavior is to grap the first replicant it finds but you can write
94 your own subclasses of L<DBIx::Class::Storage::DBI::Replicated::Balancer> to
95 support other balance systems.
97 This returns from the pool of active replicants. If there are no active
98 replicants, then you should have it return the master as an ultimate fallback.
104 my $next = ($self->pool->active_replicants)[0];
105 return $next ? $next:$self->master;
108 =head2 before: select
110 Advice on the select attribute. Each time we use a replicant
111 we need to change it via the storage pool algorithm. That way we are spreading
112 the load evenly (hopefully) across existing capacity.
116 before 'select' => sub {
118 my $next_replicant = $self->next_storage;
119 $self->current_replicant($next_replicant);
122 =head2 before: select_single
124 Advice on the select_single attribute. Each time we use a replicant
125 we need to change it via the storage pool algorithm. That way we are spreading
126 the load evenly (hopefully) across existing capacity.
130 before 'select_single' => sub {
132 my $next_replicant = $self->next_storage;
133 $self->current_replicant($next_replicant);
136 =head2 before: columns_info_for
138 Advice on the current_replicant_storage attribute. Each time we use a replicant
139 we need to change it via the storage pool algorithm. That way we are spreading
140 the load evenly (hopefully) across existing capacity.
144 before 'columns_info_for' => sub {
146 my $next_replicant = $self->next_storage;
147 $self->current_replicant($next_replicant);
152 John Napiorkowski <john.napiorkowski@takkle.com>
156 You may distribute this code under the same terms as Perl itself.