From 97dd556e06c5795cb68e2cb85a3b415ece75ea2f Mon Sep 17 00:00:00 2001 From: anaumenko Date: Wed, 18 Apr 2012 15:54:22 +0400 Subject: [PATCH 01/22] Adding main classes --- core/Base/LazyObject.class.php | 45 +++++++++++++ core/Cache/SequentialCache.class.php | 77 ++++++++++++++++++++++ test/core/SequentialCacheTest.class.php | 88 +++++++++++++++++++++++++ 3 files changed, 210 insertions(+) create mode 100644 core/Base/LazyObject.class.php create mode 100644 core/Cache/SequentialCache.class.php create mode 100644 test/core/SequentialCacheTest.class.php diff --git a/core/Base/LazyObject.class.php b/core/Base/LazyObject.class.php new file mode 100644 index 0000000000..10760db666 --- /dev/null +++ b/core/Base/LazyObject.class.php @@ -0,0 +1,45 @@ +callback = $constructor; + } + + public static function create($constructor) + { + return new self($constructor); + } + + protected function ensureInnerInited() + { + if ($this->inner) + return; + + $this->inner = call_user_func($this->callback); + + if (!is_object($this->inner)) { + throw new RuntimeException("Callback returns not object"); + } + + return $this; + } + + public function __call($function, $args) + { + $this->ensureInnerInited(); + + return call_user_func_array(array($this->inner, $function), $param_arr); + } + } \ No newline at end of file diff --git a/core/Cache/SequentialCache.class.php b/core/Cache/SequentialCache.class.php new file mode 100644 index 0000000000..fc120f42bf --- /dev/null +++ b/core/Cache/SequentialCache.class.php @@ -0,0 +1,77 @@ +addPeer($cache); + } + } + + public function addPeer($peer) { + $this->list[] = $peer; + + return $this; + } + + public function get($key) { + foreach ($this->list as $val) { + /** + * @var $cache CachePeer + */ + try { + if ($val->isAlive()) { + $result = $val->get($key); + if ($val->isAlive()) { + return $result; + } + } + } catch (Exception $e) { + //go next... + } + } + throw new RuntimeException("All peers are dead"); + } + + protected function store($action, $key, $value, $expires = Cache::EXPIRES_MEDIUM){ + return $this->foreachItem(__METHOD__, func_get_args()); + } + + public function append($key, $data) { + return $this->foreachItem(__METHOD__, func_get_args()); + } + + public function decrement($key, $value) { + return $this->foreachItem(__METHOD__, func_get_args()); + } + + public function delete($key) { + return $this->foreachItem(__METHOD__, func_get_args()); + } + + + public function increment($key, $value) { + return $this->foreachItem(__METHOD__, func_get_args()); + } + + private function foreachItem($method, $args){ + foreach ($this->list as $val) { + call_user_func_array(array($val['obj'], $method), $args); + } + + return $this; + } + } \ No newline at end of file diff --git a/test/core/SequentialCacheTest.class.php b/test/core/SequentialCacheTest.class.php new file mode 100644 index 0000000000..101916ac67 --- /dev/null +++ b/test/core/SequentialCacheTest.class.php @@ -0,0 +1,88 @@ +set('some_key', 'some_value'); + + $slave1 = new LazyObject(function(){ + $obj = new PeclMemcached("35.143.65.241", "11211", 0.01); //some not existing memcache + return $obj; + }); + + $slave2 = new LazyObject(function(){ + $obj = new PeclMemcached("165.34.176.221", "11211", 0.01); //some not existing memcache + return $obj; + }); + + $cache = new SequentialCache($slave1, $slave1, $slave2, $master); + + $result = $cache->get("some_key"); + + $this->assertEquals($result, 'some_value'); + } + + public function testMultiCacheMasterFirst() { + $master = new PeclMemcached("127.0.0.1", "11211", 0.01); //some existing memcached + $master->set('some_key', 'some_value'); + + $slave1 = new LazyObject(function(){ + $obj = new PeclMemcached("35.143.65.241", "11211", 0.01); //some not existing memcache + return $obj; + }); + + $slave2 = new LazyObject(function(){ + $obj = new PeclMemcached("165.34.176.221", "11211", 0.01); //some not existing memcache + return $obj; + }); + + $cache = new SequentialCache($master, $slave1, $slave1, $slave2); + + $result = $cache->get("some_key"); + + $this->assertEquals($result, 'some_value'); + } + + public function testMultiCacheMasterOnly() { + $master = new PeclMemcached("127.0.0.1", "11211", 0.01); //some existing memcached + $master->set('some_key', 'some_value'); + + $cache = new SequentialCache($master); + + $result = $cache->get("some_key"); + + $this->assertEquals($result, 'some_value'); + } + + /** + * @expectedException RuntimeException + */ + public function testMultiCacheNoMaster() { + $slave1 = new LazyObject(function(){ + $obj = new PeclMemcached("35.143.65.241", "11211", 0.01); //some not existing memcache + return $obj; + }); + + $slave2 = new LazyObject(function(){ + $obj = new PeclMemcached("165.34.176.221", "11211", 0.01); //some not existing memcache + return $obj; + }); + + $cache = new SequentialCache($slave1, $slave2); + + $result = $cache->get("some_key"); + + $this->assertEquals($result, 'some_value'); + } + } From 5c266ca4c2580c0b1992b92ecba638de4f197173 Mon Sep 17 00:00:00 2001 From: anaumenko Date: Wed, 18 Apr 2012 15:54:44 +0400 Subject: [PATCH 02/22] Adding main classes --- core/Cache/PeclMemcached.class.php | 40 ++++++++++++++++--- main/Monitoring/PinbedPeclMemcached.class.php | 8 ++-- test/main/Utils/PinbaTest.class.php | 34 +++++++--------- 3 files changed, 55 insertions(+), 27 deletions(-) diff --git a/core/Cache/PeclMemcached.class.php b/core/Cache/PeclMemcached.class.php index b8d9a59cf7..18de888d08 100644 --- a/core/Cache/PeclMemcached.class.php +++ b/core/Cache/PeclMemcached.class.php @@ -21,33 +21,43 @@ class PeclMemcached extends CachePeer { const DEFAULT_PORT = 11211; const DEFAULT_HOST = '127.0.0.1'; + const DEFAULT_TIMEOUT = 1; private $instance = null; + private $timeout = null; + private $host = null; + private $port = null; /** * @return PeclMemcached **/ public static function create( $host = Memcached::DEFAULT_HOST, - $port = Memcached::DEFAULT_PORT + $port = Memcached::DEFAULT_PORT, + $timeout = PeclMemcached::DEFAULT_TIMEOUT ) { - return new self($host, $port); + return new self($host, $port, $timeout); } public function __construct( $host = Memcached::DEFAULT_HOST, - $port = Memcached::DEFAULT_PORT + $port = Memcached::DEFAULT_PORT, + $timeout = PeclMemcached::DEFAULT_TIMEOUT ) { $this->instance = new Memcache(); + $this->host = $host; + $this->port = $port; + $this->timeout = $timeout; try { try { - $this->instance->pconnect($host, $port); + $this->instance->pconnect($host, $port, $timeout); } catch (BaseException $e) { - $this->instance->connect($host, $port); + $this->instance->connect($host, $port, $timeout); } + $this->setTimeout($timeout); $this->alive = true; } catch (BaseException $e) { @@ -167,5 +177,25 @@ protected function store( Assert::isUnreachable(); } + + /** + * @param float $timeout time in seconds + * @return \PeclMemcached + */ + public function setTimeout($timeout) + { + $this->timeout = $timeout; + $this->instance->setServerParams($this->host, $this->port, $timeout); + + return $this; + } + + /** + * @return float + */ + public function getTimeout() + { + return $this->timeout; + } } ?> \ No newline at end of file diff --git a/main/Monitoring/PinbedPeclMemcached.class.php b/main/Monitoring/PinbedPeclMemcached.class.php index c1df435384..439e49a660 100644 --- a/main/Monitoring/PinbedPeclMemcached.class.php +++ b/main/Monitoring/PinbedPeclMemcached.class.php @@ -22,7 +22,8 @@ final class PinbedPeclMemcached extends PeclMemcached **/ public static function create( $host = Memcached::DEFAULT_HOST, - $port = Memcached::DEFAULT_PORT + $port = Memcached::DEFAULT_PORT, + $timeout = PeclMemcached::DEFAULT_TIMEOUT ) { return new self($host, $port); @@ -30,7 +31,8 @@ public static function create( public function __construct( $host = Memcached::DEFAULT_HOST, - $port = Memcached::DEFAULT_PORT + $port = Memcached::DEFAULT_PORT, + $timeout = PeclMemcached::DEFAULT_TIMEOUT ) { $this->host = $host; @@ -42,7 +44,7 @@ public function __construct( array('pecl_memcached_connect' => $host.'_'.$port) ); - parent::__construct($host, $port); + parent::__construct($host, $port, $timeout); if (PinbaClient::isEnabled()) PinbaClient::me()->timerStop( diff --git a/test/main/Utils/PinbaTest.class.php b/test/main/Utils/PinbaTest.class.php index 87995dfa7c..68d6e483ca 100644 --- a/test/main/Utils/PinbaTest.class.php +++ b/test/main/Utils/PinbaTest.class.php @@ -2,42 +2,38 @@ final class PinbaTest extends TestCase { - protected function setUp() + protected $isSkipped = false; + public function setUp() { if (!extension_loaded('pinba')) - $this->markTestSkipped( - 'The pinba extension is not available.' - ); + $this->skip('The pinba extension is not available.'); if (!PinbaClient::isEnabled()) - $this->markTestSkipped( - 'The pinba is not enabled at php.ini (pinba.enabled=1).' - ); + $this->skip('The pinba is not enabled at php.ini (pinba.enabled=1).'); if (!extension_loaded('runkit')) { - $this->markTestSkipped( - 'The runkit extension is not available.' - ); + $this->skip('The runkit extension is not available.'); } if (!ini_get('runkit.internal_override')) - $this->markTestSkipped( - 'The runkit.internal_override is not enabled (enabled it at php.ini).' - ); + $this->skip('The runkit.internal_override is not enabled (enabled it at php.ini).'); + if ($this->isSkipped) + return; + runkit_function_rename('pinba_timer_start', 'pinba_timer_start_bak'); runkit_function_rename('pinba_timer_stop', 'pinba_timer_stop_bak'); runkit_function_rename('pinba_timer_start_callback', 'pinba_timer_start'); runkit_function_rename('pinba_timer_stop_callback', 'pinba_timer_stop'); } - - public static function tearDownAfterClass(){ + protected function skip($message){ + $this->markTestSkipped($message); + $this->isSkipped = true; + } + public function tearDown(){ - if ( - !extension_loaded('runkit') - || !ini_get('runkit.internal_override') - ) + if ($this->isSkipped) return; runkit_function_rename('pinba_timer_start', 'pinba_timer_start_callback'); From 09126ca9bdf5da4f735efd1181913145ee072d94 Mon Sep 17 00:00:00 2001 From: anaumenko Date: Wed, 18 Apr 2012 16:49:49 +0400 Subject: [PATCH 03/22] Some renames of wars and beautify --- .gitignore | 1 + core/Cache/PeclMemcached.class.php | 10 +++++----- test/main/Utils/PinbaTest.class.php | 16 ++++++++++------ 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index cd9800c93d..d1a93e3bdf 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ test/meta/Proto/ test/main/data/urls/parser.dump.new .idea test/onphp +/nbproject/private/ \ No newline at end of file diff --git a/core/Cache/PeclMemcached.class.php b/core/Cache/PeclMemcached.class.php index 18de888d08..c0e262cbb3 100644 --- a/core/Cache/PeclMemcached.class.php +++ b/core/Cache/PeclMemcached.class.php @@ -24,7 +24,7 @@ class PeclMemcached extends CachePeer const DEFAULT_TIMEOUT = 1; private $instance = null; - private $timeout = null; + private $requestTimeout = null; private $host = null; private $port = null; @@ -49,7 +49,6 @@ public function __construct( $this->instance = new Memcache(); $this->host = $host; $this->port = $port; - $this->timeout = $timeout; try { try { @@ -57,6 +56,7 @@ public function __construct( } catch (BaseException $e) { $this->instance->connect($host, $port, $timeout); } + $this->setTimeout($timeout); $this->alive = true; @@ -182,10 +182,10 @@ protected function store( * @param float $timeout time in seconds * @return \PeclMemcached */ - public function setTimeout($timeout) + public function setTimeout($requestTimeout) { - $this->timeout = $timeout; - $this->instance->setServerParams($this->host, $this->port, $timeout); + $this->requestTimeout = $requestTimeout; + $this->instance->setServerParams($this->host, $this->port, $requestTimeout); return $this; } diff --git a/test/main/Utils/PinbaTest.class.php b/test/main/Utils/PinbaTest.class.php index 68d6e483ca..6d9cfcf0c1 100644 --- a/test/main/Utils/PinbaTest.class.php +++ b/test/main/Utils/PinbaTest.class.php @@ -2,7 +2,7 @@ final class PinbaTest extends TestCase { - protected $isSkipped = false; + protected $skipped = false; public function setUp() { if (!extension_loaded('pinba')) @@ -18,7 +18,7 @@ public function setUp() if (!ini_get('runkit.internal_override')) $this->skip('The runkit.internal_override is not enabled (enabled it at php.ini).'); - if ($this->isSkipped) + if ($this->skipped) return; runkit_function_rename('pinba_timer_start', 'pinba_timer_start_bak'); @@ -27,13 +27,17 @@ public function setUp() runkit_function_rename('pinba_timer_start_callback', 'pinba_timer_start'); runkit_function_rename('pinba_timer_stop_callback', 'pinba_timer_stop'); } - protected function skip($message){ + + protected function skip($message) + { $this->markTestSkipped($message); - $this->isSkipped = true; + $this->skipped = true; } - public function tearDown(){ + + public function tearDown() + { - if ($this->isSkipped) + if ($this->skipped) return; runkit_function_rename('pinba_timer_start', 'pinba_timer_start_callback'); From 494778d423d02b1921bd7c093740e64f29be8db4 Mon Sep 17 00:00:00 2001 From: anaumenko Date: Thu, 19 Apr 2012 10:45:19 +0400 Subject: [PATCH 04/22] Some beautify --- .gitignore | 3 +- core/Base/Assert.class.php | 8 ++++++ core/Base/LazyObject.class.php | 3 +- core/Cache/SequentialCache.class.php | 38 ++++++++++++++++--------- test/core/SequentialCacheTest.class.php | 4 +-- test/main/Utils/PinbaTest.class.php | 13 ++++----- 6 files changed, 43 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index d1a93e3bdf..d0eb9d2454 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ test/meta/Proto/ test/main/data/urls/parser.dump.new .idea test/onphp -/nbproject/private/ \ No newline at end of file +/nbproject/ + diff --git a/core/Base/Assert.class.php b/core/Base/Assert.class.php index ea375b18d2..d26e283e7b 100644 --- a/core/Base/Assert.class.php +++ b/core/Base/Assert.class.php @@ -277,6 +277,14 @@ public static function isUnreachable($message = 'unreachable code reached') throw new WrongArgumentException($message); } + public static function isObject($object, $message = null) + { + if (!is_object($object, $method)) + throw new WrongArgumentException( + $message.' not object given' + ); + } + /// exceptionless methods //@{ public static function checkInteger($value) diff --git a/core/Base/LazyObject.class.php b/core/Base/LazyObject.class.php index 10760db666..399ac0ae63 100644 --- a/core/Base/LazyObject.class.php +++ b/core/Base/LazyObject.class.php @@ -10,7 +10,8 @@ ****************************************************************************/ class LazyObject { - protected $inner = false; + protected $inner = null; + protected $icallback = null; public function __construct($constructor) { diff --git a/core/Cache/SequentialCache.class.php b/core/Cache/SequentialCache.class.php index fc120f42bf..07bc19665b 100644 --- a/core/Cache/SequentialCache.class.php +++ b/core/Cache/SequentialCache.class.php @@ -9,11 +9,12 @@ * * ****************************************************************************/ - class SequentialCache extends CachePeer { - protected - $list = array(); + class SequentialCache extends CachePeer + { + protected $list = array(); - public function __construct() { + public function __construct() + { $list = func_get_args(); foreach ($list as $cache) { @@ -21,18 +22,21 @@ public function __construct() { } } - public function addPeer($peer) { + public function addPeer($peer) + { $this->list[] = $peer; return $this; } - public function get($key) { + public function get($key) + { foreach ($this->list as $val) { /** - * @var $cache CachePeer + * @var $val CachePeer */ try { + if ($val->isAlive()) { $result = $val->get($key); if ($val->isAlive()) { @@ -46,30 +50,36 @@ public function get($key) { throw new RuntimeException("All peers are dead"); } - protected function store($action, $key, $value, $expires = Cache::EXPIRES_MEDIUM){ + protected function store($action, $key, $value, $expires = Cache::EXPIRES_MEDIUM) + { return $this->foreachItem(__METHOD__, func_get_args()); } - public function append($key, $data) { + public function append($key, $data) + { return $this->foreachItem(__METHOD__, func_get_args()); } - public function decrement($key, $value) { + public function decrement($key, $value) + { return $this->foreachItem(__METHOD__, func_get_args()); } - public function delete($key) { + public function delete($key) + { return $this->foreachItem(__METHOD__, func_get_args()); } - public function increment($key, $value) { + public function increment($key, $value) + { return $this->foreachItem(__METHOD__, func_get_args()); } - private function foreachItem($method, $args){ + private function foreachItem($method, $args) + { foreach ($this->list as $val) { - call_user_func_array(array($val['obj'], $method), $args); + call_user_func_array(array($val, $method), $args); } return $this; diff --git a/test/core/SequentialCacheTest.class.php b/test/core/SequentialCacheTest.class.php index 101916ac67..845a29bb12 100644 --- a/test/core/SequentialCacheTest.class.php +++ b/test/core/SequentialCacheTest.class.php @@ -81,8 +81,6 @@ public function testMultiCacheNoMaster() { $cache = new SequentialCache($slave1, $slave2); - $result = $cache->get("some_key"); - - $this->assertEquals($result, 'some_value'); + $result = $cache->get("some_key"); //will be throw RuntimeException } } diff --git a/test/main/Utils/PinbaTest.class.php b/test/main/Utils/PinbaTest.class.php index 6d9cfcf0c1..d9fc4478eb 100644 --- a/test/main/Utils/PinbaTest.class.php +++ b/test/main/Utils/PinbaTest.class.php @@ -2,8 +2,8 @@ final class PinbaTest extends TestCase { - protected $skipped = false; - public function setUp() + protected static $skipped = false; + public static function setUpBeforeClass() { if (!extension_loaded('pinba')) $this->skip('The pinba extension is not available.'); @@ -18,7 +18,7 @@ public function setUp() if (!ini_get('runkit.internal_override')) $this->skip('The runkit.internal_override is not enabled (enabled it at php.ini).'); - if ($this->skipped) + if (self::$skipped) return; runkit_function_rename('pinba_timer_start', 'pinba_timer_start_bak'); @@ -31,13 +31,12 @@ public function setUp() protected function skip($message) { $this->markTestSkipped($message); - $this->skipped = true; + self::$skipped = true; } - public function tearDown() + public static function tearDownAfterClass() { - - if ($this->skipped) + if (self::$skipped) return; runkit_function_rename('pinba_timer_start', 'pinba_timer_start_callback'); From 5d13a49010e5f5bb1153e7210a665574e9d7173c Mon Sep 17 00:00:00 2001 From: anaumenko Date: Thu, 19 Apr 2012 15:08:00 +0400 Subject: [PATCH 05/22] Removed LazyObject, PeclMemcached is lazy now --- core/Base/LazyObject.class.php | 46 -------------- core/Cache/PeclMemcached.class.php | 45 +++++++++++--- core/Cache/SequentialCache.class.php | 2 +- main/Monitoring/PinbedPeclMemcached.class.php | 6 +- test/core/SequentialCacheTest.class.php | 61 +++++++------------ 5 files changed, 62 insertions(+), 98 deletions(-) delete mode 100644 core/Base/LazyObject.class.php diff --git a/core/Base/LazyObject.class.php b/core/Base/LazyObject.class.php deleted file mode 100644 index 399ac0ae63..0000000000 --- a/core/Base/LazyObject.class.php +++ /dev/null @@ -1,46 +0,0 @@ -callback = $constructor; - } - - public static function create($constructor) - { - return new self($constructor); - } - - protected function ensureInnerInited() - { - if ($this->inner) - return; - - $this->inner = call_user_func($this->callback); - - if (!is_object($this->inner)) { - throw new RuntimeException("Callback returns not object"); - } - - return $this; - } - - public function __call($function, $args) - { - $this->ensureInnerInited(); - - return call_user_func_array(array($this->inner, $function), $param_arr); - } - } \ No newline at end of file diff --git a/core/Cache/PeclMemcached.class.php b/core/Cache/PeclMemcached.class.php index c0e262cbb3..4001a8e6be 100644 --- a/core/Cache/PeclMemcached.class.php +++ b/core/Cache/PeclMemcached.class.php @@ -25,8 +25,10 @@ class PeclMemcached extends CachePeer private $instance = null; private $requestTimeout = null; + private $connectTimeout = null; private $host = null; private $port = null; + private $connected = false; /** * @return PeclMemcached @@ -34,35 +36,49 @@ class PeclMemcached extends CachePeer public static function create( $host = Memcached::DEFAULT_HOST, $port = Memcached::DEFAULT_PORT, - $timeout = PeclMemcached::DEFAULT_TIMEOUT + $connectTimeout = PeclMemcached::DEFAULT_TIMEOUT ) { - return new self($host, $port, $timeout); + return new self($host, $port, $connectTimeout); } public function __construct( $host = Memcached::DEFAULT_HOST, $port = Memcached::DEFAULT_PORT, - $timeout = PeclMemcached::DEFAULT_TIMEOUT + $connectTimeout = PeclMemcached::DEFAULT_TIMEOUT ) { - $this->instance = new Memcache(); $this->host = $host; $this->port = $port; + $this->connectTimeout = $connectTimeout; + } + + public function isAlive() { + $this->ensureConnected(); + return parent::isAlive(); + } + + protected function ensureConnected() { + if ($this->connected) + return $this; + $this->connected = true; + $this->instance = new Memcache(); try { try { - $this->instance->pconnect($host, $port, $timeout); + $this->instance->pconnect($this->host, $this->port, $this->connectTimeout); } catch (BaseException $e) { - $this->instance->connect($host, $port, $timeout); + $this->instance->connect($this->host, $this->port, $this->connectTimeout); } - $this->setTimeout($timeout); - $this->alive = true; + $this->setTimeout($this->connectTimeout); + } catch (BaseException $e) { // bad luck. } + + return $this; } public function __destruct() @@ -81,6 +97,7 @@ public function __destruct() **/ public function clean() { + $this->ensureConnected(); try { $this->instance->flush(); } catch (BaseException $e) { @@ -92,6 +109,7 @@ public function clean() public function increment($key, $value) { + $this->ensureConnected(); try { return $this->instance->increment($key, $value); } catch (BaseException $e) { @@ -101,6 +119,7 @@ public function increment($key, $value) public function decrement($key, $value) { + $this->ensureConnected(); try { return $this->instance->decrement($key, $value); } catch (BaseException $e) { @@ -110,6 +129,7 @@ public function decrement($key, $value) public function getList($indexes) { + $this->ensureConnected(); return ($return = $this->get($indexes)) ? $return @@ -118,6 +138,7 @@ public function getList($indexes) public function get($index) { + $this->ensureConnected(); try { return $this->instance->get($index); } catch (BaseException $e) { @@ -134,6 +155,7 @@ public function get($index) public function delete($index) { + $this->ensureConnected(); try { // second parameter required, wrt new memcached protocol: // delete key 0 (see process_delete_command in the memcached.c) @@ -148,6 +170,7 @@ public function delete($index) public function append($key, $data) { + $this->ensureConnected(); try { return $this->instance->append($key, $data); } catch (BaseException $e) { @@ -161,6 +184,7 @@ protected function store( $action, $key, $value, $expires = Cache::EXPIRES_MEDIUM ) { + $this->ensureConnected(); try { return $this->instance->$action( @@ -179,11 +203,12 @@ protected function store( } /** - * @param float $timeout time in seconds + * @param float $requestTimeout time in seconds * @return \PeclMemcached */ public function setTimeout($requestTimeout) { + $this->ensureConnected(); $this->requestTimeout = $requestTimeout; $this->instance->setServerParams($this->host, $this->port, $requestTimeout); @@ -195,7 +220,7 @@ public function setTimeout($requestTimeout) */ public function getTimeout() { - return $this->timeout; + return $this->requestTimeout; } } ?> \ No newline at end of file diff --git a/core/Cache/SequentialCache.class.php b/core/Cache/SequentialCache.class.php index 07bc19665b..5800635541 100644 --- a/core/Cache/SequentialCache.class.php +++ b/core/Cache/SequentialCache.class.php @@ -9,7 +9,7 @@ * * ****************************************************************************/ - class SequentialCache extends CachePeer + final class SequentialCache extends CachePeer { protected $list = array(); diff --git a/main/Monitoring/PinbedPeclMemcached.class.php b/main/Monitoring/PinbedPeclMemcached.class.php index 439e49a660..2d5a81890d 100644 --- a/main/Monitoring/PinbedPeclMemcached.class.php +++ b/main/Monitoring/PinbedPeclMemcached.class.php @@ -23,7 +23,7 @@ final class PinbedPeclMemcached extends PeclMemcached public static function create( $host = Memcached::DEFAULT_HOST, $port = Memcached::DEFAULT_PORT, - $timeout = PeclMemcached::DEFAULT_TIMEOUT + $connectTimeout = PeclMemcached::DEFAULT_TIMEOUT ) { return new self($host, $port); @@ -32,7 +32,7 @@ public static function create( public function __construct( $host = Memcached::DEFAULT_HOST, $port = Memcached::DEFAULT_PORT, - $timeout = PeclMemcached::DEFAULT_TIMEOUT + $connectTimeout = PeclMemcached::DEFAULT_TIMEOUT ) { $this->host = $host; @@ -44,7 +44,7 @@ public function __construct( array('pecl_memcached_connect' => $host.'_'.$port) ); - parent::__construct($host, $port, $timeout); + parent::__construct($host, $port, $connectTimeout); if (PinbaClient::isEnabled()) PinbaClient::me()->timerStop( diff --git a/test/core/SequentialCacheTest.class.php b/test/core/SequentialCacheTest.class.php index 845a29bb12..ed7de3400a 100644 --- a/test/core/SequentialCacheTest.class.php +++ b/test/core/SequentialCacheTest.class.php @@ -11,54 +11,45 @@ final class SequentialCacheTest extends TestCase { - public function testMultiCacheMaterLast() { + public function testMultiCacheAliveLast() + { $t = microtime(true); - $master = new PeclMemcached("127.0.0.1", "11211", 0.01); //some existing memcached - $master->set('some_key', 'some_value'); + $alife_peer = new PeclMemcached("127.0.0.1", "11211", 0.01); //some existing memcached + $alife_peer->set('some_key', 'some_value'); - $slave1 = new LazyObject(function(){ - $obj = new PeclMemcached("35.143.65.241", "11211", 0.01); //some not existing memcache - return $obj; - }); + $slave1 = new PeclMemcached("35.143.65.241", "11211", 0.01); //some not existing memcache - $slave2 = new LazyObject(function(){ - $obj = new PeclMemcached("165.34.176.221", "11211", 0.01); //some not existing memcache - return $obj; - }); + $slave2 = new PeclMemcached("165.34.176.221", "11211", 0.01); //some not existing memcache - $cache = new SequentialCache($slave1, $slave1, $slave2, $master); + $cache = new SequentialCache($slave1, $slave1, $slave2, $alife_peer); $result = $cache->get("some_key"); $this->assertEquals($result, 'some_value'); } - public function testMultiCacheMasterFirst() { - $master = new PeclMemcached("127.0.0.1", "11211", 0.01); //some existing memcached - $master->set('some_key', 'some_value'); + public function testMultiCacheAliveFirst() + { + $alife_peer = new PeclMemcached("127.0.0.1", "11211", 0.01); //some existing memcached + $alife_peer->set('some_key', 'some_value'); - $slave1 = new LazyObject(function(){ - $obj = new PeclMemcached("35.143.65.241", "11211", 0.01); //some not existing memcache - return $obj; - }); + $slave1 = new PeclMemcached("35.143.65.241", "11211", 0.01); //some not existing memcache - $slave2 = new LazyObject(function(){ - $obj = new PeclMemcached("165.34.176.221", "11211", 0.01); //some not existing memcache - return $obj; - }); + $slave2 = new PeclMemcached("165.34.176.221", "11211", 0.01); //some not existing memcache - $cache = new SequentialCache($master, $slave1, $slave1, $slave2); + $cache = new SequentialCache($alife_peer, $slave1, $slave1, $slave2); $result = $cache->get("some_key"); $this->assertEquals($result, 'some_value'); } - public function testMultiCacheMasterOnly() { - $master = new PeclMemcached("127.0.0.1", "11211", 0.01); //some existing memcached - $master->set('some_key', 'some_value'); + public function testMultiCacheAliveOnly() + { + $alife_peer = new PeclMemcached("127.0.0.1", "11211", 0.01); //some existing memcached + $alife_peer->set('some_key', 'some_value'); - $cache = new SequentialCache($master); + $cache = new SequentialCache($alife_peer); $result = $cache->get("some_key"); @@ -68,16 +59,10 @@ public function testMultiCacheMasterOnly() { /** * @expectedException RuntimeException */ - public function testMultiCacheNoMaster() { - $slave1 = new LazyObject(function(){ - $obj = new PeclMemcached("35.143.65.241", "11211", 0.01); //some not existing memcache - return $obj; - }); - - $slave2 = new LazyObject(function(){ - $obj = new PeclMemcached("165.34.176.221", "11211", 0.01); //some not existing memcache - return $obj; - }); + public function testMultiCacheNoAlive() + { + $slave1 = new PeclMemcached("35.143.65.241", "11211", 0.01); //some not existing memcache + $slave2 = new PeclMemcached("165.34.176.221", "11211", 0.01); //some not existing memcache $cache = new SequentialCache($slave1, $slave2); From bcc08ab23b7e6ffcd4590a25ea3112d0f1e2838f Mon Sep 17 00:00:00 2001 From: anaumenko Date: Thu, 19 Apr 2012 15:35:56 +0400 Subject: [PATCH 06/22] Added explicit master and slave peers --- core/Cache/SequentialCache.class.php | 51 ++++++++++++++++++++----- test/core/SequentialCacheTest.class.php | 6 +-- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/core/Cache/SequentialCache.class.php b/core/Cache/SequentialCache.class.php index 5800635541..484160bd0e 100644 --- a/core/Cache/SequentialCache.class.php +++ b/core/Cache/SequentialCache.class.php @@ -11,20 +11,53 @@ final class SequentialCache extends CachePeer { - protected $list = array(); + /** + * List of all peers, including master + * @var array of CachePeer + */ + protected $list = array(); + + /** + * List of slaves only + * @var array of CachePeer + */ + protected $slaves = array(); + + /** + * @var CachePeer + */ + protected $master = null; - public function __construct() + public function __construct($master, $slaves = array()) { - $list = func_get_args(); - - foreach ($list as $cache) { + $this->setMaster($master); + + foreach ($slaves as $cache) { $this->addPeer($cache); } } - - public function addPeer($peer) + + /** + * @param CachePeer $master + * @return \SequentialCache + */ + public function setMaster(CachePeer $master) + { + $this->master = $master; + $this->list = $this->slaves; + array_unshift($this->list, $this->master); + + return $this; + } + + /** + * @param CachePeer $master + * @return \SequentialCache + */ + public function addPeer(CachePeer $peer) { - $this->list[] = $peer; + $this->list[] = $peer; + $this->slaves[] = $peer; return $this; } @@ -36,7 +69,6 @@ public function get($key) * @var $val CachePeer */ try { - if ($val->isAlive()) { $result = $val->get($key); if ($val->isAlive()) { @@ -70,7 +102,6 @@ public function delete($key) return $this->foreachItem(__METHOD__, func_get_args()); } - public function increment($key, $value) { return $this->foreachItem(__METHOD__, func_get_args()); diff --git a/test/core/SequentialCacheTest.class.php b/test/core/SequentialCacheTest.class.php index ed7de3400a..b8a9d6bb42 100644 --- a/test/core/SequentialCacheTest.class.php +++ b/test/core/SequentialCacheTest.class.php @@ -21,7 +21,7 @@ public function testMultiCacheAliveLast() $slave2 = new PeclMemcached("165.34.176.221", "11211", 0.01); //some not existing memcache - $cache = new SequentialCache($slave1, $slave1, $slave2, $alife_peer); + $cache = new SequentialCache($alife_peer, array($slave1, $slave2, $alife_peer)); $result = $cache->get("some_key"); @@ -37,7 +37,7 @@ public function testMultiCacheAliveFirst() $slave2 = new PeclMemcached("165.34.176.221", "11211", 0.01); //some not existing memcache - $cache = new SequentialCache($alife_peer, $slave1, $slave1, $slave2); + $cache = new SequentialCache($alife_peer, array($slave1, $slave1, $slave2)); $result = $cache->get("some_key"); @@ -64,7 +64,7 @@ public function testMultiCacheNoAlive() $slave1 = new PeclMemcached("35.143.65.241", "11211", 0.01); //some not existing memcache $slave2 = new PeclMemcached("165.34.176.221", "11211", 0.01); //some not existing memcache - $cache = new SequentialCache($slave1, $slave2); + $cache = new SequentialCache($slave1, array($slave2)); $result = $cache->get("some_key"); //will be throw RuntimeException } From bb67475410ce908f2e428f8fad58aad0d772b9a1 Mon Sep 17 00:00:00 2001 From: anaumenko Date: Thu, 19 Apr 2012 16:07:14 +0400 Subject: [PATCH 07/22] Some speedup of SequentialCache::get() --- core/Cache/SequentialCache.class.php | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/core/Cache/SequentialCache.class.php b/core/Cache/SequentialCache.class.php index 484160bd0e..beae9733bb 100644 --- a/core/Cache/SequentialCache.class.php +++ b/core/Cache/SequentialCache.class.php @@ -68,15 +68,9 @@ public function get($key) /** * @var $val CachePeer */ - try { - if ($val->isAlive()) { - $result = $val->get($key); - if ($val->isAlive()) { - return $result; - } - } - } catch (Exception $e) { - //go next... + $result = $val->get($key); + if ($result !== null) { + return $result; } } throw new RuntimeException("All peers are dead"); From 744f32c9757859362bf68ee6c71349660ad77b3f Mon Sep 17 00:00:00 2001 From: anaumenko Date: Thu, 19 Apr 2012 16:17:36 +0400 Subject: [PATCH 08/22] Some refactor --- core/Cache/SequentialCache.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/Cache/SequentialCache.class.php b/core/Cache/SequentialCache.class.php index beae9733bb..8c954bfdab 100644 --- a/core/Cache/SequentialCache.class.php +++ b/core/Cache/SequentialCache.class.php @@ -69,7 +69,7 @@ public function get($key) * @var $val CachePeer */ $result = $val->get($key); - if ($result !== null) { + if (!empty($result) || $val->isAlive()) { return $result; } } From f7d1baa72732ca364ec5c0e01ad2b66eebb74ac7 Mon Sep 17 00:00:00 2001 From: anaumenko Date: Fri, 20 Apr 2012 11:05:25 +0400 Subject: [PATCH 09/22] Some refactor (new lines, etc) --- core/Cache/PeclMemcached.class.php | 1 + core/Cache/SequentialCache.class.php | 23 +++++++++++++++++++---- test/main/Utils/PinbaTest.class.php | 1 + 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/core/Cache/PeclMemcached.class.php b/core/Cache/PeclMemcached.class.php index 4001a8e6be..dd2b094472 100644 --- a/core/Cache/PeclMemcached.class.php +++ b/core/Cache/PeclMemcached.class.php @@ -110,6 +110,7 @@ public function clean() public function increment($key, $value) { $this->ensureConnected(); + try { return $this->instance->increment($key, $value); } catch (BaseException $e) { diff --git a/core/Cache/SequentialCache.class.php b/core/Cache/SequentialCache.class.php index 8c954bfdab..26e3c80aad 100644 --- a/core/Cache/SequentialCache.class.php +++ b/core/Cache/SequentialCache.class.php @@ -28,6 +28,10 @@ final class SequentialCache extends CachePeer */ protected $master = null; + /** + * @param CachePeer $master + * @param array $slaves or CachePeer + */ public function __construct($master, $slaves = array()) { $this->setMaster($master); @@ -37,6 +41,16 @@ public function __construct($master, $slaves = array()) } } + /** + * @param CachePeer $master + * @param array $slaves or CachePeer + * @return SequentialCache + */ + public static function create($master, $slaves = array()) + { + return new self($master, $slaves); + } + /** * @param CachePeer $master * @return \SequentialCache @@ -88,7 +102,7 @@ public function append($key, $data) public function decrement($key, $value) { - return $this->foreachItem(__METHOD__, func_get_args()); + throw new UnsupportedMethodException("decrement is not supported"); } public function delete($key) @@ -98,15 +112,16 @@ public function delete($key) public function increment($key, $value) { - return $this->foreachItem(__METHOD__, func_get_args()); + throw new UnsupportedMethodException("increment is not supported"); } private function foreachItem($method, $args) { + $result = true; foreach ($this->list as $val) { - call_user_func_array(array($val, $method), $args); + $result &= call_user_func_array(array($val, $method), $args); } - return $this; + return (bool)$result; } } \ No newline at end of file diff --git a/test/main/Utils/PinbaTest.class.php b/test/main/Utils/PinbaTest.class.php index d9fc4478eb..fc79e05a8d 100644 --- a/test/main/Utils/PinbaTest.class.php +++ b/test/main/Utils/PinbaTest.class.php @@ -3,6 +3,7 @@ final class PinbaTest extends TestCase { protected static $skipped = false; + public static function setUpBeforeClass() { if (!extension_loaded('pinba')) From 6bf62aea43e9e70a9bf2d1704254cd235b6838de Mon Sep 17 00:00:00 2001 From: anaumenko Date: Fri, 20 Apr 2012 11:16:00 +0400 Subject: [PATCH 10/22] Added typehinting to SequentialCache --- core/Cache/SequentialCache.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/Cache/SequentialCache.class.php b/core/Cache/SequentialCache.class.php index 26e3c80aad..c69d47dd06 100644 --- a/core/Cache/SequentialCache.class.php +++ b/core/Cache/SequentialCache.class.php @@ -32,7 +32,7 @@ final class SequentialCache extends CachePeer * @param CachePeer $master * @param array $slaves or CachePeer */ - public function __construct($master, $slaves = array()) + public function __construct(CachePeer $master, $slaves = array()) { $this->setMaster($master); @@ -46,7 +46,7 @@ public function __construct($master, $slaves = array()) * @param array $slaves or CachePeer * @return SequentialCache */ - public static function create($master, $slaves = array()) + public static function create(CachePeer $master, $slaves = array()) { return new self($master, $slaves); } From e9bc4ce2162b712e4e0d44009a9932cec9b51d7d Mon Sep 17 00:00:00 2001 From: anaumenko Date: Fri, 20 Apr 2012 11:59:37 +0400 Subject: [PATCH 11/22] CodingStyle fixes --- core/Cache/PeclMemcached.class.php | 106 +++++++++++++++------------ core/Cache/SequentialCache.class.php | 30 ++++---- 2 files changed, 74 insertions(+), 62 deletions(-) diff --git a/core/Cache/PeclMemcached.class.php b/core/Cache/PeclMemcached.class.php index dd2b094472..79205c3dfc 100644 --- a/core/Cache/PeclMemcached.class.php +++ b/core/Cache/PeclMemcached.class.php @@ -53,34 +53,6 @@ public function __construct( $this->connectTimeout = $connectTimeout; } - public function isAlive() { - $this->ensureConnected(); - return parent::isAlive(); - } - - protected function ensureConnected() { - if ($this->connected) - return $this; - - $this->connected = true; - $this->instance = new Memcache(); - try { - try { - $this->instance->pconnect($this->host, $this->port, $this->connectTimeout); - } catch (BaseException $e) { - $this->instance->connect($this->host, $this->port, $this->connectTimeout); - } - - $this->alive = true; - $this->setTimeout($this->connectTimeout); - - } catch (BaseException $e) { - // bad luck. - } - - return $this; - } - public function __destruct() { if ($this->alive) { @@ -92,12 +64,19 @@ public function __destruct() } } + public function isAlive() + { + $this->ensureConnected(); + return parent::isAlive(); + } + /** * @return PeclMemcached **/ public function clean() { $this->ensureConnected(); + try { $this->instance->flush(); } catch (BaseException $e) { @@ -121,6 +100,7 @@ public function increment($key, $value) public function decrement($key, $value) { $this->ensureConnected(); + try { return $this->instance->decrement($key, $value); } catch (BaseException $e) { @@ -131,6 +111,7 @@ public function decrement($key, $value) public function getList($indexes) { $this->ensureConnected(); + return ($return = $this->get($indexes)) ? $return @@ -140,6 +121,7 @@ public function getList($indexes) public function get($index) { $this->ensureConnected(); + try { return $this->instance->get($index); } catch (BaseException $e) { @@ -157,6 +139,7 @@ public function get($index) public function delete($index) { $this->ensureConnected(); + try { // second parameter required, wrt new memcached protocol: // delete key 0 (see process_delete_command in the memcached.c) @@ -172,6 +155,7 @@ public function delete($index) public function append($key, $data) { $this->ensureConnected(); + try { return $this->instance->append($key, $data); } catch (BaseException $e) { @@ -181,6 +165,52 @@ public function append($key, $data) Assert::isUnreachable(); } + /** + * @param float $requestTimeout time in seconds + * @return \PeclMemcached + */ + public function setTimeout($requestTimeout) + { + $this->ensureConnected(); + $this->requestTimeout = $requestTimeout; + $this->instance->setServerParams($this->host, $this->port, $requestTimeout); + + return $this; + } + + /** + * @return float + */ + public function getTimeout() + { + return $this->requestTimeout; + } + + protected function ensureConnected() { + if ($this->connected) + return $this; + + $this->connected = true; + $this->instance = new Memcache(); + + try { + + try { + $this->instance->pconnect($this->host, $this->port, $this->connectTimeout); + } catch (BaseException $e) { + $this->instance->connect($this->host, $this->port, $this->connectTimeout); + } + + $this->alive = true; + $this->setTimeout($this->connectTimeout); + + } catch (BaseException $e) { + // bad luck. + } + + return $this; + } + protected function store( $action, $key, $value, $expires = Cache::EXPIRES_MEDIUM ) @@ -203,25 +233,5 @@ protected function store( Assert::isUnreachable(); } - /** - * @param float $requestTimeout time in seconds - * @return \PeclMemcached - */ - public function setTimeout($requestTimeout) - { - $this->ensureConnected(); - $this->requestTimeout = $requestTimeout; - $this->instance->setServerParams($this->host, $this->port, $requestTimeout); - - return $this; - } - - /** - * @return float - */ - public function getTimeout() - { - return $this->requestTimeout; - } } ?> \ No newline at end of file diff --git a/core/Cache/SequentialCache.class.php b/core/Cache/SequentialCache.class.php index c69d47dd06..13516f1ef7 100644 --- a/core/Cache/SequentialCache.class.php +++ b/core/Cache/SequentialCache.class.php @@ -31,24 +31,24 @@ final class SequentialCache extends CachePeer /** * @param CachePeer $master * @param array $slaves or CachePeer + * @return SequentialCache */ - public function __construct(CachePeer $master, $slaves = array()) + public static function create(CachePeer $master, $slaves = array()) { - $this->setMaster($master); - - foreach ($slaves as $cache) { - $this->addPeer($cache); - } + return new self($master, $slaves); } /** * @param CachePeer $master * @param array $slaves or CachePeer - * @return SequentialCache */ - public static function create(CachePeer $master, $slaves = array()) + public function __construct(CachePeer $master, $slaves = array()) { - return new self($master, $slaves); + $this->setMaster($master); + + foreach ($slaves as $cache) { + $this->addPeer($cache); + } } /** @@ -83,6 +83,7 @@ public function get($key) * @var $val CachePeer */ $result = $val->get($key); + if (!empty($result) || $val->isAlive()) { return $result; } @@ -90,11 +91,6 @@ public function get($key) throw new RuntimeException("All peers are dead"); } - protected function store($action, $key, $value, $expires = Cache::EXPIRES_MEDIUM) - { - return $this->foreachItem(__METHOD__, func_get_args()); - } - public function append($key, $data) { return $this->foreachItem(__METHOD__, func_get_args()); @@ -114,10 +110,16 @@ public function increment($key, $value) { throw new UnsupportedMethodException("increment is not supported"); } + + protected function store($action, $key, $value, $expires = Cache::EXPIRES_MEDIUM) + { + return $this->foreachItem(__METHOD__, func_get_args()); + } private function foreachItem($method, $args) { $result = true; + foreach ($this->list as $val) { $result &= call_user_func_array(array($val, $method), $args); } From 389812861d6e656762facd5e44230c0389ce2f8a Mon Sep 17 00:00:00 2001 From: anaumenko Date: Fri, 20 Apr 2012 12:53:11 +0400 Subject: [PATCH 12/22] PinbaTest fixes some error during skipping test --- core/Base/Assert.class.php | 2 +- core/Cache/PeclMemcached.class.php | 7 ++--- core/Cache/SequentialCache.class.php | 10 +++----- test/main/Utils/PinbaTest.class.php | 38 +++++++++++++++------------- 4 files changed, 30 insertions(+), 27 deletions(-) diff --git a/core/Base/Assert.class.php b/core/Base/Assert.class.php index d26e283e7b..1c41da0ffc 100644 --- a/core/Base/Assert.class.php +++ b/core/Base/Assert.class.php @@ -279,7 +279,7 @@ public static function isUnreachable($message = 'unreachable code reached') public static function isObject($object, $message = null) { - if (!is_object($object, $method)) + if (!is_object($object)) throw new WrongArgumentException( $message.' not object given' ); diff --git a/core/Cache/PeclMemcached.class.php b/core/Cache/PeclMemcached.class.php index 79205c3dfc..e48f706656 100644 --- a/core/Cache/PeclMemcached.class.php +++ b/core/Cache/PeclMemcached.class.php @@ -28,7 +28,7 @@ class PeclMemcached extends CachePeer private $connectTimeout = null; private $host = null; private $port = null; - private $connected = false; + private $triedConnect = false; /** * @return PeclMemcached @@ -67,6 +67,7 @@ public function __destruct() public function isAlive() { $this->ensureConnected(); + return parent::isAlive(); } @@ -187,10 +188,10 @@ public function getTimeout() } protected function ensureConnected() { - if ($this->connected) + if ($this->triedConnect) return $this; - $this->connected = true; + $this->triedConnect = true; $this->instance = new Memcache(); try { diff --git a/core/Cache/SequentialCache.class.php b/core/Cache/SequentialCache.class.php index 13516f1ef7..ab9df01030 100644 --- a/core/Cache/SequentialCache.class.php +++ b/core/Cache/SequentialCache.class.php @@ -79,16 +79,14 @@ public function addPeer(CachePeer $peer) public function get($key) { foreach ($this->list as $val) { - /** - * @var $val CachePeer - */ + /* @var $val CachePeer */ $result = $val->get($key); if (!empty($result) || $val->isAlive()) { return $result; } } - throw new RuntimeException("All peers are dead"); + throw new RuntimeException('All peers are dead'); } public function append($key, $data) @@ -98,7 +96,7 @@ public function append($key, $data) public function decrement($key, $value) { - throw new UnsupportedMethodException("decrement is not supported"); + throw new UnsupportedMethodException('decrement is not supported'); } public function delete($key) @@ -108,7 +106,7 @@ public function delete($key) public function increment($key, $value) { - throw new UnsupportedMethodException("increment is not supported"); + throw new UnsupportedMethodException('increment is not supported'); } protected function store($action, $key, $value, $expires = Cache::EXPIRES_MEDIUM) diff --git a/test/main/Utils/PinbaTest.class.php b/test/main/Utils/PinbaTest.class.php index fc79e05a8d..95e88f5cfe 100644 --- a/test/main/Utils/PinbaTest.class.php +++ b/test/main/Utils/PinbaTest.class.php @@ -2,26 +2,24 @@ final class PinbaTest extends TestCase { + protected static $skipMessage = 'unknown error'; protected static $skipped = false; public static function setUpBeforeClass() { if (!extension_loaded('pinba')) - $this->skip('The pinba extension is not available.'); + return self::skip('The pinba extension is not available.'); if (!PinbaClient::isEnabled()) - $this->skip('The pinba is not enabled at php.ini (pinba.enabled=1).'); + return self::skip('The pinba is not enabled at php.ini (pinba.enabled=1).'); if (!extension_loaded('runkit')) { - $this->skip('The runkit extension is not available.'); + return self::skip('The runkit extension is not available.'); } if (!ini_get('runkit.internal_override')) - $this->skip('The runkit.internal_override is not enabled (enabled it at php.ini).'); + return self::skip('The runkit.internal_override is not enabled (enabled it at php.ini).'); - if (self::$skipped) - return; - runkit_function_rename('pinba_timer_start', 'pinba_timer_start_bak'); runkit_function_rename('pinba_timer_stop', 'pinba_timer_stop_bak'); @@ -29,12 +27,6 @@ public static function setUpBeforeClass() runkit_function_rename('pinba_timer_stop_callback', 'pinba_timer_stop'); } - protected function skip($message) - { - $this->markTestSkipped($message); - self::$skipped = true; - } - public static function tearDownAfterClass() { if (self::$skipped) @@ -47,6 +39,12 @@ public static function tearDownAfterClass() runkit_function_rename('pinba_timer_stop_bak', 'pinba_timer_stop'); } + public function setUp(){ + if (self::$skipped) { + $this->markTestSkipped(self::$skipMessage); + } + } + public function testTreeLog() { PinbaClient::me()->setTreeLogEnabled(); @@ -55,14 +53,14 @@ public function testTreeLog() PinbaClient::me()->timerStart( 'test', - array("test" => "main") + array("test" => 'main') ); $this->assertEquals(count(PinbaClient::me()->getTreeQueue()), 1); PinbaClient::me()->timerStart( 'subtest', - array("test" => "submain") + array("test" => 'submain') ); $this->assertEquals(count(PinbaClient::me()->getTreeQueue()), 2); @@ -76,6 +74,12 @@ public function testTreeLog() $this->assertEquals(count(PinbaClient::me()->getTreeQueue()), 0); } + + protected static function skip($message) + { + self::$skipMessage = $message; + self::$skipped = true; + } } final class RunkitCallback @@ -90,7 +94,7 @@ public static function start($tags, $data = array()) if (!empty($tags['treeParentId']) && $tags['treeParentId'] != "root") { if ($tags['treeParentId'] != end(self::$queue)) { - throw new Exception("Error generatin tree"); + throw new Exception('Error generatin tree'); } } @@ -107,7 +111,7 @@ public static function stop($id) $tree_id = $current['treeId']; if (end(self::$queue) != $tree_id) { - throw new Exception("Error generatin tree"); + throw new Exception('Error generatin tree'); } array_pop(self::$queue); From 941b436176a8d17a3c9fa1c496bbc0aefd039363 Mon Sep 17 00:00:00 2001 From: anaumenko Date: Fri, 20 Apr 2012 13:03:13 +0400 Subject: [PATCH 13/22] SequentialCache->ensureConnected => ensureTriedToConnect --- core/Cache/PeclMemcached.class.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/core/Cache/PeclMemcached.class.php b/core/Cache/PeclMemcached.class.php index e48f706656..38b36fc556 100644 --- a/core/Cache/PeclMemcached.class.php +++ b/core/Cache/PeclMemcached.class.php @@ -66,7 +66,7 @@ public function __destruct() public function isAlive() { - $this->ensureConnected(); + $this->ensureTriedToConnect(); return parent::isAlive(); } @@ -76,7 +76,7 @@ public function isAlive() **/ public function clean() { - $this->ensureConnected(); + $this->ensureTriedToConnect(); try { $this->instance->flush(); @@ -89,7 +89,7 @@ public function clean() public function increment($key, $value) { - $this->ensureConnected(); + $this->ensureTriedToConnect(); try { return $this->instance->increment($key, $value); @@ -100,7 +100,7 @@ public function increment($key, $value) public function decrement($key, $value) { - $this->ensureConnected(); + $this->ensureTriedToConnect(); try { return $this->instance->decrement($key, $value); @@ -111,7 +111,7 @@ public function decrement($key, $value) public function getList($indexes) { - $this->ensureConnected(); + $this->ensureTriedToConnect(); return ($return = $this->get($indexes)) @@ -121,7 +121,7 @@ public function getList($indexes) public function get($index) { - $this->ensureConnected(); + $this->ensureTriedToConnect(); try { return $this->instance->get($index); @@ -139,7 +139,7 @@ public function get($index) public function delete($index) { - $this->ensureConnected(); + $this->ensureTriedToConnect(); try { // second parameter required, wrt new memcached protocol: @@ -155,7 +155,7 @@ public function delete($index) public function append($key, $data) { - $this->ensureConnected(); + $this->ensureTriedToConnect(); try { return $this->instance->append($key, $data); @@ -172,7 +172,7 @@ public function append($key, $data) */ public function setTimeout($requestTimeout) { - $this->ensureConnected(); + $this->ensureTriedToConnect(); $this->requestTimeout = $requestTimeout; $this->instance->setServerParams($this->host, $this->port, $requestTimeout); @@ -187,7 +187,7 @@ public function getTimeout() return $this->requestTimeout; } - protected function ensureConnected() { + protected function ensureTriedToConnect() { if ($this->triedConnect) return $this; @@ -216,7 +216,7 @@ protected function store( $action, $key, $value, $expires = Cache::EXPIRES_MEDIUM ) { - $this->ensureConnected(); + $this->ensureTriedToConnect(); try { return $this->instance->$action( From d8be9378ebd2b93efe97f4b80ea1a57cfac1f242 Mon Sep 17 00:00:00 2001 From: anaumenko Date: Fri, 20 Apr 2012 13:44:56 +0400 Subject: [PATCH 14/22] coding styles fixes --- core/Cache/PeclMemcached.class.php | 11 ++++++----- core/Cache/SequentialCache.class.php | 5 ++++- test/main/Utils/PinbaTest.class.php | 18 ++++++++++++------ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/core/Cache/PeclMemcached.class.php b/core/Cache/PeclMemcached.class.php index 38b36fc556..ce02cc5c16 100644 --- a/core/Cache/PeclMemcached.class.php +++ b/core/Cache/PeclMemcached.class.php @@ -23,12 +23,12 @@ class PeclMemcached extends CachePeer const DEFAULT_HOST = '127.0.0.1'; const DEFAULT_TIMEOUT = 1; - private $instance = null; + private $instance = null; private $requestTimeout = null; private $connectTimeout = null; - private $host = null; - private $port = null; - private $triedConnect = false; + private $host = null; + private $port = null; + private $triedConnect = false; /** * @return PeclMemcached @@ -187,7 +187,8 @@ public function getTimeout() return $this->requestTimeout; } - protected function ensureTriedToConnect() { + protected function ensureTriedToConnect() + { if ($this->triedConnect) return $this; diff --git a/core/Cache/SequentialCache.class.php b/core/Cache/SequentialCache.class.php index ab9df01030..29a082cb94 100644 --- a/core/Cache/SequentialCache.class.php +++ b/core/Cache/SequentialCache.class.php @@ -82,7 +82,10 @@ public function get($key) /* @var $val CachePeer */ $result = $val->get($key); - if (!empty($result) || $val->isAlive()) { + if ( + !empty($result) + || $val->isAlive() + ) { return $result; } } diff --git a/test/main/Utils/PinbaTest.class.php b/test/main/Utils/PinbaTest.class.php index 95e88f5cfe..ba3073b79b 100644 --- a/test/main/Utils/PinbaTest.class.php +++ b/test/main/Utils/PinbaTest.class.php @@ -2,8 +2,8 @@ final class PinbaTest extends TestCase { - protected static $skipMessage = 'unknown error'; - protected static $skipped = false; + protected static $skipMessage = 'unknown error'; + protected static $skipped = false; public static function setUpBeforeClass() { @@ -39,7 +39,8 @@ public static function tearDownAfterClass() runkit_function_rename('pinba_timer_stop_bak', 'pinba_timer_stop'); } - public function setUp(){ + public function setUp() + { if (self::$skipped) { $this->markTestSkipped(self::$skipMessage); } @@ -92,7 +93,10 @@ public static function start($tags, $data = array()) self::$log[] = $tags; end(self::$log); - if (!empty($tags['treeParentId']) && $tags['treeParentId'] != "root") { + if ( + !empty($tags['treeParentId']) + && $tags['treeParentId'] != "root" + ) { if ($tags['treeParentId'] != end(self::$queue)) { throw new Exception('Error generatin tree'); } @@ -119,11 +123,13 @@ public static function stop($id) } } - function pinba_timer_start_callback ($tags, $data = array()) { + function pinba_timer_start_callback ($tags, $data = array()) + { return RunkitCallback::start($tags, $data); } - function pinba_timer_stop_callback($id){ + function pinba_timer_stop_callback($id) + { return RunkitCallback::stop($id); } ?> \ No newline at end of file From d365bcb4e675b38c9d4c4858a8ca16810db4c9d3 Mon Sep 17 00:00:00 2001 From: anaumenko Date: Fri, 20 Apr 2012 13:46:00 +0400 Subject: [PATCH 15/22] coding styles fixes --- core/Cache/PeclMemcached.class.php | 1 - core/Cache/SequentialCache.class.php | 2 +- test/main/Utils/PinbaTest.class.php | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/core/Cache/PeclMemcached.class.php b/core/Cache/PeclMemcached.class.php index ce02cc5c16..fd50b7ea3b 100644 --- a/core/Cache/PeclMemcached.class.php +++ b/core/Cache/PeclMemcached.class.php @@ -236,4 +236,3 @@ protected function store( } } -?> \ No newline at end of file diff --git a/core/Cache/SequentialCache.class.php b/core/Cache/SequentialCache.class.php index 29a082cb94..c9af273896 100644 --- a/core/Cache/SequentialCache.class.php +++ b/core/Cache/SequentialCache.class.php @@ -127,4 +127,4 @@ private function foreachItem($method, $args) return (bool)$result; } - } \ No newline at end of file + } diff --git a/test/main/Utils/PinbaTest.class.php b/test/main/Utils/PinbaTest.class.php index ba3073b79b..d6263ab770 100644 --- a/test/main/Utils/PinbaTest.class.php +++ b/test/main/Utils/PinbaTest.class.php @@ -132,4 +132,3 @@ function pinba_timer_stop_callback($id) { return RunkitCallback::stop($id); } -?> \ No newline at end of file From f8de1dc61a8c46e4bcae8b30c90b961b814eb4b2 Mon Sep 17 00:00:00 2001 From: anaumenko Date: Fri, 20 Apr 2012 16:39:21 +0400 Subject: [PATCH 16/22] coding style fixes --- core/Cache/PeclMemcached.class.php | 2 +- core/Cache/SequentialCache.class.php | 11 ++++++----- test/core/SequentialCacheTest.class.php | 1 - test/main/Utils/PinbaTest.class.php | 7 +++---- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/core/Cache/PeclMemcached.class.php b/core/Cache/PeclMemcached.class.php index fd50b7ea3b..03fecdeffb 100644 --- a/core/Cache/PeclMemcached.class.php +++ b/core/Cache/PeclMemcached.class.php @@ -109,7 +109,7 @@ public function decrement($key, $value) } } - public function getList($indexes) + public function getList(array $indexes) { $this->ensureTriedToConnect(); diff --git a/core/Cache/SequentialCache.class.php b/core/Cache/SequentialCache.class.php index c9af273896..2a08190c11 100644 --- a/core/Cache/SequentialCache.class.php +++ b/core/Cache/SequentialCache.class.php @@ -33,7 +33,7 @@ final class SequentialCache extends CachePeer * @param array $slaves or CachePeer * @return SequentialCache */ - public static function create(CachePeer $master, $slaves = array()) + public static function create(CachePeer $master, array $slaves = array()) { return new self($master, $slaves); } @@ -42,7 +42,7 @@ public static function create(CachePeer $master, $slaves = array()) * @param CachePeer $master * @param array $slaves or CachePeer */ - public function __construct(CachePeer $master, $slaves = array()) + public function __construct(CachePeer $master, array $slaves = array()) { $this->setMaster($master); @@ -89,6 +89,7 @@ public function get($key) return $result; } } + throw new RuntimeException('All peers are dead'); } @@ -117,14 +118,14 @@ protected function store($action, $key, $value, $expires = Cache::EXPIRES_MEDIUM return $this->foreachItem(__METHOD__, func_get_args()); } - private function foreachItem($method, $args) + private function foreachItem($method, array $args) { $result = true; foreach ($this->list as $val) { - $result &= call_user_func_array(array($val, $method), $args); + $result = call_user_func_array(array($val, $method), $args) && $result; } - return (bool)$result; + return $result; } } diff --git a/test/core/SequentialCacheTest.class.php b/test/core/SequentialCacheTest.class.php index b8a9d6bb42..d1304a353f 100644 --- a/test/core/SequentialCacheTest.class.php +++ b/test/core/SequentialCacheTest.class.php @@ -13,7 +13,6 @@ final class SequentialCacheTest extends TestCase { public function testMultiCacheAliveLast() { - $t = microtime(true); $alife_peer = new PeclMemcached("127.0.0.1", "11211", 0.01); //some existing memcached $alife_peer->set('some_key', 'some_value'); diff --git a/test/main/Utils/PinbaTest.class.php b/test/main/Utils/PinbaTest.class.php index d6263ab770..73d9c42653 100644 --- a/test/main/Utils/PinbaTest.class.php +++ b/test/main/Utils/PinbaTest.class.php @@ -13,9 +13,8 @@ public static function setUpBeforeClass() if (!PinbaClient::isEnabled()) return self::skip('The pinba is not enabled at php.ini (pinba.enabled=1).'); - if (!extension_loaded('runkit')) { + if (!extension_loaded('runkit')) return self::skip('The runkit extension is not available.'); - } if (!ini_get('runkit.internal_override')) return self::skip('The runkit.internal_override is not enabled (enabled it at php.ini).'); @@ -88,7 +87,7 @@ final class RunkitCallback public static $queue = array(); public static $log = array(); - public static function start($tags, $data = array()) + public static function start($tags, array $data = array()) { self::$log[] = $tags; end(self::$log); @@ -123,7 +122,7 @@ public static function stop($id) } } - function pinba_timer_start_callback ($tags, $data = array()) + function pinba_timer_start_callback ($tags, array $data = array()) { return RunkitCallback::start($tags, $data); } From de74f00c12ff9c08514b48e0b9d44c080f78f1fa Mon Sep 17 00:00:00 2001 From: anaumenko Date: Fri, 20 Apr 2012 16:43:08 +0400 Subject: [PATCH 17/22] coding style fixes --- core/Cache/PeclMemcached.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/Cache/PeclMemcached.class.php b/core/Cache/PeclMemcached.class.php index 03fecdeffb..fd50b7ea3b 100644 --- a/core/Cache/PeclMemcached.class.php +++ b/core/Cache/PeclMemcached.class.php @@ -109,7 +109,7 @@ public function decrement($key, $value) } } - public function getList(array $indexes) + public function getList($indexes) { $this->ensureTriedToConnect(); From 11b70f804f67aabc0ea7ed8dd34ffef4093ebd01 Mon Sep 17 00:00:00 2001 From: anaumenko Date: Fri, 20 Apr 2012 16:57:45 +0400 Subject: [PATCH 18/22] coding style fixes --- core/Cache/PeclMemcached.class.php | 1 + core/Cache/SequentialCache.class.php | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/core/Cache/PeclMemcached.class.php b/core/Cache/PeclMemcached.class.php index fd50b7ea3b..c787c43f22 100644 --- a/core/Cache/PeclMemcached.class.php +++ b/core/Cache/PeclMemcached.class.php @@ -218,6 +218,7 @@ protected function store( ) { $this->ensureTriedToConnect(); + try { return $this->instance->$action( diff --git a/core/Cache/SequentialCache.class.php b/core/Cache/SequentialCache.class.php index 2a08190c11..ba2bdeedd9 100644 --- a/core/Cache/SequentialCache.class.php +++ b/core/Cache/SequentialCache.class.php @@ -15,18 +15,18 @@ final class SequentialCache extends CachePeer * List of all peers, including master * @var array of CachePeer */ - protected $list = array(); + private $list = array(); /** * List of slaves only * @var array of CachePeer */ - protected $slaves = array(); + private $slaves = array(); /** * @var CachePeer */ - protected $master = null; + private $master = null; /** * @param CachePeer $master @@ -122,8 +122,9 @@ private function foreachItem($method, array $args) { $result = true; - foreach ($this->list as $val) { - $result = call_user_func_array(array($val, $method), $args) && $result; + foreach ($this->list as $peer) { + /* @var $peer CachePeer */ + $result = call_user_func_array(array($peer, $method), $args) && $result; } return $result; From 35566efdfa83a32cee0c97e0a09f28ea8a05213d Mon Sep 17 00:00:00 2001 From: "Evgeniy V. Kokovikhin" Date: Mon, 23 Apr 2012 18:33:17 +0400 Subject: [PATCH 19/22] cosmetics --- core/Cache/PeclMemcached.class.php | 2 +- core/Cache/SequentialCache.class.php | 2 +- test/core/SequentialCacheTest.class.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/Cache/PeclMemcached.class.php b/core/Cache/PeclMemcached.class.php index c787c43f22..21af1cfdec 100644 --- a/core/Cache/PeclMemcached.class.php +++ b/core/Cache/PeclMemcached.class.php @@ -1,6 +1,6 @@ Date: Tue, 24 Apr 2012 11:59:12 +0400 Subject: [PATCH 20/22] don't needed --- core/Cache/PeclMemcached.class.php | 1 - 1 file changed, 1 deletion(-) diff --git a/core/Cache/PeclMemcached.class.php b/core/Cache/PeclMemcached.class.php index 21af1cfdec..2252ba2c62 100644 --- a/core/Cache/PeclMemcached.class.php +++ b/core/Cache/PeclMemcached.class.php @@ -204,7 +204,6 @@ protected function ensureTriedToConnect() } $this->alive = true; - $this->setTimeout($this->connectTimeout); } catch (BaseException $e) { // bad luck. From b8f79468278c4ea1916bdd782f18aebff8d02a8f Mon Sep 17 00:00:00 2001 From: "Evgeniy V. Kokovikhin" Date: Tue, 24 Apr 2012 13:11:00 +0400 Subject: [PATCH 21/22] a bit tests tune --- test/core/SequentialCacheTest.class.php | 46 +++++++++++++++---------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/test/core/SequentialCacheTest.class.php b/test/core/SequentialCacheTest.class.php index 5f5291f693..531ff17cd1 100644 --- a/test/core/SequentialCacheTest.class.php +++ b/test/core/SequentialCacheTest.class.php @@ -13,14 +13,19 @@ final class SequentialCacheTest extends TestCase { public function testMultiCacheAliveLast() { - $alife_peer = new PeclMemcached("127.0.0.1", "11211", 0.01); //some existing memcached - $alife_peer->set('some_key', 'some_value'); - - $slave1 = new PeclMemcached("35.143.65.241", "11211", 0.01); //some not existing memcache + $alifePeer = new PeclMemcached("127.0.0.1", "11211"); //some existing memcached + $alifePeer->set('some_key', 'some_value'); + + $deadPeer = new Memcached("165.42.42.42", "11211"); //some not existing memcache + + $slave1 = new PeclMemcached("35.143.65.241", "11211"); //some not existing memcache - $slave2 = new PeclMemcached("165.34.176.221", "11211", 0.01); //some not existing memcache + $slave2 = + AggregateCache::create()-> + addPeer('dead', new PeclMemcached("165.34.176.221", "11211"))-> //some not existing memcache + addPeer('dead_too', new PeclMemcached("165.34.176.222", "11211")); //some not existing memcache - $cache = new SequentialCache($alife_peer, array($slave1, $slave2, $alife_peer)); + $cache = new SequentialCache($deadPeer, array($slave1, $slave2, $alifePeer)); $result = $cache->get("some_key"); @@ -29,14 +34,14 @@ public function testMultiCacheAliveLast() public function testMultiCacheAliveFirst() { - $alife_peer = new PeclMemcached("127.0.0.1", "11211", 0.01); //some existing memcached - $alife_peer->set('some_key', 'some_value'); + $alifePeer = new Memcached("127.0.0.1", "11211"); //some existing memcached + $alifePeer->set('some_key', 'some_value'); - $slave1 = new PeclMemcached("35.143.65.241", "11211", 0.01); //some not existing memcache + $slave1 = new PeclMemcached("35.143.65.241", "11211"); //some not existing memcache - $slave2 = new PeclMemcached("165.34.176.221", "11211", 0.01); //some not existing memcache + $slave2 = new PeclMemcached("165.34.176.221", "11211"); //some not existing memcache - $cache = new SequentialCache($alife_peer, array($slave1, $slave1, $slave2)); + $cache = new SequentialCache($alifePeer, array($slave1, $slave1, $slave2)); $result = $cache->get("some_key"); @@ -45,10 +50,15 @@ public function testMultiCacheAliveFirst() public function testMultiCacheAliveOnly() { - $alife_peer = new PeclMemcached("127.0.0.1", "11211", 0.01); //some existing memcached - $alife_peer->set('some_key', 'some_value'); + $alifePeer = + CyclicAggregateCache::create()-> //some existing memcached + setSummaryWeight(42)-> + addPeer('first', new PeclMemcached("127.0.0.1", "11211"), 0)-> + addPeer('second', new Memcached("127.0.0.1", "11211"), 21); + + $alifePeer->set('some_key', 'some_value'); - $cache = new SequentialCache($alife_peer); + $cache = new SequentialCache($alifePeer); $result = $cache->get("some_key"); @@ -60,11 +70,11 @@ public function testMultiCacheAliveOnly() */ public function testMultiCacheNoAlive() { - $slave1 = new PeclMemcached("35.143.65.241", "11211", 0.01); //some not existing memcache - $slave2 = new PeclMemcached("165.34.176.221", "11211", 0.01); //some not existing memcache + $dead1 = new PeclMemcached("35.143.65.241", "11211", 0.01); //some not existing memcache + $dead2 = new PeclMemcached("165.34.176.221", "11211", 0.01); //some not existing memcache - $cache = new SequentialCache($slave1, array($slave2)); + $cache = new SequentialCache($dead1, array($dead2)); - $result = $cache->get("some_key"); //will be throw RuntimeException + $result = $cache->get("some_key"); //will throw RuntimeException } } From e85e1b098507c162da12d565dc3deb7824102652 Mon Sep 17 00:00:00 2001 From: "Evgeniy V. Kokovikhin" Date: Thu, 26 Apr 2012 11:34:29 +0400 Subject: [PATCH 22/22] trailing slash was deleted --- core/Cache/PeclMemcached.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/Cache/PeclMemcached.class.php b/core/Cache/PeclMemcached.class.php index 2252ba2c62..e2c41fc41d 100644 --- a/core/Cache/PeclMemcached.class.php +++ b/core/Cache/PeclMemcached.class.php @@ -168,7 +168,7 @@ public function append($key, $data) /** * @param float $requestTimeout time in seconds - * @return \PeclMemcached + * @return PeclMemcached */ public function setTimeout($requestTimeout) {