Skip to content

Commit

Permalink
Add DynamicCatalogManager
Browse files Browse the repository at this point in the history
Move support for dynamically adding catalogs from StaticCatalogManager
to new DynamicCatalogManager.  This is only used in testing and moving
this feature makes future enhancements simpler.
  • Loading branch information
dain committed Aug 12, 2022
1 parent 6f4323d commit 7ff57ce
Show file tree
Hide file tree
Showing 12 changed files with 373 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.airlift.configuration.Config;

import javax.validation.constraints.NotNull;

public class CatalogManagerConfig
{
public enum CatalogMangerKind
{
STATIC, DYNAMIC
}

private CatalogMangerKind catalogMangerKind = CatalogMangerKind.STATIC;

@NotNull
public CatalogMangerKind getCatalogMangerKind()
{
return catalogMangerKind;
}

@Config("catalog.management")
public CatalogManagerConfig setCatalogMangerKind(CatalogMangerKind catalogMangerKind)
{
this.catalogMangerKind = catalogMangerKind;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
import com.google.inject.Binder;
import com.google.inject.Scopes;
import io.airlift.configuration.AbstractConfigurationAwareModule;
import io.trino.connector.system.GlobalSystemConnector;
import io.trino.metadata.CatalogManager;

import javax.inject.Inject;

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

public class CatalogManagerModule
extends AbstractConfigurationAwareModule
Expand All @@ -32,27 +26,13 @@ protected void setup(Binder binder)
binder.bind(DefaultCatalogFactory.class).in(Scopes.SINGLETON);
binder.bind(LazyCatalogFactory.class).in(Scopes.SINGLETON);
binder.bind(CatalogFactory.class).to(LazyCatalogFactory.class).in(Scopes.SINGLETON);
binder.bind(LazyRegister.class).asEagerSingleton();

configBinder(binder).bindConfig(StaticCatalogManagerConfig.class);
binder.bind(StaticCatalogManager.class).in(Scopes.SINGLETON);
binder.bind(ConnectorServicesProvider.class).to(StaticCatalogManager.class).in(Scopes.SINGLETON);
binder.bind(CatalogManager.class).to(StaticCatalogManager.class).in(Scopes.SINGLETON);
CatalogManagerConfig config = buildConfigObject(CatalogManagerConfig.class);
switch (config.getCatalogMangerKind()) {
case STATIC -> install(new StaticCatalogManagerModule());
case DYNAMIC -> install(new DynamicCatalogManagerModule());
}

install(new CatalogServiceProviderModule());
}

private static class LazyRegister
{
@Inject
public LazyRegister(
DefaultCatalogFactory defaultCatalogFactory,
LazyCatalogFactory lazyCatalogFactory,
StaticCatalogManager catalogManager,
GlobalSystemConnector globalSystemConnector)
{
lazyCatalogFactory.setCatalogFactory(defaultCatalogFactory);
catalogManager.registerGlobalSystemConnector(globalSystemConnector);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@

public interface ConnectorServicesProvider
{
void loadInitialCatalogs();

ConnectorServices getConnectorServices(CatalogHandle catalogHandle);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* 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.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import io.trino.connector.system.GlobalSystemConnector;
import io.trino.metadata.Catalog;
import io.trino.metadata.CatalogManager;
import io.trino.spi.TrinoException;

import javax.annotation.PreDestroy;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;
import javax.inject.Inject;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static io.trino.connector.CatalogHandle.createRootCatalogHandle;
import static io.trino.spi.StandardErrorCode.ALREADY_EXISTS;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

@ThreadSafe
public class CoordinatorDynamicCatalogManager
implements CatalogManager, ConnectorServicesProvider
{
private final CatalogFactory catalogFactory;

private final Lock catalogsUpdateLock = new ReentrantLock();
private final ConcurrentMap<String, CatalogConnector> catalogs = new ConcurrentHashMap<>();

@GuardedBy("catalogsUpdateLock")
private boolean stopped;

@Inject
public CoordinatorDynamicCatalogManager(CatalogFactory catalogFactory)
{
this.catalogFactory = requireNonNull(catalogFactory, "catalogFactory is null");
}

@PreDestroy
public void stop()
{
List<CatalogConnector> catalogs;

catalogsUpdateLock.lock();
try {
if (stopped) {
return;
}
stopped = true;

catalogs = ImmutableList.copyOf(this.catalogs.values());
this.catalogs.clear();
}
finally {
catalogsUpdateLock.unlock();
}

for (CatalogConnector connector : catalogs) {
connector.shutdown();
}
}

@Override
public void loadInitialCatalogs() {}

@Override
public Set<String> getCatalogNames()
{
return ImmutableSet.copyOf(catalogs.keySet());
}

@Override
public Optional<Catalog> getCatalog(String catalogName)
{
return Optional.ofNullable(catalogs.get(catalogName))
.map(CatalogConnector::getCatalog);
}

@Override
public ConnectorServices getConnectorServices(CatalogHandle catalogHandle)
{
CatalogConnector catalogConnector = catalogs.get(catalogHandle.getCatalogName());
checkArgument(catalogConnector != null, "No catalog '%s'", catalogHandle.getCatalogName());
return catalogConnector.getMaterializedConnector(catalogHandle.getType());
}

@Override
public CatalogHandle createCatalog(String catalogName, String connectorName, Map<String, String> properties)
{
requireNonNull(catalogName, "catalogName is null");
requireNonNull(connectorName, "connectorName is null");
requireNonNull(properties, "properties is null");

catalogsUpdateLock.lock();
try {
checkState(!stopped, "ConnectorManager is stopped");

if (catalogs.containsKey(catalogName)) {
throw new TrinoException(ALREADY_EXISTS, format("Catalog '%s' already exists", catalogName));
}

CatalogProperties catalogProperties = new CatalogProperties(createRootCatalogHandle(catalogName), connectorName, properties);
CatalogConnector catalog = catalogFactory.createCatalog(catalogProperties);
catalogs.put(catalogName, catalog);
return catalog.getCatalogHandle();
}
finally {
catalogsUpdateLock.unlock();
}
}

public void registerGlobalSystemConnector(GlobalSystemConnector connector)
{
requireNonNull(connector, "connector is null");

catalogsUpdateLock.lock();
try {
if (stopped) {
return;
}

CatalogConnector catalog = catalogFactory.createCatalog(GlobalSystemConnector.CATALOG_HANDLE, GlobalSystemConnector.NAME, connector);
if (catalogs.putIfAbsent(GlobalSystemConnector.NAME, catalog) != null) {
throw new IllegalStateException("Global system catalog already registered");
}
}
finally {
catalogsUpdateLock.unlock();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.Scopes;
import io.airlift.configuration.AbstractConfigurationAwareModule;
import io.trino.connector.system.GlobalSystemConnector;
import io.trino.metadata.CatalogManager;

import javax.inject.Inject;

public class DynamicCatalogManagerModule
extends AbstractConfigurationAwareModule
{
@Override
protected void setup(Binder binder)
{
binder.bind(CoordinatorDynamicCatalogManager.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();
}

private static class CoordinatorLazyRegister
{
@Inject
public CoordinatorLazyRegister(
DefaultCatalogFactory defaultCatalogFactory,
LazyCatalogFactory lazyCatalogFactory,
CoordinatorDynamicCatalogManager catalogManager,
GlobalSystemConnector globalSystemConnector)
{
lazyCatalogFactory.setCatalogFactory(defaultCatalogFactory);
catalogManager.registerGlobalSystemConnector(globalSystemConnector);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
package io.trino.connector;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
Expand All @@ -22,6 +21,7 @@
import io.trino.connector.system.GlobalSystemConnector;
import io.trino.metadata.Catalog;
import io.trino.metadata.CatalogManager;
import io.trino.spi.TrinoException;

import javax.annotation.PreDestroy;
import javax.annotation.concurrent.ThreadSafe;
Expand All @@ -46,6 +46,7 @@
import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.airlift.configuration.ConfigurationLoader.loadPropertiesFrom;
import static io.trino.connector.CatalogHandle.createRootCatalogHandle;
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
import static java.util.Objects.requireNonNull;

@ThreadSafe
Expand Down Expand Up @@ -123,6 +124,7 @@ public void stop()
catalogs.clear();
}

@Override
public void loadInitialCatalogs()
{
if (!state.compareAndSet(State.CREATED, State.INITIALIZED)) {
Expand Down Expand Up @@ -169,19 +171,9 @@ public void registerGlobalSystemConnector(GlobalSystemConnector connector)
}
}

@VisibleForTesting
@Override
public CatalogHandle createCatalog(String catalogName, String connectorName, Map<String, String> properties)
{
requireNonNull(catalogName, "catalogName is null");
requireNonNull(connectorName, "connectorName is null");
requireNonNull(properties, "properties is null");

checkState(state.get() != State.STOPPED, "Catalog manager is stopped");

CatalogConnector catalog = catalogFactory.createCatalog(new CatalogProperties(createRootCatalogHandle(catalogName), connectorName, properties));
if (catalogs.putIfAbsent(catalogName, catalog) != null) {
throw new IllegalStateException(String.format("Catalog '%s' already exists", catalogName));
}
return catalog.getCatalogHandle();
throw new TrinoException(NOT_SUPPORTED, "Create catalog is not supported by the static catalog manager");
}
}
Loading

0 comments on commit 7ff57ce

Please sign in to comment.