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 4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,12 +21,19 @@ class Lock | |
|
||
private $lockKey = null; | ||
private $lockValue = null; | ||
private $defaultTtl = null; | ||
|
||
public function __construct(LockBackend $backend, $lockKeyStart) | ||
public function __construct(LockBackend $backend, $lockKeyStart, $defaultTtl = null) | ||
{ | ||
$this->backend = $backend; | ||
$this->lockKeyStart = $lockKeyStart; | ||
$this->lockKey = $this->lockKeyStart; | ||
$this->defaultTtl = $defaultTtl; | ||
} | ||
|
||
public function reexpireLock() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should it be maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it looks weird to me too. But so did |
||
{ | ||
$this->expireLock($this->defaultTtl); | ||
} | ||
|
||
public function getNumberOfAcquiredLocks() | ||
|
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\DataAccess; | ||
|
||
use Piwik\Concurrency\Lock; | ||
use Piwik\Db\AdapterInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class ArchivingDbAdapter | ||
{ | ||
/** | ||
* @var AdapterInterface|\Zend_Db_Adapter_Abstract | ||
*/ | ||
private $wrapped; | ||
|
||
/** | ||
* @var Lock | ||
*/ | ||
private $archivingLock; | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
public function __construct($wrapped, Lock $archivingLock = null, LoggerInterface $logger = null) | ||
{ | ||
$this->wrapped = $wrapped; | ||
$this->archivingLock = $archivingLock; | ||
$this->logger = $logger; | ||
} | ||
|
||
public function __call($name, $arguments) | ||
{ | ||
return call_user_func_array([$this->wrapped, $name], $arguments); | ||
} | ||
|
||
public function exec($sql) | ||
{ | ||
$this->reexpireLock(); | ||
$this->logSql($sql); | ||
|
||
return call_user_func_array([$this->wrapped, __FUNCTION__], func_get_args()); | ||
} | ||
|
||
public function query($sql) | ||
{ | ||
$this->reexpireLock(); | ||
$this->logSql($sql); | ||
|
||
return call_user_func_array([$this->wrapped, __FUNCTION__], func_get_args()); | ||
} | ||
|
||
public function fetchAll($sql) | ||
{ | ||
$this->reexpireLock(); | ||
$this->logSql($sql); | ||
|
||
return call_user_func_array([$this->wrapped, __FUNCTION__], func_get_args()); | ||
} | ||
|
||
public function fetchRow($sql) | ||
{ | ||
$this->reexpireLock(); | ||
$this->logSql($sql); | ||
|
||
return call_user_func_array([$this->wrapped, __FUNCTION__], func_get_args()); | ||
} | ||
|
||
public function fetchOne($sql) | ||
{ | ||
$this->reexpireLock(); | ||
$this->logSql($sql); | ||
|
||
return call_user_func_array([$this->wrapped, __FUNCTION__], func_get_args()); | ||
} | ||
|
||
public function fetchAssoc($sql) | ||
{ | ||
$this->reexpireLock(); | ||
$this->logSql($sql); | ||
|
||
return call_user_func_array([$this->wrapped, __FUNCTION__], func_get_args()); | ||
} | ||
|
||
private function logSql($sql) | ||
{ | ||
// Log on DEBUG level all SQL archiving queries | ||
if ($this->logger) { | ||
$this->logger->debug($sql); | ||
} | ||
} | ||
|
||
private function reexpireLock() | ||
{ | ||
if ($this->archivingLock) { | ||
$this->archivingLock->reexpireLock(); | ||
} | ||
} | ||
} |
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.
@diosmosis not sure I understand why we would filter the idSites here and not just invalidate all of them? Is this because we assume they were already invalidated / will be invalidated? Would there be any harm in still invalidating them or the issue that this would cause the remember flag to be removed?
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.
I wonder maybe we could still invalidate archives for the other idSites as well just not remove the remember flag?
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.
We remove them because we know they are currently being archived. We also keep the idSites in the remember flags so they will eventually be invalidated when they are not being archiving.
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.
Just wondering if there could be meanwhile some other previously finished archives for those idSites that need to be invalidated?
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.
There could be, but because of the way the function parameters are (idSites + list of dates + period type), it's a bit harder to control exactly what to invalidate. We'd have to loop over idSites & dates and check each individually, and invalidate each archive individually instead of with one query (which is what we do now I think).
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.
@diosmosis it is very important we respect these configured values. We basically need to check if the last archive was done more than the configured time ago even if the archive is invalid (unless the archive is invalid and it is a new day I think then we don't respect these values but not sure? @mattab )
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.
It's just an easy way to check if an archive is on-going no matter what machine/process it's on. It's not really necessary, but it might be useful in the future.
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.
I reckon be good to keep it 👍
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.
@diosmosis it would be big value if it would help with this issue: #8444 -> maybe it would be fixed?
correct, these INI settings are only used when the date range includes Today.
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.
just wanting to make sure we're not going to work on #8444 as part of this issue :)