Search in sources :

Example 1 with ChangeBundle

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

the class ChangeRebuilderIT method rebuildReturnsCorrectResultEvenIfSavingToNoteDbFailed.

@Test
public void rebuildReturnsCorrectResultEvenIfSavingToNoteDbFailed() throws Exception {
    setNotesMigration(true, true);
    PushOneCommit.Result r = createChange();
    Change.Id id = r.getPatchSetId().getParentKey();
    assertChangeUpToDate(true, id);
    ObjectId oldMetaId = getMetaRef(project, changeMetaRef(id));
    // Make a ReviewDb change behind NoteDb's back.
    setNotesMigration(false, false);
    gApi.changes().id(id.get()).topic(name("a-topic"));
    setInvalidNoteDbState(id);
    assertChangeUpToDate(false, id);
    assertThat(getMetaRef(project, changeMetaRef(id))).isEqualTo(oldMetaId);
    // Force the next rebuild attempt to fail.
    rebuilderWrapper.failNextUpdate();
    setNotesMigration(true, true);
    ChangeNotes notes = notesFactory.create(dbProvider.get(), project, id);
    // Not up to date, but the actual returned state matches anyway.
    assertChangeUpToDate(false, id);
    assertThat(getMetaRef(project, changeMetaRef(id))).isEqualTo(oldMetaId);
    ChangeBundle actual = ChangeBundle.fromNotes(commentsUtil, notes);
    ChangeBundle expected = bundleReader.fromReviewDb(getUnwrappedDb(), id);
    assertThat(actual.differencesFrom(expected)).isEmpty();
    assertChangeUpToDate(false, id);
    // Another rebuild attempt succeeds
    notesFactory.create(dbProvider.get(), project, id);
    assertThat(getMetaRef(project, changeMetaRef(id))).isNotEqualTo(oldMetaId);
    assertChangeUpToDate(true, id);
}
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 2 with ChangeBundle

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

the class ChangeRebuilderIT method rebuildAutomaticallyWhenChangeOutOfDate.

@Test
public void rebuildAutomaticallyWhenChangeOutOfDate() 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);
    // On next NoteDb read, the change is transparently rebuilt.
    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 3 with ChangeBundle

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

the class ChangeRebuilderIT method rebuildReturnsDraftResultWhenRebuildingInDraftCommentNotesFails.

@Test
public void rebuildReturnsDraftResultWhenRebuildingInDraftCommentNotesFails() 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);
    ReviewDb db = getUnwrappedDb();
    Change c = db.changes().get(id);
    // Leave change meta ID alone so DraftCommentNotes does the rebuild.
    ObjectId badSha = ObjectId.fromString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
    NoteDbChangeState bogusState = new NoteDbChangeState(id, PrimaryStorage.REVIEW_DB, Optional.of(NoteDbChangeState.RefState.create(NoteDbChangeState.parse(c).getChangeMetaId(), ImmutableMap.of(user.getId(), badSha))), Optional.empty());
    c.setNoteDbState(bogusState.toString());
    db.changes().update(Collections.singleton(c));
    assertDraftsUpToDate(false, id, user);
    assertThat(getMetaRef(allUsers, refsDraftComments(id, user.getId()))).isEqualTo(oldMetaId);
    // Force the next rebuild attempt to fail (in DraftCommentNotes).
    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.
    assertChangeUpToDate(true, id);
    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).getDraftComments(user.getId());
    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) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) NoteDbChangeState(com.google.gerrit.server.notedb.NoteDbChangeState) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 4 with ChangeBundle

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

the class NoteDbChecker method rebuildAndCheckChanges.

private void rebuildAndCheckChanges(Stream<Change.Id> changeIds) throws Exception {
    ReviewDb db = getUnwrappedDb();
    List<ChangeBundle> allExpected = readExpected(changeIds);
    boolean oldWrite = notesMigration.rawWriteChangesSetting();
    boolean oldRead = notesMigration.readChanges();
    try {
        notesMigration.setWriteChanges(true);
        notesMigration.setReadChanges(true);
        List<String> msgs = new ArrayList<>();
        for (ChangeBundle expected : allExpected) {
            Change c = expected.getChange();
            try {
                changeRebuilder.rebuild(db, c.getId());
            } catch (RepositoryNotFoundException e) {
                msgs.add("Repository not found for change, cannot convert: " + c);
            }
        }
        checkActual(allExpected, msgs);
    } finally {
        notesMigration.setReadChanges(oldRead);
        notesMigration.setWriteChanges(oldWrite);
    }
}
Also used : ChangeBundle(com.google.gerrit.server.notedb.ChangeBundle) ArrayList(java.util.ArrayList) Change(com.google.gerrit.reviewdb.client.Change) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 5 with ChangeBundle

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

the class NoteDbChecker method checkActual.

private void checkActual(List<ChangeBundle> allExpected, List<String> msgs) throws Exception {
    ReviewDb db = getUnwrappedDb();
    boolean oldRead = notesMigration.readChanges();
    boolean oldWrite = notesMigration.rawWriteChangesSetting();
    try {
        notesMigration.setWriteChanges(true);
        notesMigration.setReadChanges(true);
        for (ChangeBundle expected : allExpected) {
            Change c = expected.getChange();
            ChangeBundle actual;
            try {
                actual = ChangeBundle.fromNotes(commentsUtil, notesFactory.create(db, c.getProject(), c.getId()));
            } catch (Throwable t) {
                String msg = "Error converting change: " + c;
                msgs.add(msg);
                log.error(msg, t);
                continue;
            }
            List<String> diff = expected.differencesFrom(actual);
            if (!diff.isEmpty()) {
                msgs.add("Differences between ReviewDb and NoteDb for " + c + ":");
                msgs.addAll(diff);
                msgs.add("");
            } else {
                System.err.println("NoteDb conversion of change " + c.getId() + " successful");
            }
        }
    } finally {
        notesMigration.setReadChanges(oldRead);
        notesMigration.setWriteChanges(oldWrite);
    }
    if (!msgs.isEmpty()) {
        throw new AssertionError(Joiner.on('\n').join(msgs));
    }
}
Also used : ChangeBundle(com.google.gerrit.server.notedb.ChangeBundle) Change(com.google.gerrit.reviewdb.client.Change) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

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