Enable/disable the accounts tracking preload mechanism

The default behaviour of preloading all the users in All-Users into the
tracking cache can now be disabled by configuration.

This enables a more accurate tracking of the real active users and avoid
the massive preload of the entire All-Users' accounts into the cache.

Change-Id: Ibf6d09f7a6c772f60e48f5c75ad0b2e587f8e681
diff --git a/admin/track-and-disable-inactive-users-1.3.groovy b/admin/track-and-disable-inactive-users-1.3.groovy
index 99471cf..71dcfc4 100644
--- a/admin/track-and-disable-inactive-users-1.3.groovy
+++ b/admin/track-and-disable-inactive-users-1.3.groovy
@@ -122,6 +122,7 @@
 
   final Set<Account.Id> ignoreAccountIds
   final Set<AccountGroup.UUID> ignoreGroupIds
+  final boolean preloadAccounts
 
   private final PluginConfig config
 
@@ -138,6 +139,7 @@
 
     ignoreAccountIds = ignoreAccountIdsFromConfig("ignoreAccountId")
     ignoreGroupIds = ignoreGroupIdsFromConfig("ignoreGroup", groupCache)
+    preloadAccounts = config.getBoolean("preloadAccounts", true)
 
     logger.atInfo().log("Accounts ids ignored for inactivity: %s", ignoreAccountIds)
     logger.atInfo().log("Group ids ignored for inactivity: %s", ignoreGroupIds)
@@ -258,12 +260,18 @@
 
   @Override
   void start() {
-    def currentMinutes = MILLISECONDS.toMinutes(System.currentTimeMillis())
-    accounts.all()
-        .findAll {
-          it.account().isActive() && !serviceUserClassifier.isServiceUser(it.account().id()) && !trackActiveUsersCache.getIfPresent(it.account().id().get())
-        }
-        .each { trackActiveUsersCache.put(it.account().id().get(), currentMinutes) }
+    if (autoDisableConfig.preloadAccounts) {
+      def accountsToPreload = accounts.all()
+                                .collect { it.account() }
+                                .findAll { it.isActive() }
+                                .findAll { !serviceUserClassifier.isServiceUser(it.id()) }
+                                .findAll { !trackActiveUsersCache.getIfPresent(it.id().get()) }
+
+      def currentMinutes = MILLISECONDS.toMinutes(System.currentTimeMillis())
+      def numAccountsToPreload = accountsToPreload.size()
+      logger.atInfo().log("Preloading $numAccountsToPreload accounts into ${TrackActiveUsersCache.NAME} with TS=$currentMinutes")
+      accountsToPreload.each { trackActiveUsersCache.put(it.id().get(), currentMinutes) }
+    }
   }
 
   @Override
diff --git a/admin/track-and-disable-inactive-users.md b/admin/track-and-disable-inactive-users.md
index 025d5c0..b79e635 100644
--- a/admin/track-and-disable-inactive-users.md
+++ b/admin/track-and-disable-inactive-users.md
@@ -43,6 +43,16 @@
    > **NOTE**: The `Service Users` group is always added to the list of groups of
    > accounts to not disable.
 
+```plugin.@PLUGIN@.preloadAccounts```
+:  Allow to preload the active users persistent cache with all the accounts that
+   are currently flagged as active on All-Users when the plugin starts.
+
+   > **NOTE**: When this option is enabled, all users configured in Gerrit will
+   > always be flagged as active when Gerrit or the plugin is started, even though
+   > they were not active for a long time.
+
+   Default: true.
+
 ```cache."@PLUGIN@.users_cache".maxAge```
 :  Maximum allowed inactivity time for user.
    Value should use common time unit suffixes to express their setting: