LCOV - code coverage report
Current view: top level - server/notedb - Sequences.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 38 54 70.4 %
Date: 2022-11-19 15:00:39 Functions: 8 21 38.1 %

          Line data    Source code
       1             : // Copyright (C) 2016 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.notedb;
      16             : 
      17             : import com.google.common.collect.ImmutableList;
      18             : import com.google.gerrit.metrics.Description;
      19             : import com.google.gerrit.metrics.Description.Units;
      20             : import com.google.gerrit.metrics.Field;
      21             : import com.google.gerrit.metrics.MetricMaker;
      22             : import com.google.gerrit.metrics.Timer2;
      23             : import com.google.gerrit.server.config.AllProjectsName;
      24             : import com.google.gerrit.server.config.AllUsersName;
      25             : import com.google.gerrit.server.config.GerritServerConfig;
      26             : import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
      27             : import com.google.gerrit.server.git.GitRepositoryManager;
      28             : import com.google.gerrit.server.logging.Metadata;
      29             : import com.google.inject.Inject;
      30             : import com.google.inject.Singleton;
      31             : import org.eclipse.jgit.lib.Config;
      32             : 
      33             : @Singleton
      34             : public class Sequences {
      35             :   private static final String SECTION_NOTEDB = "noteDb";
      36             :   private static final String KEY_SEQUENCE_BATCH_SIZE = "sequenceBatchSize";
      37             :   private static final int DEFAULT_ACCOUNTS_SEQUENCE_BATCH_SIZE = 1;
      38             :   private static final int DEFAULT_CHANGES_SEQUENCE_BATCH_SIZE = 20;
      39             : 
      40             :   public static final String NAME_ACCOUNTS = "accounts";
      41             :   public static final String NAME_GROUPS = "groups";
      42             :   public static final String NAME_CHANGES = "changes";
      43             : 
      44             :   public static final int FIRST_ACCOUNT_ID = 1000000;
      45             :   public static final int FIRST_GROUP_ID = 1;
      46             :   public static final int FIRST_CHANGE_ID = 1;
      47             : 
      48         151 :   private enum SequenceType {
      49         151 :     ACCOUNTS,
      50         151 :     CHANGES,
      51         151 :     GROUPS;
      52             :   }
      53             : 
      54             :   private final RepoSequence accountSeq;
      55             :   private final RepoSequence changeSeq;
      56             :   private final RepoSequence groupSeq;
      57             :   private final Timer2<SequenceType, Boolean> nextIdLatency;
      58             :   private final int accountBatchSize;
      59             :   private final int changeBatchSize;
      60         151 :   private final int groupBatchSize = 1;
      61             : 
      62             :   @Inject
      63             :   public Sequences(
      64             :       @GerritServerConfig Config cfg,
      65             :       GitRepositoryManager repoManager,
      66             :       GitReferenceUpdated gitRefUpdated,
      67             :       AllProjectsName allProjects,
      68             :       AllUsersName allUsers,
      69         151 :       MetricMaker metrics) {
      70             : 
      71         151 :     accountBatchSize =
      72         151 :         cfg.getInt(
      73             :             SECTION_NOTEDB,
      74             :             NAME_ACCOUNTS,
      75             :             KEY_SEQUENCE_BATCH_SIZE,
      76             :             DEFAULT_ACCOUNTS_SEQUENCE_BATCH_SIZE);
      77         151 :     accountSeq =
      78             :         new RepoSequence(
      79             :             repoManager,
      80             :             gitRefUpdated,
      81             :             allUsers,
      82             :             NAME_ACCOUNTS,
      83         151 :             () -> FIRST_ACCOUNT_ID,
      84             :             accountBatchSize);
      85             : 
      86         151 :     changeBatchSize =
      87         151 :         cfg.getInt(
      88             :             SECTION_NOTEDB,
      89             :             NAME_CHANGES,
      90             :             KEY_SEQUENCE_BATCH_SIZE,
      91             :             DEFAULT_CHANGES_SEQUENCE_BATCH_SIZE);
      92         151 :     changeSeq =
      93             :         new RepoSequence(
      94             :             repoManager,
      95             :             gitRefUpdated,
      96             :             allProjects,
      97             :             NAME_CHANGES,
      98           0 :             () -> FIRST_CHANGE_ID,
      99             :             changeBatchSize);
     100             : 
     101         151 :     groupSeq =
     102             :         new RepoSequence(
     103             :             repoManager,
     104             :             gitRefUpdated,
     105             :             allUsers,
     106             :             NAME_GROUPS,
     107         151 :             () -> FIRST_GROUP_ID,
     108             :             groupBatchSize);
     109             : 
     110         151 :     nextIdLatency =
     111         151 :         metrics.newTimer(
     112             :             "sequence/next_id_latency",
     113             :             new Description("Latency of requesting IDs from repo sequences")
     114         151 :                 .setCumulative()
     115         151 :                 .setUnit(Units.MILLISECONDS),
     116         151 :             Field.ofEnum(SequenceType.class, "sequence", Metadata.Builder::noteDbSequenceType)
     117         151 :                 .description("The sequence from which IDs were retrieved.")
     118         151 :                 .build(),
     119         151 :             Field.ofBoolean("multiple", Metadata.Builder::multiple)
     120         151 :                 .description("Whether more than one ID was retrieved.")
     121         151 :                 .build());
     122         151 :   }
     123             : 
     124             :   public int nextAccountId() {
     125         151 :     try (Timer2.Context<SequenceType, Boolean> timer =
     126         151 :         nextIdLatency.start(SequenceType.ACCOUNTS, false)) {
     127         151 :       return accountSeq.next();
     128             :     }
     129             :   }
     130             : 
     131             :   public int nextChangeId() {
     132          48 :     try (Timer2.Context<SequenceType, Boolean> timer =
     133          48 :         nextIdLatency.start(SequenceType.CHANGES, false)) {
     134          48 :       return changeSeq.next();
     135             :     }
     136             :   }
     137             : 
     138             :   public ImmutableList<Integer> nextChangeIds(int count) {
     139          88 :     try (Timer2.Context<SequenceType, Boolean> timer =
     140          88 :         nextIdLatency.start(SequenceType.CHANGES, count > 1)) {
     141          88 :       return changeSeq.next(count);
     142             :     }
     143             :   }
     144             : 
     145             :   public int nextGroupId() {
     146         151 :     try (Timer2.Context<SequenceType, Boolean> timer =
     147         151 :         nextIdLatency.start(SequenceType.GROUPS, false)) {
     148         151 :       return groupSeq.next();
     149             :     }
     150             :   }
     151             : 
     152             :   public int changeBatchSize() {
     153           0 :     return changeBatchSize;
     154             :   }
     155             : 
     156             :   public int groupBatchSize() {
     157           0 :     return groupBatchSize;
     158             :   }
     159             : 
     160             :   public int accountBatchSize() {
     161           0 :     return accountBatchSize;
     162             :   }
     163             : 
     164             :   public int currentChangeId() {
     165           0 :     return changeSeq.current();
     166             :   }
     167             : 
     168             :   public int currentAccountId() {
     169           0 :     return accountSeq.current();
     170             :   }
     171             : 
     172             :   public int currentGroupId() {
     173           0 :     return groupSeq.current();
     174             :   }
     175             : 
     176             :   public int lastChangeId() {
     177           0 :     return changeSeq.last();
     178             :   }
     179             : 
     180             :   public int lastGroupId() {
     181           0 :     return groupSeq.last();
     182             :   }
     183             : 
     184             :   public int lastAccountId() {
     185           0 :     return accountSeq.last();
     186             :   }
     187             : 
     188             :   public void setChangeIdValue(int value) {
     189           0 :     changeSeq.storeNew(value);
     190           0 :   }
     191             : 
     192             :   public void setAccountIdValue(int value) {
     193           0 :     accountSeq.storeNew(value);
     194           0 :   }
     195             : 
     196             :   public void setGroupIdValue(int value) {
     197           0 :     groupSeq.storeNew(value);
     198           0 :   }
     199             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750