1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
<?php
use Wikimedia\LightweightObjectStore\StorageAwareness;
use Wikimedia\ObjectCache\HashBagOStuff;
use Wikimedia\ObjectCache\MultiWriteBagOStuff;
use Wikimedia\TestingAccessWrapper;
/**
* @covers \Wikimedia\ObjectCache\MultiWriteBagOStuff
* @group BagOStuff
* @group Database
*/
class MultiWriteBagOStuffTest extends MediaWikiIntegrationTestCase {
/** @var HashBagOStuff */
private $cache1;
/** @var HashBagOStuff */
private $cache2;
/** @var MultiWriteBagOStuff */
private $cache;
protected function setUp(): void {
parent::setUp();
$this->cache1 = new HashBagOStuff();
$this->cache2 = new HashBagOStuff();
$this->cache = new MultiWriteBagOStuff( [
'caches' => [ $this->cache1, $this->cache2 ],
'replication' => 'async',
'asyncHandler' => 'DeferredUpdates::addCallableUpdate'
] );
}
public function testSet() {
$key = 'key';
$value = 'value';
$this->cache->set( $key, $value );
// Set in tier 1
$this->assertSame( $value, $this->cache1->get( $key ), 'Written to tier 1' );
// Set in tier 2
$this->assertSame( $value, $this->cache2->get( $key ), 'Written to tier 2' );
}
public function testAdd() {
$key = 'key';
$value = 'value';
$ok = $this->cache->add( $key, $value );
$this->assertTrue( $ok );
// Set in tier 1
$this->assertSame( $value, $this->cache1->get( $key ), 'Written to tier 1' );
// Set in tier 2
$this->assertSame( $value, $this->cache2->get( $key ), 'Written to tier 2' );
}
public function testSyncMergeAsync() {
$key = 'keyA';
$value = 'value';
$func = static function () use ( $value ) {
return $value;
};
// XXX: DeferredUpdates bound to transactions in CLI mode
$dbw = $this->getDb();
$dbw->begin();
$this->cache->merge( $key, $func );
// Set in tier 1
$this->assertEquals( $value, $this->cache1->get( $key ), 'Written to tier 1' );
// Not yet set in tier 2
$this->assertFalse( $this->cache2->get( $key ), 'Not written to tier 2' );
$dbw->commit();
// Set in tier 2
$this->assertEquals( $value, $this->cache2->get( $key ), 'Written to tier 2' );
}
public function testSyncMergeSync() {
// Like setUp() but without 'async'
$cache1 = new HashBagOStuff();
$cache2 = new HashBagOStuff();
$cache = new MultiWriteBagOStuff( [
'caches' => [ $cache1, $cache2 ]
] );
$value = 'value';
$func = static function () use ( $value ) {
return $value;
};
$key = 'keyB';
$dbw = $this->getDb();
$dbw->begin();
$cache->merge( $key, $func );
// Set in tier 1
$this->assertEquals( $value, $cache1->get( $key ), 'Written to tier 1' );
// Immediately set in tier 2
$this->assertEquals( $value, $cache2->get( $key ), 'Written to tier 2' );
$dbw->commit();
}
public function testSetDelayed() {
$key = 'key';
$value = (object)[ 'v' => 'saved value' ];
$expectValue = clone $value;
// XXX: DeferredUpdates bound to transactions in CLI mode
$dbw = $this->getDb();
$dbw->begin();
$this->cache->set( $key, $value );
// Test that later changes to $value don't affect the saved value (e.g. T168040)
$value->v = 'other value';
// Set in tier 1
$this->assertEquals( $expectValue, $this->cache1->get( $key ), 'Written to tier 1' );
// Not yet set in tier 2
$this->assertFalse( $this->cache2->get( $key ), 'Not written to tier 2' );
$dbw->commit();
// Set in tier 2
$this->assertEquals( $expectValue, $this->cache2->get( $key ), 'Written to tier 2' );
}
public function testMakeKey() {
$cache1 = $this->getMockBuilder( HashBagOStuff::class )
->onlyMethods( [ 'makeKey' ] )->getMock();
$cache1->expects( $this->never() )->method( 'makeKey' );
$cache2 = $this->getMockBuilder( HashBagOStuff::class )
->onlyMethods( [ 'makeKey' ] )->getMock();
$cache2->expects( $this->never() )->method( 'makeKey' );
$cache = new MultiWriteBagOStuff( [
'keyspace' => 'generic',
'caches' => [ $cache1, $cache2 ]
] );
$this->assertSame( 'generic:a:b', $cache->makeKey( 'a', 'b' ) );
}
public function testConvertGenericKey() {
$cache1 = new class extends HashBagOStuff {
protected function makeKeyInternal( $keyspace, $components ) {
return $keyspace . ':short-one-way';
}
protected function requireConvertGenericKey(): bool {
return true;
}
};
$cache2 = new class extends HashBagOStuff {
protected function makeKeyInternal( $keyspace, $components ) {
return $keyspace . ':short-another-way';
}
protected function requireConvertGenericKey(): bool {
return true;
}
};
$cache = new MultiWriteBagOStuff( [
'caches' => [ $cache1, $cache2 ]
] );
$key = $cache->makeKey( 'a', 'b' );
$cache->set( $key, 'my_value' );
$this->assertSame(
'local:a:b',
$key
);
$this->assertSame(
[ 'local:short-one-way' ],
array_keys( TestingAccessWrapper::newFromObject( $cache1 )->bag ),
'key gets re-encoded for first backend'
);
$this->assertSame(
[ 'local:short-another-way' ],
array_keys( TestingAccessWrapper::newFromObject( $cache2 )->bag ),
'key gets re-encoded for second backend'
);
}
public function testMakeGlobalKey() {
$cache1 = $this->getMockBuilder( HashBagOStuff::class )
->onlyMethods( [ 'makeGlobalKey' ] )->getMock();
$cache1->expects( $this->never() )->method( 'makeGlobalKey' );
$cache2 = $this->getMockBuilder( HashBagOStuff::class )
->onlyMethods( [ 'makeGlobalKey' ] )->getMock();
$cache2->expects( $this->never() )->method( 'makeGlobalKey' );
$cache = new MultiWriteBagOStuff( [ 'caches' => [ $cache1, $cache2 ] ] );
$this->assertSame( 'global:a:b', $cache->makeGlobalKey( 'a', 'b' ) );
}
public function testDuplicateStoreAdd() {
$bag = new HashBagOStuff();
$cache = new MultiWriteBagOStuff( [
'caches' => [ $bag, $bag ],
] );
$this->assertTrue( $cache->add( 'key', 1, 30 ) );
}
public function testIncrWithInit() {
$key = $this->cache->makeKey( 'key' );
$val = $this->cache->incrWithInit( $key, 0, 1, 3 );
$this->assertSame( 3, $val, "Correct init value" );
$val = $this->cache->incrWithInit( $key, 0, 1, 3 );
$this->assertSame( 4, $val, "Correct init value" );
$this->cache->delete( $key );
$val = $this->cache->incrWithInit( $key, 0, 5 );
$this->assertSame( 5, $val, "Correct init value" );
}
public function testErrorHandling() {
$t1Cache = $this->createPartialMock( HashBagOStuff::class, [ 'set' ] );
$t1CacheWrapper = TestingAccessWrapper::newFromObject( $t1Cache );
$t1CacheNextError = StorageAwareness::ERR_NONE;
$t1Cache->method( 'set' )
->willReturnCallback( static function () use ( $t1CacheWrapper, &$t1CacheNextError ) {
if ( $t1CacheNextError !== StorageAwareness::ERR_NONE ) {
$t1CacheWrapper->setLastError( $t1CacheNextError );
return false;
}
return true;
} );
$t2Cache = $this->createPartialMock( HashBagOStuff::class, [ 'set' ] );
$t2CacheWrapper = TestingAccessWrapper::newFromObject( $t2Cache );
$t2CacheNextError = StorageAwareness::ERR_NONE;
$t2Cache->method( 'set' )
->willReturnCallback( static function () use ( $t2CacheWrapper, &$t2CacheNextError ) {
if ( $t2CacheNextError !== StorageAwareness::ERR_NONE ) {
$t2CacheWrapper->setLastError( $t2CacheNextError );
return false;
}
return true;
} );
$cache = new MultiWriteBagOStuff( [
'keyspace' => 'repl_local',
'caches' => [ $t1Cache, $t2Cache ]
] );
$cacheWrapper = TestingAccessWrapper::newFromObject( $cache );
$key = 'a:key';
$wp1 = $cache->watchErrors();
$cache->set( $key, 'value', 3600 );
$this->assertSame( StorageAwareness::ERR_NONE, $t1Cache->getLastError() );
$this->assertSame( StorageAwareness::ERR_NONE, $t2Cache->getLastError() );
$this->assertSame( StorageAwareness::ERR_NONE, $cache->getLastError() );
$this->assertSame( StorageAwareness::ERR_NONE, $cache->getLastError( $wp1 ) );
$t1CacheNextError = StorageAwareness::ERR_NO_RESPONSE;
$t2CacheNextError = StorageAwareness::ERR_UNREACHABLE;
$cache->set( $key, 'value', 3600 );
$this->assertSame( $t1CacheNextError, $t1Cache->getLastError() );
$this->assertSame( $t2CacheNextError, $t2Cache->getLastError() );
$this->assertSame( $t2CacheNextError, $cache->getLastError() );
$this->assertSame( $t2CacheNextError, $cache->getLastError( $wp1 ) );
$t1CacheNextError = StorageAwareness::ERR_NO_RESPONSE;
$t2CacheNextError = StorageAwareness::ERR_UNEXPECTED;
$wp2 = $cache->watchErrors();
$cache->set( $key, 'value', 3600 );
$wp3 = $cache->watchErrors();
$this->assertSame( $t1CacheNextError, $t1Cache->getLastError() );
$this->assertSame( $t2CacheNextError, $t2Cache->getLastError() );
$this->assertSame( $t2CacheNextError, $cache->getLastError( $wp2 ) );
$this->assertSame( StorageAwareness::ERR_NONE, $cache->getLastError( $wp3 ) );
$cacheWrapper->setLastError( StorageAwareness::ERR_UNEXPECTED );
$wp4 = $cache->watchErrors();
$this->assertSame( StorageAwareness::ERR_UNEXPECTED, $cache->getLastError() );
$this->assertSame( StorageAwareness::ERR_UNEXPECTED, $cache->getLastError( $wp1 ) );
$this->assertSame( StorageAwareness::ERR_UNEXPECTED, $cache->getLastError( $wp2 ) );
$this->assertSame( StorageAwareness::ERR_UNEXPECTED, $cache->getLastError( $wp3 ) );
$this->assertSame( StorageAwareness::ERR_NONE, $cache->getLastError( $wp4 ) );
$this->assertSame( $t1CacheNextError, $t1Cache->getLastError() );
$this->assertSame( $t2CacheNextError, $t2Cache->getLastError() );
}
}
|