Search in sources :

Example 6 with NoteDbChangeState

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

the class NoteDbPrimaryIT method migrateBackToReviewDbPrimary.

@Test
public void migrateBackToReviewDbPrimary() throws Exception {
    Change c = createChange().getChange().change();
    Change.Id id = c.getId();
    migrator.migrateToNoteDbPrimary(id);
    assertNoteDbPrimary(id);
    gApi.changes().id(id.get()).topic("new-topic");
    assertThat(gApi.changes().id(id.get()).topic()).isEqualTo("new-topic");
    assertThat(db.changes().get(id).getTopic()).isNotEqualTo("new-topic");
    migrator.migrateToReviewDbPrimary(id, null);
    ObjectId metaId;
    try (Repository repo = repoManager.openRepository(c.getProject());
        RevWalk rw = new RevWalk(repo)) {
        metaId = repo.exactRef(RefNames.changeMetaRef(id)).getObjectId();
        RevCommit commit = rw.parseCommit(metaId);
        rw.parseBody(commit);
        assertThat(commit.getFullMessage()).contains("Read-only-until: " + formatTime(serverIdent.get(), new Timestamp(0)));
    }
    NoteDbChangeState state = NoteDbChangeState.parse(db.changes().get(id));
    assertThat(state.getPrimaryStorage()).isEqualTo(PrimaryStorage.REVIEW_DB);
    assertThat(state.getChangeMetaId()).isEqualTo(metaId);
    assertThat(gApi.changes().id(id.get()).topic()).isEqualTo("new-topic");
    assertThat(db.changes().get(id).getTopic()).isEqualTo("new-topic");
    ChangeNotes notes = notesFactory.create(db, project, id);
    // No rebuilding, change was up to date.
    assertThat(notes.getRevision()).isEqualTo(metaId);
    assertThat(notes.getReadOnlyUntil()).isNotNull();
    gApi.changes().id(id.get()).topic("reviewdb-topic");
    assertThat(db.changes().get(id).getTopic()).isEqualTo("reviewdb-topic");
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) Change(com.google.gerrit.reviewdb.client.Change) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Timestamp(java.sql.Timestamp) NoteDbChangeState(com.google.gerrit.server.notedb.NoteDbChangeState) RevCommit(org.eclipse.jgit.revwalk.RevCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 7 with NoteDbChangeState

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

the class ChangeRebuilderIT method rebuildAutomaticallyWithinBatchUpdate.

@Test
public void rebuildAutomaticallyWithinBatchUpdate() throws Exception {
    setNotesMigration(true, true);
    PushOneCommit.Result r = createChange();
    final Change.Id id = r.getPatchSetId().getParentKey();
    assertChangeUpToDate(true, id);
    // Update ReviewDb and NoteDb, then revert the corresponding NoteDb change
    // to simulate it failing.
    NoteDbChangeState oldState = NoteDbChangeState.parse(getUnwrappedDb().changes().get(id));
    String topic = name("a-topic");
    gApi.changes().id(id.get()).topic(topic);
    try (Repository repo = repoManager.openRepository(project)) {
        new TestRepository<>(repo).update(RefNames.changeMetaRef(id), oldState.getChangeMetaId());
    }
    assertChangeUpToDate(false, id);
    // Next NoteDb read comes inside the transaction started by BatchUpdate. In
    // reality this could be caused by a failed update happening between when
    // the change is parsed by ChangesCollection and when the BatchUpdate
    // executes. We simulate it here by using BatchUpdate directly and not going
    // through an API handler.
    final String msg = "message from BatchUpdate";
    try (BatchUpdate bu = batchUpdateFactory.create(db, project, identifiedUserFactory.create(user.getId()), TimeUtil.nowTs())) {
        bu.addOp(id, new BatchUpdateOp() {

            @Override
            public boolean updateChange(ChangeContext ctx) throws OrmException {
                PatchSet.Id psId = ctx.getChange().currentPatchSetId();
                ChangeMessage cm = new ChangeMessage(new ChangeMessage.Key(id, ChangeUtil.messageUuid()), ctx.getAccountId(), ctx.getWhen(), psId);
                cm.setMessage(msg);
                ctx.getDb().changeMessages().insert(Collections.singleton(cm));
                ctx.getUpdate(psId).setChangeMessage(msg);
                return true;
            }
        });
        try {
            bu.execute();
            fail("expected update to fail");
        } catch (UpdateException e) {
            assertThat(e.getMessage()).contains("cannot copy ChangeNotesState");
        }
    }
// TODO(dborowitz): Re-enable these assertions once we fix auto-rebuilding
// in the BatchUpdate path.
//// As an implementation detail, change wasn't actually rebuilt inside the
//// BatchUpdate transaction, but it was rebuilt during read for the
//// subsequent reindex. Thus it's impossible to actually observe an
//// out-of-date state in the caller.
//assertChangeUpToDate(true, id);
//// Check that the bundles are equal.
//ChangeNotes notes = notesFactory.create(dbProvider.get(), project, id);
//ChangeBundle actual = ChangeBundle.fromNotes(commentsUtil, notes);
//ChangeBundle expected = bundleReader.fromReviewDb(getUnwrappedDb(), id);
//assertThat(actual.differencesFrom(expected)).isEmpty();
//assertThat(
//        Iterables.transform(
//            notes.getChangeMessages(),
//            ChangeMessage::getMessage))
//    .contains(msg);
//assertThat(actual.getChange().getTopic()).isEqualTo(topic);
}
Also used : ChangeContext(com.google.gerrit.server.update.ChangeContext) Change(com.google.gerrit.reviewdb.client.Change) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) NoteDbChangeState(com.google.gerrit.server.notedb.NoteDbChangeState) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) OrmException(com.google.gwtorm.server.OrmException) ChangeMessage(com.google.gerrit.reviewdb.client.ChangeMessage) ObjectId(org.eclipse.jgit.lib.ObjectId) UpdateException(com.google.gerrit.server.update.UpdateException) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 8 with NoteDbChangeState

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

the class ChangeRebuilderIT method assertDraftsUpToDate.

private void assertDraftsUpToDate(boolean expected, Change.Id changeId, TestAccount account) throws Exception {
    try (Repository repo = repoManager.openRepository(allUsers)) {
        Change c = getUnwrappedDb().changes().get(changeId);
        assertThat(c).isNotNull();
        assertThat(c.getNoteDbState()).isNotNull();
        NoteDbChangeState state = NoteDbChangeState.parse(c);
        assertThat(state.areDraftsUpToDate(new RepoRefCache(repo), account.getId())).isEqualTo(expected);
    }
}
Also used : RepoRefCache(com.google.gerrit.server.git.RepoRefCache) TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) Change(com.google.gerrit.reviewdb.client.Change) NoteDbChangeState(com.google.gerrit.server.notedb.NoteDbChangeState)

Aggregations

Change (com.google.gerrit.reviewdb.client.Change)8 NoteDbChangeState (com.google.gerrit.server.notedb.NoteDbChangeState)8 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)5 Test (org.junit.Test)5 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)4 Timestamp (java.sql.Timestamp)4 Repository (org.eclipse.jgit.lib.Repository)4 ObjectId (org.eclipse.jgit.lib.ObjectId)3 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)2 RepoRefCache (com.google.gerrit.server.git.RepoRefCache)2 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)2 OrmRuntimeException (com.google.gwtorm.server.OrmRuntimeException)2 TestRepository (org.eclipse.jgit.junit.TestRepository)2 RestApiException (com.google.gerrit.extensions.restapi.RestApiException)1 ChangeMessage (com.google.gerrit.reviewdb.client.ChangeMessage)1 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)1 ChangeBundle (com.google.gerrit.server.notedb.ChangeBundle)1 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)1 BatchUpdateOp (com.google.gerrit.server.update.BatchUpdateOp)1 ChangeContext (com.google.gerrit.server.update.ChangeContext)1