memcached replication failover 구현에 대한 포스팅입니다.
memcached #1, memcached #2 각 set 후 memcached #2 replication failover
1.memcache #1 server start
1 2 3 4 5 6 |
/usr/local/memcached_rep/bin/memcached -U 0 -p 11211 -d -v -m 30720 -u root -l 0.0.0.0 -P /usr/local/memcached_rep/memcachedrep.pid -c 32767 -x 192.168.56.41 replication: connect (peer=192.168.56.41:11212) replication: marugoto copying replication: close replication: listen replication: accept |
2.telnet 192.168.56.40 11211
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
Trying 192.168.56.40... Connected to 192.168.56.40. Escape character is '^]'. stats STAT pid 3213 STAT uptime 6 STAT time 1423123557 STAT version 1.2.8 STAT pointer_size 64 STAT rusage_user 0.000294 STAT rusage_system 0.000483 STAT curr_items 0 STAT total_items 0 STAT bytes 0 STAT curr_connections 3 STAT total_connections 7 STAT connection_structures 4 STAT cmd_flush 0 STAT cmd_get 0 STAT cmd_set 0 STAT get_hits 0 STAT get_misses 0 STAT evictions 0 STAT bytes_read 7 STAT bytes_written 0 STAT limit_maxbytes 32212254720 STAT threads 2 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT replication MASTER STAT repcached_version 2.2.1 STAT repcached_qi_free 8192 STAT repcached_wdata 0 STAT repcached_wsize 0 END |
3.memcache #2 server start
1 2 3 4 |
/usr/local/memcached_rep/bin/memcached -U 0 -p 11211 -d -v -m 30720 -u root -l 0.0.0.0 -P /usr/local/memcached_rep/memcachedrep.pid -c 32767 -x 192.168.56.40 replication: connect (peer=192.168.56.40:11212) replication: marugoto copying replication: start |
telnet 192.168.56.41 11211
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
Trying 192.168.56.41... Connected to 192.168.56.41. Escape character is '^]'. stats STAT pid 3256 STAT uptime 6 STAT time 1423123567 STAT version 1.2.8 STAT pointer_size 64 STAT rusage_user 0.000370 STAT rusage_system 0.000474 STAT curr_items 0 STAT total_items 0 STAT bytes 0 STAT curr_connections 5 STAT total_connections 6 STAT connection_structures 6 STAT cmd_flush 0 STAT cmd_get 0 STAT cmd_set 0 STAT get_hits 0 STAT get_misses 0 STAT evictions 0 STAT bytes_read 21 STAT bytes_written 0 STAT limit_maxbytes 32212254720 STAT threads 2 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT replication MASTER STAT repcached_version 2.2.1 STAT repcached_qi_free 8192 STAT repcached_wdata 0 STAT repcached_wsize 2048 END |
4.set get php sample
vi mt.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
<!--?php class memcachedTools { public $filename = 'memcacheData.txt'; public $fileMode = FILE_APPEND; public $host = '127.0.0.1'; public $port = '11211'; public $memcached = null; public $list = array(); public $limit = 10000000000000; public $setlimitcount = '1000'; public function __construct($host = '127.0.0.1', $port = '11211' , $setlimitcount = 1000) { $this->memcached = new Memcache(); $this->memcached->connect($host, $port); $this->setlimitcount = $setlimitcount; } function getKeysPrint() { $i=0; foreach ($this->list as $row) { $value = $this->memcached->get($row['key']); $time = time(); $i++; var_dump("[".$i."]{key : " . $row['key'] . " , age : " . $row['age'] . " , val : " . $value . "}"); } var_dump(" Total items : " . $i); } public function setKeysToMemcached($setlimitcount = 1000) { $setlimitcount = $this->setlimitcount; var_dump("{key : " . $setlimitcount); for($i=0;$i<$setlimitcount;$i++){ $key = md5(rand(time (), true)); $val = md5(rand(time (), true)); $age = time() + 300; $this->memcached->set($key, $val, 0 , $age); } } public function getAllKeys() { $allSlabs = $this->memcached->getExtendedStats('slabs'); $items = $this->memcached->getExtendedStats('items'); foreach ($allSlabs as $server => $slabs) { foreach ($slabs as $slabId => $slabMeta) { if (!is_numeric($slabId)) { continue; } $cdump = $this->memcached->getExtendedStats('cachedump', (int)$slabId, $this->limit); foreach ($cdump as $server => $entries) { if (!$entries) { continue; } foreach ($entries as $eName => $eData) { $this->list[$eName] = array( 'key' => $eName, 'slabId' => $slabId, 'size' => $eData[0], 'age' => $eData[1] ); } } } } ksort($this->list); } } $host = '127.0.0.1'; $port = '11211'; $allowedArgs = array('-h' => 'host', '-p' => 'port', '-op' => 'action', '-c' => 'setlimitcount'); foreach ($allowedArgs as $key => $val) { $id = array_search($key, $argv); if ($id) { ${$val} = isset($argv[$id + 1]) ? $argv[$id + 1] : false; } } $obj = new memcachedTools($host, $port , $setlimitcount); switch ($action) { case 'get': $obj->getAllKeys(); $obj->getKeysPrint(); echo "Memcached get END \n" ; break; case 'set': $obj->setKeysToMemcached(); echo "Memcached set END \n" ; break; default: echo <<<EOF Example get Usage : php set.php -h 127.0.0.1 -p 11211 -op get Example set Usage : php set.php -h 127.0.0.1 -p 11211 -op set -c 10000 -h : Memcache Host address ( default is 127.0.0.1 ) -p : Memcache Port ( default is 11211 ) -p : Operation is required !! ( available options is : set , get ) -c : Memcache set count ( default is 1000 ) EOF; break; } exit; </pre> <p>5. memcache #1 set</p> <pre class="lang:sh"> php set.php -h 127.0.0.1 -p 11211 -op set -c 10000 string(12) "{key : 10000" Memcached set END </pre> <p>memcache #1 stats</p> <pre class="lang:sh"> stats STAT pid 3213 STAT uptime 351 STAT time 1423123902 STAT version 1.2.8 STAT pointer_size 64 STAT rusage_user 0.057354 STAT rusage_system 0.173153 STAT curr_items 10000 STAT total_items 10000 STAT bytes 1300000 STAT curr_connections 5 STAT total_connections 11 STAT connection_structures 7 STAT cmd_flush 0 STAT cmd_get 0 STAT cmd_set 10000 STAT get_hits 0 STAT get_misses 0 STAT evictions 0 STAT bytes_read 880014 STAT bytes_written 80667 STAT limit_maxbytes 32212254720 STAT threads 2 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT replication MASTER STAT repcached_version 2.2.1 STAT repcached_qi_free 8154 STAT repcached_wdata 0 STAT repcached_wsize 4096 END </pre> <p>memcache #2 stats</p> <pre class="lang:sh"> stats STAT pid 3256 STAT uptime 382 STAT time 1423123943 STAT version 1.2.8 STAT pointer_size 64 STAT rusage_user 0.039632 STAT rusage_system 0.117524 STAT curr_items 10000 STAT total_items 10000 STAT bytes 1300000 STAT curr_connections 5 STAT total_connections 6 STAT connection_structures 6 STAT cmd_flush 0 STAT cmd_get 0 STAT cmd_set 10000 STAT get_hits 0 STAT get_misses 0 STAT evictions 0 STAT bytes_read 928922 STAT bytes_written 671 STAT limit_maxbytes 32212254720 STAT threads 2 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT replication MASTER STAT repcached_version 2.2.1 STAT repcached_qi_free 8192 STAT repcached_wdata 0 STAT repcached_wsize 2048 END </pre> <p>6. memcache #2 set</p> <pre class="lang:sh"> php set.php -h 127.0.0.1 -p 11211 -op set -c 20000 string(12) "{key : 20000" Memcached set END </pre> <p>memcache #1 stats</p> <pre class="lang:sh"> stats STAT pid 3213 STAT uptime 484 STAT time 1423124035 STAT version 1.2.8 STAT pointer_size 64 STAT rusage_user 0.218363 STAT rusage_system 0.917973 STAT curr_items 30000 STAT total_items 30000 STAT bytes 3900000 STAT curr_connections 5 STAT total_connections 11 STAT connection_structures 7 STAT cmd_flush 0 STAT cmd_get 0 STAT cmd_set 30000 STAT get_hits 0 STAT get_misses 0 STAT evictions 0 STAT bytes_read 2748915 STAT bytes_written 81367 STAT limit_maxbytes 32212254720 STAT threads 2 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT replication MASTER STAT repcached_version 2.2.1 STAT repcached_qi_free 8154 STAT repcached_wdata 0 STAT repcached_wsize 4096 END </pre> <p>memcache #2 stats</p> <pre class="lang:sh"> stats STAT pid 3256 STAT uptime 483 STAT time 1423124044 STAT version 1.2.8 STAT pointer_size 64 STAT rusage_user 0.268240 STAT rusage_system 0.722110 STAT curr_items 30000 STAT total_items 30000 STAT bytes 3900000 STAT curr_connections 5 STAT total_connections 7 STAT connection_structures 7 STAT cmd_flush 0 STAT cmd_get 0 STAT cmd_set 30000 STAT get_hits 0 STAT get_misses 0 STAT evictions 0 STAT bytes_read 2688929 STAT bytes_written 161368 STAT limit_maxbytes 32212254720 STAT threads 2 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT replication MASTER STAT repcached_version 2.2.1 STAT repcached_qi_free 8154 STAT repcached_wdata 0 STAT repcached_wsize 4096 END </pre> <p>7. memcache #2 kill</p> <pre class="lang:sh"> webadm@ubuntu:~$ ps -elf|grep memcache 1 S webadm 3256 1 0 80 0 - 3895 ep_pol 17:06 ? 00:00:00 /usr/local/memcached_rep/bin/memcached -U 0 -p 11211 -d -v -m 30720 -u root -l 0.0.0.0 -P /usr/local/memcached_rep/memcachedrep.pid -c 32767 -x 192.168.56.40 0 S webadm 3374 3203 0 80 0 - 2616 pipe_w 17:14 pts/3 00:00:00 grep --color=auto memcache webadm@ubuntu:~$ kill -9 3256 </pre> <p>8. memcache #2 start</p> <pre class="lang:sh"> /usr/local/memcached_rep/bin/memcached -U 0 -p 11211 -d -v -m 30720 -u root -l 0.0.0.0 -P /usr/local/memcached_rep/memcachedrep.pid -c 32767 -x 192.168.56.40 webadm@ubuntu:~$ Could not open the pid file /usr/local/memcached_rep/memcachedrep.pid for writing replication: connect (peer=192.168.56.40:11212) replication: marugoto copying replication: start </pre> <p>9. memcache #2 stats<br ?--> telnet 192.168.56.41 11211 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
Trying 192.168.56.41... Connected to 192.168.56.41. Escape character is '^]'. stats STAT pid 3390 STAT uptime 30 STAT time 1423124181 STAT version 1.2.8 STAT pointer_size 64 STAT rusage_user 0.171504 STAT rusage_system 1.626436 STAT curr_items 30000 STAT total_items 30000 STAT bytes 3900000 STAT curr_connections 5 STAT total_connections 6 STAT connection_structures 6 STAT cmd_flush 0 STAT cmd_get 0 STAT cmd_set 30000 STAT get_hits 0 STAT get_misses 0 STAT evictions 0 STAT bytes_read 2797809 STAT bytes_written 0 STAT limit_maxbytes 32212254720 STAT threads 2 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT replication MASTER STAT repcached_version 2.2.1 STAT repcached_qi_free 8192 STAT repcached_wdata 0 STAT repcached_wsize 2048 END |
memcache #2 down시 memcache #1에서 set 에 대한 테스트
10. memcache #2 kill
1 2 3 4 |
ps -elf|grep memcache 1 S webadm 3390 1 1 80 0 - 3896 ep_pol 17:15 ? 00:00:01 /usr/local/memcached_rep/bin/memcached -U 0 -p 11211 -d -v -m 30720 -u root -l 0.0.0.0 -P /usr/local/memcached_rep/memcachedrep.pid -c 32767 -x 192.168.56.40 0 S webadm 3414 3203 0 80 0 - 2616 pipe_w 17:17 pts/3 00:00:00 grep --color=auto memcache webadm@ubuntu:~$ kill -9 3390 |
11. memcache #1 set
1 2 3 |
php set.php -h 127.0.0.1 -p 11211 -op set -c 20000 string(12) "{key : 20000" Memcached set END |
12. memcache #1 stats
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
stats STAT pid 3213 STAT uptime 757 STAT time 1423124308 STAT version 1.2.8 STAT pointer_size 64 STAT rusage_user 0.540510 STAT rusage_system 2.861457 STAT curr_items 50000 STAT total_items 50000 STAT bytes 6500000 STAT curr_connections 3 STAT total_connections 17 STAT connection_structures 7 STAT cmd_flush 0 STAT cmd_get 0 STAT cmd_set 50000 STAT get_hits 0 STAT get_misses 0 STAT evictions 0 STAT bytes_read 4508922 STAT bytes_written 242068 STAT limit_maxbytes 32212254720 STAT threads 2 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT replication MASTER STAT repcached_version 2.2.1 STAT repcached_qi_free 8154 STAT repcached_wdata 0 STAT repcached_wsize 0 END |
13. memcache #2 start
1 2 3 4 5 |
/usr/local/memcached_rep/bin/memcached -U 0 -p 11211 -d -v -m 30720 -u root -l 0.0.0.0 -P /usr/local/memcached_rep/memcachedrep.pid -c 32767 -x 192.168.56.40 webadm@ubuntu:~$ Could not open the pid file /usr/local/memcached_rep/memcachedrep.pid for writing replication: connect (peer=192.168.56.40:11212) replication: marugoto copying replication: start |
14. memcache #2 stats
telnet 192.168.56.41 11211
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
Trying 192.168.56.41... Connected to 192.168.56.41. Escape character is '^]'. stats STAT pid 3440 STAT uptime 15 STAT time 1423124382 STAT version 1.2.8 STAT pointer_size 64 STAT rusage_user 0.111554 STAT rusage_system 1.067313 STAT curr_items 20000 ### mamcache #2 down시 memcache#1에서 set한 20000건은??? STAT total_items 20000 ### STAT bytes 2600000 STAT curr_connections 5 STAT total_connections 6 STAT connection_structures 6 STAT cmd_flush 0 STAT cmd_get 0 STAT cmd_set 20000 STAT get_hits 0 STAT get_misses 0 STAT evictions 0 STAT bytes_read 1880021 STAT bytes_written 0 STAT limit_maxbytes 32212254720 STAT threads 2 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT replication MASTER STAT repcached_version 2.2.1 STAT repcached_qi_free 8192 STAT repcached_wdata 0 STAT repcached_wsize 2048 END |
15. memcache #1 stats
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
stats STAT pid 3213 STAT uptime 891 STAT time 1423124442 STAT version 1.2.8 STAT pointer_size 64 STAT rusage_user 0.721090 STAT rusage_system 4.069219 STAT curr_items 20000 ### mamcache #2 down시 memcache#1에서 set한 20000건은??? STAT total_items 50000 ### STAT bytes 2600000 STAT curr_connections 5 STAT total_connections 20 STAT connection_structures 7 STAT cmd_flush 0 STAT cmd_get 0 STAT cmd_set 50000 STAT get_hits 0 STAT get_misses 0 STAT evictions 0 STAT bytes_read 4508929 STAT bytes_written 242767 STAT limit_maxbytes 32212254720 STAT threads 2 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT replication MASTER STAT repcached_version 2.2.1 STAT repcached_qi_free 8154 STAT repcached_wdata 0 STAT repcached_wsize 2048 END |
위의 상태에서 memcache #1 down시 에 대한 테스트
16. memcache #1 down
1 2 3 4 5 6 7 8 9 |
ps -elf|grep memcache 1 S webadm 3213 1 0 80 0 - 4757 ep_pol 17:05 ? 00:00:04 /usr/local/memcached_rep/bin/memcached -U 0 -p 11211 -d -v -m 30720 -u root -l 0.0.0.0 -P /usr/local/memcached_rep/memcachedrep.pid -c 32767 -x 192.168.56.41 0 S webadm 3523 1290 0 80 0 - 2616 pipe_w 17:29 pts/0 00:00:00 grep --color=auto memcache webadm@ubuntu:~$ kill -9 3213 webadm@ubuntu:~$ /usr/local/memcached_rep/bin/memcached -U 0 -p 11211 -d -v -m 30720 -u root -l 0.0.0.0 -P /usr/local/memcached_rep/memcachedrep.pid -c 32767 -x 192.168.56.41 webadm@ubuntu:~$ Could not open the pid file /usr/local/memcached_rep/memcachedrep.pid for writing replication: connect (peer=192.168.56.41:11212) replication: marugoto copying replication: start |
17. memcache #2 stats
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
stats STAT pid 3440 STAT uptime 639 STAT time 1423125006 STAT version 1.2.8 STAT pointer_size 64 STAT rusage_user 0.124990 STAT rusage_system 1.096932 STAT curr_items 0 ### memcache #1 down시 모든 data 초기화됨 STAT total_items 20000 STAT bytes 0 STAT curr_connections 5 STAT total_connections 10 STAT connection_structures 6 STAT cmd_flush 0 STAT cmd_get 0 STAT cmd_set 20000 STAT get_hits 0 STAT get_misses 0 STAT evictions 0 STAT bytes_read 1880063 STAT bytes_written 4184 STAT limit_maxbytes 32212254720 STAT threads 2 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT replication MASTER STAT repcached_version 2.2.1 STAT repcached_qi_free 8191 STAT repcached_wdata 0 STAT repcached_wsize 2048 END |
18. memcache #2 stats
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
stats STAT pid 3529 STAT uptime 11 STAT time 1423124998 STAT version 1.2.8 STAT pointer_size 64 STAT rusage_user 0.000476 STAT rusage_system 0.000583 STAT curr_items 0 ### memcache #1 down시 모든 data 초기화됨 STAT total_items 0 STAT bytes 0 STAT curr_connections 5 STAT total_connections 6 STAT connection_structures 6 STAT cmd_flush 0 STAT cmd_get 0 STAT cmd_set 0 STAT get_hits 0 STAT get_misses 0 STAT evictions 0 STAT bytes_read 21 STAT bytes_written 0 STAT limit_maxbytes 32212254720 STAT threads 2 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT replication MASTER STAT repcached_version 2.2.1 STAT repcached_qi_free 8192 STAT repcached_wdata 0 STAT repcached_wsize 2048 END |