Skip to content

Commit

Permalink
Improve how we configure plan checkers
Browse files Browse the repository at this point in the history
Plan checker configurations go in the configured plan checker
configuration directory.  We load the configuration files to figure out
which plan checkers to register, and pass any configuration properties
to the PlanCheckerProviderFactory.

This change also wraps SimplePlanFragmentSerde into a
PlanCheckerProviderContext so that if plan checkers need other things
arguments added we can add them to that wrapper class. That way we won't
break anyone's plan checker provider plugins by changing the
PlanCheckerProviderFactory SPI.
  • Loading branch information
rschlussel committed Nov 7, 2024
1 parent 0a90d6e commit 1a1fdd8
Show file tree
Hide file tree
Showing 15 changed files with 271 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
import com.facebook.presto.sql.planner.plan.JsonCodecSimplePlanFragmentSerde;
import com.facebook.presto.sql.planner.sanity.PlanChecker;
import com.facebook.presto.sql.planner.sanity.PlanCheckerProviderManager;
import com.facebook.presto.sql.planner.sanity.PlanCheckerProviderManagerConfig;
import com.facebook.presto.sql.relational.RowExpressionDeterminismEvaluator;
import com.facebook.presto.sql.relational.RowExpressionDomainTranslator;
import com.facebook.presto.sql.tree.Expression;
Expand Down Expand Up @@ -801,6 +802,7 @@ public ListeningExecutorService createResourceManagerExecutor(ResourceManagerCon
binder.bind(NodeStatusNotificationManager.class).in(Scopes.SINGLETON);

binder.bind(PlanCheckerProviderManager.class).in(Scopes.SINGLETON);
configBinder(binder).bindConfig(PlanCheckerProviderManagerConfig.class);

// Worker session property providers
MapBinder<String, WorkerSessionPropertyProvider> mapBinder =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,44 @@
*/
package com.facebook.presto.sql.planner.sanity;

import com.facebook.airlift.log.Logger;
import com.facebook.presto.spi.plan.PlanCheckerProvider;
import com.facebook.presto.spi.plan.PlanCheckerProviderContext;
import com.facebook.presto.spi.plan.PlanCheckerProviderFactory;
import com.facebook.presto.spi.plan.SimplePlanFragmentSerde;
import com.google.common.collect.ImmutableList;
import com.google.inject.Inject;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

import static com.facebook.presto.util.PropertiesUtil.loadProperties;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Strings.isNullOrEmpty;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public class PlanCheckerProviderManager
{
private final SimplePlanFragmentSerde simplePlanFragmentSerde;
private static final Logger log = Logger.get(PlanCheckerProviderManager.class);
private static final String PLAN_CHECKER_PROVIDER_NAME = "plan-checker-provider.name";

private final PlanCheckerProviderContext planCheckerProviderContext;
private final Map<String, PlanCheckerProviderFactory> providerFactories = new ConcurrentHashMap<>();
private final CopyOnWriteArrayList<PlanCheckerProvider> providers = new CopyOnWriteArrayList<>();
private final File configDirectory;

@Inject
public PlanCheckerProviderManager(SimplePlanFragmentSerde simplePlanFragmentSerde)
public PlanCheckerProviderManager(SimplePlanFragmentSerde simplePlanFragmentSerde, PlanCheckerProviderManagerConfig config)
{
this.simplePlanFragmentSerde = requireNonNull(simplePlanFragmentSerde, "planNodeSerde is null");
this.planCheckerProviderContext = new PlanCheckerProviderContext(requireNonNull(simplePlanFragmentSerde, "planNodeSerde is null"));
requireNonNull(config, "config is null");
this.configDirectory = requireNonNull(config.getPlanCheckerConfigurationDir(), "configDirectory is null");
}

public void addPlanCheckerProviderFactory(PlanCheckerProviderFactory planCheckerProviderFactory)
Expand All @@ -48,8 +62,38 @@ public void addPlanCheckerProviderFactory(PlanCheckerProviderFactory planChecker
}

public void loadPlanCheckerProviders()
throws IOException
{
for (File file : listFiles(configDirectory)) {
if (file.isFile() && file.getName().endsWith(".properties")) {
// unlike function namespaces and connectors, we don't have a concept of catalog
// name here (conventionally config file name without the extension)
// because plan checkers are never referenced by name.
Map<String, String> properties = new HashMap<>(loadProperties(file));
checkState(!isNullOrEmpty(properties.get(PLAN_CHECKER_PROVIDER_NAME)),
"Plan checker configuration %s does not contain %s",
file.getAbsoluteFile(),
PLAN_CHECKER_PROVIDER_NAME);
String planCheckerProviderName = properties.remove(PLAN_CHECKER_PROVIDER_NAME);
log.info("-- Loading plan checker provider %s--", planCheckerProviderName);
PlanCheckerProviderFactory providerFactory = providerFactories.get(planCheckerProviderName);
checkState(providerFactory != null,
"No planCheckerProviderFactory found for '%s'. Available factories were %s", planCheckerProviderName, providerFactories.keySet());
providers.addIfAbsent(providerFactory.create(properties, planCheckerProviderContext));
log.info("-- Added plan checker provider [%s] --", planCheckerProviderName);
}
}
}

private static List<File> listFiles(File dir)
{
providers.addAllAbsent(providerFactories.values().stream().map(pc -> pc.create(simplePlanFragmentSerde)).collect(Collectors.toList()));
if (dir != null && dir.isDirectory()) {
File[] files = dir.listFiles();
if (files != null) {
return ImmutableList.copyOf(files);
}
}
return ImmutableList.of();
}

public List<PlanCheckerProvider> getPlanCheckerProviders()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.facebook.presto.sql.planner.sanity;

import com.facebook.airlift.configuration.Config;

import javax.validation.constraints.NotNull;

import java.io.File;

public class PlanCheckerProviderManagerConfig
{
private File planCheckerConfigurationDir = new File("etc/plan-checker-providers/");

@NotNull
public File getPlanCheckerConfigurationDir()
{
return planCheckerConfigurationDir;
}

@Config("plan-checker.config-dir")
public PlanCheckerProviderManagerConfig setPlanCheckerConfigurationDir(File dir)
{
this.planCheckerConfigurationDir = dir;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
import com.facebook.presto.sql.planner.planPrinter.PlanPrinter;
import com.facebook.presto.sql.planner.sanity.PlanChecker;
import com.facebook.presto.sql.planner.sanity.PlanCheckerProviderManager;
import com.facebook.presto.sql.planner.sanity.PlanCheckerProviderManagerConfig;
import com.facebook.presto.sql.relational.RowExpressionDeterminismEvaluator;
import com.facebook.presto.sql.relational.RowExpressionDomainTranslator;
import com.facebook.presto.sql.tree.AlterFunction;
Expand Down Expand Up @@ -439,7 +440,7 @@ private LocalQueryRunner(Session defaultSession, FeaturesConfig featuresConfig,
new AnalyzePropertyManager(),
transactionManager);
this.splitManager = new SplitManager(metadata, new QueryManagerConfig(), nodeSchedulerConfig);
this.planCheckerProviderManager = new PlanCheckerProviderManager(new JsonCodecSimplePlanFragmentSerde(jsonCodec(SimplePlanFragment.class)));
this.planCheckerProviderManager = new PlanCheckerProviderManager(new JsonCodecSimplePlanFragmentSerde(jsonCodec(SimplePlanFragment.class)), new PlanCheckerProviderManagerConfig());
this.distributedPlanChecker = new PlanChecker(featuresConfig, false, planCheckerProviderManager);
this.singleNodePlanChecker = new PlanChecker(featuresConfig, true, planCheckerProviderManager);
this.planFragmenter = new PlanFragmenter(this.metadata, this.nodePartitioningManager, new QueryManagerConfig(), featuresConfig, planCheckerProviderManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import com.facebook.presto.sql.planner.plan.JsonCodecSimplePlanFragmentSerde;
import com.facebook.presto.sql.planner.plan.SequenceNode;
import com.facebook.presto.sql.planner.sanity.PlanCheckerProviderManager;
import com.facebook.presto.sql.planner.sanity.PlanCheckerProviderManagerConfig;
import com.facebook.presto.sql.tree.Cast;
import com.facebook.presto.sql.tree.SymbolReference;
import com.facebook.presto.tpch.TpchColumnHandle;
Expand Down Expand Up @@ -156,7 +157,7 @@ public void setUp()
new SimpleTtlNodeSelectorConfig());
PartitioningProviderManager partitioningProviderManager = new PartitioningProviderManager();
nodePartitioningManager = new NodePartitioningManager(nodeScheduler, partitioningProviderManager, new NodeSelectionStats());
planCheckerProviderManager = new PlanCheckerProviderManager(new JsonCodecSimplePlanFragmentSerde(jsonCodec(SimplePlanFragment.class)));
planCheckerProviderManager = new PlanCheckerProviderManager(new JsonCodecSimplePlanFragmentSerde(jsonCodec(SimplePlanFragment.class)), new PlanCheckerProviderManagerConfig());
planFragmenter = new PlanFragmenter(metadata, nodePartitioningManager, new QueryManagerConfig(), new FeaturesConfig(), planCheckerProviderManager);
translator = new TestingRowExpressionTranslator();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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.facebook.presto.sql.planner.sanity;

import com.facebook.airlift.json.JsonCodec;
import com.facebook.presto.spi.plan.PlanCheckerProvider;
import com.facebook.presto.spi.plan.PlanCheckerProviderContext;
import com.facebook.presto.spi.plan.PlanCheckerProviderFactory;
import com.facebook.presto.spi.plan.SimplePlanFragment;
import com.facebook.presto.sql.planner.plan.JsonCodecSimplePlanFragmentSerde;
import com.google.common.collect.ImmutableList;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import static com.facebook.presto.sql.planner.sanity.TestPlanCheckerProviderManager.TestingPlanCheckerProvider.TESTING_PLAN_CHECKER_PROVIDER;
import static com.facebook.presto.testing.assertions.Assert.assertEquals;

public class TestPlanCheckerProviderManager
{
@Test
public void testLoadPlanCheckerProviders()
throws IOException
{
PlanCheckerProviderManagerConfig planCheckerProviderManagerConfig = new PlanCheckerProviderManagerConfig()
.setPlanCheckerConfigurationDir(new File("src/test/resources/plan-checkers"));
PlanCheckerProviderManager planCheckerProviderManager = new PlanCheckerProviderManager(new JsonCodecSimplePlanFragmentSerde(JsonCodec.jsonCodec(SimplePlanFragment.class)), planCheckerProviderManagerConfig);
planCheckerProviderManager.addPlanCheckerProviderFactory(new TestingPlanCheckerProviderFactory());
planCheckerProviderManager.loadPlanCheckerProviders();
assertEquals(planCheckerProviderManager.getPlanCheckerProviders(), ImmutableList.of(TESTING_PLAN_CHECKER_PROVIDER));
}

@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "No planCheckerProviderFactory found for 'test'. Available factories were \\[]")
public void testLoadUnregisteredPlanCheckerProvider()
throws IOException
{
PlanCheckerProviderManagerConfig planCheckerProviderManagerConfig = new PlanCheckerProviderManagerConfig()
.setPlanCheckerConfigurationDir(new File("src/test/resources/plan-checkers"));
PlanCheckerProviderManager planCheckerProviderManager = new PlanCheckerProviderManager(new JsonCodecSimplePlanFragmentSerde(JsonCodec.jsonCodec(SimplePlanFragment.class)), planCheckerProviderManagerConfig);
planCheckerProviderManager.loadPlanCheckerProviders();
}

public static class TestingPlanCheckerProviderFactory
implements PlanCheckerProviderFactory
{
@Override
public String getName()
{
return "test";
}

@Override
public PlanCheckerProvider create(Map<String, String> properties, PlanCheckerProviderContext planCheckerProviderContext)
{
return TESTING_PLAN_CHECKER_PROVIDER;
}
}

public static class TestingPlanCheckerProvider
implements PlanCheckerProvider
{
public static final TestingPlanCheckerProvider TESTING_PLAN_CHECKER_PROVIDER = new TestingPlanCheckerProvider();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.facebook.presto.sql.planner.sanity;

import com.google.common.collect.ImmutableMap;
import org.testng.annotations.Test;

import java.io.File;
import java.util.Map;

import static com.facebook.airlift.configuration.testing.ConfigAssertions.assertFullMapping;
import static com.facebook.airlift.configuration.testing.ConfigAssertions.assertRecordedDefaults;
import static com.facebook.airlift.configuration.testing.ConfigAssertions.recordDefaults;

public class TestPlanCheckerProviderManagerConfig
{
@Test
public void testDefaults()
{
assertRecordedDefaults(recordDefaults(PlanCheckerProviderManagerConfig.class)
.setPlanCheckerConfigurationDir(new File("etc/plan-checker-providers")));
}

@Test
public void testExplicitPropertyMappings()
{
Map<String, String> properties = new ImmutableMap.Builder<String, String>()
.put("plan-checker.config-dir", "/foo")
.build();

PlanCheckerProviderManagerConfig expected = new PlanCheckerProviderManagerConfig()
.setPlanCheckerConfigurationDir(new File("/foo"));

assertFullMapping(properties, expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
plan-checker-provider.name=test
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
import com.facebook.presto.sql.planner.plan.JsonCodecSimplePlanFragmentSerde;
import com.facebook.presto.sql.planner.sanity.PlanChecker;
import com.facebook.presto.sql.planner.sanity.PlanCheckerProviderManager;
import com.facebook.presto.sql.planner.sanity.PlanCheckerProviderManagerConfig;
import com.facebook.presto.sql.relational.RowExpressionDeterminismEvaluator;
import com.facebook.presto.sql.relational.RowExpressionDomainTranslator;
import com.facebook.presto.tracing.TracerProviderManager;
Expand Down Expand Up @@ -276,6 +277,7 @@ protected void setup(Binder binder)
configBinder(binder).bindConfig(NativeExecutionSystemConfig.class);
configBinder(binder).bindConfig(NativeExecutionNodeConfig.class);
configBinder(binder).bindConfig(NativeExecutionConnectorConfig.class);
configBinder(binder).bindConfig(PlanCheckerProviderManagerConfig.class);

// json codecs
jsonCodecBinder(binder).bindJsonCodec(ViewDefinition.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
import com.facebook.presto.sql.planner.plan.JsonCodecSimplePlanFragmentSerde;
import com.facebook.presto.sql.planner.sanity.PlanChecker;
import com.facebook.presto.sql.planner.sanity.PlanCheckerProviderManager;
import com.facebook.presto.sql.planner.sanity.PlanCheckerProviderManagerConfig;
import com.facebook.presto.tpch.TpchColumnHandle;
import com.facebook.presto.tpch.TpchTableHandle;
import com.facebook.presto.tpch.TpchTableLayoutHandle;
Expand Down Expand Up @@ -162,7 +163,7 @@ public void setUp()
new SimpleTtlNodeSelectorConfig());
PartitioningProviderManager partitioningProviderManager = new PartitioningProviderManager();
nodePartitioningManager = new NodePartitioningManager(nodeScheduler, partitioningProviderManager, new NodeSelectionStats());
planCheckerProviderManager = new PlanCheckerProviderManager(new JsonCodecSimplePlanFragmentSerde(jsonCodec(SimplePlanFragment.class)));
planCheckerProviderManager = new PlanCheckerProviderManager(new JsonCodecSimplePlanFragmentSerde(jsonCodec(SimplePlanFragment.class)), new PlanCheckerProviderManagerConfig());
planFragmenter = new PlanFragmenter(metadata, nodePartitioningManager, new QueryManagerConfig(), new FeaturesConfig(), planCheckerProviderManager);
}

Expand Down
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 com.facebook.presto.spi.plan;

import static java.util.Objects.requireNonNull;

public class PlanCheckerProviderContext
{
private final SimplePlanFragmentSerde simplePlanFragmentSerde;

public PlanCheckerProviderContext(SimplePlanFragmentSerde simplePlanFragmentSerde)
{
this.simplePlanFragmentSerde = requireNonNull(simplePlanFragmentSerde, "simplePlanFragmentSerde is null");
}

public SimplePlanFragmentSerde getSimplePlanFragmentSerde()
{
return simplePlanFragmentSerde;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
*/
package com.facebook.presto.spi.plan;

import java.util.Map;

public interface PlanCheckerProviderFactory
{
String getName();

PlanCheckerProvider create(SimplePlanFragmentSerde simplePlanFragmentSerde);
PlanCheckerProvider create(Map<String, String> properties, PlanCheckerProviderContext planCheckerProviderContext);
}
Loading

0 comments on commit 1a1fdd8

Please sign in to comment.