New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lock when archiving and avoid invalidating sites that have archiving in progress #15272
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
69c7ddf
Use a lock when archiving and do not invalidate when archiving is in …
diosmosis f90eb05
Add and fix tests + modify workflow.
diosmosis 6bc90b2
forgot to add file and remove TODO
diosmosis 8202f30
Remove use of argument.
diosmosis ae21253
Merge branch '3.x-dev' into 15170-archive-invalidation-locking
diosmosis aca6f66
Add back min archive time processed code and start on tests for it.
diosmosis d5a225b
Finish new LoaderTest.
diosmosis 1939ac9
Fix new tests.
diosmosis ee5716a
Merge branch '3.x-dev' into 15170-archive-invalidation-locking
diosmosis File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
/** | ||
* Piwik - free/libre analytics platform | ||
* | ||
* @link https://matomo.org | ||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later | ||
* | ||
*/ | ||
|
||
namespace Piwik\ArchiveProcessor; | ||
|
||
use Piwik\Concurrency\Lock; | ||
use Piwik\Concurrency\LockBackend; | ||
use Piwik\Container\StaticContainer; | ||
use Piwik\SettingsPiwik; | ||
|
||
class ArchivingStatus | ||
{ | ||
const LOCK_KEY_PREFIX = 'Archiving'; | ||
const DEFAULT_ARCHIVING_TTL = 7200; // 2 hours | ||
|
||
/** | ||
* @var LockBackend | ||
*/ | ||
private $lockBackend; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $archivingTTLSecs; | ||
|
||
/** | ||
* @var Lock[] | ||
*/ | ||
private $lockStack = []; | ||
|
||
public function __construct(LockBackend $lockBackend, $archivingTTLSecs = self::DEFAULT_ARCHIVING_TTL) | ||
{ | ||
$this->lockBackend = $lockBackend; | ||
$this->archivingTTLSecs = $archivingTTLSecs; | ||
} | ||
|
||
public function archiveStarted(Parameters $params) | ||
{ | ||
$lock = $this->makeArchivingLock($params); | ||
$lock->acquireLock($this->getInstanceProcessId(), $this->archivingTTLSecs); | ||
array_push($this->lockStack, $lock); | ||
} | ||
|
||
public function archiveFinished() | ||
{ | ||
$lock = array_pop($this->lockStack); | ||
$lock->unlock(); | ||
} | ||
|
||
public function getCurrentArchivingLock() | ||
{ | ||
if (empty($this->lockStack)) { | ||
return null; | ||
} | ||
return end($this->lockStack); | ||
} | ||
|
||
public function getSitesCurrentlyArchiving() | ||
{ | ||
$lockMeta = new Lock($this->lockBackend, self::LOCK_KEY_PREFIX . '.'); | ||
$acquiredLocks = $lockMeta->getAllAcquiredLockKeys(); | ||
|
||
$sitesCurrentlyArchiving = []; | ||
foreach ($acquiredLocks as $lockKey) { | ||
$parts = explode('.', $lockKey); | ||
if (!isset($parts[1])) { | ||
continue; | ||
} | ||
$sitesCurrentlyArchiving[] = (int) $parts[1]; | ||
} | ||
$sitesCurrentlyArchiving = array_unique($sitesCurrentlyArchiving); | ||
$sitesCurrentlyArchiving = array_values($sitesCurrentlyArchiving); | ||
|
||
return $sitesCurrentlyArchiving; | ||
} | ||
|
||
/** | ||
* @return Lock | ||
*/ | ||
private function makeArchivingLock(Parameters $params) | ||
{ | ||
$doneFlag = Rules::getDoneStringFlagFor([$params->getSite()->getId()], $params->getSegment(), | ||
$params->getPeriod()->getLabel(), $params->getRequestedPlugin()); | ||
|
||
$lockKeyParts = [ | ||
self::LOCK_KEY_PREFIX, | ||
$params->getSite()->getId(), | ||
|
||
// md5 to keep it within the 70 char limit in the table | ||
md5($params->getPeriod()->getId() . $params->getPeriod()->getRangeString() . $doneFlag), | ||
]; | ||
|
||
$lockKeyPrefix = implode('.', $lockKeyParts); | ||
return new Lock(StaticContainer::get(LockBackend::class), $lockKeyPrefix, $this->archivingTTLSecs); | ||
} | ||
|
||
private function getInstanceProcessId() | ||
{ | ||
return SettingsPiwik::getPiwikInstanceId() . '.' . getmypid(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should it be maybe
reExpireLock
? Not too important though...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it looks weird to me too. But so did
reExpireLock
and I couldn't choose :)