Search in sources :

Example 6 with ChangeBundle

use of com.google.gerrit.server.notedb.ChangeBundle in project gerrit by GerritCodeReview.

the class ChangeRebuilderImpl method rebuildReviewDb.

@Override
public void rebuildReviewDb(ReviewDb db, Project.NameKey project, Change.Id changeId) throws OrmException {
    // TODO(dborowitz): Fail fast if changes tables are disabled in ReviewDb.
    ChangeNotes notes = notesFactory.create(db, project, changeId);
    ChangeBundle bundle = ChangeBundle.fromNotes(commentsUtil, notes);
    db = ReviewDbUtil.unwrapDb(db);
    db.changes().beginTransaction(changeId);
    try {
        Change c = db.changes().get(changeId);
        PrimaryStorage ps = PrimaryStorage.of(c);
        if (ps != PrimaryStorage.NOTE_DB) {
            throw new OrmException("primary storage of " + changeId + " is " + ps);
        }
        db.changes().upsert(Collections.singleton(c));
        putExactlyEntities(db.changeMessages(), db.changeMessages().byChange(c.getId()), bundle.getChangeMessages());
        putExactlyEntities(db.patchSets(), db.patchSets().byChange(c.getId()), bundle.getPatchSets());
        putExactlyEntities(db.patchSetApprovals(), db.patchSetApprovals().byChange(c.getId()), bundle.getPatchSetApprovals());
        putExactlyEntities(db.patchComments(), db.patchComments().byChange(c.getId()), bundle.getPatchLineComments());
        db.commit();
    } finally {
        db.rollback();
    }
}
Also used : PrimaryStorage(com.google.gerrit.server.notedb.NoteDbChangeState.PrimaryStorage) OrmException(com.google.gwtorm.server.OrmException) ChangeBundle(com.google.gerrit.server.notedb.ChangeBundle) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) Change(com.google.gerrit.reviewdb.client.Change)

Example 7 with ChangeBundle

use of com.google.gerrit.server.notedb.ChangeBundle in project gerrit by GerritCodeReview.

the class ChangeRebuilderIT method rebuildIgnoresErrorIfChangeIsUpToDateAfter.

@Test
public void rebuildIgnoresErrorIfChangeIsUpToDateAfter() throws Exception {
    setNotesMigration(true, true);
    PushOneCommit.Result r = createChange();
    Change.Id id = r.getPatchSetId().getParentKey();
    assertChangeUpToDate(true, id);
    // Make a ReviewDb change behind NoteDb's back and ensure it's detected.
    setNotesMigration(false, false);
    gApi.changes().id(id.get()).topic(name("a-topic"));
    setInvalidNoteDbState(id);
    assertChangeUpToDate(false, id);
    // Force the next rebuild attempt to fail but also rebuild the change in the
    // background.
    rebuilderWrapper.stealNextUpdate();
    setNotesMigration(true, true);
    assertThat(gApi.changes().id(id.get()).info().topic).isEqualTo(name("a-topic"));
    assertChangeUpToDate(true, id);
    // Check that the bundles are equal.
    ChangeBundle actual = ChangeBundle.fromNotes(commentsUtil, notesFactory.create(dbProvider.get(), project, id));
    ChangeBundle expected = bundleReader.fromReviewDb(getUnwrappedDb(), id);
    assertThat(actual.differencesFrom(expected)).isEmpty();
}
Also used : ChangeBundle(com.google.gerrit.server.notedb.ChangeBundle) Change(com.google.gerrit.reviewdb.client.Change) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 8 with ChangeBundle

use of com.google.gerrit.server.notedb.ChangeBundle in project gerrit by GerritCodeReview.

the class ChangeRebuilderIT method rebuildReturnsDraftResultWhenRebuildingInChangeNotesFails.

@Test
public void rebuildReturnsDraftResultWhenRebuildingInChangeNotesFails() throws Exception {
    setNotesMigration(true, true);
    PushOneCommit.Result r = createChange();
    Change.Id id = r.getPatchSetId().getParentKey();
    putDraft(user, id, 1, "comment by user", null);
    assertChangeUpToDate(true, id);
    ObjectId oldMetaId = getMetaRef(allUsers, refsDraftComments(id, user.getId()));
    // Add a draft behind NoteDb's back.
    setNotesMigration(false, false);
    putDraft(user, id, 1, "second comment by user", null);
    setInvalidNoteDbState(id);
    assertDraftsUpToDate(false, id, user);
    assertThat(getMetaRef(allUsers, refsDraftComments(id, user.getId()))).isEqualTo(oldMetaId);
    // Force the next rebuild attempt to fail (in ChangeNotes).
    rebuilderWrapper.failNextUpdate();
    setNotesMigration(true, true);
    ChangeNotes notes = notesFactory.create(dbProvider.get(), project, id);
    notes.getDraftComments(user.getId());
    assertThat(getMetaRef(allUsers, refsDraftComments(id, user.getId()))).isEqualTo(oldMetaId);
    // Not up to date, but the actual returned state matches anyway.
    assertDraftsUpToDate(false, id, user);
    ChangeBundle actual = ChangeBundle.fromNotes(commentsUtil, notes);
    ChangeBundle expected = bundleReader.fromReviewDb(getUnwrappedDb(), id);
    assertThat(actual.differencesFrom(expected)).isEmpty();
    // Another rebuild attempt succeeds
    notesFactory.create(dbProvider.get(), project, id);
    assertChangeUpToDate(true, id);
    assertDraftsUpToDate(true, id, user);
    assertThat(getMetaRef(allUsers, refsDraftComments(id, user.getId()))).isNotEqualTo(oldMetaId);
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) ChangeBundle(com.google.gerrit.server.notedb.ChangeBundle) Change(com.google.gerrit.reviewdb.client.Change) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 9 with ChangeBundle

use of com.google.gerrit.server.notedb.ChangeBundle in project gerrit by GerritCodeReview.

the class NoteDbPrimaryIT method rebuildReviewDb.

@Test
public void rebuildReviewDb() throws Exception {
    Change c = createChange().getChange().change();
    Change.Id id = c.getId();
    CommentInput cin = new CommentInput();
    cin.line = 1;
    cin.message = "Published comment";
    ReviewInput rin = ReviewInput.approve();
    rin.comments = ImmutableMap.of(PushOneCommit.FILE_NAME, ImmutableList.of(cin));
    gApi.changes().id(id.get()).current().review(ReviewInput.approve());
    DraftInput din = new DraftInput();
    din.path = PushOneCommit.FILE_NAME;
    din.line = 1;
    din.message = "Draft comment";
    gApi.changes().id(id.get()).current().createDraft(din);
    gApi.changes().id(id.get()).current().review(ReviewInput.approve());
    gApi.changes().id(id.get()).current().createDraft(din);
    assertThat(db.changeMessages().byChange(id)).isNotEmpty();
    assertThat(db.patchSets().byChange(id)).isNotEmpty();
    assertThat(db.patchSetApprovals().byChange(id)).isNotEmpty();
    assertThat(db.patchComments().byChange(id)).isNotEmpty();
    ChangeBundle noteDbBundle = ChangeBundle.fromNotes(commentsUtil, notesFactory.create(db, project, id));
    setNoteDbPrimary(id);
    db.changeMessages().delete(db.changeMessages().byChange(id));
    db.patchSets().delete(db.patchSets().byChange(id));
    db.patchSetApprovals().delete(db.patchSetApprovals().byChange(id));
    db.patchComments().delete(db.patchComments().byChange(id));
    ChangeMessage bogusMessage = ChangeMessagesUtil.newMessage(c.currentPatchSetId(), identifiedUserFactory.create(admin.getId()), TimeUtil.nowTs(), "some message", null);
    db.changeMessages().insert(Collections.singleton(bogusMessage));
    rebuilderWrapper.rebuildReviewDb(db, project, id);
    assertThat(db.changeMessages().byChange(id)).isNotEmpty();
    assertThat(db.patchSets().byChange(id)).isNotEmpty();
    assertThat(db.patchSetApprovals().byChange(id)).isNotEmpty();
    assertThat(db.patchComments().byChange(id)).isNotEmpty();
    ChangeBundle reviewDbBundle = bundleReader.fromReviewDb(ReviewDbUtil.unwrapDb(db), id);
    assertThat(reviewDbBundle.differencesFrom(noteDbBundle)).isEmpty();
}
Also used : CommentInput(com.google.gerrit.extensions.api.changes.ReviewInput.CommentInput) ChangeBundle(com.google.gerrit.server.notedb.ChangeBundle) ChangeMessage(com.google.gerrit.reviewdb.client.ChangeMessage) DraftInput(com.google.gerrit.extensions.api.changes.DraftInput) Change(com.google.gerrit.reviewdb.client.Change) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 10 with ChangeBundle

use of com.google.gerrit.server.notedb.ChangeBundle in project gerrit by GerritCodeReview.

the class Rebuild method apply.

@Override
public BinaryResult apply(ChangeResource rsrc, Input input) throws ResourceNotFoundException, IOException, OrmException, ConfigInvalidException {
    if (!migration.commitChangeWrites()) {
        throw new ResourceNotFoundException();
    }
    if (!migration.readChanges()) {
        // ChangeBundle#fromNotes currently doesn't work if reading isn't enabled,
        // so don't attempt a diff.
        rebuild(rsrc);
        return BinaryResult.create("Rebuilt change successfully");
    }
    // Not the same transaction as the rebuild, so may result in spurious diffs
    // in the case of races. This should be easy enough to detect by rerunning.
    ChangeBundle reviewDbBundle = bundleReader.fromReviewDb(ReviewDbUtil.unwrapDb(db.get()), rsrc.getId());
    rebuild(rsrc);
    ChangeNotes notes = notesFactory.create(db.get(), rsrc.getChange().getProject(), rsrc.getId());
    ChangeBundle noteDbBundle = ChangeBundle.fromNotes(commentsUtil, notes);
    List<String> diffs = reviewDbBundle.differencesFrom(noteDbBundle);
    if (diffs.isEmpty()) {
        return BinaryResult.create("No differences between ReviewDb and NoteDb");
    }
    return BinaryResult.create(diffs.stream().collect(joining("\n", "Differences between ReviewDb and NoteDb:\n", "\n")));
}
Also used : ChangeBundle(com.google.gerrit.server.notedb.ChangeBundle) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) IdString(com.google.gerrit.extensions.restapi.IdString) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Aggregations

ChangeBundle (com.google.gerrit.server.notedb.ChangeBundle)10 Change (com.google.gerrit.reviewdb.client.Change)9 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)6 Test (org.junit.Test)6 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)5 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)5 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)3 ObjectId (org.eclipse.jgit.lib.ObjectId)3 DraftInput (com.google.gerrit.extensions.api.changes.DraftInput)1 ReviewInput (com.google.gerrit.extensions.api.changes.ReviewInput)1 CommentInput (com.google.gerrit.extensions.api.changes.ReviewInput.CommentInput)1 IdString (com.google.gerrit.extensions.restapi.IdString)1 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)1 ChangeMessage (com.google.gerrit.reviewdb.client.ChangeMessage)1 NoteDbChangeState (com.google.gerrit.server.notedb.NoteDbChangeState)1 PrimaryStorage (com.google.gerrit.server.notedb.NoteDbChangeState.PrimaryStorage)1 OrmException (com.google.gwtorm.server.OrmException)1 ArrayList (java.util.ArrayList)1 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)1