/** * Notify the ChronologyProtector that the LBFactory is done calling shutdownLB() for now. * May commit chronology data to persistent storage. * * @return array Empty on success; returns the (db name => position) map on failure */ public function shutdown() { if (!$this->enabled || !count($this->shutdownPositions)) { return true; // nothing to save } wfDebugLog('replication', __METHOD__ . ": saving master pos for " . implode(', ', array_keys($this->shutdownPositions)) . "\n"); $shutdownPositions = $this->shutdownPositions; $ok = $this->store->merge($this->key, function ($store, $key, $curValue) use($shutdownPositions) { /** @var $curPositions DBMasterPos[] */ if ($curValue === false) { $curPositions = $shutdownPositions; } else { $curPositions = $curValue['positions']; // Use the newest positions for each DB master foreach ($shutdownPositions as $db => $pos) { if (!isset($curPositions[$db]) || $pos->asOfTime() > $curPositions[$db]->asOfTime()) { $curPositions[$db] = $pos; } } } return array('positions' => $curPositions); }, BagOStuff::TTL_MINUTE, 10, BagOStuff::WRITE_SYNC); if (!$ok) { // Raced out too many times or stash is down wfDebugLog('replication', __METHOD__ . ": failed to save master pos for " . implode(', ', array_keys($this->shutdownPositions)) . "\n"); return $this->shutdownPositions; } return array(); }
/** * Set the cached stat info for a file path. * Negatives (404s) are not cached. By not caching negatives, we can skip cache * salting for the case when a file is created at a path were there was none before. * * @param string $path Storage path * @param array $val Stat information to cache */ protected final function setFileCache($path, array $val) { $path = FileBackend::normalizeStoragePath($path); if ($path === null) { return; // invalid storage path } $age = time() - wfTimestamp(TS_UNIX, $val['mtime']); $ttl = min(7 * 86400, max(300, floor(0.1 * $age))); $key = $this->fileCacheKey($path); // Set the cache unless it is currently salted with the value "PURGED". // Using add() handles this except it also is a no-op in that case where // the current value is not "latest" but $val is, so use CAS in that case. if (!$this->memCache->add($key, $val, $ttl) && !empty($val['latest'])) { $this->memCache->merge($key, function (BagOStuff $cache, $key, $cValue) use($val) { return is_array($cValue) && empty($cValue['latest']) ? $val : false; // do nothing (cache is salted or some error happened) }, $ttl, 1); } }
/** * @covers BagOStuff::merge * @covers BagOStuff::mergeViaLock */ public function testMerge() { $key = wfMemcKey('test'); $usleep = 0; /** * Callback method: append "merged" to whatever is in cache. * * @param BagOStuff $cache * @param string $key * @param int $existingValue * @use int $usleep * @return int */ $callback = function (BagOStuff $cache, $key, $existingValue) use(&$usleep) { // let's pretend this is an expensive callback to test concurrent merge attempts usleep($usleep); if ($existingValue === false) { return 'merged'; } return $existingValue . 'merged'; }; // merge on non-existing value $merged = $this->cache->merge($key, $callback, 0); $this->assertTrue($merged); $this->assertEquals($this->cache->get($key), 'merged'); // merge on existing value $merged = $this->cache->merge($key, $callback, 0); $this->assertTrue($merged); $this->assertEquals($this->cache->get($key), 'mergedmerged'); /* * Test concurrent merges by forking this process, if: * - not manually called with --use-bagostuff * - pcntl_fork is supported by the system * - cache type will correctly support calls over forks */ $fork = (bool) $this->getCliArg('use-bagostuff'); $fork &= function_exists('pcntl_fork'); $fork &= !$this->cache instanceof HashBagOStuff; $fork &= !$this->cache instanceof EmptyBagOStuff; $fork &= !$this->cache instanceof MultiWriteBagOStuff; if ($fork) { // callback should take awhile now so that we can test concurrent merge attempts $pid = pcntl_fork(); if ($pid == -1) { // can't fork, ignore this test... } elseif ($pid) { // wait a little, making sure that the child process is calling merge usleep(3000); // attempt a merge - this should fail $merged = $this->cache->merge($key, $callback, 0, 1); // merge has failed because child process was merging (and we only attempted once) $this->assertFalse($merged); // make sure the child's merge is completed and verify usleep(3000); $this->assertEquals($this->cache->get($key), 'mergedmergedmerged'); } else { $this->cache->merge($key, $callback, 0, 1); // Note: I'm not even going to check if the merge worked, I'll // compare values in the parent process to test if this merge worked. // I'm just going to exit this child process, since I don't want the // child to output any test results (would be rather confusing to // have test output twice) exit; } } }
public function merge($key, $callback, $exptime = 0, $attempts = 10) { return $this->writeStore->merge($key, $callback, $exptime, $attempts); }