Search in sources :

Example 26 with StorageException

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

the class ChangeKindCacheImpl method getChangeKindInternal.

private static ChangeKind getChangeKindInternal(ChangeKindCache cache, @Nullable RevWalk rw, @Nullable Config repoConfig, ChangeData change, PatchSet patch) {
    ChangeKind kind = ChangeKind.REWORK;
    // the repository.
    if (patch.id().get() > 1) {
        try {
            Collection<PatchSet> patchSetCollection = change.patchSets();
            PatchSet priorPs = patch;
            for (PatchSet ps : patchSetCollection) {
                if (ps.id().get() < patch.id().get() && (ps.id().get() > priorPs.id().get() || priorPs == patch)) {
                    // We only want the previous patch set, so walk until the last one
                    priorPs = ps;
                }
            }
            // and deletes the draft.
            if (priorPs != patch) {
                kind = cache.getChangeKind(change.project(), rw, repoConfig, priorPs.commitId(), patch.commitId());
            }
        } catch (StorageException e) {
            // Do nothing; assume we have a complex change
            logger.atWarning().withCause(e).log("Unable to get change kind for patchSet %s of change %s", patch.number(), change.getId());
        }
    }
    logger.atFine().log("Change kind for patchSet %s of change %s: %s", patch.number(), change.getId(), kind);
    return kind;
}
Also used : PatchSet(com.google.gerrit.entities.PatchSet) StorageException(com.google.gerrit.exceptions.StorageException) ChangeKind(com.google.gerrit.extensions.client.ChangeKind)

Example 27 with StorageException

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

the class ChangeResource method prepareETag.

// This includes all information relevant for ETag computation
// unrelated to the UI.
public void prepareETag(Hasher h, CurrentUser user) {
    h.putInt(JSON_FORMAT_VERSION).putLong(getChange().getLastUpdatedOn().toEpochMilli()).putInt(user.isIdentifiedUser() ? user.getAccountId().get() : 0);
    if (user.isIdentifiedUser()) {
        for (AccountGroup.UUID uuid : user.getEffectiveGroups().getKnownGroups()) {
            h.putBytes(uuid.get().getBytes(UTF_8));
        }
    }
    byte[] buf = new byte[20];
    Set<Account.Id> accounts = new HashSet<>();
    accounts.add(getChange().getOwner());
    if (getChange().getAssignee() != null) {
        accounts.add(getChange().getAssignee());
    }
    try {
        patchSetUtil.byChange(getNotes()).stream().map(PatchSet::uploader).forEach(accounts::add);
        // It's intentional to include the states for *all* reviewers into the ETag computation.
        // We need the states of all current reviewers and CCs because they are part of ChangeInfo.
        // Including removed reviewers is a cheap way of making sure that the states of accounts that
        // posted a message on the change are included. Loading all change messages to find the exact
        // set of accounts that posted a message is too expensive. However everyone who posts a
        // message is automatically added as reviewer. Hence if we include removed reviewers we can
        // be sure that we have all accounts that posted messages on the change.
        accounts.addAll(approvalUtil.getReviewers(getNotes()).all());
    } catch (StorageException e) {
    // This ETag will be invalidated if it loads next time.
    }
    for (Account.Id accountId : accounts) {
        Optional<AccountState> accountState = accountCache.get(accountId);
        if (accountState.isPresent()) {
            hashAccount(h, accountState.get(), buf);
        } else {
            h.putInt(accountId.get());
        }
    }
    ObjectId noteId;
    try {
        noteId = getNotes().loadRevision();
    } catch (StorageException e) {
        // This ETag will be invalidated if it loads next time.
        noteId = null;
    }
    hashObjectId(h, noteId, buf);
    // TODO(dborowitz): Include more NoteDb and other related refs, e.g. drafts
    // and edits.
    Iterable<ProjectState> projectStateTree = projectCache.get(getProject()).orElseThrow(illegalState(getProject())).tree();
    for (ProjectState p : projectStateTree) {
        hashObjectId(h, p.getConfig().getRevision().orElse(null), buf);
    }
    changeETagComputation.runEach(c -> {
        String pluginETag = c.getETag(changeData.project(), changeData.getId());
        if (pluginETag != null) {
            h.putString(pluginETag, UTF_8);
        }
    });
}
Also used : Account(com.google.gerrit.entities.Account) ObjectId(org.eclipse.jgit.lib.ObjectId) AccountState(com.google.gerrit.server.account.AccountState) AccountGroup(com.google.gerrit.entities.AccountGroup) ProjectState(com.google.gerrit.server.project.ProjectState) ObjectId(org.eclipse.jgit.lib.ObjectId) StorageException(com.google.gerrit.exceptions.StorageException) HashSet(java.util.HashSet)

Example 28 with StorageException

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

the class ConsistencyChecker method checkPatchSets.

private boolean checkPatchSets() {
    List<PatchSet> all;
    try {
        // Iterate in descending order.
        all = PS_ID_ORDER.sortedCopy(psUtil.byChange(notes));
    } catch (StorageException e) {
        ProblemInfo problem = problem("Failed to look up patch sets");
        logger.atWarning().withCause(e).log("Error in consistency check of change %s: %s", notes.getChangeId(), problem);
        return false;
    }
    patchSetsBySha = MultimapBuilder.hashKeys(all.size()).treeSetValues(PS_ID_ORDER).build();
    Map<String, Ref> refs;
    try {
        refs = repo.getRefDatabase().exactRef(all.stream().map(ps -> ps.id().toRefName()).toArray(String[]::new));
    } catch (IOException e) {
        ProblemInfo problem = problem("Error reading refs");
        logger.atWarning().withCause(e).log("Error in consistency check of change %s: %s", notes.getChangeId(), problem);
        refs = Collections.emptyMap();
    }
    List<DeletePatchSetFromDbOp> deletePatchSetOps = new ArrayList<>();
    for (PatchSet ps : all) {
        // Check revision format.
        int psNum = ps.id().get();
        String refName = ps.id().toRefName();
        ObjectId objId = ps.commitId();
        patchSetsBySha.put(objId, ps);
        // Check ref existence.
        ProblemInfo refProblem = null;
        Ref ref = refs.get(refName);
        if (ref == null) {
            refProblem = problem("Ref missing: " + refName);
        } else if (!objId.equals(ref.getObjectId())) {
            String actual = ref.getObjectId() != null ? ref.getObjectId().name() : "null";
            refProblem = problem(String.format("Expected %s to point to %s, found %s", ref.getName(), objId.name(), actual));
        }
        // Check object existence.
        RevCommit psCommit = parseCommit(objId, String.format("patch set %d", psNum));
        if (psCommit == null) {
            if (fix != null && fix.deletePatchSetIfCommitMissing) {
                deletePatchSetOps.add(new DeletePatchSetFromDbOp(lastProblem(), ps.id()));
            }
            continue;
        } else if (refProblem != null && fix != null) {
            fixPatchSetRef(refProblem, ps);
        }
        if (ps.id().equals(change().currentPatchSetId())) {
            currPsCommit = psCommit;
        }
    }
    // Delete any bad patch sets found above, in a single update.
    deletePatchSets(deletePatchSetOps);
    // Check for duplicates.
    for (Map.Entry<ObjectId, Collection<PatchSet>> e : patchSetsBySha.asMap().entrySet()) {
        if (e.getValue().size() > 1) {
            problem(String.format("Multiple patch sets pointing to %s: %s", e.getKey().name(), Collections2.transform(e.getValue(), PatchSet::number)));
        }
    }
    return currPs != null && currPsCommit != null;
}
Also used : ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) DynamicItem(com.google.gerrit.extensions.registration.DynamicItem) MultimapBuilder(com.google.common.collect.MultimapBuilder) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) Inject(com.google.inject.Inject) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) Collections2(com.google.common.collect.Collections2) SubmissionId(com.google.gerrit.entities.SubmissionId) PatchSetInfoFactory(com.google.gerrit.server.patch.PatchSetInfoFactory) UpdateException(com.google.gerrit.server.update.UpdateException) Accounts(com.google.gerrit.server.account.Accounts) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) Locale(java.util.Locale) Map(java.util.Map) RetryHelper(com.google.gerrit.server.update.RetryHelper) UrlFormatter(com.google.gerrit.server.config.UrlFormatter) PluginItemContext(com.google.gerrit.server.plugincontext.PluginItemContext) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Collection(java.util.Collection) Set(java.util.Set) RefUpdate(org.eclipse.jgit.lib.RefUpdate) Status(com.google.gerrit.extensions.common.ProblemInfo.Status) PersonIdent(org.eclipse.jgit.lib.PersonIdent) List(java.util.List) PS_ID_ORDER(com.google.gerrit.server.ChangeUtil.PS_ID_ORDER) Nullable(com.google.gerrit.common.Nullable) Ref(org.eclipse.jgit.lib.Ref) AutoValue(com.google.auto.value.AutoValue) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp) FluentLogger(com.google.common.flogger.FluentLogger) Iterables(com.google.common.collect.Iterables) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) FixInput(com.google.gerrit.extensions.api.changes.FixInput) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) PatchSetState(com.google.gerrit.server.notedb.PatchSetState) RepoContext(com.google.gerrit.server.update.RepoContext) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) REFS_CHANGES(com.google.gerrit.entities.RefNames.REFS_CHANGES) Change(com.google.gerrit.entities.Change) PatchSet(com.google.gerrit.entities.PatchSet) Comparator.comparing(java.util.Comparator.comparing) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) ChangeUtil(com.google.gerrit.server.ChangeUtil) ChangeContext(com.google.gerrit.server.update.ChangeContext) PatchSetInfoNotAvailableException(com.google.gerrit.server.patch.PatchSetInfoNotAvailableException) ProblemInfo(com.google.gerrit.extensions.common.ProblemInfo) CurrentUser(com.google.gerrit.server.CurrentUser) StorageException(com.google.gerrit.exceptions.StorageException) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) IOException(java.io.IOException) SetMultimap(com.google.common.collect.SetMultimap) ObjectId(org.eclipse.jgit.lib.ObjectId) Provider(com.google.inject.Provider) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) Project(com.google.gerrit.entities.Project) TimeUtil(com.google.gerrit.server.util.time.TimeUtil) PatchSetUtil(com.google.gerrit.server.PatchSetUtil) Collections(java.util.Collections) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) Repository(org.eclipse.jgit.lib.Repository) ProblemInfo(com.google.gerrit.extensions.common.ProblemInfo) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) PatchSet(com.google.gerrit.entities.PatchSet) IOException(java.io.IOException) Ref(org.eclipse.jgit.lib.Ref) Collection(java.util.Collection) StorageException(com.google.gerrit.exceptions.StorageException) Map(java.util.Map) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 29 with StorageException

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

the class ChangeIndexer method indexImpl.

private void indexImpl(ChangeData cd) {
    logger.atFine().log("Reindex change %d in index.", cd.getId().get());
    for (Index<?, ChangeData> i : getWriteIndexes()) {
        try (TraceTimer traceTimer = TraceContext.newTimer("Reindexing change in index", Metadata.builder().changeId(cd.getId().get()).patchSetId(cd.currentPatchSet().number()).indexVersion(i.getSchema().getVersion()).build())) {
            if (isFirstInsertForEntry.equals(IsFirstInsertForEntry.YES)) {
                i.insert(cd);
            } else {
                i.replace(cd);
            }
        } catch (RuntimeException e) {
            throw new StorageException(String.format("Failed to reindex change %d in index version %d (current patch set = %d)", cd.getId().get(), i.getSchema().getVersion(), cd.currentPatchSet().number()), e);
        }
    }
    fireChangeIndexedEvent(cd.project().get(), cd.getId().get());
}
Also used : TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) ChangeData(com.google.gerrit.server.query.change.ChangeData) StorageException(com.google.gerrit.exceptions.StorageException)

Example 30 with StorageException

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

the class AccountIndexerImpl method reindexIfStale.

@Override
public boolean reindexIfStale(Account.Id id) {
    try {
        StalenessCheckResult stalenessCheckResult = stalenessChecker.check(id);
        if (stalenessCheckResult.isStale()) {
            logger.atInfo().log("Reindexing stale document %s", stalenessCheckResult);
            index(id);
            return true;
        }
    } catch (IOException e) {
        throw new StorageException(e);
    }
    return false;
}
Also used : StalenessCheckResult(com.google.gerrit.server.index.StalenessCheckResult) IOException(java.io.IOException) 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