From e63354a6e71427b273182455b052ffb606e0eadc Mon Sep 17 00:00:00 2001 From: Stanley Cheung Date: Mon, 10 Aug 2015 15:46:42 -0700 Subject: [PATCH 1/5] php: wrap getConnectivityState API --- src/php/ext/grpc/channel.c | 21 +++++++++++++++++++++ src/php/ext/grpc/php_grpc.c | 12 ++++++++++++ src/php/lib/Grpc/BaseStub.php | 8 ++++++++ src/php/tests/unit_tests/EndToEndTest.php | 11 +++++++++++ 4 files changed, 52 insertions(+) diff --git a/src/php/ext/grpc/channel.c b/src/php/ext/grpc/channel.c index 7d8a6f87ab6..8a3fe114128 100644 --- a/src/php/ext/grpc/channel.c +++ b/src/php/ext/grpc/channel.c @@ -204,6 +204,26 @@ PHP_METHOD(Channel, getTarget) { RETURN_STRING(grpc_channel_get_target(channel->wrapped), 1); } +/** + * Get the connectivity state of the channel + * @param bool (optional) try to connect on the channel + * @return long The grpc connectivity state + */ +PHP_METHOD(Channel, getConnectivityState) { + wrapped_grpc_channel *channel = + (wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC); + bool try_to_connect; + /* "|b" == 1 optional bool */ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &try_to_connect) == + FAILURE) { + zend_throw_exception(spl_ce_InvalidArgumentException, + "getConnectivityState expects a bool", 1 TSRMLS_CC); + return; + } + RETURN_LONG(grpc_channel_check_connectivity_state(channel->wrapped, + (int)try_to_connect)); +} + /** * Close the channel */ @@ -219,6 +239,7 @@ PHP_METHOD(Channel, close) { static zend_function_entry channel_methods[] = { PHP_ME(Channel, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) PHP_ME(Channel, getTarget, NULL, ZEND_ACC_PUBLIC) + PHP_ME(Channel, getConnectivityState, NULL, ZEND_ACC_PUBLIC) PHP_ME(Channel, close, NULL, ZEND_ACC_PUBLIC) PHP_FE_END}; diff --git a/src/php/ext/grpc/php_grpc.c b/src/php/ext/grpc/php_grpc.c index fedcf0f5392..0f730ea7567 100644 --- a/src/php/ext/grpc/php_grpc.c +++ b/src/php/ext/grpc/php_grpc.c @@ -183,6 +183,18 @@ PHP_MINIT_FUNCTION(grpc) { REGISTER_LONG_CONSTANT("Grpc\\OP_RECV_CLOSE_ON_SERVER", GRPC_OP_RECV_CLOSE_ON_SERVER, CONST_CS); + /* Register connectivity state constants */ + REGISTER_LONG_CONSTANT("Grpc\\CHANNEL_IDLE", + GRPC_CHANNEL_IDLE, CONST_CS); + REGISTER_LONG_CONSTANT("Grpc\\CHANNEL_CONNECTING", + GRPC_CHANNEL_CONNECTING, CONST_CS); + REGISTER_LONG_CONSTANT("Grpc\\CHANNEL_READY", + GRPC_CHANNEL_READY, CONST_CS); + REGISTER_LONG_CONSTANT("Grpc\\CHANNEL_TRANSIENT_FAILURE", + GRPC_CHANNEL_TRANSIENT_FAILURE, CONST_CS); + REGISTER_LONG_CONSTANT("Grpc\\CHANNEL_FATAL_FAILURE", + GRPC_CHANNEL_FATAL_FAILURE, CONST_CS); + grpc_init_call(TSRMLS_C); grpc_init_channel(TSRMLS_C); grpc_init_server(TSRMLS_C); diff --git a/src/php/lib/Grpc/BaseStub.php b/src/php/lib/Grpc/BaseStub.php index a0c677908c7..9f5c07b5f5e 100755 --- a/src/php/lib/Grpc/BaseStub.php +++ b/src/php/lib/Grpc/BaseStub.php @@ -74,6 +74,14 @@ class BaseStub { return $this->channel->getTarget(); } + /** + * @param $try_to_connect bool + * @return int The grpc connectivity state + */ + public function getConnectivityState($try_to_connect = false) { + return $this->channel->getConnectivityState($try_to_connect); + } + /** * Close the communication channel associated with this stub */ diff --git a/src/php/tests/unit_tests/EndToEndTest.php b/src/php/tests/unit_tests/EndToEndTest.php index 27e27cdfdf3..d49bc9ac3ac 100755 --- a/src/php/tests/unit_tests/EndToEndTest.php +++ b/src/php/tests/unit_tests/EndToEndTest.php @@ -153,4 +153,15 @@ class EndToEndTest extends PHPUnit_Framework_TestCase{ public function testGetTarget() { $this->assertTrue(is_string($this->channel->getTarget())); } + + /** + * @medium + */ + public function testGetConnectivityState() { + $old_state = Grpc\CHANNEL_IDLE; + $this->assertTrue($this->channel->getConnectivityState() == $old_state); + $this->assertTrue($this->channel->getConnectivityState(true) == $old_state); + usleep(500000); + $this->assertTrue($this->channel->getConnectivityState() != $old_state); + } } From a63fdd01263f29bdf2ec0d0975f7386370de47ba Mon Sep 17 00:00:00 2001 From: Stanley Cheung Date: Tue, 11 Aug 2015 15:11:11 -0700 Subject: [PATCH 2/5] php: wrap watchConnectivityState api --- src/php/ext/grpc/channel.c | 41 ++++++++++++++++++++++- src/php/tests/unit_tests/EndToEndTest.php | 19 ++++++----- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/php/ext/grpc/channel.c b/src/php/ext/grpc/channel.c index 8a3fe114128..ebb1320c860 100644 --- a/src/php/ext/grpc/channel.c +++ b/src/php/ext/grpc/channel.c @@ -51,8 +51,10 @@ #include #include -#include "server.h" +#include "completion_queue.h" #include "credentials.h" +#include "server.h" +#include "timeval.h" zend_class_entry *grpc_ce_channel; @@ -224,6 +226,42 @@ PHP_METHOD(Channel, getConnectivityState) { (int)try_to_connect)); } +/** + * Watch the connectivity state of the channel until it changed + * @param long The previous connectivity state of the channel + * @param Timeval The deadline this function should wait until + */ +PHP_METHOD(Channel, watchConnectivityState) { + wrapped_grpc_channel *channel = + (wrapped_grpc_channel *)zend_object_store_get_object(getThis() TSRMLS_CC); + long last_state; + zval *deadline_obj; + /* "lO" == 1 long 1 object */ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lO", + &last_state, &deadline_obj, grpc_ce_timeval) == FAILURE) { + zend_throw_exception(spl_ce_InvalidArgumentException, + "watchConnectivityState expects 1 long 1 timeval", + 1 TSRMLS_CC); + return; + } + + wrapped_grpc_timeval *deadline = + (wrapped_grpc_timeval *)zend_object_store_get_object( + deadline_obj TSRMLS_CC); + grpc_channel_watch_connectivity_state( + channel->wrapped, (grpc_connectivity_state)last_state, + deadline->wrapped, completion_queue, NULL); + grpc_event event = grpc_completion_queue_pluck( + completion_queue, NULL, + gpr_inf_future(GPR_CLOCK_REALTIME)); + if (!event.success) { + zend_throw_exception(spl_ce_LogicException, + "watchConnectivityState failed", + 1 TSRMLS_CC); + } + return; +} + /** * Close the channel */ @@ -240,6 +278,7 @@ static zend_function_entry channel_methods[] = { PHP_ME(Channel, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) PHP_ME(Channel, getTarget, NULL, ZEND_ACC_PUBLIC) PHP_ME(Channel, getConnectivityState, NULL, ZEND_ACC_PUBLIC) + PHP_ME(Channel, watchConnectivityState, NULL, ZEND_ACC_PUBLIC) PHP_ME(Channel, close, NULL, ZEND_ACC_PUBLIC) PHP_FE_END}; diff --git a/src/php/tests/unit_tests/EndToEndTest.php b/src/php/tests/unit_tests/EndToEndTest.php index d49bc9ac3ac..3a44fc77664 100755 --- a/src/php/tests/unit_tests/EndToEndTest.php +++ b/src/php/tests/unit_tests/EndToEndTest.php @@ -154,14 +154,17 @@ class EndToEndTest extends PHPUnit_Framework_TestCase{ $this->assertTrue(is_string($this->channel->getTarget())); } - /** - * @medium - */ public function testGetConnectivityState() { - $old_state = Grpc\CHANNEL_IDLE; - $this->assertTrue($this->channel->getConnectivityState() == $old_state); - $this->assertTrue($this->channel->getConnectivityState(true) == $old_state); - usleep(500000); - $this->assertTrue($this->channel->getConnectivityState() != $old_state); + $this->assertTrue($this->channel->getConnectivityState() == Grpc\CHANNEL_IDLE); + } + + public function testWatchConnectivityState() { + $old_state = $this->channel->getConnectivityState(true); + $deadline = microtime(true) * 1000000 + 500000; + $this->channel->watchConnectivityState( + $old_state, + new Grpc\Timeval($deadline)); + $new_state = $this->channel->getConnectivityState(); + $this->assertTrue($old_state != $new_state); } } From 4c5c7b8647522fa9f21b33add09ae4b7d5ca6512 Mon Sep 17 00:00:00 2001 From: Stanley Cheung Date: Wed, 12 Aug 2015 16:28:58 -0700 Subject: [PATCH 3/5] php: add more tests --- src/php/ext/grpc/channel.c | 8 +++--- src/php/tests/unit_tests/EndToEndTest.php | 31 ++++++++++++++++++----- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/php/ext/grpc/channel.c b/src/php/ext/grpc/channel.c index ebb1320c860..5e0de2959aa 100644 --- a/src/php/ext/grpc/channel.c +++ b/src/php/ext/grpc/channel.c @@ -230,6 +230,8 @@ PHP_METHOD(Channel, getConnectivityState) { * Watch the connectivity state of the channel until it changed * @param long The previous connectivity state of the channel * @param Timeval The deadline this function should wait until + * @return bool If the connectivity state changes from last_state + * before deadline */ PHP_METHOD(Channel, watchConnectivityState) { wrapped_grpc_channel *channel = @@ -255,11 +257,9 @@ PHP_METHOD(Channel, watchConnectivityState) { completion_queue, NULL, gpr_inf_future(GPR_CLOCK_REALTIME)); if (!event.success) { - zend_throw_exception(spl_ce_LogicException, - "watchConnectivityState failed", - 1 TSRMLS_CC); + RETURN_BOOL(0); } - return; + RETURN_BOOL(1); } /** diff --git a/src/php/tests/unit_tests/EndToEndTest.php b/src/php/tests/unit_tests/EndToEndTest.php index 3a44fc77664..df4cebc9662 100755 --- a/src/php/tests/unit_tests/EndToEndTest.php +++ b/src/php/tests/unit_tests/EndToEndTest.php @@ -158,13 +158,30 @@ class EndToEndTest extends PHPUnit_Framework_TestCase{ $this->assertTrue($this->channel->getConnectivityState() == Grpc\CHANNEL_IDLE); } - public function testWatchConnectivityState() { - $old_state = $this->channel->getConnectivityState(true); - $deadline = microtime(true) * 1000000 + 500000; - $this->channel->watchConnectivityState( - $old_state, - new Grpc\Timeval($deadline)); + public function testWatchConnectivityStateFailed() { + $idle_state = $this->channel->getConnectivityState(true); + $this->assertTrue($idle_state == Grpc\CHANNEL_IDLE); + + $now = Grpc\Timeval::now(); + $delta = new Grpc\Timeval(1); + $deadline = $now->add($delta); + + $this->assertFalse($this->channel->watchConnectivityState( + $idle_state, $deadline)); + } + + public function testWatchConnectivityStateSuccess() { + $idle_state = $this->channel->getConnectivityState(true); + $this->assertTrue($idle_state == Grpc\CHANNEL_IDLE); + + $now = Grpc\Timeval::now(); + $delta = new Grpc\Timeval(100000); + $deadline = $now->add($delta); + + $this->assertTrue($this->channel->watchConnectivityState( + $idle_state, $deadline)); + $new_state = $this->channel->getConnectivityState(); - $this->assertTrue($old_state != $new_state); + $this->assertTrue($idle_state != $new_state); } } From 04b7a41d18744d2e882a486862ec2afb7433655f Mon Sep 17 00:00:00 2001 From: Stanley Cheung Date: Thu, 13 Aug 2015 09:39:04 -0700 Subject: [PATCH 4/5] php: add watchForReady method --- src/php/lib/Grpc/BaseStub.php | 34 +++++++++++++++++++++++ src/php/tests/unit_tests/EndToEndTest.php | 17 +++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/php/lib/Grpc/BaseStub.php b/src/php/lib/Grpc/BaseStub.php index 9f5c07b5f5e..dc507df92cf 100755 --- a/src/php/lib/Grpc/BaseStub.php +++ b/src/php/lib/Grpc/BaseStub.php @@ -82,6 +82,40 @@ class BaseStub { return $this->channel->getConnectivityState($try_to_connect); } + /** + * @param $timeout in microseconds + * @return bool true if channel is ready + */ + public function watchForReady($timeout) { + $new_state = $this->getConnectivityState(true); + if ($this->_checkConnectivityState($new_state)) { + return true; + } + + $now = Timeval::now(); + $delta = new Timeval($timeout); + $deadline = $now->add($delta); + + if (!$this->channel->watchConnectivityState($new_state, $deadline)) { + return false; + } + $new_state = $this->getConnectivityState(); + if ($this->_checkConnectivityState($new_state)) { + return true; + } + return false; + } + + private function _checkConnectivityState($new_state) { + if ($new_state == Grpc\CHANNEL_READY) { + return true; + } + if ($new_state == Grpc\CHANNEL_FATAL_ERROR) { + throw new Exception('Failed to connect to server'); + } + return false; + } + /** * Close the communication channel associated with this stub */ diff --git a/src/php/tests/unit_tests/EndToEndTest.php b/src/php/tests/unit_tests/EndToEndTest.php index df4cebc9662..4c0cf91d512 100755 --- a/src/php/tests/unit_tests/EndToEndTest.php +++ b/src/php/tests/unit_tests/EndToEndTest.php @@ -175,7 +175,7 @@ class EndToEndTest extends PHPUnit_Framework_TestCase{ $this->assertTrue($idle_state == Grpc\CHANNEL_IDLE); $now = Grpc\Timeval::now(); - $delta = new Grpc\Timeval(100000); + $delta = new Grpc\Timeval(3000000); // should finish well before $deadline = $now->add($delta); $this->assertTrue($this->channel->watchConnectivityState( @@ -184,4 +184,19 @@ class EndToEndTest extends PHPUnit_Framework_TestCase{ $new_state = $this->channel->getConnectivityState(); $this->assertTrue($idle_state != $new_state); } + + public function testWatchConnectivityStateDoNothing() { + $idle_state = $this->channel->getConnectivityState(); + $this->assertTrue($idle_state == Grpc\CHANNEL_IDLE); + + $now = Grpc\Timeval::now(); + $delta = new Grpc\Timeval(100000); + $deadline = $now->add($delta); + + $this->assertFalse($this->channel->watchConnectivityState( + $idle_state, $deadline)); + + $new_state = $this->channel->getConnectivityState(); + $this->assertTrue($new_state == Grpc\CHANNEL_IDLE); + } } From 1567c0c9a624ffab4ab786979573c41a5bce2c2c Mon Sep 17 00:00:00 2001 From: Stanley Cheung Date: Thu, 13 Aug 2015 11:12:54 -0700 Subject: [PATCH 5/5] php: connectivity state review feedback --- src/php/ext/grpc/channel.c | 5 +---- src/php/lib/Grpc/BaseStub.php | 17 ++++++++++------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/php/ext/grpc/channel.c b/src/php/ext/grpc/channel.c index 5e0de2959aa..447cfc15bee 100644 --- a/src/php/ext/grpc/channel.c +++ b/src/php/ext/grpc/channel.c @@ -256,10 +256,7 @@ PHP_METHOD(Channel, watchConnectivityState) { grpc_event event = grpc_completion_queue_pluck( completion_queue, NULL, gpr_inf_future(GPR_CLOCK_REALTIME)); - if (!event.success) { - RETURN_BOOL(0); - } - RETURN_BOOL(1); + RETURN_BOOL(event.success); } /** diff --git a/src/php/lib/Grpc/BaseStub.php b/src/php/lib/Grpc/BaseStub.php index dc507df92cf..9d6a77b8553 100755 --- a/src/php/lib/Grpc/BaseStub.php +++ b/src/php/lib/Grpc/BaseStub.php @@ -85,8 +85,9 @@ class BaseStub { /** * @param $timeout in microseconds * @return bool true if channel is ready + * @throw Exception if channel is in FATAL_ERROR state */ - public function watchForReady($timeout) { + public function waitForReady($timeout) { $new_state = $this->getConnectivityState(true); if ($this->_checkConnectivityState($new_state)) { return true; @@ -96,14 +97,16 @@ class BaseStub { $delta = new Timeval($timeout); $deadline = $now->add($delta); - if (!$this->channel->watchConnectivityState($new_state, $deadline)) { - return false; + while ($this->channel->watchConnectivityState($new_state, $deadline)) { + // state has changed before deadline + $new_state = $this->getConnectivityState(); + if ($this->_checkConnectivityState($new_state)) { + return true; + } } + // deadline has passed $new_state = $this->getConnectivityState(); - if ($this->_checkConnectivityState($new_state)) { - return true; - } - return false; + return $this->_checkConnectivityState($new_state); } private function _checkConnectivityState($new_state) {