Line data Source code
1 : // Copyright (C) 2017 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.group; 16 : 17 : import com.google.common.collect.ImmutableSet; 18 : import com.google.gerrit.entities.AccountGroup; 19 : import com.google.gerrit.entities.RefNames; 20 : import com.google.gerrit.index.IndexConfig; 21 : import com.google.gerrit.index.query.FieldBundle; 22 : import com.google.gerrit.server.config.AllUsersName; 23 : import com.google.gerrit.server.git.GitRepositoryManager; 24 : import com.google.gerrit.server.index.StalenessCheckResult; 25 : import com.google.inject.Inject; 26 : import com.google.inject.Singleton; 27 : import java.io.IOException; 28 : import java.util.Optional; 29 : import org.eclipse.jgit.lib.ObjectId; 30 : import org.eclipse.jgit.lib.Ref; 31 : import org.eclipse.jgit.lib.Repository; 32 : 33 : /** 34 : * Checks if documents in the group index are stale. 35 : * 36 : * <p>An index document is considered stale if the stored SHA1 differs from the HEAD SHA1 of the 37 : * groups branch. 38 : */ 39 : @Singleton 40 : public class StalenessChecker { 41 151 : public static final ImmutableSet<String> FIELDS = 42 151 : ImmutableSet.of(GroupField.UUID_FIELD_SPEC.getName(), GroupField.REF_STATE_SPEC.getName()); 43 : 44 : private final GroupIndexCollection indexes; 45 : private final GitRepositoryManager repoManager; 46 : private final IndexConfig indexConfig; 47 : private final AllUsersName allUsers; 48 : 49 : @Inject 50 : StalenessChecker( 51 : GroupIndexCollection indexes, 52 : GitRepositoryManager repoManager, 53 : IndexConfig indexConfig, 54 151 : AllUsersName allUsers) { 55 151 : this.indexes = indexes; 56 151 : this.repoManager = repoManager; 57 151 : this.indexConfig = indexConfig; 58 151 : this.allUsers = allUsers; 59 151 : } 60 : 61 : public StalenessCheckResult check(AccountGroup.UUID uuid) throws IOException { 62 4 : GroupIndex i = indexes.getSearchIndex(); 63 4 : if (i == null) { 64 : // No index; caller couldn't do anything if it is stale. 65 0 : return StalenessCheckResult.notStale(); 66 : } 67 : 68 4 : Optional<FieldBundle> result = 69 4 : i.getRaw(uuid, IndexedGroupQuery.createOptions(indexConfig, 0, 1, 1, FIELDS)); 70 4 : if (!result.isPresent()) { 71 : // The document is missing in the index. 72 4 : try (Repository repo = repoManager.openRepository(allUsers)) { 73 4 : Ref ref = repo.exactRef(RefNames.refsGroups(uuid)); 74 : 75 : // Stale if the group actually exists. 76 4 : if (ref == null) { 77 1 : return StalenessCheckResult.notStale(); 78 : } 79 4 : return StalenessCheckResult.stale( 80 : "Document missing in index, but found %s in the repo", ref); 81 1 : } 82 : } 83 : 84 1 : try (Repository repo = repoManager.openRepository(allUsers)) { 85 1 : Ref ref = repo.exactRef(RefNames.refsGroups(uuid)); 86 1 : ObjectId head = ref == null ? ObjectId.zeroId() : ref.getObjectId(); 87 1 : ObjectId idFromIndex = 88 1 : ObjectId.fromString(result.get().getValue(GroupField.REF_STATE_SPEC), 0); 89 1 : if (head.equals(idFromIndex)) { 90 1 : return StalenessCheckResult.notStale(); 91 : } 92 1 : return StalenessCheckResult.stale( 93 : "Document has unexpected ref state (%s != %s)", head, idFromIndex); 94 1 : } 95 : } 96 : }