[memcached] sgrimm, r507: Merge a few more missing changes from tr...

commits at code.sixapart.com commits at code.sixapart.com
Mon Apr 16 15:18:39 UTC 2007


Merge a few more missing changes from trunk to multithreaded.


U   branches/multithreaded/api/perl/dev/bench.pl
A   branches/multithreaded/api/perl/dev/cons-hash.pl
U   branches/multithreaded/server/ChangeLog
U   branches/multithreaded/server/assoc.c
U   branches/multithreaded/server/assoc.h
U   branches/multithreaded/server/daemon.c
U   branches/multithreaded/server/items.c
U   branches/multithreaded/server/items.h
U   branches/multithreaded/server/memcached.c
U   branches/multithreaded/server/slabs.c
U   branches/multithreaded/website/apis.bml


Modified: branches/multithreaded/api/perl/dev/bench.pl
===================================================================
--- branches/multithreaded/api/perl/dev/bench.pl	2007-04-16 14:02:37 UTC (rev 506)
+++ branches/multithreaded/api/perl/dev/bench.pl	2007-04-16 15:18:35 UTC (rev 507)
@@ -31,7 +31,7 @@
 }
 
 srand(1);
-my $to = 3000;
+my $to = shift || 3000;
 for (1..$to) {
     warn "$_ / $to\n" if $_ % 100 == 0;
     my @multi = map { "key$_" } map { int(rand($keys * 2)) + 1 } (1..40);

Added: branches/multithreaded/api/perl/dev/cons-hash.pl
===================================================================
--- branches/multithreaded/api/perl/dev/cons-hash.pl	2007-04-16 14:02:37 UTC (rev 506)
+++ branches/multithreaded/api/perl/dev/cons-hash.pl	2007-04-16 15:18:35 UTC (rev 507)
@@ -0,0 +1,213 @@
+#!/usr/bin/perl
+
+use strict;
+use Digest::SHA1 qw(sha1);
+use String::CRC32 qw(crc32);;
+use Data::Dumper;
+
+my $set = Set::ConsistentHash->new;
+$set->modify_targets(
+                     A => 1,
+                     B => 1,
+                     C => 2,
+                     );
+
+my $set2 = Set::ConsistentHash->new;
+$set2->modify_targets(
+                      A => 1,
+                      B => 1,
+                      C => 1,
+                      );
+
+#print Dumper($set->bucket_counts);
+#print Dumper($set2->bucket_counts);
+
+
+if (0) {
+    my %matched;
+    my $total_trials = 100_000;
+    for my $n (1..$total_trials) {
+        my $rand = crc32("trial$n");
+        my $server = $set->target_of_point($rand);
+        #print "matched $rand = $server\n";
+        $matched{$server}++;
+    }
+
+    foreach my $s ($set->targets) {
+        printf("$s: expected=%0.02f%%  actual=%0.02f%%\n", #  space=%0.02f%%\n",
+               $set->weight_percentage($s),
+               100 * $matched{$s} / $total_trials,
+               #($space{$s} / 2**32) * 100,
+               );
+    }
+}
+
+if (1) {
+    my $total_trials = 100_000;
+    my %tran;
+    for my $n (1..$total_trials) {
+        my $rand = crc32("trial$n");
+        #my $s1 = $set->target_of_point($rand);
+        #my $s2 = $set2->target_of_point($rand);
+
+        my $s1 = $set->target_of_bucket($rand);
+        my $s2 = $set2->target_of_bucket($rand);
+        $tran{"$s1-$s2"}++;
+        $tran{"was-$s1"}++;
+        $tran{"now-$s2"}++;
+    }
+
+    print Dumper(\%tran);
+}
+
+
+############################################################################
+
+package Set::ConsistentHash;
+use strict;
+use Digest::SHA1 qw(sha1);
+
+# creates a new consistent hashing set with no targets.  you'll need to add targets.
+sub new {
+    my ($class) = @_;
+    return bless {
+        weights => {},  # $target => integer $weight
+        points  => {},  # 32-bit value points on 'circle' => \$target
+        order   => [],  # 32-bit points, sorted
+        buckets      => undef, # when requested, arrayref of 1024 buckets mapping to targets
+        total_weight => undef, # when requested, total weight of all targets
+    }, $class;
+}
+
+# returns sorted list of all configured $targets
+sub targets {
+    my $self = shift;
+    return sort keys %{$self->{weights}};
+}
+
+
+# returns sum of all target's weight
+sub total_weight {
+    my $self = shift;
+    return $self->{total_weight} if defined $self->{total_weight};
+    my $sum = 0;
+    foreach my $val (values %{$self->{weights}}) {
+        $sum += $val;
+    }
+    return $self->{total_weight} = $sum;
+}
+
+# returns the configured weight percentage [0,100] of a target.
+sub weight_percentage {
+    my ($self, $target) = @_;
+    return 0 unless $self->{weights}{$target};
+    return 100 * $self->{weights}{$target} / $self->total_weight;
+}
+
+# remove all targets
+sub reset_targets {
+    my $self = shift;
+    $self->modify_targets(map { $_ => 0 } $self->targets);
+}
+
+# add/modify targets.  parameters are %weights:  $target -> $weight
+sub modify_targets {
+    my ($self, %weights) = @_;
+
+    # uncache stuff:
+    $self->{total_weight} = undef;
+    $self->{buckets}      = undef;
+
+    while (my ($target, $weight) = each %weights) {
+        if ($weight) {
+            $self->{weights}{$target} = $weight;
+        } else {
+            delete $self->{weight}{$target};
+        }
+    }
+    $self->_redo_circle;
+}
+*modify_target = \&modify_targets;
+
+sub _redo_circle {
+    my $self = shift;
+
+    my $pts = $self->{points} = {};
+    while (my ($target, $weight) = each %{$self->{weights}}) {
+        my $num_pts = $weight * 100;
+        foreach my $ptn (1..$num_pts) {
+            my $key = "$target-$ptn";
+            my $val = unpack("L", substr(sha1($key), 0, 4));
+            $pts->{$val} = \$target;
+        }
+    }
+
+    $self->{order} = [ sort { $a <=> $b } keys %$pts ];
+}
+
+# returns arrayref of 1024 buckets.  each array element is the $target for that bucket index.
+sub buckets {
+    my $self = shift;
+    return $self->{buckets} if $self->{buckets};
+    my $buckets = $self->{buckets} = [];
+    my $by = 2**22;  # 2**32 / 2**10 (1024)
+    for my $n (0..1023) {
+        my $pt = $n * $by;
+        $buckets->[$n] = $self->target_of_point($pt);
+    }
+
+    return $buckets;
+}
+
+# returns hashref of $target -> $number of occurences in 1024 buckets
+sub bucket_counts {
+    my $self = shift;
+    my $ct = {};
+    foreach my $t (@{ $self->buckets }) {
+        $ct->{$t}++;
+    }
+    return $ct;
+}
+
+# given an integer, returns $target (after modding on 1024 buckets)
+sub target_of_bucket {
+    my ($self, $bucketpos) = @_;
+    return ($self->{buckets} || $self->buckets)->[$bucketpos % 1024];
+}
+
+# given a $point [0,2**32), returns the $target that's next going around the circle
+sub target_of_point {
+    my ($self, $pt) = @_;  # $pt is 32-bit unsigned integer
+
+    my $order = $self->{order};
+    my $circle_pt = $self->{points};
+
+    my ($lo, $hi) = (0, scalar(@$order)-1);  # inclusive candidates
+
+    while (1) {
+        my $mid           = int(($lo + $hi) / 2);
+        my $val_at_mid    = $order->[$mid];
+        my $val_one_below = $mid ? $order->[$mid-1] : 0;
+
+        # match
+        return ${ $circle_pt->{$order->[$mid]} } if
+            $pt <= $val_at_mid && $pt > $val_one_below;
+
+        # wrap-around match
+        return ${ $circle_pt->{$order->[0]} } if
+            $lo == $hi;
+
+        # too low, go up.
+        if ($val_at_mid < $pt) {
+            $lo = $mid + 1;
+            $lo = $hi if $lo > $hi;
+        }
+        # too high
+        else {
+            $hi = $mid - 1;
+            $hi = $lo if $hi < $lo;
+        }
+
+        next;
+    }
+};


Property changes on: branches/multithreaded/api/perl/dev/cons-hash.pl
___________________________________________________________________
Name: svn:executable
   + *

Modified: branches/multithreaded/server/ChangeLog
===================================================================
--- branches/multithreaded/server/ChangeLog	2007-04-16 14:02:37 UTC (rev 506)
+++ branches/multithreaded/server/ChangeLog	2007-04-16 15:18:35 UTC (rev 507)
@@ -11,6 +11,9 @@
 	  null checks, adds asserts at the top of each function for any
 	  use of conn *c without checking to see if c is NULL first.
 
+        * Also adjust clean-whitespace.pl to clean *.ac files.  Add
+          script to test-suite to test for tabs.
+
 2007-04-04  Paul Lindner  <lindner at inuus.com>
 
 	* Add clarification of flush_all in the protocol docs

Modified: branches/multithreaded/server/assoc.c
===================================================================
--- branches/multithreaded/server/assoc.c	2007-04-16 14:02:37 UTC (rev 506)
+++ branches/multithreaded/server/assoc.c	2007-04-16 15:18:35 UTC (rev 507)
@@ -480,7 +480,7 @@
     primary_hashtable = malloc(hash_size);
     if (! primary_hashtable) {
         fprintf(stderr, "Failed to init hashtable.\n");
-        exit(1);
+        exit(EXIT_FAILURE);
     }
     memset(primary_hashtable, 0, hash_size);
 }

Modified: branches/multithreaded/server/assoc.h
===================================================================
--- branches/multithreaded/server/assoc.h	2007-04-16 14:02:37 UTC (rev 506)
+++ branches/multithreaded/server/assoc.h	2007-04-16 15:18:35 UTC (rev 507)
@@ -5,4 +5,3 @@
 void assoc_delete(const char *key, const size_t nkey);
 void do_assoc_move_next_bucket(void);
 uint32_t hash( const void *key, size_t length, const uint32_t initval);
-

Modified: branches/multithreaded/server/daemon.c
===================================================================
--- branches/multithreaded/server/daemon.c	2007-04-16 14:02:37 UTC (rev 506)
+++ branches/multithreaded/server/daemon.c	2007-04-16 15:18:35 UTC (rev 507)
@@ -48,7 +48,7 @@
     case 0:
         break;
     default:
-        _exit(0);
+        _exit(EXIT_SUCCESS);
     }
 
     if (setsid() == -1)

Modified: branches/multithreaded/server/items.c
===================================================================
--- branches/multithreaded/server/items.c	2007-04-16 14:02:37 UTC (rev 506)
+++ branches/multithreaded/server/items.c	2007-04-16 15:18:35 UTC (rev 507)
@@ -236,7 +236,7 @@
 
 void do_item_remove(item *it) {
     assert((it->it_flags & ITEM_SLABBED) == 0);
-    if (it->refcount) {
+    if (it->refcount != 0) {
         it->refcount--;
         DEBUG_REFCNT(it, '-');
     }

Modified: branches/multithreaded/server/items.h
===================================================================
--- branches/multithreaded/server/items.h	2007-04-16 14:02:37 UTC (rev 506)
+++ branches/multithreaded/server/items.h	2007-04-16 15:18:35 UTC (rev 507)
@@ -5,7 +5,7 @@
 void item_free(item *it);
 bool item_size_ok(const size_t nkey, const int flags, const int nbytes);
 
-int  do_item_link(item *it);    /* may fail if transgresses limits */
+int  do_item_link(item *it);     /* may fail if transgresses limits */
 void do_item_unlink(item *it);
 void do_item_remove(item *it);
 void do_item_update(item *it);   /* update LRU time to current and reposition */

Modified: branches/multithreaded/server/memcached.c
===================================================================
--- branches/multithreaded/server/memcached.c	2007-04-16 14:02:37 UTC (rev 506)
+++ branches/multithreaded/server/memcached.c	2007-04-16 15:18:35 UTC (rev 507)
@@ -97,7 +97,7 @@
                               (to avoid 64 bit time_t) */
 
 void pre_gdb(void);
-void conn_free(conn *c);
+static void conn_free(conn *c);
 
 /** exported globals **/
 struct stats stats;
@@ -1867,10 +1867,10 @@
                     if (settings.verbose > 0)
                         fprintf(stderr, "Too many open connections\n");
                     accept_new_conns(false);
-                    stop = 1;
+                    stop = true;
                 } else {
                     perror("accept()");
-                    stop = 1;
+                    stop = true;
                 }
                 break;
             }

Modified: branches/multithreaded/server/slabs.c
===================================================================
--- branches/multithreaded/server/slabs.c	2007-04-16 14:02:37 UTC (rev 506)
+++ branches/multithreaded/server/slabs.c	2007-04-16 15:18:35 UTC (rev 507)
@@ -153,7 +153,7 @@
        list.  if you really don't want this, you can rebuild without
        these three lines.  */
 
-    for(i = POWER_SMALLEST; i <= POWER_LARGEST; i++) {
+    for (i = POWER_SMALLEST; i <= POWER_LARGEST; i++) {
         if (++prealloc > maxslabs)
             return;
         do_slabs_newslab(i);

Modified: branches/multithreaded/website/apis.bml
===================================================================
--- branches/multithreaded/website/apis.bml	2007-04-16 14:02:37 UTC (rev 506)
+++ branches/multithreaded/website/apis.bml	2007-04-16 15:18:35 UTC (rev 507)
@@ -18,7 +18,10 @@
 
 <?h1 PHP API h1?>
 
-<p>There are tons of PHP libraries available, in different conditions.  The community hasn't yet decided which is the best.  When they do, let me know.  For now I'll let you search Google rather than link any.</p>
+<p>There are tons of PHP libraries available, in different conditions.  But it now seems there's an official one:</p>
+<ul>
+<li><a href="http://pecl.php.net/package/memcache">PHP PECL memcached client</a> -- official PHP client</li>
+</ul>
 
 <?h1 Python API h1?>
 




More information about the memcached-commits mailing list