Search in sources :

Example 86 with StorageException

use of com.google.gerrit.exceptions.StorageException in project gerrit by GerritCodeReview.

the class RepoSequence method acquire.

/**
 * Updates the next available sequence number in NoteDb in order to have a batch of sequence
 * numbers available that can be handed out. {@link #counter} stores the next sequence number that
 * can be handed out. When {@link #limit} is reached a new batch of sequence numbers needs to be
 * retrieved by calling this method.
 *
 * <p><strong>Note:</strong> Callers are required to acquire the {@link #counterLock} before
 * calling this method.
 *
 * @param count the number of sequence numbers which should be retrieved
 */
private void acquire(int count) {
    try (Repository repo = repoManager.openRepository(projectName);
        RevWalk rw = new RevWalk(repo)) {
        logger.atFine().log("acquire %d ids on %s in %s", count, refName, projectName);
        Optional<IntBlob> blob = IntBlob.parse(repo, refName, rw);
        afterReadRef.run();
        ObjectId oldId;
        int next;
        if (!blob.isPresent()) {
            oldId = ObjectId.zeroId();
            next = seed.get();
        } else {
            oldId = blob.get().id();
            next = blob.get().value();
        }
        next = Math.max(floor, next);
        RefUpdate refUpdate = IntBlob.tryStore(repo, rw, projectName, refName, oldId, next + count, gitRefUpdated);
        RefUpdateUtil.checkResult(refUpdate);
        counter = next;
        limit = counter + count;
        acquireCount++;
    } catch (IOException e) {
        throw new StorageException(e);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) StorageException(com.google.gerrit.exceptions.StorageException) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Example 87 with StorageException

use of com.google.gerrit.exceptions.StorageException in project gerrit by GerritCodeReview.

the class RepoSequence method current.

public int current() {
    counterLock.lock();
    try (Repository repo = repoManager.openRepository(projectName);
        RevWalk rw = new RevWalk(repo)) {
        Optional<IntBlob> blob = IntBlob.parse(repo, refName, rw);
        int current;
        if (!blob.isPresent()) {
            current = seed.get();
        } else {
            current = blob.get().value();
        }
        return current;
    } catch (IOException e) {
        throw new StorageException(e);
    } finally {
        counterLock.unlock();
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) StorageException(com.google.gerrit.exceptions.StorageException)

Example 88 with StorageException

use of com.google.gerrit.exceptions.StorageException in project gerrit by GerritCodeReview.

the class PatchScriptFactory method call.

@Override
public PatchScript call() throws LargeObjectException, AuthException, InvalidChangeOperationException, IOException, PermissionBackendException {
    try {
        permissionBackend.user(currentUser).change(notes).check(ChangePermission.READ);
    } catch (AuthException e) {
        throw new NoSuchChangeException(changeId, e);
    }
    if (!projectCache.get(notes.getProjectName()).map(ProjectState::statePermitsRead).orElse(false)) {
        throw new NoSuchChangeException(changeId);
    }
    try (Repository git = repoManager.openRepository(notes.getProjectName())) {
        try {
            validatePatchSetId(psa);
            validatePatchSetId(psb);
            ObjectId aId = getAId().orElse(null);
            ObjectId bId = getBId().orElse(null);
            if (bId == null) {
                // Change edit: create synthetic PatchSet corresponding to the edit.
                Optional<ChangeEdit> edit = editReader.byChange(notes);
                if (!edit.isPresent()) {
                    throw new NoSuchChangeException(notes.getChangeId());
                }
                bId = edit.get().getEditCommit();
            }
            return getPatchScript(git, aId, bId);
        } catch (DiffNotAvailableException e) {
            throw new StorageException(e);
        } catch (IOException e) {
            logger.atSevere().withCause(e).log("File content unavailable");
            throw new NoSuchChangeException(changeId, e);
        } catch (org.eclipse.jgit.errors.LargeObjectException err) {
            throw new LargeObjectException("File content is too large", err);
        }
    } catch (RepositoryNotFoundException e) {
        logger.atSevere().withCause(e).log("Repository %s not found", notes.getProjectName());
        throw new NoSuchChangeException(changeId, e);
    } catch (IOException e) {
        logger.atSevere().withCause(e).log("Cannot open repository %s", notes.getProjectName());
        throw new NoSuchChangeException(changeId, e);
    }
}
Also used : ChangeEdit(com.google.gerrit.server.edit.ChangeEdit) ObjectId(org.eclipse.jgit.lib.ObjectId) AuthException(com.google.gerrit.extensions.restapi.AuthException) IOException(java.io.IOException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) LargeObjectException(com.google.gerrit.server.git.LargeObjectException) Repository(org.eclipse.jgit.lib.Repository) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) ProjectState(com.google.gerrit.server.project.ProjectState) StorageException(com.google.gerrit.exceptions.StorageException)

Example 89 with StorageException

use of com.google.gerrit.exceptions.StorageException in project gerrit by GerritCodeReview.

the class PatchSetInfoFactory method get.

public PatchSetInfo get(Project.NameKey project, PatchSet patchSet) throws PatchSetInfoNotAvailableException {
    try (Repository repo = repoManager.openRepository(project);
        RevWalk rw = new RevWalk(repo)) {
        RevCommit src = rw.parseCommit(patchSet.commitId());
        PatchSetInfo info = get(rw, src, patchSet.id());
        info.setParents(toParentInfos(src.getParents(), rw));
        return info;
    } catch (IOException | StorageException e) {
        throw new PatchSetInfoNotAvailableException(e);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) StorageException(com.google.gerrit.exceptions.StorageException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) PatchSetInfo(com.google.gerrit.entities.PatchSetInfo)

Example 90 with StorageException

use of com.google.gerrit.exceptions.StorageException in project gerrit by GerritCodeReview.

the class NoteDbUpdateManager method addRewrites.

private static void addRewrites(ListMultimap<String, NoteDbRewriter> rewriters, OpenRepo openRepo) throws IOException {
    for (Map.Entry<String, Collection<NoteDbRewriter>> entry : rewriters.asMap().entrySet()) {
        String refName = entry.getKey();
        ObjectId oldTip = openRepo.cmds.get(refName).orElse(ObjectId.zeroId());
        if (oldTip.equals(ObjectId.zeroId())) {
            throw new StorageException(String.format("Ref %s is empty", refName));
        }
        ObjectId currTip = oldTip;
        try {
            for (NoteDbRewriter noteDbRewriter : entry.getValue()) {
                ObjectId nextTip = noteDbRewriter.rewriteCommitHistory(openRepo.rw, openRepo.tempIns, currTip);
                if (nextTip != null) {
                    currTip = nextTip;
                }
            }
        } catch (ConfigInvalidException e) {
            throw new StorageException("Cannot rewrite commit history", e);
        }
        if (!oldTip.equals(currTip)) {
            openRepo.cmds.add(new ReceiveCommand(oldTip, currTip, refName));
        }
    }
}
Also used : ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ObjectId(org.eclipse.jgit.lib.ObjectId) Collection(java.util.Collection) Map(java.util.Map) StorageException(com.google.gerrit.exceptions.StorageException)

Aggregations

StorageException (com.google.gerrit.exceptions.StorageException)153 IOException (java.io.IOException)68 Change (com.google.gerrit.entities.Change)47 ObjectId (org.eclipse.jgit.lib.ObjectId)37 Repository (org.eclipse.jgit.lib.Repository)33 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)30 PatchSet (com.google.gerrit.entities.PatchSet)29 RevCommit (org.eclipse.jgit.revwalk.RevCommit)28 ArrayList (java.util.ArrayList)25 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)24 Project (com.google.gerrit.entities.Project)22 Ref (org.eclipse.jgit.lib.Ref)22 ChangeData (com.google.gerrit.server.query.change.ChangeData)21 RevWalk (org.eclipse.jgit.revwalk.RevWalk)21 Account (com.google.gerrit.entities.Account)20 Inject (com.google.inject.Inject)19 Map (java.util.Map)19 Test (org.junit.Test)19 List (java.util.List)18 BranchNameKey (com.google.gerrit.entities.BranchNameKey)17