diff options
Diffstat (limited to 'gerrit-server/src/main/java/com/google/gerrit/server/Sequences.java')
-rw-r--r-- | gerrit-server/src/main/java/com/google/gerrit/server/Sequences.java | 87 |
1 files changed, 68 insertions, 19 deletions
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/Sequences.java b/gerrit-server/src/main/java/com/google/gerrit/server/Sequences.java index 4ab42f30f7..930f3f3e32 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/Sequences.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/Sequences.java @@ -18,9 +18,16 @@ import static com.google.common.base.Preconditions.checkArgument; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; +import com.google.gerrit.metrics.Description; +import com.google.gerrit.metrics.Description.Units; +import com.google.gerrit.metrics.Field; +import com.google.gerrit.metrics.MetricMaker; +import com.google.gerrit.metrics.Timer2; import com.google.gerrit.reviewdb.server.ReviewDb; import com.google.gerrit.server.config.AllProjectsName; +import com.google.gerrit.server.config.AllUsersName; import com.google.gerrit.server.config.GerritServerConfig; +import com.google.gerrit.server.extensions.events.GitReferenceUpdated; import com.google.gerrit.server.git.GitRepositoryManager; import com.google.gerrit.server.notedb.NotesMigration; import com.google.gerrit.server.notedb.RepoSequence; @@ -32,50 +39,87 @@ import java.util.ArrayList; import java.util.List; import org.eclipse.jgit.lib.Config; -@SuppressWarnings("deprecation") @Singleton public class Sequences { - public static final String CHANGES = "changes"; + public static final String NAME_ACCOUNTS = "accounts"; + public static final String NAME_CHANGES = "changes"; + + public static int getChangeSequenceGap(Config cfg) { + return cfg.getInt("noteDb", "changes", "initialSequenceGap", 1000); + } + + private enum SequenceType { + ACCOUNTS, + CHANGES; + } private final Provider<ReviewDb> db; private final NotesMigration migration; + private final RepoSequence accountSeq; private final RepoSequence changeSeq; + private final Timer2<SequenceType, Boolean> nextIdLatency; @Inject Sequences( @GerritServerConfig Config cfg, - final Provider<ReviewDb> db, + Provider<ReviewDb> db, NotesMigration migration, GitRepositoryManager repoManager, - AllProjectsName allProjects) { + GitReferenceUpdated gitRefUpdated, + AllProjectsName allProjects, + AllUsersName allUsers, + MetricMaker metrics) { this.db = db; this.migration = migration; - final int gap = cfg.getInt("noteDb", "changes", "initialSequenceGap", 0); - changeSeq = + int accountBatchSize = cfg.getInt("noteDb", "accounts", "sequenceBatchSize", 1); + accountSeq = new RepoSequence( repoManager, - allProjects, - CHANGES, - new RepoSequence.Seed() { - @Override - public int get() throws OrmException { - return db.get().nextChangeId() + gap; - } - }, - cfg.getInt("noteDb", "changes", "sequenceBatchSize", 20)); + gitRefUpdated, + allUsers, + NAME_ACCOUNTS, + () -> ReviewDb.FIRST_ACCOUNT_ID, + accountBatchSize); + + int gap = getChangeSequenceGap(cfg); + @SuppressWarnings("deprecation") + RepoSequence.Seed changeSeed = () -> db.get().nextChangeId() + gap; + int changeBatchSize = cfg.getInt("noteDb", "changes", "sequenceBatchSize", 20); + changeSeq = + new RepoSequence( + repoManager, gitRefUpdated, allProjects, NAME_CHANGES, changeSeed, changeBatchSize); + + nextIdLatency = + metrics.newTimer( + "sequence/next_id_latency", + new Description("Latency of requesting IDs from repo sequences") + .setCumulative() + .setUnit(Units.MILLISECONDS), + Field.ofEnum(SequenceType.class, "sequence"), + Field.ofBoolean("multiple")); + } + + public int nextAccountId() throws OrmException { + try (Timer2.Context timer = nextIdLatency.start(SequenceType.ACCOUNTS, false)) { + return accountSeq.next(); + } } public int nextChangeId() throws OrmException { if (!migration.readChangeSequence()) { - return db.get().nextChangeId(); + return nextChangeId(db.get()); + } + try (Timer2.Context timer = nextIdLatency.start(SequenceType.CHANGES, false)) { + return changeSeq.next(); } - return changeSeq.next(); } public ImmutableList<Integer> nextChangeIds(int count) throws OrmException { if (migration.readChangeSequence()) { - return changeSeq.next(count); + try (Timer2.Context timer = nextIdLatency.start(SequenceType.CHANGES, count > 1)) { + return changeSeq.next(count); + } } if (count == 0) { @@ -85,7 +129,7 @@ public class Sequences { List<Integer> ids = new ArrayList<>(count); ReviewDb db = this.db.get(); for (int i = 0; i < count; i++) { - ids.add(db.nextChangeId()); + ids.add(nextChangeId(db)); } return ImmutableList.copyOf(ids); } @@ -94,4 +138,9 @@ public class Sequences { public RepoSequence getChangeIdRepoSequence() { return changeSeq; } + + @SuppressWarnings("deprecation") + private static int nextChangeId(ReviewDb db) throws OrmException { + return db.nextChangeId(); + } } |