mark: A photo of Mark kneeling on top of the Taal Volcano in the Philippines. It was a long hike. (Default)
Mark Smith ([staff profile] mark) wrote in [site community profile] changelog2012-04-29 12:18 am

[dw-free] Fix MEDIUMINT jtalkid memcache storage

[commit: http://hg.dwscoalition.org/dw-free/rev/acb4c85567e3]

Fix MEDIUMINT jtalkid memcache storage

We hit 2^24 comments in one of our communities, so this is an emergency fix
to address the memcache portion of the problem. The database fix will come
after this.

Patch by [staff profile] mark.

Files modified:
  • cgi-bin/LJ/Talk.pm
  • htdocs/admin/memcache_view.bml
--------------------------------------------------------------------------------
diff -r 53f644001e05 -r acb4c85567e3 cgi-bin/LJ/Talk.pm
--- a/cgi-bin/LJ/Talk.pm	Sat Apr 28 18:50:52 2012 +0000
+++ b/cgi-bin/LJ/Talk.pm	Sun Apr 29 00:18:57 2012 +0000
@@ -679,7 +679,10 @@
     my $ret = {};
 
     # check for data in memcache
-    my $DATAVER = "1";  # single character
+    my $DATAVER = "3";  # single character
+    my $PACK_FORMAT = "NNNNC"; ## $talkid, $parenttalkid, $poster, $time, $state
+    my $RECORD_SIZE = 17;   
+
     my $memkey = [$u->{'userid'}, "talk2:$u->{'userid'}:$nodetype:$nodeid"];
     my $lockkey = $memkey->[1];
     my $packed = LJ::MemCache::get($memkey);
@@ -750,15 +753,15 @@
 
     my $memcache_good = sub {
         return $packed && substr($packed,0,1) eq $DATAVER &&
-            length($packed) % 16 == 1;
+            length($packed) % $RECORD_SIZE == 1;
     };
 
     my $memcache_decode = sub {
-        my $n = (length($packed) - 1) / 16;
+        my $n = (length($packed) - 1) / $RECORD_SIZE;
         for (my $i=0; $i<$n; $i++) {
-            my ($f1, $par, $poster, $time) = unpack("NNNN",substr($packed,$i*16+1,16));
-            my $state = chr($f1 & 255);
-            my $talkid = $f1 >> 8;
+            my ( $talkid, $par, $poster, $time, $state ) =
+                unpack( $PACK_FORMAT, substr($packed, $i*$RECORD_SIZE+1, $RECORD_SIZE ) );
+            $state = chr($state);
             $ret->{$talkid} = {
                 talkid => $talkid,
                 state => $state,
@@ -824,11 +827,12 @@
             LJ::Talk::add_talk2row_memcache($u->id, $r->{talkid}, \%row_arg);
         }
 
-        $memval .= pack("NNNN",
-                        ($r->{'talkid'} << 8) + ord($r->{'state'}),
+        $memval .= pack($PACK_FORMAT,
+                        $r->{'talkid'},
                         $r->{'parenttalkid'},
                         $r->{'posterid'},
-                        $r->{'datepost_unix'});
+                        $r->{'datepost_unix'},
+                        ord($r->{'state'}));
 
         $rp_ourcount++ if $r->{'state'} eq "A";
     }
@@ -1412,7 +1416,7 @@
             foreach my $k (qw(userid width height flags picdate)) {
                 $ur->{$k} += 0;
             }
-            $ur->{location} = uc(substr($ur->{location}, 0, 1));
+            $ur->{location} = uc(substr($ur->{location} || '', 0, 1));
 
             $LJ::CACHE_USERPIC{$id} = $ur;
             LJ::MemCache::set([$id,"userpic.$id"], LJ::MemCache::hash_to_array("userpic", $ur));
diff -r 53f644001e05 -r acb4c85567e3 htdocs/admin/memcache_view.bml
--- a/htdocs/admin/memcache_view.bml	Sat Apr 28 18:50:52 2012 +0000
+++ b/htdocs/admin/memcache_view.bml	Sun Apr 29 00:18:57 2012 +0000
@@ -101,17 +101,19 @@
      # unpack packed data
      if ($key =~ /^talk2:/) {
          my $newval;
-         my $n = (length($val) - 1) / 16;
+         my $PACK_FORMAT = "NNNNC"; ## $talkid, $parenttalkid, $poster, $time, $state
+         my $RECORD_SIZE = 17;
+                     
+         my $n = (length($val) - 1) / $RECORD_SIZE;
          for (my $i=0; $i<$n; $i++) {
-             my ($f1, $par, $poster, $time) = unpack("NNNN",substr($val,$i*16+1,16));
-             my $state = chr($f1 & 255);
-             my $talkid = $f1 >> 8;
+             my ( $talkid, $parenttalkid, $poster, $time, $state ) =
+                 unpack( $PACK_FORMAT, substr( $val, $i*$RECORD_SIZE+1, $RECORD_SIZE ) );
              $newval->{$talkid} = {
                  talkid => $talkid,
-                 state => $state,
+                 state => chr($state),
                  posterid => $poster,
                  datepost => LJ::mysql_time($time),
-                 parenttalkid => $par,
+                 parenttalkid => $parenttalkid,
              };
          }
          $val = [substr($val,0,1), $newval];
--------------------------------------------------------------------------------