Search in sources :

Example 1 with IndexConfig

use of com.google.gerrit.index.IndexConfig in project gerrit by GerritCodeReview.

the class StalenessChecker method check.

/**
 * Returns a {@link StalenessCheckResult} with structured information about staleness of the
 * provided {@link com.google.gerrit.entities.Project.NameKey}.
 */
public StalenessCheckResult check(Project.NameKey project) {
    ProjectData projectData = projectCache.get(project).orElseThrow(illegalState(project)).toProjectData();
    ProjectIndex i = indexes.getSearchIndex();
    if (i == null) {
        return StalenessCheckResult.notStale();
    }
    Optional<FieldBundle> result = i.getRaw(project, QueryOptions.create(indexConfig, 0, 1, FIELDS));
    if (!result.isPresent()) {
        return StalenessCheckResult.stale("Document %s missing from index", project);
    }
    SetMultimap<Project.NameKey, RefState> indexedRefStates = RefState.parseStates(result.get().getValue(ProjectField.REF_STATE));
    SetMultimap<Project.NameKey, RefState> currentRefStates = MultimapBuilder.hashKeys().hashSetValues().build();
    projectData.tree().stream().filter(p -> p.getProject().getConfigRefState() != null).forEach(p -> currentRefStates.put(p.getProject().getNameKey(), RefState.create(RefNames.REFS_CONFIG, p.getProject().getConfigRefState())));
    if (currentRefStates.equals(indexedRefStates)) {
        return StalenessCheckResult.notStale();
    }
    return StalenessCheckResult.stale("Document has unexpected ref states (%s != %s)", currentRefStates, indexedRefStates);
}
Also used : RefState(com.google.gerrit.index.RefState) ProjectCache.illegalState(com.google.gerrit.server.project.ProjectCache.illegalState) ImmutableSet(com.google.common.collect.ImmutableSet) IndexConfig(com.google.gerrit.index.IndexConfig) ProjectCache(com.google.gerrit.server.project.ProjectCache) MultimapBuilder(com.google.common.collect.MultimapBuilder) ProjectField(com.google.gerrit.index.project.ProjectField) Inject(com.google.inject.Inject) QueryOptions(com.google.gerrit.index.QueryOptions) ProjectData(com.google.gerrit.index.project.ProjectData) SetMultimap(com.google.common.collect.SetMultimap) StalenessCheckResult(com.google.gerrit.server.index.StalenessCheckResult) ProjectIndex(com.google.gerrit.index.project.ProjectIndex) ProjectIndexCollection(com.google.gerrit.index.project.ProjectIndexCollection) Project(com.google.gerrit.entities.Project) RefNames(com.google.gerrit.entities.RefNames) FieldBundle(com.google.gerrit.index.query.FieldBundle) Optional(java.util.Optional) RefState(com.google.gerrit.index.RefState) FieldBundle(com.google.gerrit.index.query.FieldBundle) ProjectIndex(com.google.gerrit.index.project.ProjectIndex) ProjectData(com.google.gerrit.index.project.ProjectData)

Example 2 with IndexConfig

use of com.google.gerrit.index.IndexConfig in project gerrit by GerritCodeReview.

the class ReviewersUtil method suggestAccounts.

private List<Account.Id> suggestAccounts(SuggestReviewers suggestReviewers) throws BadRequestException {
    try (Timer0.Context ctx = metrics.queryAccountsLatency.start()) {
        // For performance reasons we don't use AccountQueryProvider as it would always load the
        // complete account from the cache (or worse, from NoteDb) even though we only need the ID
        // which we can directly get from the returned results.
        Predicate<AccountState> pred = Predicate.and(AccountPredicates.isActive(), accountQueryBuilder.defaultQuery(suggestReviewers.getQuery()));
        logger.atFine().log("accounts index query: %s", pred);
        accountIndexRewriter.validateMaxTermsInQuery(pred);
        boolean useLegacyNumericFields = accountIndexes.getSearchIndex().getSchema().useLegacyNumericFields();
        FieldDef<AccountState, ?> idField = useLegacyNumericFields ? AccountField.ID : AccountField.ID_STR;
        ResultSet<FieldBundle> result = accountIndexes.getSearchIndex().getSource(pred, QueryOptions.create(indexConfig, 0, suggestReviewers.getLimit(), ImmutableSet.of(idField.getName()))).readRaw();
        List<Account.Id> matches = result.toList().stream().map(f -> fromIdField(f, useLegacyNumericFields)).collect(toList());
        logger.atFine().log("Matches: %s", matches);
        return matches;
    } catch (TooManyTermsInQueryException e) {
        throw new BadRequestException(e.getMessage());
    } catch (QueryParseException e) {
        logger.atWarning().withCause(e).log("Suggesting accounts failed, return empty result.");
        return ImmutableList.of();
    } catch (StorageException e) {
        if (e.getCause() instanceof TooManyTermsInQueryException) {
            throw new BadRequestException(e.getMessage());
        }
        if (e.getCause() instanceof QueryParseException) {
            return ImmutableList.of();
        }
        throw e;
    }
}
Also used : GroupBackend(com.google.gerrit.server.account.GroupBackend) Inject(com.google.inject.Inject) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ReviewerModifier(com.google.gerrit.server.change.ReviewerModifier) EnumSet(java.util.EnumSet) FieldDef(com.google.gerrit.index.FieldDef) ImmutableSet(com.google.common.collect.ImmutableSet) GroupBaseInfo(com.google.gerrit.extensions.common.GroupBaseInfo) Timer0(com.google.gerrit.metrics.Timer0) Account(com.google.gerrit.entities.Account) FillOptions(com.google.gerrit.server.account.AccountDirectory.FillOptions) AccountQueryBuilder(com.google.gerrit.server.query.account.AccountQueryBuilder) Set(java.util.Set) AccountIndexCollection(com.google.gerrit.server.index.account.AccountIndexCollection) Sets(com.google.common.collect.Sets) GroupReference(com.google.gerrit.entities.GroupReference) Objects(java.util.Objects) TooManyTermsInQueryException(com.google.gerrit.index.query.TooManyTermsInQueryException) List(java.util.List) Nullable(com.google.gerrit.common.Nullable) Url(com.google.gerrit.extensions.restapi.Url) MetricMaker(com.google.gerrit.metrics.MetricMaker) LazyArgs.lazy(com.google.common.flogger.LazyArgs.lazy) FluentLogger(com.google.common.flogger.FluentLogger) Singleton(com.google.inject.Singleton) AccountLoader(com.google.gerrit.server.account.AccountLoader) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) IndexConfig(com.google.gerrit.index.IndexConfig) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ReviewerState(com.google.gerrit.extensions.client.ReviewerState) ResultSet(com.google.gerrit.index.query.ResultSet) AccountPredicates(com.google.gerrit.server.query.account.AccountPredicates) GroupMembers(com.google.gerrit.server.account.GroupMembers) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) QueryParseException(com.google.gerrit.index.query.QueryParseException) Description(com.google.gerrit.metrics.Description) FieldBundle(com.google.gerrit.index.query.FieldBundle) AccountIndexRewriter(com.google.gerrit.server.index.account.AccountIndexRewriter) Predicate(com.google.gerrit.index.query.Predicate) SuggestedReviewerInfo(com.google.gerrit.extensions.common.SuggestedReviewerInfo) AccountControl(com.google.gerrit.server.account.AccountControl) CurrentUser(com.google.gerrit.server.CurrentUser) AccountField(com.google.gerrit.server.index.account.AccountField) StorageException(com.google.gerrit.exceptions.StorageException) Units(com.google.gerrit.metrics.Description.Units) ProjectState(com.google.gerrit.server.project.ProjectState) QueryOptions(com.google.gerrit.index.QueryOptions) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) IOException(java.io.IOException) Collectors.toList(java.util.stream.Collectors.toList) Provider(com.google.inject.Provider) Project(com.google.gerrit.entities.Project) AccountState(com.google.gerrit.server.account.AccountState) ServiceUserClassifier(com.google.gerrit.server.account.ServiceUserClassifier) Collections(java.util.Collections) TooManyTermsInQueryException(com.google.gerrit.index.query.TooManyTermsInQueryException) FieldBundle(com.google.gerrit.index.query.FieldBundle) AccountState(com.google.gerrit.server.account.AccountState) QueryParseException(com.google.gerrit.index.query.QueryParseException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) Timer0(com.google.gerrit.metrics.Timer0) StorageException(com.google.gerrit.exceptions.StorageException)

Aggregations

ImmutableSet (com.google.common.collect.ImmutableSet)2 Project (com.google.gerrit.entities.Project)2 IndexConfig (com.google.gerrit.index.IndexConfig)2 QueryOptions (com.google.gerrit.index.QueryOptions)2 FieldBundle (com.google.gerrit.index.query.FieldBundle)2 Inject (com.google.inject.Inject)2 Strings (com.google.common.base.Strings)1 ImmutableList (com.google.common.collect.ImmutableList)1 MultimapBuilder (com.google.common.collect.MultimapBuilder)1 SetMultimap (com.google.common.collect.SetMultimap)1 Sets (com.google.common.collect.Sets)1 FluentLogger (com.google.common.flogger.FluentLogger)1 LazyArgs.lazy (com.google.common.flogger.LazyArgs.lazy)1 Nullable (com.google.gerrit.common.Nullable)1 Account (com.google.gerrit.entities.Account)1 GroupReference (com.google.gerrit.entities.GroupReference)1 RefNames (com.google.gerrit.entities.RefNames)1 StorageException (com.google.gerrit.exceptions.StorageException)1 ReviewerState (com.google.gerrit.extensions.client.ReviewerState)1 GroupBaseInfo (com.google.gerrit.extensions.common.GroupBaseInfo)1