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));
}
}
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;
}
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);
}
}
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);
}
}
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(" ")));
}
Aggregations