ChangeRebuilderImpl: Handle non-monotonic patch set timestamps

Some data created by old versions of Gerrit has timestamps out of order
in ReviewDb. Naively converting these to NoteDb results in out-of-order
commits in the meta graph. Since at least I099fd703 (possibly earlier),
ChangeNotesParser depends on ordering in the meta graph to set the
currentPatchSetId properly, so these changes were showing up with the
current patch set being the latest patch set by creation time.

The easiest way to fix this is to use the dependency mechanism in
ChangeRebuilderImpl to ensure that each patch set has a dependency on
the previous patch set. However, this breaks the createdOn timestamp,
replacing the bogus old timestamp with a newer one. Handle this case in
ChangeBundle by ignoring createdOn if ReviewDb is out of order.

Change-Id: I1a245ff528cd96b6549c237bb9073b1820af93de
This commit is contained in:
Dave Borowitz
2017-02-06 12:45:21 -05:00
committed by Edwin Kempin
parent 72f61b20fc
commit c6db35e1dc
4 changed files with 203 additions and 6 deletions

View File

@@ -790,7 +790,7 @@ public class ChangeRebuilderIT extends AbstractDaemonTest {
PushOneCommit.FILE_NAME,
"new contents",
r.getChangeId())
.to("refs/heads/master");
.to("refs/for/master");
r.assertOkStatus();
PatchSet.Id psId = r.getPatchSetId();
@@ -1232,6 +1232,48 @@ public class ChangeRebuilderIT extends AbstractDaemonTest {
checker.rebuildAndCheckChanges(c.getId());
}
@Test
public void patchSetsOutOfOrder() throws Exception {
String id = createChange().getChangeId();
amendChange(id);
PushOneCommit.Result r = amendChange(id);
ChangeData cd = r.getChange();
PatchSet.Id psId3 = cd.change().currentPatchSetId();
assertThat(psId3.get()).isEqualTo(3);
PatchSet ps1 = db.patchSets().get(new PatchSet.Id(cd.getId(), 1));
PatchSet ps3 = db.patchSets().get(psId3);
assertThat(ps1.getCreatedOn()).isLessThan(ps3.getCreatedOn());
// Simulate an old Gerrit bug by setting the created timestamp of the latest
// patch set ID to the timestamp of PS1.
ps3.setCreatedOn(ps1.getCreatedOn());
db.patchSets().update(Collections.singleton(ps3));
checker.rebuildAndCheckChanges(cd.getId());
setNotesMigration(true, true);
cd = changeDataFactory.create(db, project, cd.getId());
assertThat(cd.change().currentPatchSetId()).isEqualTo(psId3);
List<PatchSet> patchSets = ImmutableList.copyOf(cd.patchSets());
assertThat(patchSets).hasSize(3);
PatchSet newPs1 = patchSets.get(0);
assertThat(newPs1.getId()).isEqualTo(ps1.getId());
assertThat(newPs1.getCreatedOn()).isEqualTo(ps1.getCreatedOn());
PatchSet newPs2 = patchSets.get(1);
assertThat(newPs2.getCreatedOn()).isGreaterThan(newPs1.getCreatedOn());
PatchSet newPs3 = patchSets.get(2);
assertThat(newPs3.getId()).isEqualTo(ps3.getId());
// Migrated with a newer timestamp than the original, to preserve ordering.
assertThat(newPs3.getCreatedOn()).isAtLeast(newPs2.getCreatedOn());
assertThat(newPs3.getCreatedOn()).isGreaterThan(ps1.getCreatedOn());
}
private void assertChangesReadOnly(RestApiException e) throws Exception {
Throwable cause = e.getCause();
assertThat(cause).isInstanceOf(UpdateException.class);