Introduce global quotas
Have a way to specify quotas that are always applicable in addition
to the quotas specified in scoped namespaces.
Change-Id: I0b9f2834d1b77c40c8e32025074614b1d4ba8f38
diff --git a/src/main/java/com/googlesource/gerrit/plugins/quota/GlobalQuotaSection.java b/src/main/java/com/googlesource/gerrit/plugins/quota/GlobalQuotaSection.java
new file mode 100644
index 0000000..a378cb4
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/quota/GlobalQuotaSection.java
@@ -0,0 +1,45 @@
+// Copyright (C) 2014 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.quota;
+
+import com.google.gerrit.entities.Project;
+import org.eclipse.jgit.lib.Config;
+
+/**
+ * Provides the representation of global-quota config
+ *
+ * @param cfg quota.cfg file
+ */
+public record GlobalQuotaSection(Config cfg) implements QuotaSection {
+ public static final String GLOBAL_QUOTA = "global-quota";
+
+ public String getNamespace() {
+ return GLOBAL_QUOTA;
+ }
+
+ public boolean matches(Project.NameKey project) {
+ return true;
+ }
+
+ @Override
+ public String section() {
+ return GLOBAL_QUOTA;
+ }
+
+ @Override
+ public String subSection() {
+ return null;
+ }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/quota/MaxRepositoriesQuotaValidator.java b/src/main/java/com/googlesource/gerrit/plugins/quota/MaxRepositoriesQuotaValidator.java
index 12b6e19..91a2bae 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/quota/MaxRepositoriesQuotaValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/quota/MaxRepositoriesQuotaValidator.java
@@ -35,7 +35,11 @@
@Override
public void validateNewProject(CreateProjectArgs args) throws ValidationException {
- QuotaSection quotaSection = quotaFinder.firstMatching(args.getProject());
+ validateNewProject(quotaFinder.firstMatching(args.getProject()));
+ validateNewProject(quotaFinder.getGlobalNamespacedQuota());
+ }
+
+ private void validateNewProject(QuotaSection quotaSection) throws ValidationException {
if (quotaSection != null) {
Integer maxProjects = quotaSection.getMaxProjects();
if (maxProjects != null) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/quota/MaxRepositorySizeQuota.java b/src/main/java/com/googlesource/gerrit/plugins/quota/MaxRepositorySizeQuota.java
index bd2ddd6..6b565f3 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/quota/MaxRepositorySizeQuota.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/quota/MaxRepositorySizeQuota.java
@@ -43,6 +43,9 @@
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicLong;
@@ -87,7 +90,17 @@
}
protected Optional<Long> getMaxPackSize(Project.NameKey project) {
- QuotaSection quotaSection = quotaFinder.firstMatching(project);
+ List<Long> maxPackCandidates = new ArrayList<>();
+ getMaxPackSize(quotaFinder.firstMatching(project), project).ifPresent(maxPackCandidates::add);
+ getMaxPackSize(quotaFinder.getGlobalNamespacedQuota(), project)
+ .ifPresent(maxPackCandidates::add);
+
+ return maxPackCandidates.isEmpty()
+ ? Optional.empty()
+ : Optional.of(Collections.min(maxPackCandidates));
+ }
+
+ protected Optional<Long> getMaxPackSize(QuotaSection quotaSection, Project.NameKey project) {
if (quotaSection == null) {
return Optional.empty();
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/quota/NamespacedQuotaSection.java b/src/main/java/com/googlesource/gerrit/plugins/quota/NamespacedQuotaSection.java
new file mode 100644
index 0000000..fd7f729
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/quota/NamespacedQuotaSection.java
@@ -0,0 +1,45 @@
+// Copyright (C) 2014 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.quota;
+
+import com.google.gerrit.entities.Project;
+import org.eclipse.jgit.lib.Config;
+
+public record NamespacedQuotaSection(Config cfg, String namespace, String resolvedNamespace)
+ implements QuotaSection {
+ public static final String QUOTA = "quota";
+
+ public NamespacedQuotaSection(Config cfg, String namespace) {
+ this(cfg, namespace, namespace);
+ }
+
+ public String getNamespace() {
+ return resolvedNamespace;
+ }
+
+ public boolean matches(Project.NameKey project) {
+ return new Namespace(resolvedNamespace).matches(project);
+ }
+
+ @Override
+ public String section() {
+ return QUOTA;
+ }
+
+ @Override
+ public String subSection() {
+ return namespace();
+ }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/quota/QuotaFinder.java b/src/main/java/com/googlesource/gerrit/plugins/quota/QuotaFinder.java
index 87e21fd..9584461 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/quota/QuotaFinder.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/quota/QuotaFinder.java
@@ -37,37 +37,45 @@
}
public QuotaSection firstMatching(Config cfg, Project.NameKey project) {
- Set<String> namespaces = cfg.getSubsections(QuotaSection.QUOTA);
+ Set<String> namespaces = cfg.getSubsections(NamespacedQuotaSection.QUOTA);
String p = project.get();
for (String n : namespaces) {
if ("?/*".equals(n) || n.endsWith("/?/*")) {
String prefix = n.substring(0, n.length() - 3);
Matcher m = Pattern.compile("^" + prefix + "([^/]+)/.*$").matcher(p);
if (m.matches()) {
- return new QuotaSection(cfg, n, prefix + m.group(1) + "/*");
+ return new NamespacedQuotaSection(cfg, n, prefix + m.group(1) + "/*");
}
} else if (n.endsWith("/*")) {
if (p.startsWith(n.substring(0, n.length() - 1))) {
- return new QuotaSection(cfg, n);
+ return new NamespacedQuotaSection(cfg, n);
}
} else if (n.startsWith("^")) {
if (p.matches(n.substring(1))) {
- return new QuotaSection(cfg, n);
+ return new NamespacedQuotaSection(cfg, n);
}
} else if (p.equals(n)) {
- return new QuotaSection(cfg, n);
+ return new NamespacedQuotaSection(cfg, n);
}
}
return null;
}
- public QuotaSection getGlobalNamespacedQuota(Config cfg) {
- return new QuotaSection(cfg, "*");
+ public QuotaSection getGlobalNamespacedQuota() {
+ return getGlobalNamespacedQuota(getQuotaConfig());
}
- public List<QuotaSection> getQuotaNamespaces(Config cfg) {
- return cfg.getSubsections(QuotaSection.QUOTA).stream()
- .map(ns -> new QuotaSection(cfg, ns))
+ public QuotaSection getGlobalNamespacedQuota(Config cfg) {
+ return new GlobalQuotaSection(cfg);
+ }
+
+ public QuotaSection getFallbackNamespacedQuota(Config cfg) {
+ return new NamespacedQuotaSection(cfg, "*");
+ }
+
+ public List<NamespacedQuotaSection> getQuotaNamespaces(Config cfg) {
+ return cfg.getSubsections(NamespacedQuotaSection.QUOTA).stream()
+ .map(ns -> new NamespacedQuotaSection(cfg, ns))
.toList();
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/quota/QuotaSection.java b/src/main/java/com/googlesource/gerrit/plugins/quota/QuotaSection.java
index a1be14b..0a9d0fd 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/quota/QuotaSection.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/quota/QuotaSection.java
@@ -19,56 +19,50 @@
import java.util.List;
import java.util.Optional;
import org.eclipse.jgit.lib.Config;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-public record QuotaSection(Config cfg, String namespace, String resolvedNamespace) {
- private static final Logger log = LoggerFactory.getLogger(QuotaSection.class);
- public static final String QUOTA = "quota";
- public static final String KEY_MAX_PROJECTS = "maxProjects";
- public static final String KEY_MAX_REPO_SIZE = "maxRepoSize";
- public static final String KEY_MAX_TOTAL_SIZE = "maxTotalSize";
+public interface QuotaSection {
+ String KEY_MAX_PROJECTS = "maxProjects";
+ String KEY_MAX_REPO_SIZE = "maxRepoSize";
+ String KEY_MAX_TOTAL_SIZE = "maxTotalSize";
- public QuotaSection(Config cfg, String namespace) {
- this(cfg, namespace, namespace);
- }
+ String getNamespace();
- public String getNamespace() {
- return resolvedNamespace;
- }
+ boolean matches(Project.NameKey project);
- public boolean matches(Project.NameKey project) {
- return new Namespace(resolvedNamespace).matches(project);
- }
-
- public Integer getMaxProjects() {
- if (!cfg.getNames(QUOTA, namespace).contains(KEY_MAX_PROJECTS)) {
+ default Integer getMaxProjects() {
+ if (!cfg().getNames(section(), subSection()).contains(KEY_MAX_PROJECTS)) {
return null;
}
- return cfg.getInt(QUOTA, namespace, KEY_MAX_PROJECTS, Integer.MAX_VALUE);
+ return cfg().getInt(section(), subSection(), KEY_MAX_PROJECTS, Integer.MAX_VALUE);
}
- public Long getMaxRepoSize() {
- if (!cfg.getNames(QUOTA, namespace).contains(KEY_MAX_REPO_SIZE)) {
+ default Long getMaxRepoSize() {
+ if (!cfg().getNames(section(), subSection()).contains(KEY_MAX_REPO_SIZE)) {
return null;
}
- return cfg.getLong(QUOTA, namespace, KEY_MAX_REPO_SIZE, Long.MAX_VALUE);
+ return cfg().getLong(section(), subSection(), KEY_MAX_REPO_SIZE, Long.MAX_VALUE);
}
- public Long getMaxTotalSize() {
- if (!cfg.getNames(QUOTA, namespace).contains(KEY_MAX_TOTAL_SIZE)) {
+ default Long getMaxTotalSize() {
+ if (!cfg().getNames(section(), subSection()).contains(KEY_MAX_TOTAL_SIZE)) {
return null;
}
- return cfg.getLong(QUOTA, namespace, KEY_MAX_TOTAL_SIZE, Long.MAX_VALUE);
+ return cfg().getLong(section(), subSection(), KEY_MAX_TOTAL_SIZE, Long.MAX_VALUE);
}
- public List<TaskQuota> getAllQuotas() {
+ default List<TaskQuota> getAllQuotas() {
return Arrays.stream(TaskQuotaKeys.values())
.flatMap(
type ->
- Arrays.stream(cfg.getStringList(QUOTA, namespace, type.key))
+ Arrays.stream(cfg().getStringList(section(), subSection(), type.key))
.map(type.processor)
.flatMap(Optional::stream))
.toList();
}
+
+ Config cfg();
+
+ String section();
+
+ String subSection();
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/quota/TaskQuotas.java b/src/main/java/com/googlesource/gerrit/plugins/quota/TaskQuotas.java
index 2fc703e..aa71530 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/quota/TaskQuotas.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/quota/TaskQuotas.java
@@ -36,6 +36,7 @@
private final QuotaFinder quotaFinder;
private final Map<Integer, List<TaskQuota>> quotasByTask = new ConcurrentHashMap<>();
private final Map<QuotaSection, List<TaskQuota>> quotasByNamespace = new HashMap<>();
+ private final List<TaskQuota> globalQuotas = new ArrayList<>();
private final Pattern PROJECT_PATTERN = Pattern.compile("\\s+/?(.*)\\s+(\\(\\S+\\))$");
private final Config quotaConfig;
@@ -65,24 +66,25 @@
quotasByNamespace.putAll(
quotaFinder.getQuotaNamespaces(quotaConfig).stream()
.collect(Collectors.toMap(Function.identity(), QuotaSection::getAllQuotas)));
+ globalQuotas.addAll(quotaFinder.getGlobalNamespacedQuota(quotaConfig).getAllQuotas());
}
@Override
public boolean isReadyToStart(WorkQueue.Task<?> task) {
Optional<Project.NameKey> estimatedProject = estimateProject(task);
- List<TaskQuota> quotas =
+ List<TaskQuota> applicableQuotas = new ArrayList<>(globalQuotas);
+ applicableQuotas.addAll(
estimatedProject
.map(
project -> {
return quotasByNamespace.getOrDefault(
Optional.ofNullable(quotaFinder.firstMatching(quotaConfig, project))
- .orElse(quotaFinder.getGlobalNamespacedQuota(quotaConfig)),
+ .orElse(quotaFinder.getFallbackNamespacedQuota(quotaConfig)),
List.of());
})
- .orElse(List.of());
+ .orElse(List.of()));
QueueStats.Queue queue = QueueStats.Queue.fromKey(task.getQueueName());
-
if (!QueueStats.acquire(queue, 1)) {
return false;
}
@@ -90,7 +92,7 @@
List<TaskQuota> acquiredQuotas = new ArrayList<>();
quotasByTask.put(task.getTaskId(), acquiredQuotas);
- for (TaskQuota quota : quotas) {
+ for (TaskQuota quota : applicableQuotas) {
if (quota.isApplicable(task)) {
if (!quota.isReadyToStart(task)) {
log.debug("Task [{}] will be parked due task quota rules", task);
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index 795638f..544df87 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -118,6 +118,16 @@
maxTotalSize = 20 m
```
+One could also add quotas in global section that would be applicable to all
+the projects (including the namespaces that are already defined).
+
+```
+ [global-quota]
+ maxProjects = 500
+ maxRepoSize = 300 m
+ maxTotalSize = 200 m
+```
+
If one prefers computing a repository size by adding the size of the git objects,
the following section should be added into the `gerrit.config` file:
diff --git a/src/test/java/com/googlesource/gerrit/plugins/quota/MaxRepositorySizeQuotaTest.java b/src/test/java/com/googlesource/gerrit/plugins/quota/MaxRepositorySizeQuotaTest.java
index 2c1c916..05e0453 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/quota/MaxRepositorySizeQuotaTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/quota/MaxRepositorySizeQuotaTest.java
@@ -19,8 +19,8 @@
import static com.google.gerrit.server.quota.QuotaResponse.Status.ERROR;
import static com.google.gerrit.server.quota.QuotaResponse.Status.NO_OP;
import static com.google.gerrit.server.quota.QuotaResponse.Status.OK;
+import static com.googlesource.gerrit.plugins.quota.NamespacedQuotaSection.QUOTA;
import static com.googlesource.gerrit.plugins.quota.QuotaSection.KEY_MAX_REPO_SIZE;
-import static com.googlesource.gerrit.plugins.quota.QuotaSection.QUOTA;
import static org.mockito.Mockito.when;
import com.google.common.cache.CacheBuilder;
@@ -114,6 +114,6 @@
when(repoSizeLoader.load(PROJECT_NAME)).thenReturn(new AtomicLong(currentRepoSize));
when(quotaFinder.firstMatching(PROJECT_NAME))
- .thenReturn(new QuotaSection(config, PROJECT_NAME.get()));
+ .thenReturn(new NamespacedQuotaSection(config, PROJECT_NAME.get()));
}
}