Search in sources :

Example 1 with NoSuchChangeException

use of com.google.gerrit.server.project.NoSuchChangeException in project gerrit by GerritCodeReview.

the class ReceiveCommits method parseReplaceCommand.

private void parseReplaceCommand(ReceiveCommand cmd, Change.Id changeId) {
    logDebug("Parsing replace command");
    if (cmd.getType() != ReceiveCommand.Type.CREATE) {
        reject(cmd, "invalid usage");
        return;
    }
    RevCommit newCommit;
    try {
        newCommit = rp.getRevWalk().parseCommit(cmd.getNewId());
        logDebug("Replacing with {}", newCommit);
    } catch (IOException e) {
        logError("Cannot parse " + cmd.getNewId().name() + " as commit", e);
        reject(cmd, "invalid commit");
        return;
    }
    Change changeEnt;
    try {
        changeEnt = notesFactory.createChecked(db, project.getNameKey(), changeId).getChange();
    } catch (NoSuchChangeException e) {
        logError("Change not found " + changeId, e);
        reject(cmd, "change " + changeId + " not found");
        return;
    } catch (OrmException e) {
        logError("Cannot lookup existing change " + changeId, e);
        reject(cmd, "database error");
        return;
    }
    if (!project.getNameKey().equals(changeEnt.getProject())) {
        reject(cmd, "change " + changeId + " does not belong to project " + project.getName());
        return;
    }
    logDebug("Replacing change {}", changeEnt.getId());
    requestReplace(cmd, true, changeEnt, newCommit);
}
Also used : NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) OrmException(com.google.gwtorm.server.OrmException) IOException(java.io.IOException) Change(com.google.gerrit.reviewdb.client.Change) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 2 with NoSuchChangeException

use of com.google.gerrit.server.project.NoSuchChangeException in project gerrit by GerritCodeReview.

the class MergeSuperSet method topicClosure.

/**
   * Completes {@code cs} with any additional changes from its topics
   *
   * <p>{@link #completeChangeSetIncludingTopics} calls this repeatedly, alternating with {@link
   * #completeChangeSetWithoutTopic}, to discover what additional changes should be submitted with a
   * change until the set stops growing.
   *
   * <p>{@code topicsSeen} and {@code visibleTopicsSeen} keep track of topics already explored to
   * avoid wasted work.
   *
   * @return the resulting larger {@link ChangeSet}
   */
private ChangeSet topicClosure(ReviewDb db, ChangeSet cs, CurrentUser user, Set<String> topicsSeen, Set<String> visibleTopicsSeen) throws OrmException {
    List<ChangeData> visibleChanges = new ArrayList<>();
    List<ChangeData> nonVisibleChanges = new ArrayList<>();
    for (ChangeData cd : cs.changes()) {
        visibleChanges.add(cd);
        String topic = cd.change().getTopic();
        if (Strings.isNullOrEmpty(topic) || visibleTopicsSeen.contains(topic)) {
            continue;
        }
        for (ChangeData topicCd : query().byTopicOpen(topic)) {
            try {
                topicCd.changeControl(user);
                if (topicCd.changeControl().isVisible(db, topicCd)) {
                    visibleChanges.add(topicCd);
                } else {
                    nonVisibleChanges.add(topicCd);
                }
            } catch (OrmException e) {
                if (e.getCause() instanceof NoSuchChangeException) {
                // Ignore and skip this change
                } else {
                    throw e;
                }
            }
        }
        topicsSeen.add(topic);
        visibleTopicsSeen.add(topic);
    }
    for (ChangeData cd : cs.nonVisibleChanges()) {
        nonVisibleChanges.add(cd);
        String topic = cd.change().getTopic();
        if (Strings.isNullOrEmpty(topic) || topicsSeen.contains(topic)) {
            continue;
        }
        for (ChangeData topicCd : query().byTopicOpen(topic)) {
            topicCd.changeControl(user);
            nonVisibleChanges.add(topicCd);
        }
        topicsSeen.add(topic);
    }
    return new ChangeSet(visibleChanges, nonVisibleChanges);
}
Also used : NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) OrmException(com.google.gwtorm.server.OrmException) ArrayList(java.util.ArrayList) ChangeData(com.google.gerrit.server.query.change.ChangeData)

Example 3 with NoSuchChangeException

use of com.google.gerrit.server.project.NoSuchChangeException in project gerrit by GerritCodeReview.

the class ChangeRebuilderImpl method execute.

public Result execute(ReviewDb db, Change.Id changeId, NoteDbUpdateManager manager, boolean checkReadOnly) throws OrmException, IOException {
    db = ReviewDbUtil.unwrapDb(db);
    Change change = checkNoteDbState(ChangeNotes.readOneReviewDbChange(db, changeId));
    if (change == null) {
        throw new NoSuchChangeException(changeId);
    }
    final String oldNoteDbState = change.getNoteDbState();
    Result r = manager.stageAndApplyDelta(change);
    final String newNoteDbState = change.getNoteDbState();
    try {
        db.changes().atomicUpdate(changeId, new AtomicUpdate<Change>() {

            @Override
            public Change update(Change change) {
                if (checkReadOnly) {
                    NoteDbChangeState.checkNotReadOnly(change, skewMs);
                }
                String currNoteDbState = change.getNoteDbState();
                if (Objects.equals(currNoteDbState, newNoteDbState)) {
                    // Another thread completed the same rebuild we were about to.
                    throw new AbortUpdateException();
                } else if (!Objects.equals(oldNoteDbState, currNoteDbState)) {
                    // Another thread updated the state to something else.
                    throw new ConflictingUpdateException(change, oldNoteDbState);
                }
                change.setNoteDbState(newNoteDbState);
                return change;
            }
        });
    } catch (ConflictingUpdateException e) {
        // the other thread.
        throw new OrmException(e.getMessage());
    } catch (AbortUpdateException e) {
        if (NoteDbChangeState.parse(changeId, newNoteDbState).isUpToDate(manager.getChangeRepo().cmds.getRepoRefCache(), manager.getAllUsersRepo().cmds.getRepoRefCache())) {
            // Result was flushed to the repo by whatever thread won the race.
            return r;
        }
    // If the state doesn't match, that means another thread attempted this
    // rebuild, but failed. Fall through and try to update the ref again.
    }
    if (migration.failChangeWrites()) {
        // results instead of reading from the repo.
        throw new OrmException(NoteDbUpdateManager.CHANGES_READ_ONLY);
    }
    manager.execute();
    return r;
}
Also used : NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) OrmException(com.google.gwtorm.server.OrmException) Change(com.google.gerrit.reviewdb.client.Change) Result(com.google.gerrit.server.notedb.NoteDbUpdateManager.Result)

Example 4 with NoSuchChangeException

use of com.google.gerrit.server.project.NoSuchChangeException in project gerrit by GerritCodeReview.

the class EventBroker method isVisibleTo.

protected boolean isVisibleTo(Event event, CurrentUser user) throws OrmException {
    if (event instanceof RefEvent) {
        RefEvent refEvent = (RefEvent) event;
        String ref = refEvent.getRefName();
        if (PatchSet.isChangeRef(ref)) {
            Change.Id cid = PatchSet.Id.fromRef(ref).getParentKey();
            try {
                Change change = notesFactory.createChecked(dbProvider.get(), refEvent.getProjectNameKey(), cid).getChange();
                return isVisibleTo(change, user);
            } catch (NoSuchChangeException e) {
                log.debug("Change {} cannot be found, falling back on ref visibility check", cid.id);
            }
        }
        return isVisibleTo(refEvent.getBranchNameKey(), user);
    } else if (event instanceof ProjectEvent) {
        return isVisibleTo(((ProjectEvent) event).getProjectNameKey(), user);
    }
    return true;
}
Also used : RefEvent(com.google.gerrit.server.events.RefEvent) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) Change(com.google.gerrit.reviewdb.client.Change) ProjectEvent(com.google.gerrit.server.events.ProjectEvent)

Example 5 with NoSuchChangeException

use of com.google.gerrit.server.project.NoSuchChangeException 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)

Aggregations

NoSuchChangeException (com.google.gerrit.server.project.NoSuchChangeException)32 Change (com.google.gerrit.reviewdb.client.Change)13 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)10 IOException (java.io.IOException)9 Change (com.google.gerrit.entities.Change)7 PatchSet (com.google.gerrit.entities.PatchSet)7 OrmException (com.google.gwtorm.server.OrmException)7 Repository (org.eclipse.jgit.lib.Repository)7 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)6 ObjectId (org.eclipse.jgit.lib.ObjectId)5 RevWalk (org.eclipse.jgit.revwalk.RevWalk)5 StorageException (com.google.gerrit.exceptions.StorageException)4 AuthException (com.google.gerrit.extensions.restapi.AuthException)4 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)4 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)4 LargeObjectException (com.google.gerrit.server.git.LargeObjectException)4 InvalidChangeOperationException (com.google.gerrit.server.project.InvalidChangeOperationException)4 PatchScript (com.google.gerrit.common.data.PatchScript)3 DiffPreferencesInfo (com.google.gerrit.extensions.client.DiffPreferencesInfo)3 DiffInfo (com.google.gerrit.extensions.common.DiffInfo)3