Index groups that are created during init

During the initialization of a new site some default groups are
automatically created ('Administrators' and 'Non-Interactive Users').
These new groups must be added to the index so that they become
queryable. For this init must have the group index available so that
it can write the new groups to it. Reading from the index during init
is not needed and not supported.

Alternatively we could require users to run the Reindex program after
the initial site initialization, but likely many people would forget
about it and then wonder why the default groups are not available from
the group index.

Change-Id: I274b142a6efde224bba68f8b5c459736d7a1f985
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin 2017-01-05 09:36:37 +01:00
parent c3b5956d89
commit a4c095f171
17 changed files with 343 additions and 62 deletions

View File

@ -40,6 +40,7 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.ResultSet;
import com.google.inject.Provider;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
@ -81,7 +82,7 @@ public class ElasticGroupIndex
private final Gson gson;
private final GroupMapping mapping;
private final GroupCache groupCache;
private final Provider<GroupCache> groupCache;
private final ElasticQueryBuilder queryBuilder;
@AssistedInject
@ -89,7 +90,7 @@ public class ElasticGroupIndex
@GerritServerConfig Config cfg,
FillArgs fillArgs,
SitePaths sitePaths,
GroupCache groupCache,
Provider<GroupCache> groupCache,
@Assisted Schema<AccountGroup> schema) {
super(cfg, fillArgs, sitePaths, schema, GROUPS_PREFIX);
this.groupCache = groupCache;
@ -221,7 +222,7 @@ public class ElasticGroupIndex
source.getAsJsonObject().get(GroupField.UUID.getName()).getAsString());
// Use the GroupCache rather than depending on any stored fields in the
// document (of which there shouldn't be any).
return groupCache.get(uuid);
return groupCache.get().get(uuid);
}
}
}

View File

@ -30,6 +30,7 @@ import com.google.gerrit.server.query.QueryParseException;
import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.ResultSet;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.assistedinject.Assisted;
import org.apache.lucene.document.Document;
@ -75,7 +76,7 @@ public class LuceneGroupIndex extends
private final GerritIndexWriterConfig indexWriterConfig;
private final QueryBuilder<AccountGroup> queryBuilder;
private final GroupCache groupCache;
private final Provider<GroupCache> groupCache;
private static Directory dir(Schema<AccountGroup> schema, Config cfg,
SitePaths sitePaths) throws IOException {
@ -91,7 +92,7 @@ public class LuceneGroupIndex extends
LuceneGroupIndex(
@GerritServerConfig Config cfg,
SitePaths sitePaths,
GroupCache groupCache,
Provider<GroupCache> groupCache,
@Assisted Schema<AccountGroup> schema) throws IOException {
super(schema, sitePaths, dir(schema, cfg, sitePaths), GROUPS, null,
new GerritIndexWriterConfig(cfg, GROUPS), new SearcherFactory());
@ -193,6 +194,6 @@ public class LuceneGroupIndex extends
new AccountGroup.UUID(doc.getField(UUID.getName()).stringValue());
// Use the GroupCache rather than depending on any stored fields in the
// document (of which there shouldn't be any).
return groupCache.get(uuid);
return groupCache.get().get(uuid);
}
}

View File

@ -41,12 +41,14 @@ java_sources(
java_library(
name = 'init',
srcs = glob([SRCS + 'init/*.java']),
srcs = glob([SRCS + 'init/**/*.java']),
resources = glob([RSRCS + 'init/*']),
deps = DEPS + [
':init-api',
':util',
'//gerrit-common:annotations',
'//gerrit-elasticsearch:elasticsearch',
'//gerrit-lucene:lucene',
'//lib:args4j',
'//lib:derby',
'//lib:gwtjsonrpc',

View File

@ -40,13 +40,14 @@ java_library(
java_library(
name = "init",
srcs = glob([SRCS + "init/*.java"]),
srcs = glob([SRCS + "init/**/*.java"]),
resources = glob([RSRCS + "init/*"]),
visibility = ["//visibility:public"],
deps = DEPS + [
":init-api",
":util",
"//gerrit-common:annotations",
'//gerrit-elasticsearch:elasticsearch',
"//gerrit-launcher:launcher", # We want this dep to be provided_deps
"//gerrit-lucene:lucene",
"//lib:args4j",

View File

@ -28,12 +28,16 @@ import com.google.gerrit.pgm.init.api.InitFlags;
import com.google.gerrit.pgm.init.api.InstallAllPlugins;
import com.google.gerrit.pgm.init.api.InstallPlugins;
import com.google.gerrit.pgm.init.api.LibraryDownload;
import com.google.gerrit.pgm.init.index.IndexManagerOnInit;
import com.google.gerrit.pgm.init.index.elasticsearch.ElasticIndexModuleOnInit;
import com.google.gerrit.pgm.init.index.lucene.LuceneIndexModuleOnInit;
import com.google.gerrit.pgm.util.SiteProgram;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.config.GerritServerConfigModule;
import com.google.gerrit.server.config.SitePath;
import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.index.IndexModule;
import com.google.gerrit.server.plugins.JarScanner;
import com.google.gerrit.server.schema.SchemaUpdater;
import com.google.gerrit.server.schema.UpdateUI;
@ -343,48 +347,58 @@ public class BaseInit extends SiteProgram {
final SchemaUpdater schemaUpdater;
final SchemaFactory<ReviewDb> schema;
final GitRepositoryManager repositoryManager;
final IndexManagerOnInit indexManager;
@Inject
SiteRun(final ConsoleUI ui, final SitePaths site, final InitFlags flags,
final SchemaUpdater schemaUpdater,
final SchemaFactory<ReviewDb> schema,
final GitRepositoryManager repositoryManager) {
SiteRun(ConsoleUI ui,
SitePaths site,
InitFlags flags,
SchemaUpdater schemaUpdater,
SchemaFactory<ReviewDb> schema,
GitRepositoryManager repositoryManager,
IndexManagerOnInit indexManager) {
this.ui = ui;
this.site = site;
this.flags = flags;
this.schemaUpdater = schemaUpdater;
this.schema = schema;
this.repositoryManager = repositoryManager;
this.indexManager = indexManager;
}
void upgradeSchema() throws OrmException {
final List<String> pruneList = new ArrayList<>();
schemaUpdater.update(new UpdateUI() {
@Override
public void message(String msg) {
System.err.println(msg);
System.err.flush();
}
try {
indexManager.start();
schemaUpdater.update(new UpdateUI() {
@Override
public void message(String msg) {
System.err.println(msg);
System.err.flush();
}
@Override
public boolean yesno(boolean def, String msg) {
return ui.yesno(def, msg);
}
@Override
public boolean yesno(boolean def, String msg) {
return ui.yesno(def, msg);
}
@Override
public boolean isBatch() {
return ui.isBatch();
}
@Override
public boolean isBatch() {
return ui.isBatch();
}
@Override
public void pruneSchema(StatementExecutor e, List<String> prune) {
for (String p : prune) {
if (!pruneList.contains(p)) {
pruneList.add(p);
@Override
public void pruneSchema(StatementExecutor e, List<String> prune) {
for (String p : prune) {
if (!pruneList.contains(p)) {
pruneList.add(p);
}
}
}
}
});
});
} finally {
indexManager.stop();
}
if (!pruneList.isEmpty()) {
StringBuilder msg = new StringBuilder();
@ -426,7 +440,18 @@ public class BaseInit extends SiteProgram {
bind(InitFlags.class).toInstance(init.flags);
}
});
sysInjector = createDbInjector(SINGLE_USER).createChildInjector(modules);
Injector dbInjector = createDbInjector(SINGLE_USER);
switch (IndexModule.getIndexType(dbInjector)) {
case LUCENE:
modules.add(new LuceneIndexModuleOnInit());
break;
case ELASTICSEARCH:
modules.add(new ElasticIndexModuleOnInit());
break;
default:
throw new IllegalStateException("unsupported index.type");
}
sysInjector = dbInjector.createChildInjector(modules);
}
return sysInjector;
}

View File

@ -0,0 +1,55 @@
// Copyright (C) 2017 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.pgm.init.index;
import com.google.gerrit.extensions.events.LifecycleListener;
import com.google.gerrit.server.index.IndexDefinition;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import java.util.Collection;
/**
* This class starts/stops the indexes from the init program so that init can
* write updates the indexes.
*/
public class IndexManagerOnInit {
private final LifecycleListener indexManager;
private final Collection<IndexDefinition<?, ?, ?>> defs;
@Inject
IndexManagerOnInit(
@Named(IndexModuleOnInit.INDEX_MANAGER) LifecycleListener indexManager,
Collection<IndexDefinition<?, ?, ?>> defs) {
this.indexManager = indexManager;
this.defs = defs;
}
public void start() {
indexManager.start();
for (IndexDefinition<?, ?, ?> def : defs) {
def.getIndexCollection().start();
}
}
public void stop() {
indexManager.stop();
for (IndexDefinition<?, ?, ?> def : defs) {
def.getIndexCollection().stop();
}
}
}

View File

@ -0,0 +1,85 @@
// Copyright (C) 2017 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.pgm.init.index;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.gerrit.extensions.events.LifecycleListener;
import com.google.gerrit.server.account.GroupCache;
import com.google.gerrit.server.index.IndexDefinition;
import com.google.gerrit.server.index.SchemaDefinitions;
import com.google.gerrit.server.index.SingleVersionModule;
import com.google.gerrit.server.index.SingleVersionModule.SingleVersionListener;
import com.google.gerrit.server.index.group.AllGroupsIndexer;
import com.google.gerrit.server.index.group.GroupIndexCollection;
import com.google.gerrit.server.index.group.GroupIndexDefinition;
import com.google.gerrit.server.index.group.GroupSchemaDefinitions;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.ProvisionException;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Names;
import com.google.inject.util.Providers;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
public class IndexModuleOnInit extends AbstractModule {
static final String INDEX_MANAGER = "IndexModuleOnInit/IndexManager";
@Override
protected void configure() {
// The GroupIndex implementations (LuceneGroupIndex and ElasticGroupIndex)
// need GroupCache only for reading from the index. On init we only want to
// write to the index, hence we don't need the group cache.
bind(GroupCache.class).toProvider(Providers.of(null));
// GroupIndexDefinition wants to have AllGroupsIndexer but it is only used
// by the Reindex program and the OnlineReindexer which are both not used
// during init, hence we don't need AllGroupsIndexer.
bind(AllGroupsIndexer.class).toProvider(Providers.of(null));
bind(GroupIndexCollection.class);
bind(new TypeLiteral<Map<String, Integer>>() {})
.annotatedWith(Names.named(SingleVersionModule.SINGLE_VERSIONS))
.toInstance(ImmutableMap.<String, Integer> of());
bind(LifecycleListener.class).annotatedWith(Names.named(INDEX_MANAGER))
.to(SingleVersionListener.class);
}
@Provides
Collection<IndexDefinition<?, ?, ?>> getIndexDefinitions(
GroupIndexDefinition groups) {
Collection<IndexDefinition<?, ?, ?>> result =
ImmutableList.<IndexDefinition<?, ?, ?>> of(
groups);
Set<String> expected =
FluentIterable.of(GroupSchemaDefinitions.INSTANCE)
.transform(SchemaDefinitions::getName)
.toSet();
Set<String> actual = FluentIterable.from(result)
.transform(IndexDefinition::getName)
.toSet();
if (!expected.equals(actual)) {
throw new ProvisionException(
"need index definitions for all schemas: "
+ expected + " != " + actual);
}
return result;
}
}

View File

@ -0,0 +1,34 @@
// Copyright (C) 2017 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.pgm.init.index.elasticsearch;
import com.google.gerrit.elasticsearch.ElasticGroupIndex;
import com.google.gerrit.pgm.init.index.IndexModuleOnInit;
import com.google.gerrit.server.index.group.GroupIndex;
import com.google.inject.AbstractModule;
import com.google.inject.assistedinject.FactoryModuleBuilder;
public class ElasticIndexModuleOnInit extends AbstractModule {
@Override
protected void configure() {
install(
new FactoryModuleBuilder()
.implement(GroupIndex.class, ElasticGroupIndex.class)
.build(GroupIndex.Factory.class));
install(new IndexModuleOnInit());
}
}

View File

@ -0,0 +1,33 @@
// Copyright (C) 2017 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.pgm.init.index.lucene;
import com.google.gerrit.lucene.LuceneGroupIndex;
import com.google.gerrit.pgm.init.index.IndexModuleOnInit;
import com.google.gerrit.server.index.group.GroupIndex;
import com.google.inject.AbstractModule;
import com.google.inject.assistedinject.FactoryModuleBuilder;
public class LuceneIndexModuleOnInit extends AbstractModule {
@Override
protected void configure() {
install(
new FactoryModuleBuilder()
.implement(GroupIndex.class, LuceneGroupIndex.class)
.build(GroupIndex.Factory.class));
install(new IndexModuleOnInit());
}
}

View File

@ -15,6 +15,7 @@
package com.google.gerrit.server.index;
import com.google.common.collect.ImmutableSortedMap;
import com.google.inject.Provider;
/**
* Definition of an index over a Gerrit data type.
@ -32,13 +33,13 @@ public abstract class IndexDefinition<K, V, I extends Index<K, V>> {
private final SchemaDefinitions<V> schemaDefs;
private final IndexCollection<K, V, I> indexCollection;
private final IndexFactory<K, V, I> indexFactory;
private final SiteIndexer<K, V, I> siteIndexer;
private final Provider<SiteIndexer<K, V, I>> siteIndexer;
protected IndexDefinition(
SchemaDefinitions<V> schemaDefs,
IndexCollection<K, V, I> indexCollection,
IndexFactory<K, V, I> indexFactory,
SiteIndexer<K, V, I> siteIndexer) {
Provider<SiteIndexer<K, V, I>> siteIndexer) {
this.schemaDefs = schemaDefs;
this.indexCollection = indexCollection;
this.indexFactory = indexFactory;
@ -66,6 +67,6 @@ public abstract class IndexDefinition<K, V, I extends Index<K, V>> {
}
public final SiteIndexer<K, V, I> getSiteIndexer() {
return siteIndexer;
return siteIndexer.get();
}
}

View File

@ -33,7 +33,7 @@ import java.util.Set;
@Singleton
public class SingleVersionModule extends LifecycleModule {
static final String SINGLE_VERSIONS = "IndexModule/SingleVersions";
public static final String SINGLE_VERSIONS = "IndexModule/SingleVersions";
private final Map<String, Integer> singleVersions;
@ -50,7 +50,7 @@ public class SingleVersionModule extends LifecycleModule {
}
@Singleton
static class SingleVersionListener implements LifecycleListener {
public static class SingleVersionListener implements LifecycleListener {
private final Set<String> disabled;
private final Collection<IndexDefinition<?, ?, ?>> defs;
private final Map<String, Integer> singleVersions;

View File

@ -18,6 +18,7 @@ import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.server.account.AccountState;
import com.google.gerrit.server.index.IndexDefinition;
import com.google.inject.Inject;
import com.google.inject.util.Providers;
public class AccountIndexDefinition
extends IndexDefinition<Account.Id, AccountState, AccountIndex> {
@ -28,6 +29,6 @@ public class AccountIndexDefinition
AccountIndex.Factory indexFactory,
AllAccountsIndexer allAccountsIndexer) {
super(AccountSchemaDefinitions.INSTANCE, indexCollection, indexFactory,
allAccountsIndexer);
Providers.of(allAccountsIndexer));
}
}

View File

@ -18,6 +18,7 @@ import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.server.index.IndexDefinition;
import com.google.gerrit.server.query.change.ChangeData;
import com.google.inject.Inject;
import com.google.inject.util.Providers;
public class ChangeIndexDefinition
extends IndexDefinition<Change.Id, ChangeData, ChangeIndex> {
@ -28,6 +29,6 @@ public class ChangeIndexDefinition
ChangeIndex.Factory indexFactory,
AllChangesIndexer allChangesIndexer) {
super(ChangeSchemaDefinitions.INSTANCE, indexCollection, indexFactory,
allChangesIndexer);
Providers.of(allChangesIndexer));
}
}

View File

@ -14,9 +14,11 @@
package com.google.gerrit.server.index.group;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.server.index.IndexDefinition;
import com.google.inject.Inject;
import com.google.inject.util.Providers;
public class GroupIndexDefinition
extends IndexDefinition<AccountGroup.UUID, AccountGroup, GroupIndex> {
@ -24,8 +26,8 @@ public class GroupIndexDefinition
@Inject
GroupIndexDefinition(GroupIndexCollection indexCollection,
GroupIndex.Factory indexFactory,
AllGroupsIndexer allGroupsIndexer) {
@Nullable AllGroupsIndexer allGroupsIndexer) {
super(GroupSchemaDefinitions.INSTANCE, indexCollection, indexFactory,
allGroupsIndexer);
Providers.of(allGroupsIndexer));
}
}

View File

@ -24,6 +24,7 @@ import com.google.gerrit.server.GerritPersonIdent;
import com.google.gerrit.server.account.GroupUUID;
import com.google.gerrit.server.config.SitePath;
import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.index.group.GroupIndexCollection;
import com.google.gwtorm.jdbc.JdbcExecutor;
import com.google.gwtorm.jdbc.JdbcSchema;
import com.google.gwtorm.server.OrmException;
@ -46,6 +47,7 @@ public class SchemaCreator {
private final AllUsersCreator allUsersCreator;
private final PersonIdent serverUser;
private final DataSourceType dataSourceType;
private final GroupIndexCollection indexCollection;
private AccountGroup admin;
private AccountGroup batch;
@ -55,20 +57,23 @@ public class SchemaCreator {
AllProjectsCreator ap,
AllUsersCreator auc,
@GerritPersonIdent PersonIdent au,
DataSourceType dst) {
this(site.site_path, ap, auc, au, dst);
DataSourceType dst,
GroupIndexCollection ic) {
this(site.site_path, ap, auc, au, dst, ic);
}
public SchemaCreator(@SitePath Path site,
AllProjectsCreator ap,
AllUsersCreator auc,
@GerritPersonIdent PersonIdent au,
DataSourceType dst) {
DataSourceType dst,
GroupIndexCollection ic) {
site_path = site;
allProjectsCreator = ap;
allUsersCreator = auc;
serverUser = au;
dataSourceType = dst;
indexCollection = ic;
}
public void create(final ReviewDb db) throws OrmException, IOException,
@ -82,6 +87,7 @@ public class SchemaCreator {
sVer.versionNbr = SchemaVersion.getBinaryVersion();
db.schemaVersion().insert(Collections.singleton(sVer));
createDefaultGroups(db);
initSystemConfig(db);
allProjectsCreator
.setAdministrators(GroupReference.forGroup(admin))
@ -93,6 +99,30 @@ public class SchemaCreator {
dataSourceType.getIndexScript().run(db);
}
private void createDefaultGroups(ReviewDb db)
throws OrmException, IOException {
admin = newGroup(db, "Administrators", null);
admin.setDescription("Gerrit Site Administrators");
db.accountGroups().insert(Collections.singleton(admin));
db.accountGroupNames()
.insert(Collections.singleton(new AccountGroupName(admin)));
index(admin);
batch = newGroup(db, "Non-Interactive Users", null);
batch.setDescription("Users who perform batch actions on Gerrit");
batch.setOwnerGroupUUID(admin.getGroupUUID());
db.accountGroups().insert(Collections.singleton(batch));
db.accountGroupNames()
.insert(Collections.singleton(new AccountGroupName(batch)));
index(batch);
}
private void index(AccountGroup group) throws IOException {
if (indexCollection.getSearchIndex() != null) {
indexCollection.getSearchIndex().replace(group);
}
}
private AccountGroup newGroup(ReviewDb c, String name, AccountGroup.UUID uuid)
throws OrmException {
if (uuid == null) {
@ -104,27 +134,14 @@ public class SchemaCreator {
uuid);
}
private SystemConfig initSystemConfig(final ReviewDb c) throws OrmException {
admin = newGroup(c, "Administrators", null);
admin.setDescription("Gerrit Site Administrators");
c.accountGroups().insert(Collections.singleton(admin));
c.accountGroupNames().insert(
Collections.singleton(new AccountGroupName(admin)));
batch = newGroup(c, "Non-Interactive Users", null);
batch.setDescription("Users who perform batch actions on Gerrit");
batch.setOwnerGroupUUID(admin.getGroupUUID());
c.accountGroups().insert(Collections.singleton(batch));
c.accountGroupNames().insert(
Collections.singleton(new AccountGroupName(batch)));
final SystemConfig s = SystemConfig.create();
private SystemConfig initSystemConfig(ReviewDb db) throws OrmException {
SystemConfig s = SystemConfig.create();
try {
s.sitePath = site_path.toRealPath().normalize().toString();
} catch (IOException e) {
s.sitePath = site_path.toAbsolutePath().normalize().toString();
}
c.systemConfig().insert(Collections.singleton(s));
db.systemConfig().insert(Collections.singleton(s));
return s;
}
}

View File

@ -185,6 +185,9 @@ public abstract class AbstractQueryGroupsTest extends GerritServerTests {
GroupInfo group = createGroup(name("group"));
assertQuery("uuid:" + group.id, group);
GroupInfo admins = gApi.groups().id("Administrators").get();
assertQuery("uuid:" + admins.id, admins);
}
@Test

View File

@ -20,12 +20,14 @@ import com.google.gerrit.lifecycle.LifecycleManager;
import com.google.gerrit.reviewdb.client.CurrentSchemaVersion;
import com.google.gerrit.reviewdb.client.SystemConfig;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.index.group.GroupIndexCollection;
import com.google.gerrit.server.schema.SchemaCreator;
import com.google.gerrit.server.schema.SchemaVersion;
import com.google.gwtorm.jdbc.Database;
import com.google.gwtorm.jdbc.SimpleDataSource;
import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.SchemaFactory;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
@ -77,6 +79,23 @@ public class InMemoryDatabase implements SchemaFactory<ReviewDb> {
private boolean created;
@Inject
InMemoryDatabase(Injector injector) throws OrmException {
// Don't inject SchemaCreator directly.
// SchemaCreator needs to get GroupIndexCollection injected, but
// GroupIndexCollection was not bound yet. Creating a child injector with a
// binding for GroupIndexCollection to create an instance of SchemaCreator
// prevents that Guice creates a just-in-time binding for
// GroupIndexCollection in the root injector. If a binding for
// GroupIndexCollection is created in the root injector then IndexModule
// fails to create this binding later, because it already exists.
this(injector.createChildInjector(new AbstractModule() {
@Override
protected void configure() {
bind(GroupIndexCollection.class);
}
}).getInstance(SchemaCreator.class));
}
InMemoryDatabase(SchemaCreator schemaCreator) throws OrmException {
this.schemaCreator = schemaCreator;