Skip to content

Commit

Permalink
Database based SessionPropertyConfigurationManager implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
phd3 authored and electrum committed Sep 27, 2019
1 parent f1a1277 commit 90fbe03
Show file tree
Hide file tree
Showing 21 changed files with 1,452 additions and 88 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.prestosql</groupId>
<artifactId>presto-session-property-managers</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
</dependency>

<dependency>
<groupId>io.prestosql</groupId>
<artifactId>presto-array</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import io.prestosql.server.GracefulShutdownHandler;
import io.prestosql.server.PluginManager;
import io.prestosql.server.ServerMainModule;
import io.prestosql.server.SessionPropertyDefaults;
import io.prestosql.server.ShutdownAction;
import io.prestosql.server.security.ServerSecurityModule;
import io.prestosql.spi.Plugin;
Expand Down Expand Up @@ -120,6 +121,7 @@ public class TestingPrestoServer
private final TestingAccessControlManager accessControl;
private final ProcedureTester procedureTester;
private final Optional<InternalResourceGroupManager<?>> resourceGroupManager;
private final SessionPropertyDefaults sessionPropertyDefaults;
private final SplitManager splitManager;
private final PageSourceManager pageSourceManager;
private final NodePartitioningManager nodePartitioningManager;
Expand Down Expand Up @@ -279,6 +281,7 @@ public TestingPrestoServer(
dispatchManager = injector.getInstance(DispatchManager.class);
queryManager = (SqlQueryManager) injector.getInstance(QueryManager.class);
resourceGroupManager = Optional.of(injector.getInstance(InternalResourceGroupManager.class));
sessionPropertyDefaults = injector.getInstance(SessionPropertyDefaults.class);
nodePartitioningManager = injector.getInstance(NodePartitioningManager.class);
clusterMemoryManager = injector.getInstance(ClusterMemoryManager.class);
statsCalculator = injector.getInstance(StatsCalculator.class);
Expand All @@ -287,6 +290,7 @@ public TestingPrestoServer(
dispatchManager = null;
queryManager = null;
resourceGroupManager = Optional.empty();
sessionPropertyDefaults = null;
nodePartitioningManager = null;
clusterMemoryManager = null;
statsCalculator = null;
Expand Down Expand Up @@ -429,6 +433,11 @@ public Optional<InternalResourceGroupManager<?>> getResourceGroupManager()
return resourceGroupManager;
}

public SessionPropertyDefaults getSessionPropertyDefaults()
{
return sessionPropertyDefaults;
}

public NodePartitioningManager getNodePartitioningManager()
{
return nodePartitioningManager;
Expand Down
64 changes: 64 additions & 0 deletions presto-session-property-managers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<artifactId>bootstrap</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>log</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>json</artifactId>
Expand All @@ -32,6 +37,27 @@
<artifactId>configuration</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>concurrent</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>stats</artifactId>
</dependency>

<dependency>
<groupId>org.weakref</groupId>
<artifactId>jmxutils</artifactId>
</dependency>

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand All @@ -42,6 +68,11 @@
<artifactId>guice</artifactId>
</dependency>

<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
</dependency>

<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
Expand All @@ -62,6 +93,26 @@
<artifactId>jackson-core</artifactId>
</dependency>

<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-core</artifactId>
</dependency>

<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-sqlobject</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
<groupId>io.prestosql</groupId>
<artifactId>presto-plugin-toolkit</artifactId>
</dependency>

<!-- Presto SPI -->
<dependency>
<groupId>io.prestosql</groupId>
Expand All @@ -75,6 +126,12 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>slice</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
Expand All @@ -99,5 +156,12 @@
<artifactId>testing</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>testing-mysql-server</artifactId>
<scope>test</scope>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,24 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import io.prestosql.spi.session.SessionConfigurationContext;
import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;

public class SessionMatchSpec
Expand Down Expand Up @@ -120,4 +127,45 @@ public Map<String, String> getSessionProperties()
{
return sessionProperties;
}

public static class Mapper
implements RowMapper<SessionMatchSpec>
{
@Override
public SessionMatchSpec map(ResultSet resultSet, StatementContext context)
throws SQLException
{
Map<String, String> sessionProperties = getProperties(
Optional.ofNullable(resultSet.getString("session_property_names")),
Optional.ofNullable(resultSet.getString("session_property_values")));

return new SessionMatchSpec(
Optional.ofNullable(resultSet.getString("user_regex")).map(Pattern::compile),
Optional.ofNullable(resultSet.getString("source_regex")).map(Pattern::compile),
Optional.ofNullable(resultSet.getString("client_tags")).map(tag -> Splitter.on(",").splitToList(tag)),
Optional.ofNullable(resultSet.getString("query_type")),
Optional.ofNullable(resultSet.getString("group_regex")).map(Pattern::compile),
sessionProperties);
}

private static Map<String, String> getProperties(Optional<String> names, Optional<String> values)
{
if (!names.isPresent()) {
return ImmutableMap.of();
}

checkArgument(values.isPresent(), "names are present, but values are not");
List<String> sessionPropertyNames = Splitter.on(",").splitToList(names.get());
List<String> sessionPropertyValues = Splitter.on(",").splitToList(values.get());
checkArgument(sessionPropertyNames.size() == sessionPropertyValues.size(),
"The number of property names and values should be the same");

Map<String, String> sessionProperties = new HashMap<>();
for (int i = 0; i < sessionPropertyNames.size(); i++) {
sessionProperties.put(sessionPropertyNames.get(i), sessionPropertyValues.get(i));
}

return sessionProperties;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@
package io.prestosql.plugin.session;

import com.google.common.collect.ImmutableList;
import io.prestosql.plugin.session.db.DbSessionPropertyManagerFactory;
import io.prestosql.spi.Plugin;
import io.prestosql.spi.session.SessionPropertyConfigurationManagerFactory;

public class FileSessionPropertyManagerPlugin
public class SessionPropertyConfigurationManagerPlugin
implements Plugin
{
@Override
public Iterable<SessionPropertyConfigurationManagerFactory> getSessionPropertyConfigurationManagerFactories()
{
return ImmutableList.of(new FileSessionPropertyManagerFactory());
return ImmutableList.of(
new FileSessionPropertyManagerFactory(),
new DbSessionPropertyManagerFactory());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.prestosql.plugin.session.db;

import com.google.common.collect.ImmutableMap;
import io.prestosql.plugin.session.SessionMatchSpec;
import io.prestosql.spi.session.SessionConfigurationContext;
import io.prestosql.spi.session.SessionPropertyConfigurationManager;

import javax.inject.Inject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.Objects.requireNonNull;

/**
* A {@link SessionPropertyConfigurationManager} implementation that connects to a database for fetching information
* about session property overrides given {@link SessionConfigurationContext}.
*/
public class DbSessionPropertyManager
implements SessionPropertyConfigurationManager
{
private final DbSpecsProvider specsProvider;

@Inject
public DbSessionPropertyManager(DbSpecsProvider specsProvider)
{
this.specsProvider = requireNonNull(specsProvider, "specsProvider is null");
}

@Override
public Map<String, String> getSystemSessionProperties(SessionConfigurationContext context)
{
List<SessionMatchSpec> sessionMatchSpecs = specsProvider.get();

// later properties override earlier properties
Map<String, String> combinedProperties = new HashMap<>();
for (SessionMatchSpec sessionMatchSpec : sessionMatchSpecs) {
combinedProperties.putAll(sessionMatchSpec.match(context));
}

return ImmutableMap.copyOf(combinedProperties);
}

@Override
public Map<String, Map<String, String>> getCatalogSessionProperties(SessionConfigurationContext context)
{
// NOT IMPLEMENTED YET
return ImmutableMap.of();
}
}
Loading

0 comments on commit 90fbe03

Please sign in to comment.