Line data Source code
1 : // Copyright (C) 2018 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.index.project; 16 : 17 : import static com.google.gerrit.server.project.ProjectCache.illegalState; 18 : 19 : import com.google.common.collect.ImmutableSet; 20 : import com.google.common.collect.MultimapBuilder; 21 : import com.google.common.collect.SetMultimap; 22 : import com.google.gerrit.entities.Project; 23 : import com.google.gerrit.entities.RefNames; 24 : import com.google.gerrit.index.IndexConfig; 25 : import com.google.gerrit.index.QueryOptions; 26 : import com.google.gerrit.index.RefState; 27 : import com.google.gerrit.index.project.ProjectData; 28 : import com.google.gerrit.index.project.ProjectField; 29 : import com.google.gerrit.index.project.ProjectIndex; 30 : import com.google.gerrit.index.project.ProjectIndexCollection; 31 : import com.google.gerrit.index.query.FieldBundle; 32 : import com.google.gerrit.server.index.StalenessCheckResult; 33 : import com.google.gerrit.server.project.ProjectCache; 34 : import com.google.inject.Inject; 35 : import java.util.Optional; 36 : 37 : /** 38 : * Checker that compares values stored in the project index to metadata in NoteDb to detect index 39 : * documents that should have been updated (= stale). 40 : */ 41 : public class StalenessChecker { 42 1 : private static final ImmutableSet<String> FIELDS = 43 1 : ImmutableSet.of(ProjectField.NAME.getName(), ProjectField.REF_STATE.getName()); 44 : 45 : private final ProjectCache projectCache; 46 : private final ProjectIndexCollection indexes; 47 : private final IndexConfig indexConfig; 48 : 49 : @Inject 50 : StalenessChecker( 51 1 : ProjectCache projectCache, ProjectIndexCollection indexes, IndexConfig indexConfig) { 52 1 : this.projectCache = projectCache; 53 1 : this.indexes = indexes; 54 1 : this.indexConfig = indexConfig; 55 1 : } 56 : 57 : /** 58 : * Returns a {@link StalenessCheckResult} with structured information about staleness of the 59 : * provided {@link com.google.gerrit.entities.Project.NameKey}. 60 : */ 61 : public StalenessCheckResult check(Project.NameKey project) { 62 1 : ProjectData projectData = 63 1 : projectCache.get(project).orElseThrow(illegalState(project)).toProjectData(); 64 1 : ProjectIndex i = indexes.getSearchIndex(); 65 1 : if (i == null) { 66 0 : return StalenessCheckResult 67 0 : .notStale(); // No index; caller couldn't do anything if it is stale. 68 : } 69 : 70 1 : Optional<FieldBundle> result = 71 1 : i.getRaw(project, QueryOptions.create(indexConfig, 0, 1, FIELDS)); 72 1 : if (!result.isPresent()) { 73 0 : return StalenessCheckResult.stale("Document %s missing from index", project); 74 : } 75 : 76 1 : SetMultimap<Project.NameKey, RefState> indexedRefStates = 77 1 : RefState.parseStates(result.get().getValue(ProjectField.REF_STATE)); 78 : 79 : SetMultimap<Project.NameKey, RefState> currentRefStates = 80 1 : MultimapBuilder.hashKeys().hashSetValues().build(); 81 1 : projectData.tree().stream() 82 1 : .filter(p -> p.getProject().getConfigRefState() != null) 83 1 : .forEach( 84 : p -> 85 1 : currentRefStates.put( 86 1 : p.getProject().getNameKey(), 87 1 : RefState.create(RefNames.REFS_CONFIG, p.getProject().getConfigRefState()))); 88 : 89 1 : if (currentRefStates.equals(indexedRefStates)) { 90 1 : return StalenessCheckResult.notStale(); 91 : } 92 1 : return StalenessCheckResult.stale( 93 : "Document has unexpected ref states (%s != %s)", currentRefStates, indexedRefStates); 94 : } 95 : }