Make NoteDbUpdateManager AutoCloseable

OpenRepo instances can be auto-opened if the caller didn't call
set{Change,AllUsers}Repo. These instances were closed in a finally
block within execute() but not from stage().

Make the whole class AutoCloseable so users know they have to close it
when they're finished.

Change-Id: I247d3749008029e9969b0e10fa7f8c562cb09d7f
This commit is contained in:
Dave Borowitz
2016-07-07 13:05:05 -04:00
parent 31b09e354a
commit 17699c8bf0
7 changed files with 121 additions and 89 deletions

View File

@@ -792,9 +792,10 @@ public class BatchUpdate implements AutoCloseable {
private void call(ReviewDb db, Repository repo, RevWalk rw)
throws Exception {
@SuppressWarnings("resource") // Not always opened.
NoteDbUpdateManager updateManager = null;
try {
ChangeContext ctx;
NoteDbUpdateManager updateManager = null;
db.changes().beginTransaction(id);
try {
ctx = newChangeContext(db, repo, rw, id);
@@ -848,6 +849,10 @@ public class BatchUpdate implements AutoCloseable {
} catch (Exception e) {
Throwables.propagateIfPossible(e, RestApiException.class);
throw new UpdateException(e);
} finally {
if (updateManager != null) {
updateManager.close();
}
}
}

View File

@@ -552,32 +552,34 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
Change.Id cid = getChangeId();
ReviewDb db = args.db.get();
ChangeRebuilder rebuilder = args.rebuilder.get();
NoteDbUpdateManager manager = rebuilder.stage(db, cid);
if (manager == null) {
return super.openHandle(repo, oldId); // May be null in tests.
}
NoteDbUpdateManager.Result 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.
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());
NoteDbUpdateManager.Result r;
try (NoteDbUpdateManager manager = rebuilder.stage(db, cid)) {
if (manager == null) {
return super.openHandle(repo, oldId); // May be null in tests.
}
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.
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());

View File

@@ -161,10 +161,11 @@ public class ChangeRebuilderImpl extends ChangeRebuilder {
if (change == null) {
throw new NoSuchChangeException(changeId);
}
NoteDbUpdateManager manager =
updateManagerFactory.create(change.getProject());
buildUpdates(manager, ChangeBundle.fromReviewDb(db, changeId));
return execute(db, changeId, manager);
try (NoteDbUpdateManager manager =
updateManagerFactory.create(change.getProject())) {
buildUpdates(manager, ChangeBundle.fromReviewDb(db, changeId));
return execute(db, changeId, manager);
}
}
private static class AbortUpdateException extends OrmRuntimeException {
@@ -246,10 +247,10 @@ public class ChangeRebuilderImpl extends ChangeRebuilder {
checkArgument(allChanges.containsKey(project));
boolean ok = true;
ProgressMonitor pm = new TextProgressMonitor(new PrintWriter(System.out));
NoteDbUpdateManager manager = updateManagerFactory.create(project);
pm.beginTask(
FormatUtil.elide(project.get(), 50), allChanges.get(project).size());
try (ObjectInserter allUsersInserter = allUsersRepo.newObjectInserter();
try (NoteDbUpdateManager manager = updateManagerFactory.create(project);
ObjectInserter allUsersInserter = allUsersRepo.newObjectInserter();
RevWalk allUsersRw = new RevWalk(allUsersInserter.newReader())) {
manager.setAllUsersRepo(allUsersRepo, allUsersRw, allUsersInserter,
new ChainedReceiveCommands(allUsersRepo));

View File

@@ -218,11 +218,12 @@ public class ChangeUpdate extends AbstractChangeUpdate {
}
public ObjectId commit() throws IOException, OrmException {
NoteDbUpdateManager updateManager =
updateManagerFactory.create(getProjectName());
updateManager.add(this);
updateManager.stageAndApplyDelta(getChange());
updateManager.execute();
try (NoteDbUpdateManager updateManager =
updateManagerFactory.create(getProjectName())) {
updateManager.add(this);
updateManager.stageAndApplyDelta(getChange());
updateManager.execute();
}
return getResult();
}

View File

@@ -189,22 +189,24 @@ public class DraftCommentNotes extends AbstractChangeNotes<DraftCommentNotes> {
Change.Id cid = getChangeId();
ReviewDb db = args.db.get();
ChangeRebuilder rebuilder = args.rebuilder.get();
NoteDbUpdateManager manager = rebuilder.stage(db, cid);
if (manager == null) {
return super.openHandle(repo); // May be null in tests.
}
NoteDbUpdateManager.Result r = manager.stageAndApplyDelta(change);
try {
rebuilder.execute(db, cid, manager);
repo.scanForRepoChanges();
} catch (OrmException | IOException e) {
// See ChangeNotes#rebuildAndOpen.
args.metrics.autoRebuildFailureCount.increment(CHANGES);
checkNotNull(r.staged());
return LoadHandle.create(
ChangeNotesCommit.newStagedRevWalk(
repo, r.staged().allUsersObjects()),
draftsId(r));
NoteDbUpdateManager.Result r;
try (NoteDbUpdateManager manager = rebuilder.stage(db, cid)) {
if (manager == null) {
return super.openHandle(repo); // May be null in tests.
}
r = manager.stageAndApplyDelta(change);
try {
rebuilder.execute(db, cid, manager);
repo.scanForRepoChanges();
} catch (OrmException | IOException e) {
// See ChangeNotes#rebuildAndOpen.
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) {

View File

@@ -69,7 +69,7 @@ import java.util.Set;
* To see the state that would be applied prior to executing the full sequence
* of updates, use {@link #stage()}.
*/
public class NoteDbUpdateManager {
public class NoteDbUpdateManager implements AutoCloseable {
public static String CHANGES_READ_ONLY = "NoteDb changes are read-only";
public interface Factory {
@@ -202,6 +202,23 @@ public class NoteDbUpdateManager {
toDelete = new HashSet<>();
}
@Override
public void close() {
try {
if (allUsersRepo != null) {
OpenRepo r = allUsersRepo;
allUsersRepo = null;
r.close();
}
} finally {
if (changeRepo != null) {
OpenRepo r = changeRepo;
changeRepo = null;
r.close();
}
}
}
public NoteDbUpdateManager setChangeRepo(Repository repo, RevWalk rw,
@Nullable ObjectInserter ins, ChainedReceiveCommands cmds) {
checkState(changeRepo == null, "change repo already initialized");
@@ -404,12 +421,7 @@ public class NoteDbUpdateManager {
execute(changeRepo);
execute(allUsersRepo);
} finally {
if (allUsersRepo != null) {
allUsersRepo.close();
}
if (changeRepo != null) {
changeRepo.close();
}
close();
}
}

View File

@@ -1008,10 +1008,12 @@ public class ChangeNotesTest extends AbstractChangeNotesTest {
ChangeUpdate update2 = newUpdate(c, otherUser);
update2.putApproval("Code-Review", (short) 2);
NoteDbUpdateManager updateManager = updateManagerFactory.create(project);
updateManager.add(update1);
updateManager.add(update2);
updateManager.execute();
try (NoteDbUpdateManager updateManager =
updateManagerFactory.create(project)) {
updateManager.add(update1);
updateManager.add(update2);
updateManager.execute();
}
ChangeNotes notes = newNotes(c);
List<PatchSetApproval> psas =
@@ -1038,20 +1040,22 @@ public class ChangeNotesTest extends AbstractChangeNotesTest {
CommentRange range1 = new CommentRange(1, 1, 2, 1);
Timestamp time1 = TimeUtil.nowTs();
PatchSet.Id psId = c.currentPatchSetId();
NoteDbUpdateManager updateManager = updateManagerFactory.create(project);
PatchLineComment comment1 = newPublishedComment(psId, "file1",
uuid1, range1, range1.getEndLine(), otherUser, null, time1, message1,
(short) 0, "abcd1234abcd1234abcd1234abcd1234abcd1234");
update1.setPatchSetId(psId);
update1.putComment(comment1);
updateManager.add(update1);
ChangeUpdate update2 = newUpdate(c, otherUser);
update2.putApproval("Code-Review", (short) 2);
updateManager.add(update2);
RevCommit tipCommit;
updateManager.execute();
try (NoteDbUpdateManager updateManager =
updateManagerFactory.create(project)) {
PatchLineComment comment1 = newPublishedComment(psId, "file1",
uuid1, range1, range1.getEndLine(), otherUser, null, time1, message1,
(short) 0, "abcd1234abcd1234abcd1234abcd1234abcd1234");
update1.setPatchSetId(psId);
update1.putComment(comment1);
updateManager.add(update1);
ChangeUpdate update2 = newUpdate(c, otherUser);
update2.putApproval("Code-Review", (short) 2);
updateManager.add(update2);
updateManager.execute();
}
ChangeNotes notes = newNotes(c);
ObjectId tip = notes.getRevision();
@@ -1094,10 +1098,12 @@ public class ChangeNotesTest extends AbstractChangeNotesTest {
Ref initial2 = repo.exactRef(update2.getRefName());
assertThat(initial2).isNotNull();
NoteDbUpdateManager updateManager = updateManagerFactory.create(project);
updateManager.add(update1);
updateManager.add(update2);
updateManager.execute();
try (NoteDbUpdateManager updateManager =
updateManagerFactory.create(project)) {
updateManager.add(update1);
updateManager.add(update2);
updateManager.execute();
}
Ref ref1 = repo.exactRef(update1.getRefName());
assertThat(ref1.getObjectId()).isEqualTo(update1.getResult());
@@ -2222,9 +2228,11 @@ public class ChangeNotesTest extends AbstractChangeNotesTest {
newUpdate(c, otherUser).createDraftUpdateIfNull();
comment2.setStatus(Status.DRAFT);
draftUpdate.putComment(comment2);
NoteDbUpdateManager manager = updateManagerFactory.create(c.getProject());
manager.add(draftUpdate);
manager.execute();
try (NoteDbUpdateManager manager =
updateManagerFactory.create(c.getProject())) {
manager.add(draftUpdate);
manager.execute();
}
// Looking at drafts directly shows the zombie comment.
DraftCommentNotes draftNotes = draftNotesFactory.create(c, otherUserId);
@@ -2270,10 +2278,11 @@ public class ChangeNotesTest extends AbstractChangeNotesTest {
Status.PUBLISHED);
update2.putComment(comment2);
NoteDbUpdateManager manager = updateManagerFactory.create(project);
manager.add(update1);
manager.add(update2);
manager.execute();
try (NoteDbUpdateManager manager = updateManagerFactory.create(project)) {
manager.add(update1);
manager.add(update2);
manager.execute();
}
ChangeNotes notes = newNotes(c);
List<PatchLineComment> comments = notes.getComments().get(new RevId(rev));