Load each code owner config only once for computing code owner statuses

When computing code owner statuses we need to iterate over the relevant
code owner configs for each of the files that have been touched in the
change. In this process code owner config files that apply to multiple
files are loaded multiple times (e.g. the root code owner config is
loaded once per file). To reduce the number of code owner config loads
we add a transient in-memory cache that caches the loaded code owner
config files for the time of the code owner computation. Once the
computation is done the cache is dropped.

The implementation of the cache is rather simple. It has a HashMap in
which it stores loaded code owner configs. If a requested code owner
config is available in the map it is returned, otherwise the code owner
config is loaded and put into the cache. The implementation of the cache
is not thread-safe, but since we create a new instance for every request
and the request processes file synchronously it also doesn't need to be
thread-safe.

The cache is instantiated in CodeOwnerConfigHierarchy which loads the
code owner config files that are relevant for a path. Previously
CodeOwnerConfigHierarchy was a singleton, but now the @Singleton
annotation was removed so that each instance gets its own cache. The
cache is passed over into PathCodeOwners which does the actual loading
of code owner configs.

CodeOwnerApprovalCache takes care to instantiate
CodeOwnerConfigHierarchy outside the loop that iterates over all files,
so that the same instance, and hence the same cache, is used for all
files.

Other classes that get CodeOwnerConfigHierarchy injected
(CheckCodeOwner, AbstractGetCodeOwnersForPath) are non-sigleton and
hence don't need to be adapted.

PathCodeOwners also has other callers that do not require caching. Hence
PathCodeOwners can be used, with or without cache. To support this we
add a new CodeOwnerConfigLoader interface that is implemented by
CodeOwners (loads code owner configs from the backend) and the new cache
(retrieves code owner configs from the cache, or falls back to
CodeOwners).

There are no unit tests for the new cache class yet, but the test
coverage shows that it is fully covered by the existing tests (except
the catch block for exception handling).

This change also adds a new metric for counting the number of code owner
configs that have been returned from cache. This way we will be able to
see what difference this change made.

Signed-off-by: Edwin Kempin <ekempin@google.com>
Change-Id: Id72def08ee26b2f7457a4e83e42ee46e7cbec0dc
11 files changed
tree: 5bcaa6b812015a1e2f322c44d160d20d9737430a
  1. java/
  2. javatests/
  3. proto/
  4. resources/
  5. test/
  6. ui/
  7. .eslintrc.json
  8. .gitignore
  9. .gitreview
  10. bower.json
  11. BUILD
  12. LICENSE
  13. package-lock.json
  14. package.json
  15. README.md
README.md

Gerrit Code Review code-owners plugin

This plugin provides support for defining code owners for files in a repository.

If the code-owners plugin is enabled, changes can only be submitted if all touched files are covered by approvals from code owners.

Also see resources/Documentation/about.md.

IMPORTANT: Before installing/enabling the plugin follow the instructions from the setup guide, see resources/Documentation/setup-guide.md.