Search in sources :

Example 1 with ChangeRebuilder

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

the class ChangeNotes method rebuildAndOpen.

private LoadHandle rebuildAndOpen(Repository repo, ObjectId oldId) throws IOException {
    Timer1.Context timer = args.metrics.autoRebuildLatency.start(CHANGES);
    try {
        Change.Id cid = getChangeId();
        ReviewDb db = args.db.get();
        ChangeRebuilder rebuilder = args.rebuilder.get();
        NoteDbUpdateManager.Result r;
        try (NoteDbUpdateManager manager = rebuilder.stage(db, cid)) {
            if (manager == null) {
                // May be null in tests.
                return super.openHandle(repo, oldId);
            }
            manager.setRefLogMessage("Auto-rebuilding change");
            r = manager.stageAndApplyDelta(change);
            try {
                rebuilder.execute(db, cid, manager);
                repo.scanForRepoChanges();
            } catch (OrmException | IOException e) {
                // Rebuilding failed. Most likely cause is contention on one or more
                // change refs; there are other types of errors that can happen during
                // rebuilding, but generally speaking they should happen during stage(),
                // not execute(). Assume that some other worker is going to successfully
                // store the rebuilt state, which is deterministic given an input
                // ChangeBundle.
                //
                // Parse notes from the staged result so we can return something useful
                // to the caller instead of throwing.
                log.debug("Rebuilding change {} failed: {}", getChangeId(), e.getMessage());
                args.metrics.autoRebuildFailureCount.increment(CHANGES);
                rebuildResult = checkNotNull(r);
                checkNotNull(r.newState());
                checkNotNull(r.staged());
                return LoadHandle.create(ChangeNotesCommit.newStagedRevWalk(repo, r.staged().changeObjects()), r.newState().getChangeMetaId());
            }
        }
        return LoadHandle.create(ChangeNotesCommit.newRevWalk(repo), r.newState().getChangeMetaId());
    } catch (NoSuchChangeException e) {
        return super.openHandle(repo, oldId);
    } catch (OrmException e) {
        throw new IOException(e);
    } finally {
        log.debug("Rebuilt change {} in project {} in {} ms", getChangeId(), getProjectName(), TimeUnit.MILLISECONDS.convert(timer.stop(), TimeUnit.NANOSECONDS));
    }
}
Also used : NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) OrmException(com.google.gwtorm.server.OrmException) Change(com.google.gerrit.reviewdb.client.Change) IOException(java.io.IOException) ChangeRebuilder(com.google.gerrit.server.notedb.rebuild.ChangeRebuilder) Timer1(com.google.gerrit.metrics.Timer1) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 2 with ChangeRebuilder

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

the class DraftCommentNotes method rebuildAndOpen.

private LoadHandle rebuildAndOpen(Repository repo) throws NoSuchChangeException, IOException {
    Timer1.Context timer = args.metrics.autoRebuildLatency.start(CHANGES);
    try {
        Change.Id cid = getChangeId();
        ReviewDb db = args.db.get();
        ChangeRebuilder rebuilder = args.rebuilder.get();
        NoteDbUpdateManager.Result r;
        try (NoteDbUpdateManager manager = rebuilder.stage(db, cid)) {
            if (manager == null) {
                // May be null in tests.
                return super.openHandle(repo);
            }
            r = manager.stageAndApplyDelta(change);
            try {
                rebuilder.execute(db, cid, manager);
                repo.scanForRepoChanges();
            } catch (OrmException | IOException e) {
                // See ChangeNotes#rebuildAndOpen.
                log.debug("Rebuilding change {} via drafts failed: {}", getChangeId(), e.getMessage());
                args.metrics.autoRebuildFailureCount.increment(CHANGES);
                checkNotNull(r.staged());
                return LoadHandle.create(ChangeNotesCommit.newStagedRevWalk(repo, r.staged().allUsersObjects()), draftsId(r));
            }
        }
        return LoadHandle.create(ChangeNotesCommit.newRevWalk(repo), draftsId(r));
    } catch (NoSuchChangeException e) {
        return super.openHandle(repo);
    } catch (OrmException e) {
        throw new IOException(e);
    } finally {
        log.debug("Rebuilt change {} in {} in {} ms via drafts", getChangeId(), change != null ? "project " + change.getProject() : "unknown project", TimeUnit.MILLISECONDS.convert(timer.stop(), TimeUnit.NANOSECONDS));
    }
}
Also used : NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) OrmException(com.google.gwtorm.server.OrmException) Change(com.google.gerrit.reviewdb.client.Change) IOException(java.io.IOException) ChangeRebuilder(com.google.gerrit.server.notedb.rebuild.ChangeRebuilder) Timer1(com.google.gerrit.metrics.Timer1) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 3 with ChangeRebuilder

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

the class NoteDbModule method configure.

@Override
public void configure() {
    factory(ChangeUpdate.Factory.class);
    factory(ChangeDraftUpdate.Factory.class);
    factory(DeleteCommentRewriter.Factory.class);
    factory(DraftCommentNotes.Factory.class);
    factory(RobotCommentUpdate.Factory.class);
    factory(RobotCommentNotes.Factory.class);
    factory(NoteDbUpdateManager.Factory.class);
    if (!useTestBindings) {
        install(ChangeNotesCache.module());
        if (cfg.getBoolean("noteDb", null, "testRebuilderWrapper", false)) {
            // Yes, another variety of test bindings with a different way of
            // configuring it.
            bind(ChangeRebuilder.class).to(TestChangeRebuilderWrapper.class);
        } else {
            bind(ChangeRebuilder.class).to(ChangeRebuilderImpl.class);
        }
    } else {
        bind(ChangeRebuilder.class).toInstance(new ChangeRebuilder(null) {

            @Override
            public Result rebuild(ReviewDb db, Change.Id changeId) {
                return null;
            }

            @Override
            public Result rebuildEvenIfReadOnly(ReviewDb db, Id changeId) {
                return null;
            }

            @Override
            public Result rebuild(NoteDbUpdateManager manager, ChangeBundle bundle) {
                return null;
            }

            @Override
            public NoteDbUpdateManager stage(ReviewDb db, Change.Id changeId) {
                return null;
            }

            @Override
            public Result execute(ReviewDb db, Change.Id changeId, NoteDbUpdateManager manager) {
                return null;
            }

            @Override
            public void buildUpdates(NoteDbUpdateManager manager, ChangeBundle bundle) {
            // Do nothing.
            }

            @Override
            public void rebuildReviewDb(ReviewDb db, Project.NameKey project, Id changeId) {
            // Do nothing.
            }
        });
        bind(new TypeLiteral<Cache<ChangeNotesCache.Key, ChangeNotesState>>() {
        }).annotatedWith(Names.named(ChangeNotesCache.CACHE_NAME)).toInstance(CacheBuilder.newBuilder().<ChangeNotesCache.Key, ChangeNotesState>build());
    }
}
Also used : Change(com.google.gerrit.reviewdb.client.Change) Result(com.google.gerrit.server.notedb.NoteDbUpdateManager.Result) Project(com.google.gerrit.reviewdb.client.Project) Id(com.google.gerrit.reviewdb.client.Change.Id) Id(com.google.gerrit.reviewdb.client.Change.Id) ChangeRebuilder(com.google.gerrit.server.notedb.rebuild.ChangeRebuilder) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) Cache(com.google.common.cache.Cache)

Aggregations

Change (com.google.gerrit.reviewdb.client.Change)3 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)3 ChangeRebuilder (com.google.gerrit.server.notedb.rebuild.ChangeRebuilder)3 Timer1 (com.google.gerrit.metrics.Timer1)2 NoSuchChangeException (com.google.gerrit.server.project.NoSuchChangeException)2 OrmException (com.google.gwtorm.server.OrmException)2 IOException (java.io.IOException)2 Cache (com.google.common.cache.Cache)1 Id (com.google.gerrit.reviewdb.client.Change.Id)1 Project (com.google.gerrit.reviewdb.client.Project)1 Result (com.google.gerrit.server.notedb.NoteDbUpdateManager.Result)1