[dw-free] better organization of LJ functions
[commit: http://hg.dwscoalition.org/dw-free/rev/70869e7cfca3]
http://bugs.dwscoalition.org/show_bug.cgi?id=3965
Move new_account_cluster and random_cluster from package LJ in LJ/User.pm to
Patch by
kareila.
Files modified:
http://bugs.dwscoalition.org/show_bug.cgi?id=3965
Move new_account_cluster and random_cluster from package LJ in LJ/User.pm to
Patch by
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Files modified:
- cgi-bin/LJ/DB.pm
- cgi-bin/LJ/User.pm
- cgi-bin/ljlib.pl
-------------------------------------------------------------------------------- diff -r 4852ffcec614 -r 70869e7cfca3 cgi-bin/LJ/DB.pm --- a/cgi-bin/LJ/DB.pm Mon Oct 03 13:47:53 2011 +0800 +++ b/cgi-bin/LJ/DB.pm Mon Oct 03 13:50:36 2011 +0800 @@ -397,6 +397,48 @@ $LJ::DBIRole->use_diff_db(@_); } +# <LJFUNC> +# name: LJ::DB::new_account_cluster +# des: Which cluster to put a new account on. $DEFAULT_CLUSTER if it's +# a scalar, random element from [ljconfig[default_cluster]] if it's arrayref. +# also verifies that the database seems to be available. +# returns: clusterid where the new account should be created; 0 on error +# (such as no clusters available). +# </LJFUNC> +sub new_account_cluster +{ + # if it's not an arrayref, put it in an array ref so we can use it below + my $clusters = ref $LJ::DEFAULT_CLUSTER ? $LJ::DEFAULT_CLUSTER : [ $LJ::DEFAULT_CLUSTER+0 ]; + + # select a random cluster from the set we've chosen in $LJ::DEFAULT_CLUSTER + return LJ::DB::random_cluster(@$clusters); +} + +# returns the clusterid of a random cluster which is up +# -- accepts @clusters as an arg to enforce a subset, otherwise +# uses @LJ::CLUSTERS +sub random_cluster { + my @clusters = @_ ? @_ : @LJ::CLUSTERS; + + # iterate through the new clusters from a random point + my $size = @clusters; + my $start = int(rand() * $size); + foreach (1..$size) { + my $cid = $clusters[$start++ % $size]; + + # verify that this cluster is in @LJ::CLUSTERS + my @check = grep { $_ == $cid } @LJ::CLUSTERS; + next unless scalar(@check) >= 1 && $check[0] == $cid; + + # try this cluster to see if we can use it, return if so + my $dbcm = LJ::get_cluster_master($cid); + return $cid if $dbcm; + } + + # if we get here, we found no clusters that were up... + return 0; +} + package LJ; diff -r 4852ffcec614 -r 70869e7cfca3 cgi-bin/LJ/User.pm --- a/cgi-bin/LJ/User.pm Mon Oct 03 13:47:53 2011 +0800 +++ b/cgi-bin/LJ/User.pm Mon Oct 03 13:50:36 2011 +0800 @@ -128,7 +128,7 @@ my $username = LJ::canonical_username($opts{user}) or return; - my $cluster = $opts{cluster} || LJ::new_account_cluster(); + my $cluster = $opts{cluster} || LJ::DB::new_account_cluster(); my $caps = $opts{caps} || $LJ::NEWUSER_CAPS; my $journaltype = $opts{journaltype} || "P"; @@ -7118,7 +7118,6 @@ ### to match the sections above -- please check up there if adding. ### ### Categories: -### 1. Creating and Deleting Accounts ### 3. Working with All Types of Accounts ### 4. Login, Session, and Rename Functions ### 5. Database and Memcache Functions @@ -7130,58 +7129,6 @@ ### 24. Styles and S2-Related Functions ######################################################################## -### 1. Creating and Deleting Accounts - -=head1 LJ Methods - -=head2 Creating and Deleting Accounts (LJ) -=cut - -# <LJFUNC> -# name: LJ::new_account_cluster -# des: Which cluster to put a new account on. $DEFAULT_CLUSTER if it's -# a scalar, random element from [ljconfig[default_cluster]] if it's arrayref. -# also verifies that the database seems to be available. -# returns: clusterid where the new account should be created; 0 on error -# (such as no clusters available). -# </LJFUNC> -sub new_account_cluster -{ - # if it's not an arrayref, put it in an array ref so we can use it below - my $clusters = ref $LJ::DEFAULT_CLUSTER ? $LJ::DEFAULT_CLUSTER : [ $LJ::DEFAULT_CLUSTER+0 ]; - - # select a random cluster from the set we've chosen in $LJ::DEFAULT_CLUSTER - return LJ::random_cluster(@$clusters); -} - - -# returns the clusterid of a random cluster which is up -# -- accepts @clusters as an arg to enforce a subset, otherwise -# uses @LJ::CLUSTERS -sub random_cluster { - my @clusters = @_ ? @_ : @LJ::CLUSTERS; - - # iterate through the new clusters from a random point - my $size = @clusters; - my $start = int(rand() * $size); - foreach (1..$size) { - my $cid = $clusters[$start++ % $size]; - - # verify that this cluster is in @LJ::CLUSTERS - my @check = grep { $_ == $cid } @LJ::CLUSTERS; - next unless scalar(@check) >= 1 && $check[0] == $cid; - - # try this cluster to see if we can use it, return if so - my $dbcm = LJ::get_cluster_master($cid); - return $cid if $dbcm; - } - - # if we get here, we found no clusters that were up... - return 0; -} - - -######################################################################## ### 2. Working with All Types of Accounts =head2 Working with All Types of Accounts (LJ) diff -r 4852ffcec614 -r 70869e7cfca3 cgi-bin/ljlib.pl --- a/cgi-bin/ljlib.pl Mon Oct 03 13:47:53 2011 +0800 +++ b/cgi-bin/ljlib.pl Mon Oct 03 13:50:36 2011 +0800 @@ -2008,7 +2008,7 @@ my ($cid, $action) = @_; # fall back to selecting a random cluster - $cid = LJ::random_cluster() unless defined $cid; + $cid = LJ::DB::random_cluster() unless defined $cid; # accept a user object $cid = ref $cid ? $cid->{clusterid}+0 : $cid+0; --------------------------------------------------------------------------------