]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/libs/redis/RedisConnRef.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / includes / libs / redis / RedisConnRef.php
1 <?php
2 /**
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16  * http://www.gnu.org/copyleft/gpl.html
17  *
18  * @file
19  */
20 use Psr\Log\LoggerInterface;
21 use Psr\Log\LoggerAwareInterface;
22
23 /**
24  * Helper class to handle automatically marking connectons as reusable (via RAII pattern)
25  *
26  * This class simply wraps the Redis class and can be used the same way
27  *
28  * @ingroup Redis
29  * @since 1.21
30  */
31 class RedisConnRef implements LoggerAwareInterface {
32         /** @var RedisConnectionPool */
33         protected $pool;
34         /** @var Redis */
35         protected $conn;
36
37         protected $server; // string
38         protected $lastError; // string
39
40         /**
41          * @var LoggerInterface
42          */
43         protected $logger;
44
45         /**
46          * @param RedisConnectionPool $pool
47          * @param string $server
48          * @param Redis $conn
49          * @param LoggerInterface $logger
50          */
51         public function __construct(
52                 RedisConnectionPool $pool, $server, Redis $conn, LoggerInterface $logger
53         ) {
54                 $this->pool = $pool;
55                 $this->server = $server;
56                 $this->conn = $conn;
57                 $this->logger = $logger;
58         }
59
60         public function setLogger( LoggerInterface $logger ) {
61                 $this->logger = $logger;
62         }
63
64         /**
65          * @return string
66          * @since 1.23
67          */
68         public function getServer() {
69                 return $this->server;
70         }
71
72         public function getLastError() {
73                 return $this->lastError;
74         }
75
76         public function clearLastError() {
77                 $this->lastError = null;
78         }
79
80         public function __call( $name, $arguments ) {
81                 $conn = $this->conn; // convenience
82
83                 // Work around https://github.com/nicolasff/phpredis/issues/70
84                 $lname = strtolower( $name );
85                 if ( ( $lname === 'blpop' || $lname == 'brpop' )
86                         && is_array( $arguments[0] ) && isset( $arguments[1] )
87                 ) {
88                         $this->pool->resetTimeout( $conn, $arguments[1] + 1 );
89                 } elseif ( $lname === 'brpoplpush' && isset( $arguments[2] ) ) {
90                         $this->pool->resetTimeout( $conn, $arguments[2] + 1 );
91                 }
92
93                 $conn->clearLastError();
94                 try {
95                         $res = call_user_func_array( [ $conn, $name ], $arguments );
96                         if ( preg_match( '/^ERR operation not permitted\b/', $conn->getLastError() ) ) {
97                                 $this->pool->reauthenticateConnection( $this->server, $conn );
98                                 $conn->clearLastError();
99                                 $res = call_user_func_array( [ $conn, $name ], $arguments );
100                                 $this->logger->info(
101                                         "Used automatic re-authentication for method '$name'.",
102                                         [ 'redis_server' => $this->server ]
103                                 );
104                         }
105                 } catch ( RedisException $e ) {
106                         $this->pool->resetTimeout( $conn ); // restore
107                         throw $e;
108                 }
109
110                 $this->lastError = $conn->getLastError() ?: $this->lastError;
111
112                 $this->pool->resetTimeout( $conn ); // restore
113
114                 return $res;
115         }
116
117         /**
118          * @param string $script
119          * @param array $params
120          * @param int $numKeys
121          * @return mixed
122          * @throws RedisException
123          */
124         public function luaEval( $script, array $params, $numKeys ) {
125                 $sha1 = sha1( $script ); // 40 char hex
126                 $conn = $this->conn; // convenience
127                 $server = $this->server; // convenience
128
129                 // Try to run the server-side cached copy of the script
130                 $conn->clearLastError();
131                 $res = $conn->evalSha( $sha1, $params, $numKeys );
132                 // If we got a permission error reply that means that (a) we are not in
133                 // multi()/pipeline() and (b) some connection problem likely occurred. If
134                 // the password the client gave was just wrong, an exception should have
135                 // been thrown back in getConnection() previously.
136                 if ( preg_match( '/^ERR operation not permitted\b/', $conn->getLastError() ) ) {
137                         $this->pool->reauthenticateConnection( $server, $conn );
138                         $conn->clearLastError();
139                         $res = $conn->eval( $script, $params, $numKeys );
140                         $this->logger->info(
141                                 "Used automatic re-authentication for Lua script '$sha1'.",
142                                 [ 'redis_server' => $server ]
143                         );
144                 }
145                 // If the script is not in cache, use eval() to retry and cache it
146                 if ( preg_match( '/^NOSCRIPT/', $conn->getLastError() ) ) {
147                         $conn->clearLastError();
148                         $res = $conn->eval( $script, $params, $numKeys );
149                         $this->logger->info(
150                                 "Used eval() for Lua script '$sha1'.",
151                                 [ 'redis_server' => $server ]
152                         );
153                 }
154
155                 if ( $conn->getLastError() ) { // script bug?
156                         $this->logger->error(
157                                 'Lua script error on server "{redis_server}": {lua_error}',
158                                 [
159                                         'redis_server' => $server,
160                                         'lua_error' => $conn->getLastError()
161                                 ]
162                         );
163                 }
164
165                 $this->lastError = $conn->getLastError() ?: $this->lastError;
166
167                 return $res;
168         }
169
170         /**
171          * @param Redis $conn
172          * @return bool
173          */
174         public function isConnIdentical( Redis $conn ) {
175                 return $this->conn === $conn;
176         }
177
178         function __destruct() {
179                 $this->pool->freeConnection( $this->server, $this->conn );
180         }
181 }