Committer: afedorov
LJSUP-10900: Implement MessagePack protocol driver to access to Relation Service.U trunk/cgi-bin/LJ/RelationService/MysqlAPI.pm U trunk/cgi-bin/LJ/RelationService.pm U trunk/cgi-bin/LJ/User.pm U trunk/cgi-bin/ljrelation.pl
Modified: trunk/cgi-bin/LJ/RelationService/MysqlAPI.pm =================================================================== --- trunk/cgi-bin/LJ/RelationService/MysqlAPI.pm 2012-02-09 14:03:09 UTC (rev 21153) +++ trunk/cgi-bin/LJ/RelationService/MysqlAPI.pm 2012-02-09 14:15:51 UTC (rev 21154) @@ -6,8 +6,20 @@ sub find_relation_destinations { my $class = shift; my $u = shift; + my $type = shift; my %opts = @_; - my $limit = $opts{limit} || 50000; + + if ( $type eq 'F' ) { + return $class->_find_relation_destinations_type_f($u, %opts); + } else { + return $class->_find_relation_destinations_type_other($u, $type, %opts); + } +} + +sub _find_relation_destinations_type_f { + my $class = shift; + my $u = shift; + my %opts = @_; my $nogearman = $opts{nogearman} || 0; ## stricly disable gearman, @@ -16,19 +28,64 @@ my $uids = $class->_friend_friendof_uids($u, %opts, - limit => $limit, + limit => $opts{limit}, nogearman => $nogearman, mode => "friends", ); return @$uids; } +sub _find_relation_destinations_type_other { + my $class = shift; + my $u = shift; + my $type = shift; + my %opts = @_; + + my $db = LJ::isdb($opts{db}) ? shift : undef; + + my $userid = $u->userid; + my $typeid = LJ::get_reluser_id($type)+0; + my $uids; + if ($typeid) { + # clustered reluser2 table + $db = LJ::get_cluster_reader($u); + $uids = $db->selectcol_arrayref(" + SELECT targetid + FROM reluser2 + WHERE userid=? + AND type=? + ", undef, $userid, $typeid); + } else { + # non-clustered reluser global table + $db ||= LJ::get_db_reader(); + $uids = $db->selectcol_arrayref(" + SELECT targetid + FROM reluser + WHERE userid=? + AND type=? + ", undef, $userid, $type); + } + return @$uids; +} + ## friendofs sub find_relation_sources { my $class = shift; my $u = shift; + my $type = shift; my %opts = @_; - my $limit = $opts{limit} || 50000; + + if ( $type eq 'F' ) { + return $class->_find_relation_sources_type_f($u, %opts); + } else { + return $class->_find_relation_sources_type_other($u, $type, %opts); + } +} + +sub _find_relation_sources_type_f { + my $class = shift; + my $u = shift; + my %opts = @_; my $nogearman = $opts{nogearman} || 0; ## stricly disable gearman @@ -37,17 +94,51 @@ my $uids = $class->_friend_friendof_uids($u, %opts, - limit => $limit, + limit => $opts{limit}, nogearman => $nogearman, mode => "friendofs", ); return @$uids; } +sub _find_relation_sources_type_other { + my $class = shift; + my $u = shift; + my $type = shift; + my %opts = @_; + + my $db = LJ::isdb($opts{db}) ? shift : undef; + + my $userid = $u->userid; + my $typeid = LJ::get_reluser_id($type)+0; + my $uids; + if ($typeid) { + # clustered reluser2 table + $db = LJ::get_cluster_reader($u); + $uids = $db->selectcol_arrayref(" + SELECT userid + FROM reluser2 + WHERE targetid=? + AND type=? + ", undef, $userid, $typeid); + } else { + # non-clustered reluser global table + $db ||= LJ::get_db_reader(); + $uids = $db->selectcol_arrayref(" + SELECT userid + FROM reluser + WHERE targetid=? + AND type=? + ", undef, $userid, $type); + } + return @$uids; +} + ## friends rows sub load_relation_destinations { my $class = shift; my $u = shift; + my $type = shift; my %opts = @_; my $limit = $opts{limit} || 50000; my $nogearman = $opts{nogearman} || 0; Modified: trunk/cgi-bin/LJ/RelationService.pm =================================================================== --- trunk/cgi-bin/LJ/RelationService.pm 2012-02-09 14:03:09 UTC (rev 21153) +++ trunk/cgi-bin/LJ/RelationService.pm 2012-02-09 14:15:51 UTC (rev 21154) @@ -7,22 +7,27 @@ use LJ::RelationService::RSAPI; use LJ::RelationService::MysqlAPI; -my $PARAMS = {}; +use Data::Dumper; sub _load_alt_api { my $class = shift; my $method = shift; + my $type = shift; return 0 unless LJ::is_enabled('send_test_load_to_rs2'); my $ext_block = LJ::ExtBlock->load_by_id('lj11_params'); my $values = $ext_block ? LJ::JSON->from_json($ext_block->blocktext) : {}; - $PARAMS->{rs_ratio_read} = $values->{rs_ratio_read} || 0; - $PARAMS->{rs_ratio_update} = $values->{rs_ratio_update} || 0; - my $rate = ($method eq 'read') ? $PARAMS->{rs_ratio_read} : - ($method eq 'update') ? $PARAMS->{rs_ratio_update} : 0; + if ($type eq 'F') { + return 0 unless $values->{rs_enable_type_f}; + } else { + return 0 unless $values->{rs_enable_type_other}; + } + my $rate = ($method eq 'read') ? $values->{rs_ratio_read} : + ($method eq 'update') ? $values->{rs_ratio_update} : 0; + return 0 unless $rate; ## @@ -32,7 +37,6 @@ return 0; } - sub relation_api { my $class = shift; my $u = shift; @@ -49,16 +53,21 @@ sub find_relation_destinations { my $class = shift; my $u = shift; + my $type = shift; + my %opts = @_; + + $opts{offset} ||= 0; + $opts{limit} ||= 50000; - if ($class->_load_alt_api('read')){ + if ($class->_load_alt_api('read', $type)){ my $alt = $class->alt_api($u); - if ($alt){ - $alt->find_relation_destinations($u, @_); + if ($alt) { + $alt->find_relation_destinations($u, $type, %opts); } } my $interface = $class->relation_api($u); - return $interface->find_relation_destinations($u, @_); + return $interface->find_relation_destinations($u, $type, %opts); } @@ -66,26 +75,50 @@ sub find_relation_sources { my $class = shift; my $u = shift; + my $type = shift; + my %opts = @_; - if ($class->_load_alt_api('read')){ + $opts{offset} ||= 0; + $opts{limit} ||= 50000; + + if ($class->_load_alt_api('read', $type)){ my $alt = $class->alt_api($u); - if ($alt){ - $alt->find_relation_sources($u, @_); + if ($alt) { + $alt->find_relation_sources($u, $type, %opts); } } my $interface = $class->relation_api($u); - return $interface->find_relation_sources($u, @_); + return $interface->find_relation_sources($u, $type, %opts); } sub load_relation_destinations { my $class = shift; my $u = shift; + my $type = shift; + my %opts = @_; + $opts{offset} ||= 0; + $opts{limit} ||= 50000; + + my $result; + + if ($class->_load_alt_api('read', $type)){ + my $alt = $class->alt_api($u); + if ($alt) { + $result = $alt->load_relation_destinations($u, $type, %opts); + warn Dumper({load_relation_destinations_rs2=>$result}); + } + } + + + + my $interface = $class->relation_api($u); - return $interface->load_relation_destinations($u, @_); - + $result = $interface->load_relation_destinations($u, $type, %opts); + warn Dumper({load_relation_destinations_mysql=>$result}); + return $result; } sub create_relation_to { Modified: trunk/cgi-bin/LJ/User.pm =================================================================== --- trunk/cgi-bin/LJ/User.pm 2012-02-09 14:03:09 UTC (rev 21153) +++ trunk/cgi-bin/LJ/User.pm 2012-02-09 14:15:51 UTC (rev 21154) @@ -3955,7 +3955,7 @@ *userid = \&id; sub id { my $u = shift; - return $u->{userid}; + return int($u->{userid}); } sub clusterid { @@ -4693,7 +4693,7 @@ my $limit = int(delete $args{limit}) || 50000; Carp::croak("unknown option") if %args; - return LJ::RelationService->find_relation_sources($u, limit => $limit); + return LJ::RelationService->find_relation_sources($u, 'F', limit => $limit); } # returns array of friend uids. by default, limited at 50,000 items. @@ -4702,7 +4702,7 @@ my $limit = int(delete $args{limit}) || 50000; Carp::croak("unknown option") if %args; - return LJ::RelationService->find_relation_destinations($u, limit => $limit); + return LJ::RelationService->find_relation_destinations($u, 'F', limit => $limit); } # helper method since the logic for both friends and friendofs is so similar @@ -9032,7 +9032,8 @@ my $u = LJ::load_userid($userid); return LJ::RelationService->load_relation_destinations( - $u, uuid => $uuid, + $u, 'F', + uuid => $uuid, mask => $mask, memcache_only => $memcache_only, force_db => $force, @@ -9053,7 +9054,7 @@ return undef unless $userid; my $u = LJ::load_userid($userid); - return LJ::RelationService->find_relation_sources($u, + return LJ::RelationService->find_relation_sources($u, 'F', nolimit => $opts->{force} || 0, skip_memcached => $opts->{force}, ); Modified: trunk/cgi-bin/ljrelation.pl =================================================================== --- trunk/cgi-bin/ljrelation.pl 2012-02-09 14:03:09 UTC (rev 21153) +++ trunk/cgi-bin/ljrelation.pl 2012-02-09 14:15:51 UTC (rev 21154) @@ -1,6 +1,8 @@ package LJ; use strict; +use LJ::RelationService; + ######################### # Types of relations: # P - poster @@ -128,22 +130,14 @@ sub load_rel_user { my $db = isdb($_[0]) ? shift : undef; - my ($userid, $type) = @_; - return undef unless $type and $userid; - my $u = LJ::want_user($userid); - $userid = LJ::want_userid($userid); - my $typeid = LJ::get_reluser_id($type)+0; - if ($typeid) { - # clustered reluser2 table - $db = LJ::get_cluster_reader($u); - return $db->selectcol_arrayref("SELECT targetid FROM reluser2 WHERE userid=? AND type=?", - undef, $userid, $typeid); - } else { - # non-clustered reluser global table - $db ||= LJ::get_db_reader(); - return $db->selectcol_arrayref("SELECT targetid FROM reluser WHERE userid=? AND type=?", - undef, $userid, $type); - } + my ($u, $type, %args) = @_; + + return undef unless $u and $type; + + my $limit = int(delete $args{limit}) || 50000; + + my @uids = LJ::RelationService->find_relation_destinations($u, $type, limit => $limit, db => $db, %args); + return \@uids; } # <LJFUNC> @@ -192,22 +186,14 @@ sub load_rel_target { my $db = isdb($_[0]) ? shift : undef; - my ($targetid, $type) = @_; - return undef unless $type and $targetid; - my $u = LJ::want_user($targetid); - $targetid = LJ::want_userid($targetid); - my $typeid = LJ::get_reluser_id($type)+0; - if ($typeid) { - # clustered reluser2 table - $db = LJ::get_cluster_reader($u); - return $db->selectcol_arrayref("SELECT userid FROM reluser2 WHERE targetid=? AND type=?", - undef, $targetid, $typeid); - } else { - # non-clustered reluser global table - $db ||= LJ::get_db_reader(); - return $db->selectcol_arrayref("SELECT userid FROM reluser WHERE targetid=? AND type=?", - undef, $targetid, $type); - } + my ($u, $type, %args) = @_; + + return undef unless $u and $type; + + my $limit = int(delete $args{limit}) || 50000; + + my @uids = LJ::RelationService->find_relation_sources($u, $type, limit => $limit, db => $db, %args); + return \@uids; } # <LJFUNC>