Search in sources :

Example 46 with OrmException

use of com.google.gwtorm.server.OrmException in project gerrit by GerritCodeReview.

the class ExternalIdIT method retryOnLockFailure.

@Test
public void retryOnLockFailure() throws Exception {
    Retryer<RefsMetaExternalIdsUpdate> retryer = ExternalIdsUpdate.retryerBuilder().withBlockStrategy(new BlockStrategy() {

        @Override
        public void block(long sleepTime) {
        // Don't sleep in tests.
        }
    }).build();
    ExternalId.Key fooId = ExternalId.Key.create("foo", "foo");
    ExternalId.Key barId = ExternalId.Key.create("bar", "bar");
    final AtomicBoolean doneBgUpdate = new AtomicBoolean(false);
    ExternalIdsUpdate update = new ExternalIdsUpdate(repoManager, allUsers, metricMaker, externalIds, new DisabledExternalIdCache(), serverIdent.get(), serverIdent.get(), () -> {
        if (!doneBgUpdate.getAndSet(true)) {
            try {
                extIdsUpdate.create().insert(ExternalId.create(barId, admin.id));
            } catch (IOException | ConfigInvalidException | OrmException e) {
            // Ignore, the successful insertion of the external ID is asserted later
            }
        }
    }, retryer);
    assertThat(doneBgUpdate.get()).isFalse();
    update.insert(ExternalId.create(fooId, admin.id));
    assertThat(doneBgUpdate.get()).isTrue();
    assertThat(externalIds.get(fooId)).isNotNull();
    assertThat(externalIds.get(barId)).isNotNull();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) RefsMetaExternalIdsUpdate(com.google.gerrit.server.account.externalids.ExternalIdsUpdate.RefsMetaExternalIdsUpdate) OrmException(com.google.gwtorm.server.OrmException) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ExternalIdsUpdate(com.google.gerrit.server.account.externalids.ExternalIdsUpdate) RefsMetaExternalIdsUpdate(com.google.gerrit.server.account.externalids.ExternalIdsUpdate.RefsMetaExternalIdsUpdate) IOException(java.io.IOException) BlockStrategy(com.github.rholder.retry.BlockStrategy) DisabledExternalIdCache(com.google.gerrit.server.account.externalids.DisabledExternalIdCache) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 47 with OrmException

use of com.google.gwtorm.server.OrmException in project gerrit by GerritCodeReview.

the class AbstractSubmit method setChangeStatusToNew.

private void setChangeStatusToNew(PushOneCommit.Result... changes) throws Exception {
    for (PushOneCommit.Result change : changes) {
        try (BatchUpdate bu = batchUpdateFactory.create(db, project, userFactory.create(admin.id), TimeUtil.nowTs())) {
            bu.addOp(change.getChange().getId(), new BatchUpdateOp() {

                @Override
                public boolean updateChange(ChangeContext ctx) throws OrmException {
                    ctx.getChange().setStatus(Change.Status.NEW);
                    ctx.getUpdate(ctx.getChange().currentPatchSetId()).setStatus(Change.Status.NEW);
                    return true;
                }
            });
            bu.execute();
        }
    }
}
Also used : ChangeContext(com.google.gerrit.server.update.ChangeContext) OrmException(com.google.gwtorm.server.OrmException) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp)

Example 48 with OrmException

use of com.google.gwtorm.server.OrmException in project gerrit by GerritCodeReview.

the class ConsistencyCheckerIT method mergeChange.

private ChangeControl mergeChange(ChangeControl ctl) throws Exception {
    final ObjectId oldId = getDestRef(ctl);
    final ObjectId newId = ObjectId.fromString(psUtil.current(db, ctl.getNotes()).getRevision().get());
    final String dest = ctl.getChange().getDest().get();
    try (BatchUpdate bu = newUpdate(adminId)) {
        bu.addOp(ctl.getId(), new BatchUpdateOp() {

            @Override
            public void updateRepo(RepoContext ctx) throws IOException {
                ctx.addRefUpdate(oldId, newId, dest);
            }

            @Override
            public boolean updateChange(ChangeContext ctx) throws OrmException {
                ctx.getChange().setStatus(Change.Status.MERGED);
                ctx.getUpdate(ctx.getChange().currentPatchSetId()).fixStatus(Change.Status.MERGED);
                return true;
            }
        });
        bu.execute();
    }
    return reload(ctl);
}
Also used : RepoContext(com.google.gerrit.server.update.RepoContext) ChangeContext(com.google.gerrit.server.update.ChangeContext) ObjectId(org.eclipse.jgit.lib.ObjectId) OrmException(com.google.gwtorm.server.OrmException) IOException(java.io.IOException) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp)

Example 49 with OrmException

use of com.google.gwtorm.server.OrmException in project gerrit by GerritCodeReview.

the class CatServlet method doGet.

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse rsp) throws IOException {
    String keyStr = req.getPathInfo();
    // We shouldn't have to do this extra decode pass, but somehow we
    // are now receiving our "^1" suffix as "%5E1", which confuses us
    // downstream. Other times we get our embedded "," as "%2C", which
    // is equally bad. And yet when these happen a "%2F" is left as-is,
    // rather than escaped as "%252F", which makes me feel really really
    // uncomfortable with a blind decode right here.
    //
    keyStr = Url.decode(keyStr);
    if (!keyStr.startsWith("/")) {
        rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    keyStr = keyStr.substring(1);
    final Patch.Key patchKey;
    final int side;
    {
        final int c = keyStr.lastIndexOf('^');
        if (c == 0) {
            rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        if (c < 0) {
            side = 0;
        } else {
            try {
                side = Integer.parseInt(keyStr.substring(c + 1));
                keyStr = keyStr.substring(0, c);
            } catch (NumberFormatException e) {
                rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }
        }
        try {
            patchKey = Patch.Key.parse(keyStr);
        } catch (NumberFormatException e) {
            rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
    }
    final Change.Id changeId = patchKey.getParentKey().getParentKey();
    String revision;
    try {
        final ReviewDb db = requestDb.get();
        final ChangeControl control = changeControl.validateFor(db, changeId, userProvider.get());
        if (patchKey.getParentKey().get() == 0) {
            // change edit
            try {
                Optional<ChangeEdit> edit = changeEditUtil.byChange(control.getChange());
                if (edit.isPresent()) {
                    revision = ObjectId.toString(edit.get().getEditCommit());
                } else {
                    rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
                    return;
                }
            } catch (AuthException e) {
                rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }
        } else {
            PatchSet patchSet = psUtil.get(db, control.getNotes(), patchKey.getParentKey());
            if (patchSet == null) {
                rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }
            revision = patchSet.getRevision().get();
        }
    } catch (NoSuchChangeException e) {
        rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    } catch (OrmException e) {
        getServletContext().log("Cannot query database", e);
        rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return;
    }
    String path = patchKey.getFileName();
    String restUrl = String.format("%s/changes/%d/revisions/%s/files/%s/download?parent=%d", req.getContextPath(), changeId.get(), revision, Url.encode(path), side);
    rsp.sendRedirect(restUrl);
}
Also used : ChangeEdit(com.google.gerrit.server.edit.ChangeEdit) AuthException(com.google.gerrit.extensions.restapi.AuthException) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) OrmException(com.google.gwtorm.server.OrmException) ChangeControl(com.google.gerrit.server.project.ChangeControl) Patch(com.google.gerrit.reviewdb.client.Patch) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 50 with OrmException

use of com.google.gwtorm.server.OrmException in project gerrit by GerritCodeReview.

the class PerThreadReviewDbModule method configure.

@Override
protected void configure() {
    final List<ReviewDb> dbs = Collections.synchronizedList(new ArrayList<ReviewDb>());
    final ThreadLocal<ReviewDb> localDb = new ThreadLocal<>();
    bind(ReviewDb.class).toProvider(new Provider<ReviewDb>() {

        @Override
        public ReviewDb get() {
            ReviewDb db = localDb.get();
            if (db == null) {
                try {
                    db = schema.open();
                    dbs.add(db);
                    localDb.set(db);
                } catch (OrmException e) {
                    throw new ProvisionException("unable to open ReviewDb", e);
                }
            }
            return db;
        }
    });
    listener().toInstance(new LifecycleListener() {

        @Override
        public void start() {
        // Do nothing.
        }

        @Override
        public void stop() {
            for (ReviewDb db : dbs) {
                db.close();
            }
        }
    });
}
Also used : ProvisionException(com.google.inject.ProvisionException) OrmException(com.google.gwtorm.server.OrmException) LifecycleListener(com.google.gerrit.extensions.events.LifecycleListener) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Aggregations

OrmException (com.google.gwtorm.server.OrmException)172 IOException (java.io.IOException)78 Change (com.google.gerrit.reviewdb.client.Change)50 Repository (org.eclipse.jgit.lib.Repository)41 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)33 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)31 ObjectId (org.eclipse.jgit.lib.ObjectId)29 Account (com.google.gerrit.reviewdb.client.Account)28 RevWalk (org.eclipse.jgit.revwalk.RevWalk)28 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)24 ChangeData (com.google.gerrit.server.query.change.ChangeData)24 Map (java.util.Map)22 ArrayList (java.util.ArrayList)21 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)20 Inject (com.google.inject.Inject)18 Provider (com.google.inject.Provider)17 RestApiException (com.google.gerrit.extensions.restapi.RestApiException)16 Set (java.util.Set)16 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)15 CurrentUser (com.google.gerrit.server.CurrentUser)14