ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
memcacheSync.php
Go to the documentation of this file.
1 #!/usr/bin/env php
2 <?php
3 
4 
5 // Check that the memcache library is enabled
6 if(!class_exists('Memcache') && !class_exists('Memcached')) {
7  echo("Error: the memcached (or memcache) PHP extension appears to be unavailable.\n");
8  echo("\n");
9  echo("This is most likely because PHP doesn't load it for the command line\n");
10  echo("version. You probably need to enable it somehow.\n");
11  echo("\n");
12  if(is_executable('/usr/sbin/phpenmod')) {
13  echo("It is possible that running one of the following commands as root will fix it:\n");
14  echo(" phpenmod -s cli memcached\n");
15  echo(" phpenmod -s cli memcache\n");
16  }
17 
18  exit(1);
19 }
20 
21 // This is the base directory of the SimpleSAMLphp installation
22 $baseDir = dirname(dirname(__FILE__));
23 
24 // Add library autoloader
25 require_once($baseDir.'/lib/_autoload.php');
26 
27 // Initialize the configuration
28 $configdir = SimpleSAML\Utils\Config::getConfigDir();
30 
31 // Things we should warn the user about
34 
35 // We use the stats interface to determine which servers exists
37 
39 foreach ($stats as $group) {
40  foreach ($group as $server => $state) {
41 
42  if ($state === false) {
43  echo("WARNING: Server ".$server." is down.\n");
45  continue;
46  }
47 
48  $items = $state['curr_items'];
49  echo("Server ".$server." has ".$items." items.\n");
50  $serverKeys = getServerKeys($server);
51  $keys = array_merge($keys, $serverKeys);
52  }
53 }
54 
55 echo("Total number of keys: ".count($keys)."\n");
56 $keys = array_unique($keys);
57 echo("Total number of unique keys: ".count($keys)."\n");
58 
59 echo("Starting synchronization.\n");
60 
62 $sync = 0;
63 foreach ($keys as $key) {
65  if ($res === null) {
66  $skipped += 1;
67  } else {
68  $sync += 1;
69  }
70 }
71 
72 
73 echo("Synchronization done.\n");
74 echo($sync." keys in sync.\n");
75 if ($skipped > 0) {
76  echo($skipped." keys skipped.\n");
77  echo("Keys are skipped because they are either expired, or are of a type unknown\n");
78  echo("to SimpleSAMLphp.\n");
79 }
80 
81 if ($warnServerDown > 0) {
82  echo("WARNING: ".$warnServerDown." server(s) down. Not all servers are synchronized.\n");
83 }
84 
85 if ($warnBigSlab > 0) {
86  echo("WARNING: ".$warnBigSlab." slab(s) may have contained more keys than we were told about.\n");
87 }
88 
97 {
98  $server = explode(':', $server);
99  $host = $server[0];
100  $port = (int) $server[1];
101 
102  echo("Connecting to: ".$host.":".$port."\n");
103  $socket = fsockopen($host, $port);
104  echo("Connected. Finding keys.\n");
105 
106  if (fwrite($socket, "stats slabs\r\n") === false) {
107  echo("Error requesting slab dump from server.\n");
108  exit(1);
109  }
110 
111  // Read list of slabs
112  $slabs = array();
113  while (($line = fgets($socket)) !== false) {
114  $line = rtrim($line);
115  if ($line === 'END') {
116  break;
117  }
118 
119  if (preg_match('/^STAT (\d+):/', $line, $matches)) {
120  $slab = (int) $matches[1];
121  if (!in_array($slab, $slabs, true)) {
122  $slabs[] = $slab;
123  }
124  }
125  }
126 
127  // Dump keys in slabs
128  $keys = array();
129  foreach ($slabs as $slab) {
130 
131  if (fwrite($socket, "stats cachedump ".$slab." 1000000\r\n") === false) {
132  echo("Error requesting cache dump from server.\n");
133  exit(1);
134  }
135 
136  /* We keep track of the result size, to be able to warn the user if it is
137  * so big that keys may have been lost.
138  */
139  $resultSize = 0;
140 
141  while (($line = fgets($socket)) !== false) {
142  $resultSize += strlen($line);
143 
144  $line = rtrim($line);
145  if ($line === 'END') {
146  break;
147  }
148 
149  if (preg_match('/^ITEM (.*) \[\d+ b; \d+ s\]/', $line, $matches)) {
150  $keys[] = $matches[1];
151  } else {
152  echo("Unknown result from cache dump: ".$line."\n");
153  }
154  }
155  if ($resultSize > 1900000 || count($keys) >= 1000000) {
156  echo("WARNING: Slab ".$slab." on server ".$host.":".$port.
157  " may have contained more keys than we were told about.\n");
158  $GLOBALS['warnBigSlab'] += 1;
159  }
160  }
161 
162  echo("Found ".count($keys)." key(s).\n");
163  fclose($socket);
164 
165  return $keys;
166 }
static getRawStats()
Retrieve statistics directly in the form returned by getExtendedStats, for all server groups...
Definition: Memcache.php:482
$warnBigSlab
$warnServerDown
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
$configdir
static get($key)
Find data stored with a given key.
Definition: Memcache.php:46
$keys
static setConfigDir($path, $configSet='simplesaml')
Set the directory for configuration files for the given configuration set.
if(!class_exists('Memcache') &&!class_exists('Memcached')) $baseDir
if(!array_key_exists('stateid', $_REQUEST)) $state
Handle linkback() response from LinkedIn.
Definition: linkback.php:10
foreach($_POST as $key=> $value) $res
$stats
$sync
if($skipped > 0) if($warnServerDown > 0) if($warnBigSlab > 0) getServerKeys($server)
Fetch all keys available in an server.
Create styles array
The data for the language used.
$server
Definition: getUserInfo.php:12
$skipped
$key
Definition: croninfo.php:18