Line data Source code
1 : // Copyright (C) 2019 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.acceptance; 16 : 17 : import com.google.gerrit.entities.GroupReference; 18 : import com.google.gerrit.extensions.events.LifecycleListener; 19 : import com.google.gerrit.lifecycle.LifecycleModule; 20 : import com.google.gerrit.server.config.GerritServerConfig; 21 : import com.google.gerrit.server.group.db.Groups; 22 : import com.google.gerrit.server.index.group.GroupIndexer; 23 : import com.google.gerrit.server.util.ReplicaUtil; 24 : import com.google.inject.Inject; 25 : import com.google.inject.Scopes; 26 : import java.io.IOException; 27 : import java.util.stream.Stream; 28 : import org.eclipse.jgit.errors.ConfigInvalidException; 29 : import org.eclipse.jgit.lib.Config; 30 : 31 : /** Reindex all groups at Gerrit daemon startup. */ 32 : public class ReindexGroupsAtStartup implements LifecycleListener { 33 : private final GroupIndexer groupIndexer; 34 : private final Groups groups; 35 : private final Config cfg; 36 : 37 138 : public static class ReindexGroupsAtStartupModule extends LifecycleModule { 38 : @Override 39 : protected void configure() { 40 138 : listener().to(ReindexGroupsAtStartup.class).in(Scopes.SINGLETON); 41 138 : } 42 : } 43 : 44 : @Inject 45 : public ReindexGroupsAtStartup( 46 138 : GroupIndexer groupIndexer, Groups groups, @GerritServerConfig Config cfg) { 47 138 : this.groupIndexer = groupIndexer; 48 138 : this.groups = groups; 49 138 : this.cfg = cfg; 50 138 : } 51 : 52 : @Override 53 : public void start() { 54 : // Gerrit replicas without a reindex 55 138 : if (ReplicaUtil.isReplica(cfg) 56 4 : && !cfg.getBoolean("index", "scheduledIndexer", "runOnStartup", true)) { 57 1 : return; 58 : } 59 : 60 : Stream<GroupReference> allGroupReferences; 61 : try { 62 138 : allGroupReferences = groups.getAllGroupReferences(); 63 0 : } catch (ConfigInvalidException | IOException e) { 64 0 : throw new IllegalStateException("Unable to reindex groups, tests may fail", e); 65 138 : } 66 : 67 138 : allGroupReferences.forEach(group -> groupIndexer.index(group.getUUID())); 68 138 : } 69 : 70 : @Override 71 138 : public void stop() {} 72 : }