From e0a88b7bdfe3adc3319224a76bc69d5efddfa9ee Mon Sep 17 00:00:00 2001 From: Pavlo Yatsukhnenko Date: Thu, 13 Oct 2022 08:14:12 +0300 Subject: [PATCH] Issue #2106 Expose the transferred number of bytes --- cluster_library.c | 9 ++++++--- library.c | 7 ++++++- redis_cluster.c | 5 ++++- tests/RedisClusterTest.php | 10 +++++++++- tests/RedisTest.php | 2 ++ 5 files changed, 27 insertions(+), 6 deletions(-) diff --git a/cluster_library.c b/cluster_library.c index 90a6f95..a2b0264 100644 --- a/cluster_library.c +++ b/cluster_library.c @@ -299,9 +299,8 @@ static int cluster_send_readonly(RedisSock *redis_sock) { /* Send MULTI to a specific ReidsSock */ static int cluster_send_multi(redisCluster *c, short slot) { - if (cluster_send_direct(SLOT_SOCK(c,slot), RESP_MULTI_CMD, - sizeof(RESP_MULTI_CMD) - 1, TYPE_LINE) == 0) - { + if (cluster_send_direct(SLOT_SOCK(c,slot), ZEND_STRL(RESP_MULTI_CMD), TYPE_LINE) == 0) { + c->flags->txBytes += sizeof(RESP_MULTI_CMD) - 1; c->cmd_sock->mode = MULTI; return 0; } @@ -1513,6 +1512,9 @@ PHP_REDIS_API int cluster_send_slot(redisCluster *c, short slot, char *cmd, /* Point our cluster to this slot and it's socket */ c->cmd_slot = slot; c->cmd_sock = SLOT_SOCK(c, slot); + if (c->flags->mode != MULTI) { + c->flags->txBytes = 0; + } /* Enable multi mode on this slot if we've been directed to but haven't * send it to this node yet */ @@ -1527,6 +1529,7 @@ PHP_REDIS_API int cluster_send_slot(redisCluster *c, short slot, char *cmd, if (cluster_sock_write(c, cmd, cmd_len, 1) == -1) { return -1; } + c->flags->txBytes += cmd_len; /* Check our response */ if (cluster_check_response(c, &c->reply_type) != 0 || diff --git a/library.c b/library.c index b2e7923..0709963 100644 --- a/library.c +++ b/library.c @@ -3128,7 +3128,12 @@ redis_sock_write(RedisSock *redis_sock, char *cmd, size_t sz) if (redis_check_eof(redis_sock, 0, 0) == 0 && php_stream_write(redis_sock->stream, cmd, sz) == sz ) { - return redis_sock->txBytes = sz; + if (IS_MULTI(redis_sock)) { + redis_sock->txBytes += sz; + } else { + redis_sock->txBytes = sz; + } + return sz; } return -1; } diff --git a/redis_cluster.c b/redis_cluster.c index 22a83e0..b0afe85 100644 --- a/redis_cluster.c +++ b/redis_cluster.c @@ -1712,7 +1712,8 @@ PHP_METHOD(RedisCluster, clearlasterror) { } PHP_METHOD(RedisCluster, gettransferredbytes) { - CLUSTER_THROW_EXCEPTION("Not implemented", 0); + redisCluster *c = GET_CONTEXT(); + RETURN_LONG(c->flags->txBytes); } /* }}} */ @@ -1833,6 +1834,8 @@ PHP_METHOD(RedisCluster, multi) { /* Flag that we're in MULTI mode */ c->flags->mode = MULTI; + c->flags->txBytes = 0; + /* Return our object so we can chain MULTI calls */ RETVAL_ZVAL(getThis(), 1, 0); } diff --git a/tests/RedisClusterTest.php b/tests/RedisClusterTest.php index 88e8469..b9fff00 100644 --- a/tests/RedisClusterTest.php +++ b/tests/RedisClusterTest.php @@ -50,7 +50,6 @@ class Redis_Cluster_Test extends Redis_Test { public function testReset() { return $this->markTestSkipped(); } public function testInvalidAuthArgs() { return $this->markTestSkipped(); } public function testScanErrors() { return $this->markTestSkipped(); } - public function testTransferredBytes() { return $this->markTestSkipped(); } public function testlMove() { return $this->markTestSkipped(); } public function testlPos() { return $this->marktestSkipped(); } @@ -759,6 +758,15 @@ class Redis_Cluster_Test extends Redis_Test { ini_set('redis.pconnect.pooling_enabled', $prev_value); } + public function testTransferredBytes() { + $this->assertTrue($this->redis->ping('')); + $this->assertEquals(strlen("*1\r\n$4\r\nPING\r\n"), $this->redis->getTransferredBytes()); + $this->assertEquals(['cluster_enabled' => 1], $this->redis->info('', 'cluster')); + $this->assertEquals(strlen("*2\r\n$4\r\nINFO\r\n$7\r\ncluster\r\n"), $this->redis->getTransferredBytes()); + $this->assertEquals([true, true], $this->redis->multi()->ping('')->ping('')->exec()); + $this->assertEquals(strlen("*1\r\n$5\r\nMULTI\r\n*1\r\n$4\r\nEXEC\r\n") + 2 * strlen("*2\r\n$4\r\nPING\r\n"), $this->redis->getTransferredBytes()); + } + /** * @inheritdoc */ diff --git a/tests/RedisTest.php b/tests/RedisTest.php index 53226a9..57ca4c2 100644 --- a/tests/RedisTest.php +++ b/tests/RedisTest.php @@ -5757,6 +5757,8 @@ class Redis_Test extends TestSuite $this->assertEquals(strlen("*1\r\n$4\r\nPING\r\n"), $this->redis->getTransferredBytes()); $this->assertEquals(['cluster_enabled' => 0], $this->redis->info('cluster')); $this->assertEquals(strlen("*2\r\n$4\r\nINFO\r\n$7\r\ncluster\r\n"), $this->redis->getTransferredBytes()); + $this->assertEquals([true, true], $this->redis->multi()->ping()->ping()->exec()); + $this->assertEquals(strlen("*1\r\n$5\r\nMULTI\r\n*1\r\n$4\r\nEXEC\r\n") + 2 * strlen("*2\r\n$4\r\nPING\r\n"), $this->redis->getTransferredBytes()); } /**