Skip to content
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

Move Archive.php archive invalidation to Loader… #15616

Merged
merged 19 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 4 additions & 72 deletions core/Archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Piwik\Archive\ArchiveQueryFactory;
use Piwik\Archive\Parameters;
use Piwik\ArchiveProcessor\Rules;
use Piwik\Archive\ArchiveInvalidator;
use Piwik\Container\StaticContainer;
use Piwik\DataAccess\ArchiveSelector;

Expand Down Expand Up @@ -167,11 +166,6 @@ class Archive implements ArchiveQuery
*/
private static $cache;

/**
* @var ArchiveInvalidator
*/
private $invalidator;

/**
* @param Parameters $params
* @param bool $forceIndexedBySite Whether to force index the result of a query by site ID.
Expand All @@ -183,8 +177,6 @@ public function __construct(Parameters $params, $forceIndexedBySite = false,
$this->params = $params;
$this->forceIndexedBySite = $forceIndexedBySite;
$this->forceIndexedByDate = $forceIndexedByDate;

$this->invalidator = StaticContainer::get('Piwik\Archive\ArchiveInvalidator');
}

/**
Expand Down Expand Up @@ -453,67 +445,6 @@ public static function createDataTableFromArchive($recordName, $idSite, $period,
return $dataTable;
}

private function getSiteIdsThatAreRequestedInThisArchiveButWereNotInvalidatedYet()
{
if (is_null(self::$cache)) {
self::$cache = Cache::getTransientCache();
}

$id = 'Archive.SiteIdsOfRememberedReportsInvalidated';

if (!self::$cache->contains($id)) {
self::$cache->save($id, array());
}

$siteIdsAlreadyHandled = self::$cache->fetch($id);
$siteIdsRequested = $this->params->getIdSites();

foreach ($siteIdsRequested as $index => $siteIdRequested) {
$siteIdRequested = (int) $siteIdRequested;

if (in_array($siteIdRequested, $siteIdsAlreadyHandled)) {
unset($siteIdsRequested[$index]); // was already handled previously, do not do it again
} else {
$siteIdsAlreadyHandled[] = $siteIdRequested; // we will handle this id this time
}
}

self::$cache->save($id, $siteIdsAlreadyHandled);

return $siteIdsRequested;
}

private function invalidatedReportsIfNeeded()
{
$siteIdsRequested = $this->getSiteIdsThatAreRequestedInThisArchiveButWereNotInvalidatedYet();

if (empty($siteIdsRequested)) {
return; // all requested site ids were already handled
}

$sitesPerDays = $this->invalidator->getRememberedArchivedReportsThatShouldBeInvalidated();

foreach ($sitesPerDays as $date => $siteIds) {
if (empty($siteIds)) {
continue;
}

$siteIdsToActuallyInvalidate = array_intersect($siteIds, $siteIdsRequested);

if (empty($siteIdsToActuallyInvalidate)) {
continue; // all site ids that should be handled are already handled
}

try {
$this->invalidator->markArchivesAsInvalidated($siteIdsToActuallyInvalidate, array(Date::factory($date)), false);
} catch (\Exception $e) {
Site::clearCache();
throw $e;
}
}

Site::clearCache();
}

/**
* Queries archive tables for data and returns the result.
Expand Down Expand Up @@ -638,8 +569,6 @@ private function getArchiveIds($archiveNames)
*/
private function cacheArchiveIdsAfterLaunching($archiveGroups, $plugins)
{
$this->invalidatedReportsIfNeeded();

$today = Date::today();

foreach ($this->params->getPeriods() as $period) {
Expand Down Expand Up @@ -856,8 +785,11 @@ public static function getPluginForReport($report)
*/
private function prepareArchive(array $archiveGroups, Site $site, Period $period)
{
// if cron archiving is running, we will invalidate in CronArchive, not here
$invalidateBeforeArchiving = !SettingsServer::isArchivePhpTriggered();

$parameters = new ArchiveProcessor\Parameters($site, $period, $this->params->getSegment());
$archiveLoader = new ArchiveProcessor\Loader($parameters);
$archiveLoader = new ArchiveProcessor\Loader($parameters, $invalidateBeforeArchiving);

$periodString = $period->getRangeString();

Expand Down
100 changes: 86 additions & 14 deletions core/ArchiveProcessor/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
*/
namespace Piwik\ArchiveProcessor;

use Piwik\Archive\ArchiveInvalidator;
use Piwik\Cache;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\Context;
use Piwik\DataAccess\ArchiveSelector;
use Piwik\Date;
use Piwik\Piwik;
use Piwik\Site;

/**
* This class uses PluginsArchiver class to trigger data aggregation and create archives.
Expand All @@ -33,10 +35,22 @@ class Loader
*/
protected $params;

public function __construct(Parameters $params)
/**
* @var ArchiveInvalidator
*/
private $invalidator;

/**
* @var \Piwik\Cache\Cache
*/
private $cache;

public function __construct(Parameters $params, $invalidateBeforeArchiving = false)
{
$this->params = $params;
}
$this->invalidateBeforeArchiving = $invalidateBeforeArchiving;
$this->invalidator = StaticContainer::get(ArchiveInvalidator::class);
$this->cache = Cache::getTransientCache(); }

/**
* @return bool
Expand Down Expand Up @@ -65,11 +79,19 @@ private function prepareArchiveImpl($pluginName)
{
$this->params->setRequestedPlugin($pluginName);

list($idArchive, $visits, $visitsConverted) = $this->loadExistingArchiveIdFromDb();
if (!empty($idArchive)) {
list($idArchive, $visits, $visitsConverted, $isAnyArchiveExists) = $this->loadExistingArchiveIdFromDb();
if (!empty($idArchive)) { // we have a usable idarchive (it's not invalidated and it's new enough)
return $idArchive;
}

// if there is an archive, but we can't use it for some reason, invalidate existing archives before
// we start archiving. if the archive is made invalid, we will correctly re-archive below.
if ($this->invalidateBeforeArchiving
&& $isAnyArchiveExists
) {
$this->invalidatedReportsIfNeeded();
}

/** @var ArchivingStatus $archivingStatus */
$archivingStatus = StaticContainer::get(ArchivingStatus::class);
$archivingStatus->archiveStarted($this->params);
Expand Down Expand Up @@ -170,20 +192,12 @@ protected function isArchivingForcedToTrigger()
*/
public function loadExistingArchiveIdFromDb()
{
$noArchiveFound = array(false, false, false);

if ($this->isArchivingForcedToTrigger()) {
return $noArchiveFound;
return [false, false, false, false]; // no usable archive found, no existing archive
}

$minDatetimeArchiveProcessedUTC = $this->getMinTimeArchiveProcessed();
$idAndVisits = ArchiveSelector::getArchiveIdAndVisits($this->params, $minDatetimeArchiveProcessedUTC);

if (!$idAndVisits) {
return $noArchiveFound;
}

return $idAndVisits;
return ArchiveSelector::getArchiveIdAndVisits($this->params, $minDatetimeArchiveProcessedUTC);
}

/**
Expand Down Expand Up @@ -242,4 +256,62 @@ private function getIdSitesToArchiveWhenNoVisits()

return $cache->fetch($cacheKey);
}

private function invalidatedReportsIfNeeded()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@diosmosis might be good to add some tests for this maybe? Like it could return the invalidated date/siteIds which we then can test against?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added tests

{
$siteIdsRequested = $this->getSiteIdsThatAreRequestedInThisArchiveButWereNotInvalidatedYet();

if (empty($siteIdsRequested)) {
return; // all requested site ids were already handled
}

$sitesPerDays = $this->invalidator->getRememberedArchivedReportsThatShouldBeInvalidated();

foreach ($sitesPerDays as $date => $siteIds) {
if (empty($siteIds)) {
continue;
}

$siteIdsToActuallyInvalidate = array_intersect($siteIds, $siteIdsRequested);

if (empty($siteIdsToActuallyInvalidate)) {
continue; // all site ids that should be handled are already handled
}

try {
$this->invalidator->markArchivesAsInvalidated($siteIdsToActuallyInvalidate, array(Date::factory($date)), false);
} catch (\Exception $e) {
Site::clearCache();
throw $e;
}
}

Site::clearCache();
}

private function getSiteIdsThatAreRequestedInThisArchiveButWereNotInvalidatedYet()
{
$id = 'Archive.SiteIdsOfRememberedReportsInvalidated';

if (!$this->cache->contains($id)) {
$this->cache->save($id, array());
}

$siteIdsAlreadyHandled = $this->cache->fetch($id);
$siteIdsRequested = $this->params->getIdSites();

foreach ($siteIdsRequested as $index => $siteIdRequested) {
$siteIdRequested = (int) $siteIdRequested;

if (in_array($siteIdRequested, $siteIdsAlreadyHandled)) {
unset($siteIdsRequested[$index]); // was already handled previously, do not do it again
} else {
$siteIdsAlreadyHandled[] = $siteIdRequested; // we will handle this id this time
}
}

$this->cache->save($id, $siteIdsAlreadyHandled);

return $siteIdsRequested;
}
}
Loading