2016-06-09 19:38:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
2018-06-15 11:06:40 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
|
2016-06-09 19:38:05 +00:00
|
|
|
/**
|
2016-07-20 16:38:36 +00:00
|
|
|
* Interface to key-value storage behind an HTTP server.
|
|
|
|
|
*
|
|
|
|
|
* Uses URL of the form "baseURL/{KEY}" to store, fetch, and delete values.
|
|
|
|
|
*
|
|
|
|
|
* E.g., when base URL is `/v1/sessions/`, then the store would do:
|
|
|
|
|
*
|
|
|
|
|
* `PUT /v1/sessions/12345758`
|
|
|
|
|
*
|
2016-06-09 19:38:05 +00:00
|
|
|
* and fetch would do:
|
2016-07-20 16:38:36 +00:00
|
|
|
*
|
|
|
|
|
* `GET /v1/sessions/12345758`
|
|
|
|
|
*
|
2016-06-09 19:38:05 +00:00
|
|
|
* delete would do:
|
2016-07-20 16:38:36 +00:00
|
|
|
*
|
|
|
|
|
* `DELETE /v1/sessions/12345758`
|
2016-06-09 19:38:05 +00:00
|
|
|
*
|
|
|
|
|
* Configure with:
|
2016-07-20 16:38:36 +00:00
|
|
|
*
|
2016-06-09 19:38:05 +00:00
|
|
|
* @code
|
|
|
|
|
* $wgObjectCaches['sessions'] = array(
|
|
|
|
|
* 'class' => 'RESTBagOStuff',
|
|
|
|
|
* 'url' => 'http://localhost:7231/wikimedia.org/v1/sessions/'
|
|
|
|
|
* );
|
|
|
|
|
* @endcode
|
|
|
|
|
*/
|
|
|
|
|
class RESTBagOStuff extends BagOStuff {
|
2018-06-15 11:06:40 +00:00
|
|
|
/**
|
|
|
|
|
* Default connection timeout in seconds. The kernel retransmits the SYN
|
|
|
|
|
* packet after 1 second, so 1.2 seconds allows for 1 retransmit without
|
|
|
|
|
* permanent failure.
|
|
|
|
|
*/
|
|
|
|
|
const DEFAULT_CONN_TIMEOUT = 1.2;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Default request timeout
|
|
|
|
|
*/
|
|
|
|
|
const DEFAULT_REQ_TIMEOUT = 3.0;
|
2016-06-09 19:38:05 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var MultiHttpClient
|
|
|
|
|
*/
|
|
|
|
|
private $client;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* REST URL to use for storage.
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private $url;
|
|
|
|
|
|
|
|
|
|
public function __construct( $params ) {
|
|
|
|
|
if ( empty( $params['url'] ) ) {
|
|
|
|
|
throw new InvalidArgumentException( 'URL parameter is required' );
|
|
|
|
|
}
|
|
|
|
|
if ( empty( $params['client'] ) ) {
|
2018-06-15 11:06:40 +00:00
|
|
|
// Pass through some params to the HTTP client.
|
|
|
|
|
$clientParams = [
|
|
|
|
|
'connTimeout' => $params['connTimeout'] ?? self::DEFAULT_CONN_TIMEOUT,
|
|
|
|
|
'reqTimeout' => $params['reqTimeout'] ?? self::DEFAULT_REQ_TIMEOUT,
|
|
|
|
|
];
|
|
|
|
|
foreach ( [ 'caBundlePath', 'proxy' ] as $key ) {
|
|
|
|
|
if ( isset( $params[$key] ) ) {
|
|
|
|
|
$clientParams[$key] = $params[$key];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->client = new MultiHttpClient( $clientParams );
|
2016-06-09 19:38:05 +00:00
|
|
|
} else {
|
|
|
|
|
$this->client = $params['client'];
|
|
|
|
|
}
|
2018-06-15 11:06:40 +00:00
|
|
|
// The parent constructor calls setLogger() which sets the logger in $this->client
|
|
|
|
|
parent::__construct( $params );
|
2016-06-09 19:38:05 +00:00
|
|
|
// Make sure URL ends with /
|
|
|
|
|
$this->url = rtrim( $params['url'], '/' ) . '/';
|
2016-08-24 21:22:11 +00:00
|
|
|
// Default config, R+W > N; no locks on reads though; writes go straight to state-machine
|
|
|
|
|
$this->attrMap[self::ATTR_SYNCWRITES] = self::QOS_SYNCWRITES_QC;
|
2016-06-09 19:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
2018-06-15 11:06:40 +00:00
|
|
|
public function setLogger( LoggerInterface $logger ) {
|
|
|
|
|
parent::setLogger( $logger );
|
|
|
|
|
$this->client->setLogger( $logger );
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-09 19:38:05 +00:00
|
|
|
/**
|
2017-08-11 21:45:25 +00:00
|
|
|
* @param string $key
|
2017-08-20 11:20:59 +00:00
|
|
|
* @param int $flags Bitfield of BagOStuff::READ_* constants [optional]
|
2016-06-09 19:38:05 +00:00
|
|
|
* @return mixed Returns false on failure and if the item does not exist
|
|
|
|
|
*/
|
|
|
|
|
protected function doGet( $key, $flags = 0 ) {
|
|
|
|
|
$req = [
|
|
|
|
|
'method' => 'GET',
|
2017-02-25 21:53:36 +00:00
|
|
|
'url' => $this->url . rawurlencode( $key ),
|
2016-06-09 19:38:05 +00:00
|
|
|
];
|
2017-02-25 21:53:36 +00:00
|
|
|
|
2016-06-09 19:38:05 +00:00
|
|
|
list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
|
|
|
|
|
if ( $rcode === 200 ) {
|
|
|
|
|
if ( is_string( $rbody ) ) {
|
|
|
|
|
return unserialize( $rbody );
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if ( $rcode === 0 || ( $rcode >= 400 && $rcode != 404 ) ) {
|
|
|
|
|
return $this->handleError( "Failed to fetch $key", $rcode, $rerr );
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle storage error
|
|
|
|
|
* @param string $msg Error message
|
2017-08-11 21:45:25 +00:00
|
|
|
* @param int $rcode Error code from client
|
2016-06-09 19:38:05 +00:00
|
|
|
* @param string $rerr Error message from client
|
|
|
|
|
* @return false
|
|
|
|
|
*/
|
|
|
|
|
protected function handleError( $msg, $rcode, $rerr ) {
|
|
|
|
|
$this->logger->error( "$msg : ({code}) {error}", [
|
|
|
|
|
'code' => $rcode,
|
|
|
|
|
'error' => $rerr
|
|
|
|
|
] );
|
|
|
|
|
$this->setLastError( $rcode === 0 ? self::ERR_UNREACHABLE : self::ERR_UNEXPECTED );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set an item
|
|
|
|
|
*
|
|
|
|
|
* @param string $key
|
2017-08-11 21:45:25 +00:00
|
|
|
* @param mixed $value
|
|
|
|
|
* @param int $exptime Either an interval in seconds or a unix timestamp for expiry
|
|
|
|
|
* @param int $flags Bitfield of BagOStuff::WRITE_* constants
|
2016-06-09 19:38:05 +00:00
|
|
|
* @return bool Success
|
|
|
|
|
*/
|
|
|
|
|
public function set( $key, $value, $exptime = 0, $flags = 0 ) {
|
|
|
|
|
$req = [
|
|
|
|
|
'method' => 'PUT',
|
|
|
|
|
'url' => $this->url . rawurlencode( $key ),
|
|
|
|
|
'body' => serialize( $value )
|
|
|
|
|
];
|
|
|
|
|
list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
|
2018-06-15 11:06:40 +00:00
|
|
|
if ( $rcode === 200 || $rcode === 201 || $rcode === 204 ) {
|
2016-06-09 19:38:05 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return $this->handleError( "Failed to store $key", $rcode, $rerr );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete an item.
|
|
|
|
|
*
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @return bool True if the item was deleted or not found, false on failure
|
|
|
|
|
*/
|
|
|
|
|
public function delete( $key ) {
|
|
|
|
|
$req = [
|
|
|
|
|
'method' => 'DELETE',
|
|
|
|
|
'url' => $this->url . rawurlencode( $key ),
|
|
|
|
|
];
|
|
|
|
|
list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
|
2018-06-15 11:06:40 +00:00
|
|
|
if ( in_array( $rcode, [ 200, 204, 205, 404, 410 ] ) ) {
|
2016-06-09 19:38:05 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return $this->handleError( "Failed to delete $key", $rcode, $rerr );
|
|
|
|
|
}
|
|
|
|
|
}
|