MediaWiki  master
RESTBagOStuff.php
Go to the documentation of this file.
1 <?php
2 
3 use Psr\Log\LoggerInterface;
4 
91  private const DEFAULT_CONN_TIMEOUT = 1.2;
92 
96  private const DEFAULT_REQ_TIMEOUT = 3.0;
97 
101  private $client;
102 
107  private $url;
108 
113  private $httpParams;
114 
119  private $serializationType;
120 
125  private $hmacKey;
126 
130  private $extendedErrorBodyFields;
131 
132  public function __construct( $params ) {
133  $params['segmentationSize'] ??= INF;
134  if ( empty( $params['url'] ) ) {
135  throw new InvalidArgumentException( 'URL parameter is required' );
136  }
137 
138  if ( empty( $params['client'] ) ) {
139  // Pass through some params to the HTTP client.
140  $clientParams = [
141  'connTimeout' => $params['connTimeout'] ?? self::DEFAULT_CONN_TIMEOUT,
142  'reqTimeout' => $params['reqTimeout'] ?? self::DEFAULT_REQ_TIMEOUT,
143  ];
144  foreach ( [ 'caBundlePath', 'proxy', 'telemetry' ] as $key ) {
145  if ( isset( $params[$key] ) ) {
146  $clientParams[$key] = $params[$key];
147  }
148  }
149  $this->client = new MultiHttpClient( $clientParams );
150  } else {
151  $this->client = $params['client'];
152  }
153 
154  $this->httpParams['writeMethod'] = $params['httpParams']['writeMethod'] ?? 'PUT';
155  $this->httpParams['readHeaders'] = $params['httpParams']['readHeaders'] ?? [];
156  $this->httpParams['writeHeaders'] = $params['httpParams']['writeHeaders'] ?? [];
157  $this->httpParams['deleteHeaders'] = $params['httpParams']['deleteHeaders'] ?? [];
158  $this->extendedErrorBodyFields = $params['extendedErrorBodyFields'] ?? [];
159  $this->serializationType = $params['serialization_type'] ?? 'PHP';
160  $this->hmacKey = $params['hmac_key'] ?? '';
161 
162  // The parent constructor calls setLogger() which sets the logger in $this->client
163  parent::__construct( $params );
164 
165  // Make sure URL ends with /
166  $this->url = rtrim( $params['url'], '/' ) . '/';
167 
169  }
170 
171  public function setLogger( LoggerInterface $logger ) {
172  parent::setLogger( $logger );
173  $this->client->setLogger( $logger );
174  }
175 
176  protected function doGet( $key, $flags = 0, &$casToken = null ) {
177  $getToken = ( $casToken === self::PASS_BY_REF );
178  $casToken = null;
179 
180  $req = [
181  'method' => 'GET',
182  'url' => $this->url . rawurlencode( $key ),
183  'headers' => $this->httpParams['readHeaders'],
184  ];
185 
186  $value = false;
187  $valueSize = false;
188  [ $rcode, , $rhdrs, $rbody, $rerr ] = $this->client->run( $req );
189  if ( $rcode === 200 && is_string( $rbody ) ) {
190  $value = $this->decodeBody( $rbody );
191  $valueSize = strlen( $rbody );
192  // @FIXME: use some kind of hash or UUID header as CAS token
193  if ( $getToken && $value !== false ) {
194  $casToken = $rbody;
195  }
196  } elseif ( $rcode === 0 || ( $rcode >= 400 && $rcode != 404 ) ) {
197  $this->handleError( 'Failed to fetch {cacheKey}', $rcode, $rerr, $rhdrs, $rbody,
198  [ 'cacheKey' => $key ] );
199  }
200 
201  $this->updateOpStats( self::METRIC_OP_GET, [ $key => [ 0, $valueSize ] ] );
202 
203  return $value;
204  }
205 
206  protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
207  $req = [
208  'method' => $this->httpParams['writeMethod'],
209  'url' => $this->url . rawurlencode( $key ),
210  'body' => $this->encodeBody( $value ),
211  'headers' => $this->httpParams['writeHeaders'],
212  ];
213 
214  [ $rcode, , $rhdrs, $rbody, $rerr ] = $this->client->run( $req );
215  $res = ( $rcode === 200 || $rcode === 201 || $rcode === 204 );
216  if ( !$res ) {
217  $this->handleError( 'Failed to store {cacheKey}', $rcode, $rerr, $rhdrs, $rbody,
218  [ 'cacheKey' => $key ] );
219  }
220 
221  $this->updateOpStats( self::METRIC_OP_SET, [ $key => [ strlen( $rbody ), 0 ] ] );
222 
223  return $res;
224  }
225 
226  protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
227  // NOTE: This is non-atomic
228  if ( $this->get( $key ) === false ) {
229  return $this->set( $key, $value, $exptime, $flags );
230  }
231 
232  // key already set
233  return false;
234  }
235 
236  protected function doDelete( $key, $flags = 0 ) {
237  $req = [
238  'method' => 'DELETE',
239  'url' => $this->url . rawurlencode( $key ),
240  'headers' => $this->httpParams['deleteHeaders'],
241  ];
242 
243  [ $rcode, , $rhdrs, $rbody, $rerr ] = $this->client->run( $req );
244  $res = in_array( $rcode, [ 200, 204, 205, 404, 410 ] );
245  if ( !$res ) {
246  $this->handleError( 'Failed to delete {cacheKey}', $rcode, $rerr, $rhdrs, $rbody,
247  [ 'cacheKey' => $key ] );
248  }
249 
250  $this->updateOpStats( self::METRIC_OP_DELETE, [ $key ] );
251 
252  return $res;
253  }
254 
255  protected function doIncrWithInit( $key, $exptime, $step, $init, $flags ) {
256  // NOTE: This is non-atomic
257  $curValue = $this->doGet( $key );
258  if ( $curValue === false ) {
259  $newValue = $this->doSet( $key, $init, $exptime ) ? $init : false;
260  } elseif ( $this->isInteger( $curValue ) ) {
261  $sum = max( $curValue + $step, 0 );
262  $newValue = $this->doSet( $key, $sum, $exptime ) ? $sum : false;
263  } else {
264  $newValue = false;
265  }
266 
267  return $newValue;
268  }
269 
276  private function decodeBody( $body ) {
277  $pieces = explode( '.', $body, 3 );
278  if ( count( $pieces ) !== 3 || $pieces[0] !== $this->serializationType ) {
279  return false;
280  }
281  [ , $hmac, $serialized ] = $pieces;
282  if ( $this->hmacKey !== '' ) {
283  $checkHmac = hash_hmac( 'sha256', $serialized, $this->hmacKey, true );
284  if ( !hash_equals( $checkHmac, base64_decode( $hmac ) ) ) {
285  return false;
286  }
287  }
288 
289  switch ( $this->serializationType ) {
290  case 'JSON':
291  $value = json_decode( $serialized, true );
292  return ( json_last_error() === JSON_ERROR_NONE ) ? $value : false;
293 
294  case 'PHP':
295  return unserialize( $serialized );
296 
297  default:
298  throw new \DomainException(
299  "Unknown serialization type: $this->serializationType"
300  );
301  }
302  }
303 
311  private function encodeBody( $body ) {
312  switch ( $this->serializationType ) {
313  case 'JSON':
314  $value = json_encode( $body );
315  if ( $value === false ) {
316  throw new InvalidArgumentException( __METHOD__ . ": body could not be encoded." );
317  }
318  break;
319 
320  case 'PHP':
321  $value = serialize( $body );
322  break;
323 
324  default:
325  throw new \DomainException(
326  "Unknown serialization type: $this->serializationType"
327  );
328  }
329 
330  if ( $this->hmacKey !== '' ) {
331  $hmac = base64_encode(
332  hash_hmac( 'sha256', $value, $this->hmacKey, true )
333  );
334  } else {
335  $hmac = '';
336  }
337  return $this->serializationType . '.' . $hmac . '.' . $value;
338  }
339 
350  protected function handleError( $msg, $rcode, $rerr, $rhdrs, $rbody, $context = [] ) {
351  $message = "$msg : ({code}) {error}";
352  $context = [
353  'code' => $rcode,
354  'error' => $rerr
355  ] + $context;
356 
357  if ( $this->extendedErrorBodyFields !== [] ) {
358  $body = $this->decodeBody( $rbody );
359  if ( $body ) {
360  $extraFields = '';
361  foreach ( $this->extendedErrorBodyFields as $field ) {
362  if ( isset( $body[$field] ) ) {
363  $extraFields .= " : ({$field}) {$body[$field]}";
364  }
365  }
366  if ( $extraFields !== '' ) {
367  $message .= " {extra_fields}";
368  $context['extra_fields'] = $extraFields;
369  }
370  }
371  }
372 
373  $this->logger->error( $message, $context );
374  $this->setLastError( $rcode === 0 ? self::ERR_UNREACHABLE : self::ERR_UNEXPECTED );
375  }
376 }
setLastError( $error)
Set the "last error" registry due to a problem encountered during an attempted operation.
Definition: BagOStuff.php:497
LoggerInterface $logger
Definition: BagOStuff.php:89
Storage medium specific cache for storing items (e.g.
const PASS_BY_REF
Idiom for doGet() to return extra information by reference.
updateOpStats(string $op, array $keyInfo)
isInteger( $value)
Check if a value is an integer.
Class to handle multiple HTTP requests.
Interface to key-value storage behind an HTTP server.
__construct( $params)
setLogger(LoggerInterface $logger)
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.
doIncrWithInit( $key, $exptime, $step, $init, $flags)
doAdd( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
doGet( $key, $flags=0, &$casToken=null)
Get an item.
doDelete( $key, $flags=0)
Delete an item.
handleError( $msg, $rcode, $rerr, $rhdrs, $rbody, $context=[])
Handle storage error.
const ATTR_DURABILITY
Durability of writes; see QOS_DURABILITY_* (higher means stronger)
const QOS_DURABILITY_DISK
Data is saved to disk and writes do not usually block on fsync()