Committer: gariev
LJSUP-10124: [internal] Make user cluster DB switching dynamicallyU trunk/cgi-bin/LJ/DBUtil.pm U trunk/cgi-bin/LJ/User.pm U trunk/cgi-bin/ljdb.pl U trunk/cgi-bin/ljlib.pl
Modified: trunk/cgi-bin/LJ/DBUtil.pm =================================================================== --- trunk/cgi-bin/LJ/DBUtil.pm 2011-10-12 18:20:17 UTC (rev 20298) +++ trunk/cgi-bin/LJ/DBUtil.pm 2011-10-13 00:47:07 UTC (rev 20299) @@ -15,14 +15,25 @@ print STDERR " - cluster $cid... " if $verbose; # find approparite db server to connect to - my $role = "cluster$cid"; - if (my $ab = $LJ::CLUSTER_PAIR_ACTIVE{$cid}) { - $role .= "b" if $ab eq 'a'; - $role .= "a" if $ab eq 'b'; - - print STDERR "{active=$ab, using=$role}\n" if $verbose; + my $role; + if ($LJ::IS_DEV_SERVER) { + $role = "cluster$cid"; } else { - die "invalid cluster: $cid ?\n"; + my $block_id = 'cluster_config.rc'; + my $block = LJ::ExtBlock->load_by_id($block_id, {cache_valid => 15}); + if ($block && $block->data->{$cid}) { + my $c = $block->data->{$cid}; + my $ab = ($c->{'active'} eq 'a') ? 'b' : 'a'; + if ($c->{'dead'} && ($c->{'dead'} eq $ab) { + ## oops, inactive db is dead + warn " inactivve DB $ab is marked dead"; + return; + } else { + $role = "cluster${cid}$ab"; + } + } else { + $role = "cluster${cid}a"; + } } $LJ::DBIRole->clear_req_cache(); @@ -53,8 +64,7 @@ my $clid = shift; my $verbose = shift; - my $dbr = ($LJ::IS_DEV_SERVER) ? - LJ::get_cluster_reader($clid) : LJ::DBUtil->get_inactive_db($clid); + my $dbr = LJ::DBUtil->get_inactive_db($clid); unless ($dbr) { warn "Using master database for cluster #$clid" if $verbose; Modified: trunk/cgi-bin/LJ/User.pm =================================================================== --- trunk/cgi-bin/LJ/User.pm 2011-10-12 18:20:17 UTC (rev 20298) +++ trunk/cgi-bin/LJ/User.pm 2011-10-13 00:47:07 UTC (rev 20299) @@ -3472,15 +3472,6 @@ return LJ::load_userid($inviterid); } -# front-end to LJ::cmd_buffer_add, which has terrible interface -# cmd: scalar -# args: hashref -sub cmd_buffer_add { - my ($u, $cmd, $args) = @_; - $args ||= {}; - return LJ::cmd_buffer_add($u->{clusterid}, $u->{userid}, $cmd, $args); -} - sub subscriptions { my $u = shift; return LJ::Subscription->subscriptions_of_user($u); @@ -6112,60 +6103,60 @@ return 0 unless $dbcm; my @keys = qw( - bio:# - cctry_uid:# - commsettings:# - dayct:# - fgrp:# - friendofs:# - friendofs2:# - friends:# - friends2:# - ident:# - inbox:newct:# - intids:# - invites:# - jablastseen:# - jabuser:# - kws:# - lastcomm:# - linkobj:# - log2ct:# - log2lt:# - logtag:# - mcrate:# - memct:# - memkwcnt:# - memkwid:# - msn:mutual_friends_wlids:uid=# - prtcfg:# - pw:# - rate:tracked:# - rcntalk:# - s1overr:# - s1uc:# - saui:# - subscriptions_count:# - supportpointsum:# - synd:# - tags2:# - talk2ct:# - talkleftct:# - tc:# - timeactive:# - timezone_guess:# - tu:# - txtmsgsecurity:# - uid2uniqs:# - upiccom:# - upicinf:# - upicquota:# - upicurl:# - userid:# + bio:* + cctry_uid:* + commsettings:* + dayct:* + fgrp:* + friendofs:* + friendofs2:* + friends:* + friends2:* + ident:* + inbox:newct:* + intids:* + invites:* + jablastseen:* + jabuser:* + kws:* + lastcomm:* + linkobj:* + log2ct:* + log2lt:* + logtag:* + mcrate:* + memct:* + memkwcnt:* + memkwid:* + msn:mutual_friends_wlids:uid=* + prtcfg:* + pw:* + rate:tracked:* + rcntalk:* + s1overr:* + s1uc:* + saui:* + subscriptions_count:* + supportpointsum:* + synd:* + tags2:* + talk2ct:* + talkleftct:* + tc:* + timeactive:* + timezone_guess:* + tu:* + txtmsgsecurity:* + uid2uniqs:* + upiccom:* + upicinf:* + upicquota:* + upicurl:* + userid:* ); foreach my $key (@keys) { - $key =~ s/#/$u->{userid}/g; + $key =~ s/\*/$u->{userid}/g; LJ::MemCache::delete([ $u->{userid}, $key ]); } @@ -9024,7 +9015,7 @@ ); $sclient->insert($job); } else { - LJ::cmd_buffer_add($u->{clusterid}, $u->{userid}, 'dirty', { what => 'friends' }); + die "No schwartz client found"; } }; } Modified: trunk/cgi-bin/ljdb.pl =================================================================== --- trunk/cgi-bin/ljdb.pl 2011-10-12 18:20:17 UTC (rev 20298) +++ trunk/cgi-bin/ljdb.pl 2011-10-13 00:47:07 UTC (rev 20299) @@ -184,37 +184,6 @@ return $LJ::DBIRole->get_dbh_conn( _connection_options(), $fdsn ); } -sub backup_in_progress { - my $name = shift; - my $dbh = dbh_by_role("master") - or die "Couldn't contact master to find name of '$name'"; - - # return 0 if this a/b is the active side, as wecan't ever have a backup of active side in progress - my ($cid, $is_a_or_b) = user_cluster_details($name); - if ($cid) { - my $active_ab = $LJ::CLUSTER_PAIR_ACTIVE{$cid} or - die "Neither 'a' nor 'b' is active for clusterid $cid?\n"; - die "Bogus active side" unless $active_ab =~ /^[ab]$/; - - # can't have a backup in progress for an active a/b side. short-circuit - # and don't even ask the database, as it might lie if the process - # was killed or something - return 0 if $active_ab eq $is_a_or_b; - } - - my $fdsn = $dbh->selectrow_array("SELECT rootfdsn FROM dbinfo WHERE name=?", undef, $name); - die "No rootfdsn found for db name '$name'\n" unless $fdsn; - $fdsn =~ /\bhost=([\w\.\-]+)/ or die "Can't find host for database '$name'"; - my $host = $1; - - eval "use IO::Socket::INET; 1;" or die; - my $sock = IO::Socket::INET->new(PeerAddr => "$host:7602") or return 0; - print $sock "is_backup_in_progress\r\n"; - my $answer = <$sock>; - chomp $answer; - return $answer eq "1"; -} - sub user_cluster_details { my $name = shift; my $dbh = dbh_by_role("master") or die; @@ -345,15 +314,7 @@ # </LJFUNC> sub get_cluster_reader { - my $arg = shift; - my $id = isu($arg) ? $arg->{'clusterid'} : $arg; - my @roles = ("cluster${id}slave", "cluster${id}"); - if (my $ab = $LJ::CLUSTER_PAIR_ACTIVE{$id}) { - $ab = lc($ab); - # master-master cluster - @roles = ("cluster${id}${ab}") if $ab eq "a" || $ab eq "b"; - } - return LJ::get_dbh(@roles); + return LJ::get_cluster_master(@_); } # <LJFUNC> @@ -368,12 +329,7 @@ # </LJFUNC> sub get_cluster_def_reader { - my @dbh_opts = scalar(@_) == 2 ? (shift @_) : (); - my $arg = shift; - my $id = LJ::isu($arg) ? $arg->{'clusterid'} : $arg; - return LJ::get_cluster_reader(@dbh_opts, $id) if - $LJ::DEF_READER_ACTUALLY_SLAVE{$id}; - return LJ::get_dbh(@dbh_opts, LJ::master_role($id)); + return LJ::get_cluster_master(@_); } # <LJFUNC> @@ -397,13 +353,15 @@ # returns the DBI::Role role name of a cluster master given a clusterid sub master_role { my $id = shift; - my $role = "cluster${id}"; - if (my $ab = $LJ::CLUSTER_PAIR_ACTIVE{$id}) { - $ab = lc($ab); - # master-master cluster - $role = "cluster${id}${ab}" if $ab eq "a" || $ab eq "b"; + + if ($LJ::IS_DEV_SERVER) { + return "cluster${id}"; + } else { + my $block_id = 'cluster_config.rc'; + my $block = LJ::ExtBlock->load_by_id($block_id, {cache_valid => 15}); + my $ab = ($block && $block->data->{$id}->{'active'}) ? $block->data->{$id}->{'active'} : 'a'; + return "cluster${id}${ab}"; } - return $role; } # <LJFUNC> @@ -559,8 +517,7 @@ my $opts = shift || {}; foreach my $cluster_id (@LJ::CLUSTERS) { - my $dbr = ($LJ::IS_DEV_SERVER) ? - LJ::get_cluster_reader($cluster_id) : LJ::DBUtil->get_inactive_db($cluster_id, $opts->{verbose}); + my $dbr = LJ::DBUtil->get_inactive_db($cluster_id, $opts->{verbose}); $coderef->($cluster_id, $dbr); } } Modified: trunk/cgi-bin/ljlib.pl =================================================================== --- trunk/cgi-bin/ljlib.pl 2011-10-12 18:20:17 UTC (rev 20298) +++ trunk/cgi-bin/ljlib.pl 2011-10-13 00:47:07 UTC (rev 20299) @@ -1102,18 +1102,8 @@ my $max_hints = $LJ::MAX_SCROLLBACK_LASTN; # temporary my $sort_key = "revttime"; - my $clusterid = $u->{'clusterid'} + 0; - my @sources = ("cluster$clusterid"); + my $logdb = LJ::get_cluster_def_reader($u); - if (my $ab = $LJ::CLUSTER_PAIR_ACTIVE{$clusterid}) { - @sources = ("cluster${clusterid}${ab}"); - } - - unshift @sources, ("cluster${clusterid}lite", "cluster${clusterid}slave") - if $opts->{'clustersource'} eq "slave"; - - my $logdb = LJ::get_dbh(@sources); - # community/friend views need to post by log time, not event time $sort_key = "rlogtime" if ($opts->{'order'} eq "logtime" || $opts->{'friendsview'}); @@ -2485,73 +2475,6 @@ } # <LJFUNC> -# name: LJ::cmd_buffer_add -# des: Schedules some command to be run sometime in the future which would -# be too slow to do synchronously with the web request. An example -# is deleting a journal entry, which requires recursing through a lot -# of tables and deleting all the appropriate stuff. -# args: db, journalid, cmd, hargs -# des-db: Global db handle to run command on, or user clusterid if cluster -# des-journalid: Journal id command affects. This is indexed in the -# [dbtable[cmdbuffer]] table, so that all of a user's queued -# actions can be run before that user is potentially moved -# between clusters. -# des-cmd: Text of the command name. 30 chars max. -# des-hargs: Hashref of command arguments. -# </LJFUNC> -sub cmd_buffer_add -{ - my ($db, $journalid, $cmd, $args) = @_; - - return 0 unless $cmd; - - my $cid = ref $db ? 0 : $db+0; - $db = $cid ? LJ::get_cluster_master($cid) : $db; - my $ab = $LJ::CLUSTER_PAIR_ACTIVE{$cid}; - - return 0 unless $db; - - my $arg_str; - if (ref $args eq 'HASH') { - foreach (sort keys %$args) { - $arg_str .= LJ::eurl($_) . "=" . LJ::eurl($args->{$_}) . "&"; - } - chop $arg_str; - } else { - $arg_str = $args || ""; - } - - my $rv; - if ($ab eq 'a' || $ab eq 'b') { - # get a lock - my $locked = $db->selectrow_array("SELECT GET_LOCK('cmd-buffer-$cid',10)"); - return 0 unless $locked; # 10 second timeout elapsed - - # a or b -- a goes odd, b goes even! - my $max = $db->selectrow_array('SELECT MAX(cbid) FROM cmdbuffer'); - $max += $ab eq 'a' ? ($max & 1 ? 2 : 1) : ($max & 1 ? 1 : 2); - - # insert command - $db->do('INSERT INTO cmdbuffer (cbid, journalid, instime, cmd, args) ' . - 'VALUES (?, ?, NOW(), ?, ?)', undef, - $max, $journalid, $cmd, $arg_str); - $rv = $db->err ? 0 : 1; - - # release lock - $db->selectrow_array("SELECT RELEASE_LOCK('cmd-buffer-$cid')"); - } else { - # old method - $db->do("INSERT INTO cmdbuffer (journalid, cmd, instime, args) ". - "VALUES (?, ?, NOW(), ?)", undef, - $journalid, $cmd, $arg_str); - $rv = $db->err ? 0 : 1; - } - - return $rv; -} - - -# <LJFUNC> # name: LJ::get_keyword_id # class: # des: Get the id for a keyword. @@ -3150,12 +3073,6 @@ return; } - # cluster switchovers - if ($cmd eq 'cluster_switch') { - $LJ::CLUSTER_PAIR_ACTIVE{ $arg->{'cluster'} } = $arg->{ 'role' }; - return; - } - if ($cmd eq LJ::AdTargetedInterests->procnotify_key) { LJ::AdTargetedInterests->reload; return;