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.
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.
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
19 * @author Aaron Schulz
23 * Bloom filter implemented using Redis
25 * The Redis server must be >= 2.6 and should have volatile-lru or volatile-ttl
26 * if there is any eviction policy. It should not be allkeys-* in any case. Also,
27 * this can be used in a simple master/slave setup or with Redis Sentinel preferably.
29 * Some bits are based on https://github.com/ErikDubbelboer/redis-lua-scaling-bloom-filter
30 * but are simplified to use a single filter instead of up to 32 filters.
34 class BloomCacheRedis
extends BloomCache
{
35 /** @var RedisConnectionPool */
37 /** @var RedisLockManager */
41 /** @var integer Federate each filter into this many redis bitfield objects */
42 protected $segments = 128;
46 * - redisServers : list of servers (address:<port>) (the first is the master)
47 * - redisConf : additional redis configuration
49 * @param array $config
51 public function __construct( array $config ) {
52 parent
::__construct( $config );
54 $redisConf = $config['redisConfig'];
55 $redisConf['serializer'] = 'none'; // manage that in this class
56 $this->redisPool
= RedisConnectionPool
::singleton( $redisConf );
57 $this->servers
= $config['redisServers'];
58 $this->lockMgr
= new RedisLockManager( array(
59 'lockServers' => array( 'srv1' => $this->servers
[0] ),
60 'srvsByBucket' => array( 0 => array( 'srv1' ) ),
61 'redisConfig' => $config['redisConfig']
65 protected function doInit( $key, $size, $precision ) {
66 $conn = $this->getConnection( 'master' );
71 // 80000000 items at p = .001 take up 500MB and fit into one value.
72 // Do not hit the 512MB redis value limit by reducing the demands.
73 $size = min( $size, 80000000 * $this->segments
);
74 $precision = max( round( $precision, 3 ), .001 );
75 $epoch = microtime( true );
79 local kMetadata, kData = unpack(KEYS)
80 local aEntries, aPrec, aEpoch = unpack(ARGV)
81 if redis.call('EXISTS',kMetadata) == 0 or redis.call('EXISTS',kData) == 0 then
82 redis.call('DEL',kMetadata)
83 redis.call('HSET',kMetadata,'entries',aEntries)
84 redis.call('HSET',kMetadata,'precision',aPrec)
85 redis.call('HSET',kMetadata,'epoch',aEpoch)
86 redis.call('SET',kData,'')
94 $conn->script( 'load', $script );
95 $conn->multi( Redis
::MULTI
);
96 for ( $i = 0; $i < $this->segments
; ++
$i ) {
97 $res = $conn->luaEval( $script,
99 "$key:$i:bloom-metadata", # KEYS[1]
100 "$key:$i:bloom-data", # KEYS[2]
101 ceil( $size / $this->segments
), # ARGV[1]
102 $precision, # ARGV[2]
105 2 # number of first argument(s) that are keys
108 $results = $conn->exec();
109 $res = $results && !in_array( false, $results, true );
110 } catch ( RedisException
$e ) {
111 $this->handleException( $conn, $e );
114 return ( $res !== false );
117 protected function doAdd( $key, array $members ) {
118 $conn = $this->getConnection( 'master' );
125 local kMetadata, kData = unpack(KEYS)
126 local aMember = unpack(ARGV)
128 -- Check if the filter was initialized
129 if redis.call('EXISTS',kMetadata) == 0 or redis.call('EXISTS',kData) == 0 then
133 -- Initial expected entries and desired precision
134 local entries = 1*redis.call('HGET',kMetadata,'entries')
135 local precision = 1*redis.call('HGET',kMetadata,'precision')
136 local hash = redis.sha1hex(aMember)
138 -- Based on the math from: http://en.wikipedia.org/wiki/Bloom_filter#Probability_of_false_positives
139 -- 0.480453013 = ln(2)^2
140 local bits = math.ceil((entries * math.log(precision)) / -0.480453013)
142 -- 0.693147180 = ln(2)
143 local k = math.floor(0.693147180 * bits / entries)
145 -- This uses a variation on:
146 -- 'Less Hashing, Same Performance: Building a Better Bloom Filter'
147 -- http://www.eecs.harvard.edu/~kirsch/pubs/bbbf/esa06.pdf
149 h[0] = tonumber(string.sub(hash, 1, 8 ), 16)
150 h[1] = tonumber(string.sub(hash, 9, 16), 16)
151 h[2] = tonumber(string.sub(hash, 17, 24), 16)
152 h[3] = tonumber(string.sub(hash, 25, 32), 16)
155 local pos = (h[i % 2] + i * h[2 + (((i + (i % 2)) % 4) / 2)]) % bits
156 redis.call('SETBIT', kData, pos, 1)
164 $conn->script( 'load', $script );
165 $conn->multi( Redis
::PIPELINE
);
166 foreach ( $members as $member ) {
167 $i = $this->getSegment( $member );
168 $conn->luaEval( $script,
170 "$key:$i:bloom-metadata", # KEYS[1],
171 "$key:$i:bloom-data", # KEYS[2]
174 2 # number of first argument(s) that are keys
177 $results = $conn->exec();
178 $res = $results && !in_array( false, $results, true );
179 } catch ( RedisException
$e ) {
180 $this->handleException( $conn, $e );
183 if ( $res === false ) {
184 wfDebug( "Could not add to the '$key' bloom filter; it may be missing." );
187 return ( $res !== false );
190 protected function doSetStatus( $virtualKey, array $values ) {
191 $conn = $this->getConnection( 'master' );
198 $res = $conn->hMSet( "$virtualKey:filter-metadata", $values );
199 } catch ( RedisException
$e ) {
200 $this->handleException( $conn, $e );
203 return ( $res !== false );
206 protected function doGetStatus( $virtualKey ) {
207 $conn = $this->getConnection( 'slave' );
214 $res = $conn->hGetAll( "$virtualKey:filter-metadata" );
215 } catch ( RedisException
$e ) {
216 $this->handleException( $conn, $e );
219 if ( is_array( $res ) ) {
220 $res['lastID'] = isset( $res['lastID'] ) ?
$res['lastID'] : null;
221 $res['asOfTime'] = isset( $res['asOfTime'] ) ?
$res['asOfTime'] : null;
222 $res['epoch'] = isset( $res['epoch'] ) ?
$res['epoch'] : null;
228 protected function doIsHit( $key, $member ) {
229 $conn = $this->getConnection( 'slave' );
236 local kMetadata, kData = unpack(KEYS)
237 local aMember = unpack(ARGV)
239 -- Check if the filter was initialized
240 if redis.call('EXISTS',kMetadata) == 0 or redis.call('EXISTS',kData) == 0 then
244 -- Initial expected entries and desired precision.
245 -- This determines the size of the first and subsequent filters.
246 local entries = redis.call('HGET',kMetadata,'entries')
247 local precision = redis.call('HGET',kMetadata,'precision')
248 local hash = redis.sha1hex(aMember)
250 -- This uses a variation on:
251 -- 'Less Hashing, Same Performance: Building a Better Bloom Filter'
252 -- http://www.eecs.harvard.edu/~kirsch/pubs/bbbf/esa06.pdf
254 h[0] = tonumber(string.sub(hash, 1, 8 ), 16)
255 h[1] = tonumber(string.sub(hash, 9, 16), 16)
256 h[2] = tonumber(string.sub(hash, 17, 24), 16)
257 h[3] = tonumber(string.sub(hash, 25, 32), 16)
259 -- 0.480453013 = ln(2)^2
260 local bits = math.ceil((entries * math.log(precision)) / -0.480453013)
262 -- 0.693147180 = ln(2)
263 local k = math.floor(0.693147180 * bits / entries)
267 local pos = (h[i % 2] + i * h[2 + (((i + (i % 2)) % 4) / 2)]) % bits
268 if redis.call('GETBIT', kData, pos) == 0 then
279 $i = $this->getSegment( $member );
280 $res = $conn->luaEval( $script,
282 "$key:$i:bloom-metadata", # KEYS[1],
283 "$key:$i:bloom-data", # KEYS[2]
286 2 # number of first argument(s) that are keys
288 } catch ( RedisException
$e ) {
289 $this->handleException( $conn, $e );
292 return is_int( $res ) ?
(bool)$res : null;
295 protected function doDelete( $key ) {
296 $conn = $this->getConnection( 'master' );
304 for ( $i = 0; $i < $this->segments
; ++
$i ) {
305 $keys[] = "$key:$i:bloom-metadata";
306 $keys[] = "$key:$i:bloom-data";
308 $res = $conn->delete( $keys );
309 } catch ( RedisException
$e ) {
310 $this->handleException( $conn, $e );
313 return ( $res !== false );
316 public function getScopedLock( $virtualKey ) {
317 $status = Status
::newGood();
318 return ScopedLock
::factory( $this->lockMgr
,
319 array( $virtualKey ), LockManager
::LOCK_EX
, $status );
323 * @param string $member
326 protected function getSegment( $member ) {
327 return hexdec( substr( md5( $member ), 0, 2 ) ) %
$this->segments
;
331 * @param string $to (master/slave)
332 * @return RedisConnRef|bool Returns false on failure
334 protected function getConnection( $to ) {
335 if ( $to === 'master' ) {
336 $conn = $this->redisPool
->getConnection( $this->servers
[0] );
338 static $lastServer = null;
342 $conn = $this->redisPool
->getConnection( $lastServer );
344 return $conn; // reuse connection
347 $servers = $this->servers
;
348 $attempts = min( 3, count( $servers ) );
349 for ( $i = 1; $i <= $attempts; ++
$i ) {
350 $index = mt_rand( 0, count( $servers ) - 1 );
351 $conn = $this->redisPool
->getConnection( $servers[$index] );
353 $lastServer = $servers[$index];
356 unset( $servers[$index] ); // skip next time
357 $servers = array_values( $servers ); // reindex
365 * @param RedisConnRef $conn
366 * @param Exception $e
368 protected function handleException( RedisConnRef
$conn, $e ) {
369 $this->redisPool
->handleError( $conn, $e );