Commit 670c4e7c authored by robertDouglass's avatar robertDouglass
Browse files

rewrote the server cluster code. Adding memcache.module. Added some tests...

rewrote the server cluster code. Adding memcache.module. Added some tests (warning... sloppy code in the tests =)
parent 157a03c4
......@@ -84,6 +84,16 @@ function dmemcache_stats($bin = 'default') {
return $mc->getStats();
}
}
/**
$conf['memcache'][] = array(
'#servers' => array('localhost:11211', 'localhost:11212'),
'#bins' => array('default'),
);
$conf['memcache'][] = array(
'#servers' => array('localhost:11212'),
'#bins' => array('alias', 'src'),
);
*/
/**
* Returns an Memcache object based on the bin requested. Note that there is nothing preventing
......@@ -102,6 +112,9 @@ function dmemcache_object($bin = 'default', $flush = FALSE) {
static $caches;
if ($flush) {
foreach ($caches as $cluster) {
$cluster->close();
}
unset($caches);
}
......@@ -109,28 +122,39 @@ function dmemcache_object($bin = 'default', $flush = FALSE) {
// initialize the static cache
$caches = array();
if (isset($conf['memcache_servers'])) {
foreach ($conf['memcache_servers'] as $bin_name => $hosts) {
if (!isset($caches[$bin_name])) {
$caches[$bin_name] = new Memcache;
}
foreach ($hosts as $host => $ports) {
foreach ($ports as $port) {
// NOTE: We add the server with the persistent flag set to TRUE.
// According to the documentation, this will have no effect if the memcache extension
// is loaded dynamically. If your application is generating too many memcache
// connections, you need to compile PHP with the memcache extension.
$caches[$bin_name]->addServer($host, $port);
if (isset($conf['memcache'])) {
// Each cluster is a list of host:port and bin groups
foreach ($conf['memcache'] as $cluster_config) {
$cluster = new Memcache;
$first_server = TRUE;
foreach ($cluster_config['#servers'] as $server) {
list($host, $port) = explode(':', $server);
if ($first_server) {
$cluster->connect($host, $port);
$first_server = FALSE;
}
else {
$cluster->addServer($host, $port);
}
}
// Now that the $cluster has all of its servers connected and added
// we cycle through the bins and map this cluster to each of them.
// This allows the retrieval of the right cluster for each bin later.
// Note: It is the administrator's responsibility to make sure the $conf
// array is structured correctly, ie no clusters without bins and a mapping
// for the default bin.
foreach ($cluster_config['#bins'] as $bin_name) {
$caches[$bin_name] = &$cluster;
}
}
}
// If $conf['memcache_servers'] was not set, we try to initialize the default server
// If $conf['memcache'] was not set, we try to initialize the default server
else {
$mc = new Memcache;
$mc->connect('localhost', '11211');
if ($mc->getServerStatus('localhost', 11211)) {
$caches['default'] = $mc;
$cluster = new Memcache;
$cluster->connect('localhost', '11211');
if ($cluster->getServerStatus('localhost', 11211)) {
$caches['default'] = &$cluster;
}
}
}
......
......@@ -18,11 +18,8 @@ print "</ul>";
global $conf;
$conf = array(
'memcache_servers' => array('default' => array('localhost' => array(11211)),
),
);
// by not defining any $conf for memcache we're essentially saying "give me localhost:11211"
// and only the 'default' bin will be active.
##############
// Begin Tests
......@@ -33,10 +30,13 @@ $conf = array(
printHeader();
$mc = dmemcache_object();
formatStats($mc->getStats());
$mc->close();
unset($mc);
// Test 2. Set a number of keys and retrieve their values
#########################################################
printHeader();
$mc = dmemcache_object();
$keys = array('a', time(), 'http://www.robshouse.net/home/page?q=xyz&p=x', 'What about space?');
print '<ol>';
......@@ -44,11 +44,14 @@ foreach ($keys as $key) {
testKeyValue($mc, $key, 'Hi Robert');
}
print '</ol>';
$mc->close();
unset($mc);
// Test 3. Set a number of PROBLEMATIC keys and retrieve their values
#####################################################################
printHeader();
$mc = dmemcache_object();
print '<ol>';
$key = ' ';
testKeyValue($mc, $key, 'Hi Dude');
......@@ -58,28 +61,39 @@ testKeyValue($mc, $key, 'Hi Dude 2');
print '<li><em>space and line break different?</em>='. $mc->get(' ').'</li>';
print '</ol>';
$mc->close();
unset($mc);
// Test 4. Test flushing the $mc object from dmemcache_object
#############################################################
printHeader();
$mc = dmemcache_object();
formatStats($mc->getStats());
$conf = array(
'memcache_servers' => array('default' => array('localhost' => array(11212)),
),
$conf['memcache'][] = array(
'#servers' => array('localhost:11212'),
'#bins' => array('default'),
);
$mc->close();
unset($mc);
$mc = dmemcache_object('default', TRUE);
formatStats($mc->getStats());
$mc->close();
unset($mc);
// Test 5. Confirm that space and line break are treated as the same character
##############################################################################
printHeader();
$mc = dmemcache_object();
$mc->set("\n", "This is a new line", FALSE, 5);
print '<ol>';
print '<li>'. $mc->get("\n"). '</li>';
print '<li>'. $mc->get(" "). '</li>';
print '</ol>';
$mc->close();
unset($mc);
// Test 6. Try out dmemcache_set and dmemcache_get
##################################################
......@@ -107,41 +121,24 @@ print '</ol>';
printHeader();
// Set up $conf so that both available servers map to default
$GLOBALS['conf']['memcache_servers'] = array('default' => array('localhost' => array(11211, 11212)),
$conf['memcache'][] = array(
'#servers' => array('localhost:11211', 'localhost:11212'),
'#bins' => array('default'),
);
// clear the $mc object
dmemcache_object('default', TRUE);
// make independent connections so we can display stats
// make independent connections so we can display stats. These will be used in the next 3 tests.
$mc1 = new Memcache;
$mc1->connect('localhost', 11211);
$mc2 = new Memcache;
$mc2->connect('localhost', 11212);
// helper function specific to this test
function formatStats2($mc1, $mc2, $flush = FALSE) {
static $count, $first;
$stats1 = $mc1->getStats();
$stats2 = $mc2->getStats();
if ($flush) {
unset($first);
}
if (!isset($first)) {
$count = 1;
$first = FALSE;
print "<table border='1'><tr><th> </th><th>Server 1</th><th>Server 2</th></tr>";
}
print "<tr><td>$count</td><td>". $stats1['bytes']. "</td><td>". $stats2['bytes']. "</td></tr>";
$count++;
}
$last_key = $last_value = '';
$time = time();
$time = microtime();
for ($i = 1; $i < 10001; $i++) {
$last_key = $time.$i. 'key';
$last_value = 'Some very random thoughts about things in general'. $time;
......@@ -154,6 +151,7 @@ for ($i = 1; $i < 10001; $i++) {
print "</table>";
// Test 9. Try using flush to clear servers
// Conclusion: It probably works, except that it doesn't actuall clear the memory.
// It only sets it as invalid so that it gets overwritten.
......@@ -166,6 +164,8 @@ dmemcache_flush();
formatStats2($mc1, $mc2, TRUE);
print "</table>";
// Test 10. See what the extended stats offer
#############################################
printHeader();
......@@ -182,6 +182,12 @@ foreach ($types as $type) {
print "</pre>";
// done with mc1 and mc2
$mc1->close();
$mc2->close();
unset($mc1);
unset($mc2);
// Test 11. Test delete.
########################
printHeader();
......@@ -191,6 +197,141 @@ print "<h2>". dmemcache_get('delete me'). "</h2>";
dmemcache_delete('delete me');
print "<h2>Nothing here ---->". dmemcache_get('delete me'). "<--</h2>";
// Test 12. Save things to different bins
#########################################
printHeader();
$mc1 = new Memcache;
$mc1->connect('localhost', 11211);
$mc2 = new Memcache;
$mc2->connect('localhost', 11212);
// Set up two clusters with four bins
$conf['memcache'][] = array(
'#servers' => array('localhost:11211'),
'#bins' => array('default', 'antwerp'),
);
$conf['memcache'][] = array(
'#servers' => array('localhost:11212'),
'#bins' => array('vancouver', 'barcelona'),
);
// flush the cluster cache
dmemcache_object('', TRUE);
$first = TRUE;
print "<h3>Bin: default</h3>";
$last_key = $last_value = '';
$time = microtime();
for ($i = 1; $i < 10001; $i++) {
$last_key = $time.$i. 'key';
$last_value = 'default '. $i;
dmemcache_set($last_key, $last_value, 20, 'default');
if ($i % 1000 == 1) {
$cluster = dmemcache_object('default');
print_r($cluster);
formatStats2($mc1, $mc2, $first);
$first = FALSE;
flush();
$keyin_keyout[] = array('orig' => $last_value, 'cache' => dmemcache_get($last_key, 'default'));
}
}
print "</table>";
print "<h4>Values for default</h4>";
print '<table border="1"><tr><th>Original</th><th>Cached</th></tr>';
foreach ($keyin_keyout as $values) {
print "<tr><td>". $values['orig']. "</td><td>". $values['cache']. "</td></tr>";
}
print "</table>";
print "<h3>Bin: vancouver</h3>";
$first = TRUE;
$keyin_keyout = array();
$last_key = $last_value = '';
$time = microtime();
for ($i = 1; $i < 10001; $i++) {
$last_key = $time.$i. 'key';
$last_value = 'vancouver '. $i;
dmemcache_set($last_key, $last_value, 20, 'vancouver');
if ($i % 1000 == 1) {
$cluster = dmemcache_object('vancouver');
print_r($cluster);
formatStats2($mc1, $mc2, $first);
$first = FALSE;
flush();
$keyin_keyout[] = array('orig' => $last_value, 'cache' => dmemcache_get($last_key, 'vancouver'));
}
}
print "</table>";
print "<h4>Values for vancouver</h4>";
print '<table border="1"><tr><th>Original</th><th>Cached</th></tr>';
foreach ($keyin_keyout as $values) {
print "<tr><td>". $values['orig']. "</td><td>". $values['cache']. "</td></tr>";
}
print "</table>";
print "<h3>Bin: barcelona</h3>";
$first = TRUE;
$keyin_keyout = array();
$last_key = $last_value = '';
$time = microtime();
for ($i = 1; $i < 10001; $i++) {
$last_key = $time.$i. 'key';
$last_value = 'barcelona '. $i;
dmemcache_set($last_key, $last_value, 20, 'barcelona');
if ($i % 1000 == 1) {
$cluster = dmemcache_object('barcelona');
print_r($cluster);
formatStats2($mc1, $mc2, $first);
$first = FALSE;
flush();
$keyin_keyout[] = array('orig' => $last_value, 'cache' => dmemcache_get($last_key, 'barcelona'));
}
}
print "</table>";
print "<h4>Values for barcelona</h4>";
print '<table border="1"><tr><th>Original</th><th>Cached</th></tr>';
foreach ($keyin_keyout as $values) {
print "<tr><td>". $values['orig']. "</td><td>". $values['cache']. "</td></tr>";
}
print "</table>";
print "<h3>Bin: antwerp</h3>";
$first = TRUE;
$keyin_keyout = array();
$last_key = $last_value = '';
$time = microtime();
for ($i = 1; $i < 10001; $i++) {
$last_key = $time.$i. 'key';
$last_value = 'antwerp '. $i;
dmemcache_set($last_key, $last_value, 20, 'antwerp');
if ($i % 1000 == 1) {
$cluster = dmemcache_object('antwerp');
print_r($cluster);
formatStats2($mc1, $mc2, $first);
$first = FALSE;
flush();
$keyin_keyout[] = array('orig' => $last_value, 'cache' => dmemcache_get($last_key, 'antwerp'));
}
}
print "</table>";
print "<h4>Values for antwerp</h4>";
print '<table border="1"><tr><th>Original</th><th>Cached</th></tr>';
foreach ($keyin_keyout as $values) {
print "<tr><td>". $values['orig']. "</td><td>". $values['cache']. "</td></tr>";
}
print "</table>";
$mc1->close();
$mc2->close();
unset($mc1);
unset($mc2);
// Test 13. View the globaldebug messages
#########################################
printHeader();
###################
......@@ -224,4 +365,22 @@ function printHeader() {
print "<a name='$count'><h2>Test ". $count++. "</h2></a>";
}
function formatStats2($mc1, $mc2, $flush = FALSE) {
static $count, $first;
$stats1 = $mc1->getStats();
$stats2 = $mc2->getStats();
if ($flush) {
unset($first);
}
if (!isset($first)) {
$count = 1;
$first = FALSE;
print "<table border='1'><tr><th> </th><th>Server 1</th><th>Server 2</th></tr>";
}
print "<tr><td>$count</td><td>". $stats1['bytes']. "</td><td>". $stats2['bytes']. "</td></tr>";
$count++;
}
?>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment