1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-20 04:20:55 +01:00

Begin standardizing garbage collectors

Summary: Ref T9494. Improve support infrastructure for garbage collectors.

Test Plan:
  - Ran `bin/phd debug trigger`, saw collectors execute.

{F857852}

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T9494

Differential Revision: https://secure.phabricator.com/D14218
This commit is contained in:
epriestley 2015-10-01 16:58:43 -07:00
parent e431ab2189
commit 878a493301
27 changed files with 364 additions and 15 deletions

View file

@ -1829,6 +1829,7 @@ phutil_register_library_map(array(
'PhabricatorConfigAllController' => 'applications/config/controller/PhabricatorConfigAllController.php',
'PhabricatorConfigApplication' => 'applications/config/application/PhabricatorConfigApplication.php',
'PhabricatorConfigCacheController' => 'applications/config/controller/PhabricatorConfigCacheController.php',
'PhabricatorConfigCollectorsModule' => 'applications/config/module/PhabricatorConfigCollectorsModule.php',
'PhabricatorConfigColumnSchema' => 'applications/config/schema/PhabricatorConfigColumnSchema.php',
'PhabricatorConfigConfigPHIDType' => 'applications/config/phid/PhabricatorConfigConfigPHIDType.php',
'PhabricatorConfigController' => 'applications/config/controller/PhabricatorConfigController.php',
@ -5761,6 +5762,7 @@ phutil_register_library_map(array(
'PhabricatorConfigAllController' => 'PhabricatorConfigController',
'PhabricatorConfigApplication' => 'PhabricatorApplication',
'PhabricatorConfigCacheController' => 'PhabricatorConfigController',
'PhabricatorConfigCollectorsModule' => 'PhabricatorConfigModule',
'PhabricatorConfigColumnSchema' => 'PhabricatorConfigStorageSchema',
'PhabricatorConfigConfigPHIDType' => 'PhabricatorPHIDType',
'PhabricatorConfigController' => 'PhabricatorController',

View file

@ -3,6 +3,16 @@
final class PhabricatorAuthSessionGarbageCollector
extends PhabricatorGarbageCollector {
const COLLECTORCONST = 'auth.sessions';
public function getCollectorName() {
return pht('Auth Sessions');
}
public function hasAutomaticPolicy() {
return true;
}
public function collectGarbage() {
$session_table = new PhabricatorAuthSession();
$conn_w = $session_table->establishConnection('w');

View file

@ -3,6 +3,16 @@
final class PhabricatorAuthTemporaryTokenGarbageCollector
extends PhabricatorGarbageCollector {
const COLLECTORCONST = 'auth.tokens';
public function getCollectorName() {
return pht('Auth Tokens');
}
public function hasAutomaticPolicy() {
return true;
}
public function collectGarbage() {
$session_table = new PhabricatorAuthTemporaryToken();
$conn_w = $session_table->establishConnection('w');

View file

@ -3,6 +3,16 @@
final class PhabricatorCacheGeneralGarbageCollector
extends PhabricatorGarbageCollector {
const COLLECTORCONST = 'cache.general';
public function getCollectorName() {
return pht('General Cache');
}
public function getDefaultRetentionPolicy() {
return phutil_units('30 days in seconds');
}
public function collectGarbage() {
$key = 'gcdaemon.ttl.general-cache';
$ttl = PhabricatorEnv::getEnvConfig($key);

View file

@ -3,6 +3,16 @@
final class PhabricatorCacheMarkupGarbageCollector
extends PhabricatorGarbageCollector {
const COLLECTORCONST = 'cache.markup';
public function getCollectorName() {
return pht('Markup Cache');
}
public function getDefaultRetentionPolicy() {
return phutil_units('30 days in seconds');
}
public function collectGarbage() {
$key = 'gcdaemon.ttl.markup-cache';
$ttl = PhabricatorEnv::getEnvConfig($key);

View file

@ -3,6 +3,16 @@
final class PhabricatorCacheTTLGarbageCollector
extends PhabricatorGarbageCollector {
const COLLECTORCONST = 'cache.general.ttl';
public function getCollectorName() {
return pht('General Cache (TTL)');
}
public function hasAutomaticPolicy() {
return true;
}
public function collectGarbage() {
$cache = new PhabricatorKeyValueDatabaseCache();
$conn_w = $cache->establishConnection('w');

View file

@ -3,6 +3,16 @@
final class ConduitConnectionGarbageCollector
extends PhabricatorGarbageCollector {
const COLLECTORCONST = 'conduit.connections';
public function getCollectorName() {
return pht('Conduit Connections');
}
public function getDefaultRetentionPolicy() {
return phutil_units('180 days in seconds');
}
public function collectGarbage() {
$key = 'gcdaemon.ttl.conduit-logs';
$ttl = PhabricatorEnv::getEnvConfig($key);

View file

@ -3,6 +3,16 @@
final class ConduitLogGarbageCollector
extends PhabricatorGarbageCollector {
const COLLECTORCONST = 'conduit.logs';
public function getCollectorName() {
return pht('Conduit Logs');
}
public function getDefaultRetentionPolicy() {
return phutil_units('180 days in seconds');
}
public function collectGarbage() {
$key = 'gcdaemon.ttl.conduit-logs';
$ttl = PhabricatorEnv::getEnvConfig($key);

View file

@ -3,6 +3,16 @@
final class ConduitTokenGarbageCollector
extends PhabricatorGarbageCollector {
const COLLECTORCONST = 'conduit.tokens';
public function getCollectorName() {
return pht('Conduit Tokens');
}
public function hasAutomaticPolicy() {
return true;
}
public function collectGarbage() {
$table = new PhabricatorConduitToken();
$conn_w = $table->establishConnection('w');

View file

@ -0,0 +1,61 @@
<?php
final class PhabricatorConfigCollectorsModule extends PhabricatorConfigModule {
public function getModuleKey() {
return 'collectors';
}
public function getModuleName() {
return pht('Garbage Collectors');
}
public function renderModuleStatus(AphrontRequest $request) {
$viewer = $request->getViewer();
$collectors = PhabricatorGarbageCollector::getAllCollectors();
$collectors = msort($collectors, 'getCollectorConstant');
$rows = array();
foreach ($collectors as $key => $collector) {
if ($collector->hasAutomaticPolicy()) {
$policy_view = phutil_tag('em', array(), pht('Automatic'));
} else {
$policy = $collector->getDefaultRetentionPolicy();
if ($policy === null) {
$policy_view = pht('Indefinite');
} else {
$days = ceil($policy / phutil_units('1 day in seconds'));
$policy_view = pht(
'%s Day(s)',
new PhutilNumber($days));
}
}
$rows[] = array(
$collector->getCollectorConstant(),
$collector->getCollectorName(),
$policy_view,
);
}
$table = id(new AphrontTableView($rows))
->setHeaders(
array(
pht('Constant'),
pht('Name'),
pht('Retention Policy'),
))
->setColumnClasses(
array(
null,
'pri wide',
null,
));
return id(new PHUIObjectBoxView())
->setHeaderText(pht('Garbage Collectors'))
->setTable($table);
}
}

View file

@ -3,6 +3,16 @@
final class PhabricatorDaemonLogEventGarbageCollector
extends PhabricatorGarbageCollector {
const COLLECTORCONST = 'daemon.processes';
public function getCollectorName() {
return pht('Daemon Processes');
}
public function getDefaultRetentionPolicy() {
return phutil_units('7 days in seconds');
}
public function collectGarbage() {
$ttl = PhabricatorEnv::getEnvConfig('gcdaemon.ttl.daemon-logs');
if ($ttl <= 0) {

View file

@ -3,6 +3,16 @@
final class PhabricatorDaemonLogGarbageCollector
extends PhabricatorGarbageCollector {
const COLLECTORCONST = 'daemon.logs';
public function getCollectorName() {
return pht('Daemon Logs');
}
public function getDefaultRetentionPolicy() {
return phutil_units('7 days in seconds');
}
public function collectGarbage() {
$ttl = PhabricatorEnv::getEnvConfig('gcdaemon.ttl.daemon-logs');
if ($ttl <= 0) {

View file

@ -3,6 +3,16 @@
final class PhabricatorDaemonTaskGarbageCollector
extends PhabricatorGarbageCollector {
const COLLECTORCONST = 'worker.tasks';
public function getCollectorName() {
return pht('Archived Tasks');
}
public function getDefaultRetentionPolicy() {
return phutil_units('14 days in seconds');
}
public function collectGarbage() {
$key = 'gcdaemon.ttl.task-archive';
$ttl = PhabricatorEnv::getEnvConfig($key);

View file

@ -3,6 +3,16 @@
final class DifferentialParseCacheGarbageCollector
extends PhabricatorGarbageCollector {
const COLLECTORCONST = 'differential.parse';
public function getCollectorName() {
return pht('Differential Parse Cache');
}
public function getDefaultRetentionPolicy() {
return phutil_units('14 days in seconds');
}
public function collectGarbage() {
$key = 'gcdaemon.ttl.differential-parse-cache';
$ttl = PhabricatorEnv::getEnvConfig($key);

View file

@ -3,6 +3,16 @@
final class DrydockLogGarbageCollector
extends PhabricatorGarbageCollector {
const COLLECTORCONST = 'drydock.logs';
public function getCollectorName() {
return pht('Drydock Logs');
}
public function getDefaultRetentionPolicy() {
return phutil_units('30 days in seconds');
}
public function collectGarbage() {
$log_table = new DrydockLog();
$conn_w = $log_table->establishConnection('w');

View file

@ -3,6 +3,16 @@
final class PhabricatorFileTemporaryGarbageCollector
extends PhabricatorGarbageCollector {
const COLLECTORCONST = 'files.ttl';
public function getCollectorName() {
return pht('Files (TTL)');
}
public function hasAutomaticPolicy() {
return true;
}
public function collectGarbage() {
$files = id(new PhabricatorFile())->loadAllWhere(
'ttl < %d LIMIT 100',

View file

@ -3,6 +3,16 @@
final class HeraldTranscriptGarbageCollector
extends PhabricatorGarbageCollector {
const COLLECTORCONST = 'herald.transcripts';
public function getCollectorName() {
return pht('Herald Transcripts');
}
public function getDefaultRetentionPolicy() {
return phutil_units('30 days in seconds');
}
public function collectGarbage() {
$ttl = PhabricatorEnv::getEnvConfig('gcdaemon.ttl.herald-transcripts');
if ($ttl <= 0) {

View file

@ -3,6 +3,16 @@
final class MetaMTAMailReceivedGarbageCollector
extends PhabricatorGarbageCollector {
const COLLECTORCONST = 'metamta.received';
public function getCollectorName() {
return pht('Mail (Received)');
}
public function getDefaultRetentionPolicy() {
return phutil_units('90 days in seconds');
}
public function collectGarbage() {
$ttl = phutil_units('90 days in seconds');

View file

@ -3,6 +3,16 @@
final class MetaMTAMailSentGarbageCollector
extends PhabricatorGarbageCollector {
const COLLECTORCONST = 'metamta.sent';
public function getCollectorName() {
return pht('Mail (Sent)');
}
public function getDefaultRetentionPolicy() {
return phutil_units('90 days in seconds');
}
public function collectGarbage() {
$ttl = phutil_units('90 days in seconds');

View file

@ -3,6 +3,16 @@
final class MultimeterEventGarbageCollector
extends PhabricatorGarbageCollector {
const COLLECTORCONST = 'multimeter.events';
public function getCollectorName() {
return pht('Multimeter Events');
}
public function getDefaultRetentionPolicy() {
return phutil_units('90 days in seconds');
}
public function collectGarbage() {
$ttl = phutil_units('90 days in seconds');

View file

@ -3,6 +3,16 @@
final class FeedStoryNotificationGarbageCollector
extends PhabricatorGarbageCollector {
const COLLECTORCONST = 'feed.notifications';
public function getCollectorName() {
return pht('Notifications');
}
public function getDefaultRetentionPolicy() {
return phutil_units('90 days in seconds');
}
public function collectGarbage() {
$ttl = 90 * 24 * 60 * 60;

View file

@ -3,6 +3,16 @@
final class PeopleUserLogGarbageCollector
extends PhabricatorGarbageCollector {
const COLLECTORCONST = 'user.logs';
public function getCollectorName() {
return pht('User Activity Logs');
}
public function getDefaultRetentionPolicy() {
return phutil_units('180 days in seconds');
}
public function collectGarbage() {
$ttl = phutil_units('180 days in seconds');

View file

@ -3,6 +3,16 @@
final class PhabricatorSystemActionGarbageCollector
extends PhabricatorGarbageCollector {
const COLLECTORCONST = 'system.actions';
public function getCollectorName() {
return pht('Rate Limiting Actions');
}
public function getDefaultRetentionPolicy() {
return phutil_units('3 days in seconds');
}
public function collectGarbage() {
$ttl = phutil_units('3 days in seconds');

View file

@ -3,6 +3,16 @@
final class PhabricatorSystemDestructionGarbageCollector
extends PhabricatorGarbageCollector {
const COLLECTORCONST = 'system.destruction.logs';
public function getCollectorName() {
return pht('Destruction Logs');
}
public function getDefaultRetentionPolicy() {
return phutil_units('90 days in seconds');
}
public function collectGarbage() {
$ttl = phutil_units('90 days in seconds');

View file

@ -1,12 +1,85 @@
<?php
/**
* @task info Getting Collector Information
* @task collect Collecting Garbage
*/
abstract class PhabricatorGarbageCollector extends Phobject {
/* -( Getting Collector Information )-------------------------------------- */
/**
* Get a human readable name for what this collector cleans up, like
* "User Activity Logs".
*
* @return string Human-readable collector name.
* @task info
*/
abstract public function getCollectorName();
/**
* Specify that the collector has an automatic retention policy and
* is not configurable.
*
* @return bool True if the collector has an automatic retention policy.
* @task info
*/
public function hasAutomaticPolicy() {
return false;
}
/**
* Get the default retention policy for this collector.
*
* Return the age (in seconds) when resources start getting collected, or
* `null` to retain resources indefinitely.
*
* @return int|null Lifetime, or `null` for indefinite retention.
* @task info
*/
public function getDefaultRetentionPolicy() {
throw new PhutilMethodNotImplementedException();
}
/**
* Get a unique string constant identifying this collector.
*
* @return string Collector constant.
* @task info
*/
final public function getCollectorConstant() {
return $this->getPhobjectClassConstant('COLLECTORCONST', 64);
}
/* -( Collecting Garbage )------------------------------------------------- */
/**
* Collect garbage from whatever source this GC handles.
*
* @return bool True if there is more garbage to collect.
* @task collect
*/
abstract public function collectGarbage();
/**
* Load all of the available garbage collectors.
*
* @return list<PhabricatorGarbageCollector> Garbage collectors.
* @task collect
*/
final public static function getAllCollectors() {
return id(new PhutilClassMapQuery())
->setAncestorClass(__CLASS__)
->setUniqueMethod('getCollectorConstant')
->execute();
}
}

View file

@ -348,7 +348,9 @@ final class PhabricatorTriggerDaemon
$next = $this->nextCollection;
if ($next && (PhabricatorTime::getNow() >= $next)) {
$this->nextCollection = null;
$this->garbageCollectors = $this->loadGarbageCollectors();
$all_collectors = PhabricatorGarbageCollector::getAllCollectors();
$this->garbageCollectors = $all_collectors;
}
// If we're in a collection cycle, continue collection.
@ -377,18 +379,4 @@ final class PhabricatorTriggerDaemon
return false;
}
/**
* Load all of the available garbage collectors.
*
* @return list<PhabricatorGarbageCollector> Garbage collectors.
* @task garbage
*/
private function loadGarbageCollectors() {
return id(new PhutilClassMapQuery())
->setAncestorClass('PhabricatorGarbageCollector')
->execute();
}
}

View file

@ -1388,6 +1388,11 @@ final class PhabricatorUSEnglishTranslation
'Mail sent in the last %s days.',
),
'%s Day(s)' => array(
'%s Day',
'%s Days',
),
);
}