Search in sources :

Example 26 with ReviewDb

use of com.google.gerrit.reviewdb.server.ReviewDb 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 27 with ReviewDb

use of com.google.gerrit.reviewdb.server.ReviewDb in project gerrit by GerritCodeReview.

the class RequestScopedReviewDbProvider method get.

@SuppressWarnings("resource")
@Override
public ReviewDb get() {
    if (db == null) {
        ReviewDb c;
        try {
            c = schema.open();
        } catch (OrmException e) {
            throw new ProvisionException("Cannot open ReviewDb", e);
        }
        try {
            cleanup.get().add(() -> {
                c.close();
                db = null;
            });
        } catch (Throwable e) {
            c.close();
            throw new ProvisionException("Cannot defer cleanup of ReviewDb", e);
        }
        db = c;
    }
    return db;
}
Also used : ProvisionException(com.google.inject.ProvisionException) OrmException(com.google.gwtorm.server.OrmException) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 28 with ReviewDb

use of com.google.gerrit.reviewdb.server.ReviewDb in project gerrit by GerritCodeReview.

the class Schema_135 method migrateData.

@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException {
    try (Repository git = repoManager.openRepository(allProjectsName);
        MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, allProjectsName, git)) {
        ProjectConfig config = ProjectConfig.read(md);
        AccessSection meta = config.getAccessSection(RefNames.REFS_CONFIG, true);
        Permission createRefsMetaConfigPermission = meta.getPermission(Permission.CREATE, true);
        Set<GroupReference> groups = Stream.concat(config.getAccessSection(AccessSection.GLOBAL_CAPABILITIES, true).getPermission(GlobalCapability.ADMINISTRATE_SERVER, true).getRules().stream().map(PermissionRule::getGroup), Stream.of(systemGroupBackend.getGroup(PROJECT_OWNERS))).filter(g -> createRefsMetaConfigPermission.getRule(g) == null).collect(toSet());
        for (GroupReference group : groups) {
            createRefsMetaConfigPermission.add(new PermissionRule(config.resolve(group)));
        }
        md.getCommitBuilder().setAuthor(serverUser);
        md.getCommitBuilder().setCommitter(serverUser);
        md.setMessage(COMMIT_MSG);
        config.commit(md);
    } catch (ConfigInvalidException | IOException ex) {
        throw new OrmException(ex);
    }
}
Also used : PermissionRule(com.google.gerrit.common.data.PermissionRule) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) OrmException(com.google.gwtorm.server.OrmException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) GlobalCapability(com.google.gerrit.common.data.GlobalCapability) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate) Inject(com.google.inject.Inject) AccessSection(com.google.gerrit.common.data.AccessSection) SystemGroupBackend(com.google.gerrit.server.group.SystemGroupBackend) PROJECT_OWNERS(com.google.gerrit.server.group.SystemGroupBackend.PROJECT_OWNERS) Collectors.toSet(java.util.stream.Collectors.toSet) Permission(com.google.gerrit.common.data.Permission) Set(java.util.Set) IOException(java.io.IOException) PersonIdent(org.eclipse.jgit.lib.PersonIdent) Provider(com.google.inject.Provider) AllProjectsName(com.google.gerrit.server.config.AllProjectsName) Stream(java.util.stream.Stream) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) RefNames(com.google.gerrit.reviewdb.client.RefNames) ProjectConfig(com.google.gerrit.server.git.ProjectConfig) GroupReference(com.google.gerrit.common.data.GroupReference) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) GitReferenceUpdated(com.google.gerrit.server.extensions.events.GitReferenceUpdated) Repository(org.eclipse.jgit.lib.Repository) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) PermissionRule(com.google.gerrit.common.data.PermissionRule) IOException(java.io.IOException) AccessSection(com.google.gerrit.common.data.AccessSection) ProjectConfig(com.google.gerrit.server.git.ProjectConfig) Repository(org.eclipse.jgit.lib.Repository) OrmException(com.google.gwtorm.server.OrmException) Permission(com.google.gerrit.common.data.Permission) GroupReference(com.google.gerrit.common.data.GroupReference) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate)

Example 29 with ReviewDb

use of com.google.gerrit.reviewdb.server.ReviewDb in project gerrit by GerritCodeReview.

the class Schema_147 method migrateData.

@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException, SQLException {
    try (Repository repo = repoManager.openRepository(allUsersName)) {
        Set<Account.Id> accountIdsFromReviewDb = db.accounts().all().toList().stream().map(a -> a.getId()).collect(toSet());
        Set<Account.Id> accountIdsFromUserBranches = repo.getRefDatabase().getRefs(RefNames.REFS_USERS).values().stream().map(r -> Account.Id.fromRef(r.getName())).filter(Objects::nonNull).collect(toSet());
        accountIdsFromUserBranches.removeAll(accountIdsFromReviewDb);
        for (Account.Id accountId : accountIdsFromUserBranches) {
            AccountsUpdate.deleteUserBranch(repo, serverIdent, accountId);
        }
    } catch (IOException e) {
        throw new OrmException("Failed to delete user branches for non-existing accounts.", e);
    }
}
Also used : AllUsersName(com.google.gerrit.server.config.AllUsersName) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) OrmException(com.google.gwtorm.server.OrmException) Inject(com.google.inject.Inject) Set(java.util.Set) IOException(java.io.IOException) Objects(java.util.Objects) PersonIdent(org.eclipse.jgit.lib.PersonIdent) Provider(com.google.inject.Provider) SQLException(java.sql.SQLException) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) RefNames(com.google.gerrit.reviewdb.client.RefNames) Account(com.google.gerrit.reviewdb.client.Account) Collectors.toSet(java.util.stream.Collectors.toSet) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) Repository(org.eclipse.jgit.lib.Repository) AccountsUpdate(com.google.gerrit.server.account.AccountsUpdate) Account(com.google.gerrit.reviewdb.client.Account) Repository(org.eclipse.jgit.lib.Repository) OrmException(com.google.gwtorm.server.OrmException) IOException(java.io.IOException)

Example 30 with ReviewDb

use of com.google.gerrit.reviewdb.server.ReviewDb in project gerrit by GerritCodeReview.

the class Schema_130 method migrateData.

@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException {
    SortedSet<Project.NameKey> repoList = repoManager.list();
    SortedSet<Project.NameKey> repoUpgraded = new TreeSet<>();
    ui.message("\tMigrating " + repoList.size() + " repositories ...");
    for (Project.NameKey projectName : repoList) {
        try (Repository git = repoManager.openRepository(projectName);
            MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, projectName, git)) {
            ProjectConfigSchemaUpdate cfg = ProjectConfigSchemaUpdate.read(md);
            cfg.removeForceFromPermission("pushTag");
            if (cfg.isUpdated()) {
                repoUpgraded.add(projectName);
            }
            cfg.save(serverUser, COMMIT_MSG);
        } catch (ConfigInvalidException | IOException ex) {
            throw new OrmException("Cannot migrate project " + projectName, ex);
        }
    }
    ui.message("\tMigration completed:  " + repoUpgraded.size() + " repositories updated:");
    ui.message("\t" + repoUpgraded.stream().map(n -> n.get()).collect(joining(" ")));
}
Also used : Project(com.google.gerrit.reviewdb.client.Project) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) OrmException(com.google.gwtorm.server.OrmException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) SortedSet(java.util.SortedSet) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate) Inject(com.google.inject.Inject) IOException(java.io.IOException) Collectors.joining(java.util.stream.Collectors.joining) TreeSet(java.util.TreeSet) PersonIdent(org.eclipse.jgit.lib.PersonIdent) Provider(com.google.inject.Provider) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) GitReferenceUpdated(com.google.gerrit.server.extensions.events.GitReferenceUpdated) Repository(org.eclipse.jgit.lib.Repository) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) IOException(java.io.IOException) Project(com.google.gerrit.reviewdb.client.Project) Repository(org.eclipse.jgit.lib.Repository) OrmException(com.google.gwtorm.server.OrmException) TreeSet(java.util.TreeSet) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate)

Aggregations

ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)63 OrmException (com.google.gwtorm.server.OrmException)25 Change (com.google.gerrit.reviewdb.client.Change)21 Account (com.google.gerrit.reviewdb.client.Account)17 ArrayList (java.util.ArrayList)14 IOException (java.io.IOException)12 GitRepositoryManager (com.google.gerrit.server.git.GitRepositoryManager)10 Repository (org.eclipse.jgit.lib.Repository)9 Project (com.google.gerrit.reviewdb.client.Project)8 GerritPersonIdent (com.google.gerrit.server.GerritPersonIdent)8 Provider (com.google.inject.Provider)8 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)7 Inject (com.google.inject.Inject)7 AllUsersName (com.google.gerrit.server.config.AllUsersName)6 ChangeControl (com.google.gerrit.server.project.ChangeControl)6 PersonIdent (org.eclipse.jgit.lib.PersonIdent)6 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)5 AccountGroupMember (com.google.gerrit.reviewdb.client.AccountGroupMember)5 GerritServerConfig (com.google.gerrit.server.config.GerritServerConfig)5 GitReferenceUpdated (com.google.gerrit.server.extensions.events.GitReferenceUpdated)5