Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CatalogStoreManager to load all catalog stores #23115

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -53,6 +54,8 @@ public CatalogStoreManager(SecretsResolver secretsResolver, CatalogStoreConfig c
{
this.secretsResolver = requireNonNull(secretsResolver, "secretsResolver is null");
this.catalogStoreKind = requireNonNull(catalogStoreConfig.getCatalogStoreKind(), "catalogStoreKind is null");
addCatalogStoreFactory(new InMemoryCatalogStoreFactory());
addCatalogStoreFactory(new FileCatalogStoreFactory());
}

public void addCatalogStoreFactory(CatalogStoreFactory catalogStoreFactory)
Expand All @@ -65,21 +68,24 @@ public void addCatalogStoreFactory(CatalogStoreFactory catalogStoreFactory)
}

public void loadConfiguredCatalogStore()
throws IOException
{
loadConfiguredCatalogStore(catalogStoreKind, CATALOG_STORE_CONFIGURATION);
}

@VisibleForTesting
void loadConfiguredCatalogStore(String catalogStoreName, File catalogStoreFile)
throws IOException
{
if (configuredCatalogStore.get().isPresent()) {
return;
}
Map<String, String> properties = new HashMap<>();
if (catalogStoreFile.exists()) {
properties = new HashMap<>(loadPropertiesFrom(catalogStoreFile.getPath()));
try {
properties = new HashMap<>(loadPropertiesFrom(catalogStoreFile.getPath()));
}
catch (IOException e) {
throw new UncheckedIOException("Failed to read configuration file: " + catalogStoreFile, e);
}
}
setConfiguredCatalogStore(catalogStoreName, properties);
}
Expand Down Expand Up @@ -135,7 +141,8 @@ public void removeCatalog(CatalogName catalogName)
getCatalogStore().removeCatalog(catalogName);
}

private CatalogStore getCatalogStore()
@VisibleForTesting
public CatalogStore getCatalogStore()
{
return configuredCatalogStore.get().orElseThrow(() -> new IllegalStateException("Catalog store is not configured"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import io.trino.server.ServerConfig;
import io.trino.spi.catalog.CatalogStore;

import java.util.Locale;

import static io.airlift.configuration.ConfigBinder.configBinder;

public class DynamicCatalogManagerModule
Expand All @@ -34,18 +32,9 @@ protected void setup(Binder binder)
{
if (buildConfigObject(ServerConfig.class).isCoordinator()) {
binder.bind(CoordinatorDynamicCatalogManager.class).in(Scopes.SINGLETON);
CatalogStoreConfig config = buildConfigObject(CatalogStoreConfig.class);
switch (config.getCatalogStoreKind().toLowerCase(Locale.ROOT)) {
case "memory" -> binder.bind(CatalogStore.class).to(InMemoryCatalogStore.class).in(Scopes.SINGLETON);
case "file" -> {
configBinder(binder).bindConfig(FileCatalogStoreConfig.class);
binder.bind(CatalogStore.class).to(FileCatalogStore.class).in(Scopes.SINGLETON);
}
default -> {
binder.bind(CatalogStoreManager.class).in(Scopes.SINGLETON);
binder.bind(CatalogStore.class).to(CatalogStoreManager.class).in(Scopes.SINGLETON);
}
}
configBinder(binder).bindConfig(CatalogStoreConfig.class);
binder.bind(CatalogStoreManager.class).in(Scopes.SINGLETON);
binder.bind(CatalogStore.class).to(CatalogStoreManager.class).in(Scopes.SINGLETON);
binder.bind(ConnectorServicesProvider.class).to(CoordinatorDynamicCatalogManager.class).in(Scopes.SINGLETON);
binder.bind(CatalogManager.class).to(CoordinatorDynamicCatalogManager.class).in(Scopes.SINGLETON);
binder.bind(CoordinatorLazyRegister.class).asEagerSingleton();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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 io.trino.connector;

import com.google.inject.Injector;
import io.airlift.bootstrap.Bootstrap;
import io.trino.spi.catalog.CatalogStore;
import io.trino.spi.catalog.CatalogStoreFactory;

import java.util.Map;

public class FileCatalogStoreFactory
implements CatalogStoreFactory
{
@Override
public String getName()
{
return "file";
}

@Override
public CatalogStore create(Map<String, String> config)
{
Bootstrap app = new Bootstrap(new FileCatalogStoreModule());

Injector injector = app
.doNotInitializeLogging()
.setRequiredConfigurationProperties(config)
.initialize();

return injector.getInstance(CatalogStore.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 io.trino.connector;

import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.Scopes;
import io.trino.spi.catalog.CatalogStore;

import static io.airlift.configuration.ConfigBinder.configBinder;

public class FileCatalogStoreModule
implements Module
{
@Override
public void configure(Binder binder)
{
configBinder(binder).bindConfig(FileCatalogStoreConfig.class);
binder.bind(CatalogStore.class).to(FileCatalogStore.class).in(Scopes.SINGLETON);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 io.trino.connector;

import io.trino.spi.catalog.CatalogStore;
import io.trino.spi.catalog.CatalogStoreFactory;

import java.util.Map;

public class InMemoryCatalogStoreFactory
implements CatalogStoreFactory
{
@Override
public String getName()
{
return "memory";
}

@Override
public CatalogStore create(Map<String, String> config)
{
return new InMemoryCatalogStore();
}
}
4 changes: 1 addition & 3 deletions core/trino-main/src/main/java/io/trino/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,7 @@ private void doStart(String trinoVersion)
injector.getInstance(PluginInstaller.class).loadPlugins();

var catalogStoreManager = injector.getInstance(Key.get(new TypeLiteral<Optional<CatalogStoreManager>>() {}));
if (catalogStoreManager.isPresent()) {
catalogStoreManager.get().loadConfiguredCatalogStore();
}
catalogStoreManager.ifPresent(CatalogStoreManager::loadConfiguredCatalogStore);

ConnectorServicesProvider connectorServicesProvider = injector.getInstance(ConnectorServicesProvider.class);
connectorServicesProvider.loadInitialCatalogs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.Scopes;
import com.google.inject.TypeLiteral;
import io.airlift.bootstrap.Bootstrap;
import io.airlift.bootstrap.LifeCycleManager;
import io.airlift.discovery.client.Announcer;
Expand All @@ -47,6 +48,7 @@
import io.trino.SystemSessionPropertiesProvider;
import io.trino.connector.CatalogManagerConfig.CatalogMangerKind;
import io.trino.connector.CatalogManagerModule;
import io.trino.connector.CatalogStoreManager;
import io.trino.connector.ConnectorServicesProvider;
import io.trino.cost.StatsCalculator;
import io.trino.dispatcher.DispatchManager;
Expand Down Expand Up @@ -374,6 +376,9 @@ private TestingTrinoServer(

pluginInstaller = injector.getInstance(PluginInstaller.class);

var catalogStoreManager = injector.getInstance(Key.get(new TypeLiteral<Optional<CatalogStoreManager>>() {}));
catalogStoreManager.ifPresent(CatalogStoreManager::loadConfiguredCatalogStore);

Optional<CatalogManager> catalogManager = Optional.empty();
if (injector.getExistingBinding(Key.get(CatalogManager.class)) != null) {
catalogManager = Optional.of(injector.getInstance(CatalogManager.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package io.trino.server.testing;

import com.google.inject.Key;
import io.trino.connector.CatalogStoreManager;
import io.trino.connector.ConnectorServicesProvider;
import io.trino.connector.CoordinatorDynamicCatalogManager;
import io.trino.connector.FileCatalogStore;
Expand All @@ -39,6 +40,8 @@ void testDefaultCatalogManagementForCoordinator()
assertThat(server.getInstance(Key.get(CatalogManager.class)))
.isInstanceOf(CoordinatorDynamicCatalogManager.class);
assertThat(server.getInstance(Key.get(CatalogStore.class)))
.isInstanceOf(CatalogStoreManager.class)
.extracting(catalogStore -> ((CatalogStoreManager) catalogStore).getCatalogStore())
.isInstanceOf(InMemoryCatalogStore.class);
}
}
Expand Down Expand Up @@ -71,6 +74,8 @@ void testDefaultCatalogStore()
{
try (TestingTrinoServer server = TestingTrinoServer.builder().build()) {
assertThat(server.getInstance(Key.get(CatalogStore.class)))
.isInstanceOf(CatalogStoreManager.class)
.extracting(catalogStore -> ((CatalogStoreManager) catalogStore).getCatalogStore())
.isInstanceOf(InMemoryCatalogStore.class);
}
}
Expand All @@ -83,6 +88,8 @@ void testExplicitCatalogStore()
.addProperty("catalog.store", "file")
.build()) {
assertThat(server.getInstance(Key.get(CatalogStore.class)))
.isInstanceOf(CatalogStoreManager.class)
.extracting(catalogStore -> ((CatalogStoreManager) catalogStore).getCatalogStore())
.isInstanceOf(FileCatalogStore.class);
}
}
Expand Down