Search in sources :

Example 1 with NoteDbChangeState

use of com.google.gerrit.server.notedb.NoteDbChangeState 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 2 with NoteDbChangeState

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

the class ChangeRebuilderIT method rebuilderRespectsReadOnlyInNoteDbChangeState.

@Test
public void rebuilderRespectsReadOnlyInNoteDbChangeState() throws Exception {
    TestTimeUtil.resetWithClockStep(1, SECONDS);
    PushOneCommit.Result r = createChange();
    PatchSet.Id psId1 = r.getPatchSetId();
    Change.Id id = psId1.getParentKey();
    checker.rebuildAndCheckChanges(id);
    setNotesMigration(true, true);
    ReviewDb db = getUnwrappedDb();
    Change c = db.changes().get(id);
    NoteDbChangeState state = NoteDbChangeState.parse(c);
    Timestamp until = new Timestamp(TimeUtil.nowMs() + MILLISECONDS.convert(1, DAYS));
    state = state.withReadOnlyUntil(until);
    c.setNoteDbState(state.toString());
    db.changes().update(Collections.singleton(c));
    try {
        rebuilderWrapper.rebuild(db, id);
        assert_().fail("expected rebuild to fail");
    } catch (OrmRuntimeException e) {
        assertThat(e.getMessage()).contains("read-only until");
    }
    TestTimeUtil.setClock(new Timestamp(until.getTime() + MILLISECONDS.convert(1, SECONDS)));
    rebuilderWrapper.rebuild(db, id);
}
Also used : OrmRuntimeException(com.google.gwtorm.server.OrmRuntimeException) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) Timestamp(java.sql.Timestamp) 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 3 with NoteDbChangeState

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

the class NoteDbPrimaryIT method migrateToNoteDbAlreadyReadOnly.

@Test
public void migrateToNoteDbAlreadyReadOnly() throws Exception {
    PushOneCommit.Result r = createChange();
    Change.Id id = r.getChange().getId();
    Change c = db.changes().get(id);
    NoteDbChangeState state = NoteDbChangeState.parse(c);
    Timestamp until = new Timestamp(TimeUtil.nowMs() + MILLISECONDS.convert(1, DAYS));
    state = state.withReadOnlyUntil(until);
    c.setNoteDbState(state.toString());
    db.changes().update(Collections.singleton(c));
    exception.expect(OrmRuntimeException.class);
    exception.expectMessage("read-only until " + until);
    migrator.migrateToNoteDbPrimary(id);
}
Also used : Change(com.google.gerrit.reviewdb.client.Change) Timestamp(java.sql.Timestamp) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) NoteDbChangeState(com.google.gerrit.server.notedb.NoteDbChangeState) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 4 with NoteDbChangeState

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

the class NoteDbPrimaryIT method testReadOnly.

private void testReadOnly(Change.Id id) throws Exception {
    Timestamp before = TimeUtil.nowTs();
    Timestamp until = new Timestamp(before.getTime() + 1000 * 3600);
    // Set read-only.
    Change c = db.changes().get(id);
    assertThat(c).named("change " + id).isNotNull();
    NoteDbChangeState state = NoteDbChangeState.parse(c);
    state = state.withReadOnlyUntil(until);
    c.setNoteDbState(state.toString());
    db.changes().update(Collections.singleton(c));
    assertThat(gApi.changes().id(id.get()).get().subject).isEqualTo(PushOneCommit.SUBJECT);
    assertThat(gApi.changes().id(id.get()).get().topic).isNull();
    try {
        gApi.changes().id(id.get()).topic("a-topic");
        assert_().fail("expected read-only exception");
    } catch (RestApiException e) {
        Optional<Throwable> oe = Throwables.getCausalChain(e).stream().filter(x -> x instanceof OrmRuntimeException).findFirst();
        assertThat(oe).named("OrmRuntimeException in causal chain of " + e).isPresent();
        assertThat(oe.get().getMessage()).contains("read-only");
    }
    assertThat(gApi.changes().id(id.get()).get().topic).isNull();
    TestTimeUtil.setClock(new Timestamp(until.getTime() + 1000));
    assertThat(gApi.changes().id(id.get()).get().subject).isEqualTo(PushOneCommit.SUBJECT);
    gApi.changes().id(id.get()).topic("a-topic");
    assertThat(gApi.changes().id(id.get()).get().topic).isEqualTo("a-topic");
}
Also used : OrmRuntimeException(com.google.gwtorm.server.OrmRuntimeException) Optional(java.util.Optional) Change(com.google.gerrit.reviewdb.client.Change) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) Timestamp(java.sql.Timestamp) NoteDbChangeState(com.google.gerrit.server.notedb.NoteDbChangeState)

Example 5 with NoteDbChangeState

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

the class NoteDbPrimaryIT method setNoteDbPrimary.

private void setNoteDbPrimary(Change.Id id) throws Exception {
    Change c = db.changes().get(id);
    assertThat(c).named("change " + id).isNotNull();
    NoteDbChangeState state = NoteDbChangeState.parse(c);
    assertThat(state.getPrimaryStorage()).named("storage of " + id).isEqualTo(REVIEW_DB);
    try (Repository changeRepo = repoManager.openRepository(c.getProject());
        Repository allUsersRepo = repoManager.openRepository(allUsers)) {
        assertThat(state.isUpToDate(new RepoRefCache(changeRepo), new RepoRefCache(allUsersRepo))).named("change " + id + " up to date").isTrue();
    }
    c.setNoteDbState(NoteDbChangeState.NOTE_DB_PRIMARY_STATE);
    db.changes().update(Collections.singleton(c));
}
Also used : RepoRefCache(com.google.gerrit.server.git.RepoRefCache) 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