Move groups sequence to NoteDB

This mirrors commit 5be9c31 ("Move account sequence to NoteDb"), with
the following differences:

* The group IDs go into refs/sequences/groups

* SchemaCreator creates groups, so there, we manually instantiate
  Sequences.

* Since group creation is rare, there is no tuning for batch size.

This is the first step in the procedure to move the group IDs to
NoteDb. The next steps are:

* Ensure that this code is stable and rolled out

* Run the schema upgrade

* Flip the readSequenceFromNoteDb flag

* Ensure that code is stable

* Delete readSequenceFromNoteDb flag, and associated dead code.

Change-Id: I3c8f8c0e86de93184b3ea5e801d0f2b25121a10d
This commit is contained in:
Han-Wen Nienhuys
2017-11-08 13:33:33 +01:00
committed by Edwin Kempin
parent 8d62412a45
commit e5ca19cf4b
8 changed files with 176 additions and 17 deletions

View File

@@ -42,6 +42,7 @@ import org.eclipse.jgit.lib.Config;
@Singleton
public class Sequences {
public static final String NAME_ACCOUNTS = "accounts";
public static final String NAME_GROUPS = "groups";
public static final String NAME_CHANGES = "changes";
public static int getChangeSequenceGap(Config cfg) {
@@ -50,17 +51,20 @@ public class Sequences {
private enum SequenceType {
ACCOUNTS,
CHANGES;
CHANGES,
GROUPS;
}
private final Provider<ReviewDb> db;
private final NotesMigration migration;
private final RepoSequence accountSeq;
private final RepoSequence changeSeq;
private final RepoSequence groupSeq;
private final boolean readGroupSeqFromNoteDb;
private final Timer2<SequenceType, Boolean> nextIdLatency;
@Inject
Sequences(
public Sequences(
@GerritServerConfig Config cfg,
Provider<ReviewDb> db,
NotesMigration migration,
@@ -90,6 +94,13 @@ public class Sequences {
new RepoSequence(
repoManager, gitRefUpdated, allProjects, NAME_CHANGES, changeSeed, changeBatchSize);
RepoSequence.Seed groupSeed = () -> nextGroupId(db.get());
int groupBatchSize = 1;
groupSeq =
new RepoSequence(
repoManager, gitRefUpdated, allUsers, NAME_GROUPS, groupSeed, groupBatchSize);
readGroupSeqFromNoteDb = readGroupFromNoteDbSetting(cfg);
nextIdLatency =
metrics.newTimer(
"sequence/next_id_latency",
@@ -100,6 +111,10 @@ public class Sequences {
Field.ofBoolean("multiple"));
}
public static boolean readGroupFromNoteDbSetting(Config cfg) {
return cfg.getBoolean("noteDb", "groups", "readSequenceFromNoteDb", false);
}
public int nextAccountId() throws OrmException {
try (Timer2.Context timer = nextIdLatency.start(SequenceType.ACCOUNTS, false)) {
return accountSeq.next();
@@ -134,6 +149,17 @@ public class Sequences {
return ImmutableList.copyOf(ids);
}
public int nextGroupId() throws OrmException {
if (readGroupSeqFromNoteDb) {
try (Timer2.Context timer = nextIdLatency.start(SequenceType.GROUPS, false)) {
return groupSeq.next();
}
}
int groupId = nextGroupId(db.get());
groupSeq.increaseTo(groupId + 1); // NoteDb stores next available group ID.
return groupId;
}
@VisibleForTesting
public RepoSequence getChangeIdRepoSequence() {
return changeSeq;
@@ -143,4 +169,9 @@ public class Sequences {
private static int nextChangeId(ReviewDb db) throws OrmException {
return db.nextChangeId();
}
@SuppressWarnings("deprecation")
static int nextGroupId(ReviewDb db) throws OrmException {
return db.nextAccountGroupId();
}
}