Merge "jquery.tablesorter: Never initialize twice on the same element"
[lhc/web/wiklou.git] / includes / libs / objectcache / RESTBagOStuff.php
1 <?php
2
3 use Psr\Log\LoggerInterface;
4
5 /**
6 * Interface to key-value storage behind an HTTP server.
7 *
8 * Uses URL of the form "baseURL/{KEY}" to store, fetch, and delete values.
9 *
10 * E.g., when base URL is `/v1/sessions/`, then the store would do:
11 *
12 * `PUT /v1/sessions/12345758`
13 *
14 * and fetch would do:
15 *
16 * `GET /v1/sessions/12345758`
17 *
18 * delete would do:
19 *
20 * `DELETE /v1/sessions/12345758`
21 *
22 * Configure with:
23 *
24 * @code
25 * $wgObjectCaches['sessions'] = array(
26 * 'class' => 'RESTBagOStuff',
27 * 'url' => 'http://localhost:7231/wikimedia.org/v1/sessions/'
28 * );
29 * @endcode
30 */
31 class RESTBagOStuff extends BagOStuff {
32 /**
33 * Default connection timeout in seconds. The kernel retransmits the SYN
34 * packet after 1 second, so 1.2 seconds allows for 1 retransmit without
35 * permanent failure.
36 */
37 const DEFAULT_CONN_TIMEOUT = 1.2;
38
39 /**
40 * Default request timeout
41 */
42 const DEFAULT_REQ_TIMEOUT = 3.0;
43
44 /**
45 * @var MultiHttpClient
46 */
47 private $client;
48
49 /**
50 * REST URL to use for storage.
51 * @var string
52 */
53 private $url;
54
55 public function __construct( $params ) {
56 if ( empty( $params['url'] ) ) {
57 throw new InvalidArgumentException( 'URL parameter is required' );
58 }
59 if ( empty( $params['client'] ) ) {
60 // Pass through some params to the HTTP client.
61 $clientParams = [
62 'connTimeout' => $params['connTimeout'] ?? self::DEFAULT_CONN_TIMEOUT,
63 'reqTimeout' => $params['reqTimeout'] ?? self::DEFAULT_REQ_TIMEOUT,
64 ];
65 foreach ( [ 'caBundlePath', 'proxy' ] as $key ) {
66 if ( isset( $params[$key] ) ) {
67 $clientParams[$key] = $params[$key];
68 }
69 }
70 $this->client = new MultiHttpClient( $clientParams );
71 } else {
72 $this->client = $params['client'];
73 }
74 // The parent constructor calls setLogger() which sets the logger in $this->client
75 parent::__construct( $params );
76 // Make sure URL ends with /
77 $this->url = rtrim( $params['url'], '/' ) . '/';
78 // Default config, R+W > N; no locks on reads though; writes go straight to state-machine
79 $this->attrMap[self::ATTR_SYNCWRITES] = self::QOS_SYNCWRITES_QC;
80 }
81
82 public function setLogger( LoggerInterface $logger ) {
83 parent::setLogger( $logger );
84 $this->client->setLogger( $logger );
85 }
86
87 /**
88 * @param string $key
89 * @param int $flags Bitfield of BagOStuff::READ_* constants [optional]
90 * @return mixed Returns false on failure and if the item does not exist
91 */
92 protected function doGet( $key, $flags = 0 ) {
93 $req = [
94 'method' => 'GET',
95 'url' => $this->url . rawurlencode( $key ),
96 ];
97
98 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
99 if ( $rcode === 200 ) {
100 if ( is_string( $rbody ) ) {
101 return unserialize( $rbody );
102 }
103 return false;
104 }
105 if ( $rcode === 0 || ( $rcode >= 400 && $rcode != 404 ) ) {
106 return $this->handleError( "Failed to fetch $key", $rcode, $rerr );
107 }
108 return false;
109 }
110
111 /**
112 * Handle storage error
113 * @param string $msg Error message
114 * @param int $rcode Error code from client
115 * @param string $rerr Error message from client
116 * @return false
117 */
118 protected function handleError( $msg, $rcode, $rerr ) {
119 $this->logger->error( "$msg : ({code}) {error}", [
120 'code' => $rcode,
121 'error' => $rerr
122 ] );
123 $this->setLastError( $rcode === 0 ? self::ERR_UNREACHABLE : self::ERR_UNEXPECTED );
124 return false;
125 }
126
127 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
128 // @TODO: respect WRITE_SYNC (e.g. EACH_QUORUM)
129 // @TODO: respect $exptime
130 $req = [
131 'method' => 'PUT',
132 'url' => $this->url . rawurlencode( $key ),
133 'body' => serialize( $value )
134 ];
135 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
136 if ( $rcode === 200 || $rcode === 201 || $rcode === 204 ) {
137 return true;
138 }
139 return $this->handleError( "Failed to store $key", $rcode, $rerr );
140 }
141
142 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
143 // @TODO: make this atomic
144 if ( $this->get( $key ) === false ) {
145 return $this->set( $key, $value, $exptime, $flags );
146 }
147
148 return false; // key already set
149 }
150
151 public function delete( $key, $flags = 0 ) {
152 // @TODO: respect WRITE_SYNC (e.g. EACH_QUORUM)
153 $req = [
154 'method' => 'DELETE',
155 'url' => $this->url . rawurlencode( $key ),
156 ];
157 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
158 if ( in_array( $rcode, [ 200, 204, 205, 404, 410 ] ) ) {
159 return true;
160 }
161 return $this->handleError( "Failed to delete $key", $rcode, $rerr );
162 }
163
164 public function incr( $key, $value = 1 ) {
165 // @TODO: make this atomic
166 $n = $this->get( $key, self::READ_LATEST );
167 if ( $this->isInteger( $n ) ) { // key exists?
168 $n = max( $n + intval( $value ), 0 );
169 // @TODO: respect $exptime
170 return $this->set( $key, $n ) ? $n : false;
171 }
172
173 return false;
174 }
175 }