Skip to content

Commit

Permalink
Add test infrastructure to run queries using native workers
Browse files Browse the repository at this point in the history
Extend HiveQueryRunner and DistributedQueryRunner to allow for setting up a
Java-based coordinator and a set of native workers. Native workers are created
by launching separate processes using native binary. The path to the binary is
specified using PRESTO_SERVER environment variable. The new test that uses
this infrastructure, TestHiveNativeWorkersQueries.java, is disabled by
default. In the future, we'll figure out how to get native worker binary on a
CI machine to enable this test.

The new test passed locally.
  • Loading branch information
mbasmanova committed Sep 10, 2020
1 parent 2e179d3 commit f3a43a8
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
import org.joda.time.DateTimeZone;

import java.io.File;
import java.net.URI;
import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;

import static com.facebook.airlift.log.Level.ERROR;
import static com.facebook.airlift.log.Level.WARN;
Expand Down Expand Up @@ -91,13 +93,13 @@ public static DistributedQueryRunner createQueryRunner(
Optional<Path> baseDataDir)
throws Exception
{
return createQueryRunner(tables, extraProperties, extraCoordinatorProperties, "sql-standard", ImmutableMap.of(), baseDataDir);
return createQueryRunner(tables, extraProperties, extraCoordinatorProperties, "sql-standard", ImmutableMap.of(), Optional.empty(), baseDataDir, Optional.empty());
}

public static DistributedQueryRunner createQueryRunner(Iterable<TpchTable<?>> tables, Map<String, String> extraProperties, Optional<Path> baseDataDir)
throws Exception
{
return createQueryRunner(tables, extraProperties, ImmutableMap.of(), "sql-standard", ImmutableMap.of(), baseDataDir);
return createQueryRunner(tables, extraProperties, ImmutableMap.of(), "sql-standard", ImmutableMap.of(), Optional.empty(), baseDataDir, Optional.empty());
}

public static DistributedQueryRunner createQueryRunner(
Expand All @@ -108,7 +110,7 @@ public static DistributedQueryRunner createQueryRunner(
Optional<Path> baseDataDir)
throws Exception
{
return createQueryRunner(tables, extraProperties, ImmutableMap.of(), security, extraHiveProperties, baseDataDir);
return createQueryRunner(tables, extraProperties, ImmutableMap.of(), security, extraHiveProperties, Optional.empty(), baseDataDir, Optional.empty());
}

public static DistributedQueryRunner createQueryRunner(
Expand All @@ -117,7 +119,9 @@ public static DistributedQueryRunner createQueryRunner(
Map<String, String> extraCoordinatorProperties,
String security,
Map<String, String> extraHiveProperties,
Optional<Path> baseDataDir)
Optional<Integer> workerCount,
Optional<Path> baseDataDir,
Optional<BiFunction<Integer, URI, Process>> externalWorkerLauncher)
throws Exception
{
assertEquals(DateTimeZone.getDefault(), TIME_ZONE, "Timezone not configured correctly. Add -Duser.timezone=America/Bahia_Banderas to your JVM arguments");
Expand All @@ -131,10 +135,11 @@ public static DistributedQueryRunner createQueryRunner(

DistributedQueryRunner queryRunner =
DistributedQueryRunner.builder(createSession(Optional.of(new SelectedRole(ROLE, Optional.of("admin")))))
.setNodeCount(4)
.setNodeCount(workerCount.orElse(4))
.setExtraProperties(systemProperties)
.setCoordinatorProperties(extraCoordinatorProperties)
.setBaseDataDir(baseDataDir)
.setExternalWorkerLauncher(externalWorkerLauncher)
.build();
try {
queryRunner.installPlugin(new TpchPlugin());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* 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.hive;

import com.facebook.presto.spi.api.Experimental;
import com.facebook.presto.testing.QueryRunner;
import com.facebook.presto.tests.AbstractTestQueryFramework;
import com.facebook.presto.tests.DistributedQueryRunner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.testng.annotations.Test;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;

import static com.google.common.base.Preconditions.checkArgument;
import static io.airlift.tpch.TpchTable.NATION;
import static java.lang.String.format;

@Experimental
public class TestHiveExternalWorkersQueries
extends AbstractTestQueryFramework
{
protected TestHiveExternalWorkersQueries()
{
super(TestHiveExternalWorkersQueries::createQueryRunner);
}

private static QueryRunner createQueryRunner()
throws Exception
{
String prestoServerPath = System.getenv("PRESTO_SERVER");
String baseDataDir = System.getenv("DATA_DIR");

return createQueryRunner(Optional.ofNullable(prestoServerPath), Optional.ofNullable(baseDataDir).map(Paths::get));
}

private static QueryRunner createQueryRunner(Optional<String> prestoServerPath, Optional<Path> baseDataDir)
throws Exception
{
if (!prestoServerPath.isPresent()) {
return HiveQueryRunner.createQueryRunner(
ImmutableList.of(NATION),
ImmutableMap.of(),
"sql-standard",
ImmutableMap.of("hive.storage-format", "DWRF"),
baseDataDir);
}

checkArgument(baseDataDir.isPresent(), "Path to data files must be specified when testing external workers");

// Make TPC-H tables in DWRF format using Java-based workers
HiveQueryRunner.createQueryRunner(
ImmutableList.of(NATION),
ImmutableMap.of(),
"sql-standard",
ImmutableMap.of("hive.storage-format", "DWRF"),
baseDataDir).close();

Path tempDirectoryPath = Files.createTempDirectory(TestHiveExternalWorkersQueries.class.getSimpleName());

// Make query runner with external workers for tests
DistributedQueryRunner queryRunner = HiveQueryRunner.createQueryRunner(ImmutableList.of(NATION),
ImmutableMap.of("optimizer.optimize-hash-generation", "false"),
ImmutableMap.of(),
"sql-standard",
ImmutableMap.of(),
Optional.of(1),
baseDataDir,
Optional.of((workerIndex, discoveryUri) -> {
try {
if (workerIndex == 0) {
// Write discovery URL to /tmp/config.properties
Files.write(tempDirectoryPath.resolve("config.properties"),
format("discovery.uri=%s\n", discoveryUri).getBytes());
}
return new ProcessBuilder(prestoServerPath.get(), "--logtostderr=1", "--v=1")
.directory(tempDirectoryPath.toFile())
.redirectErrorStream(true)
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
.redirectError(ProcessBuilder.Redirect.INHERIT)
.start();
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}));

return queryRunner;
}

@Test
public void testFiltersAndProjections()
{
assertQuery("SELECT * FROM nation");
assertQuery("SELECT * FROM nation WHERE nationkey = 4");
assertQuery("SELECT * FROM nation WHERE nationkey <> 4");
assertQuery("SELECT * FROM nation WHERE nationkey < 4");
assertQuery("SELECT * FROM nation WHERE nationkey <= 4");
assertQuery("SELECT * FROM nation WHERE nationkey > 4");
assertQuery("SELECT * FROM nation WHERE nationkey >= 4");
assertQuery("SELECT nationkey * 10, nationkey % 5, -nationkey, nationkey / 3 FROM nation");
assertQuery("SELECT *, nationkey / 3 FROM nation");
}

@Test
public void testAggregations()
{
assertQuery("SELECT count(*) FROM nation");
assertQuery("SELECT regionkey, count(*) FROM nation GROUP BY regionkey");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.BiFunction;
import java.util.function.Function;

import static com.facebook.presto.testing.TestingSession.TESTING_CATALOG;
Expand All @@ -91,6 +92,7 @@ public class DistributedQueryRunner
private final TestingDiscoveryServer discoveryServer;
private final TestingPrestoServer coordinator;
private final List<TestingPrestoServer> servers;
private final List<Process> externalWorkers;

private final Closer closer = Closer.create();

Expand All @@ -111,7 +113,7 @@ public DistributedQueryRunner(Session defaultSession, int nodeCount)
public DistributedQueryRunner(Session defaultSession, int nodeCount, Map<String, String> extraProperties)
throws Exception
{
this(defaultSession, nodeCount, extraProperties, ImmutableMap.of(), DEFAULT_SQL_PARSER_OPTIONS, ENVIRONMENT, Optional.empty());
this(defaultSession, nodeCount, extraProperties, ImmutableMap.of(), DEFAULT_SQL_PARSER_OPTIONS, ENVIRONMENT, Optional.empty(), Optional.empty());
}

public static Builder builder(Session defaultSession)
Expand All @@ -126,7 +128,8 @@ private DistributedQueryRunner(
Map<String, String> coordinatorProperties,
SqlParserOptions parserOptions,
String environment,
Optional<Path> baseDataDir)
Optional<Path> baseDataDir,
Optional<BiFunction<Integer, URI, Process>> externalWorkerLauncher)
throws Exception
{
requireNonNull(defaultSession, "defaultSession is null");
Expand All @@ -136,19 +139,40 @@ private DistributedQueryRunner(
discoveryServer = new TestingDiscoveryServer(environment);
closer.register(() -> closeUnchecked(discoveryServer));
log.info("Created TestingDiscoveryServer in %s", nanosSince(start).convertToMostSuccinctTimeUnit());
URI discoveryUrl = discoveryServer.getBaseUrl();
log.info("Discovery URL %s", discoveryUrl);

ImmutableList.Builder<TestingPrestoServer> servers = ImmutableList.builder();
Map<String, String> extraCoordinatorProperties = new HashMap<>();

for (int i = 1; i < nodeCount; i++) {
TestingPrestoServer worker = closer.register(createTestingPrestoServer(discoveryServer.getBaseUrl(), false, extraProperties, parserOptions, environment, baseDataDir));
servers.add(worker);
if (externalWorkerLauncher.isPresent()) {
ImmutableList.Builder<Process> externalWorkersBuilder = ImmutableList.builder();
for (int i = 0; i < nodeCount; i++) {
externalWorkersBuilder.add(externalWorkerLauncher.get().apply(i, discoveryUrl));
}
externalWorkers = externalWorkersBuilder.build();
closer.register(() -> {
for (Process worker : externalWorkers) {
worker.destroyForcibly();
}
});

// Don't use coordinator as worker
extraCoordinatorProperties.put("node-scheduler.include-coordinator", "false");
}
else {
externalWorkers = ImmutableList.of();

for (int i = 1; i < nodeCount; i++) {
TestingPrestoServer worker = closer.register(createTestingPrestoServer(discoveryUrl, false, extraProperties, parserOptions, environment, baseDataDir));
servers.add(worker);
}
}

Map<String, String> extraCoordinatorProperties = new HashMap<>();
extraCoordinatorProperties.put("experimental.iterative-optimizer-enabled", "true");
extraCoordinatorProperties.putAll(extraProperties);
extraCoordinatorProperties.putAll(coordinatorProperties);
coordinator = closer.register(createTestingPrestoServer(discoveryServer.getBaseUrl(), true, extraCoordinatorProperties, parserOptions, environment, baseDataDir));
coordinator = closer.register(createTestingPrestoServer(discoveryUrl, true, extraCoordinatorProperties, parserOptions, environment, baseDataDir));
servers.add(coordinator);

this.servers = servers.build();
Expand Down Expand Up @@ -217,10 +241,11 @@ private static TestingPrestoServer createTestingPrestoServer(URI discoveryUri, b

private boolean allNodesGloballyVisible()
{
int expectedActiveNodes = externalWorkers.size() + servers.size();
for (TestingPrestoServer server : servers) {
AllNodes allNodes = server.refreshNodes();
if (!allNodes.getInactiveNodes().isEmpty() ||
(allNodes.getActiveNodes().size() != servers.size())) {
(allNodes.getActiveNodes().size() != expectedActiveNodes)) {
return false;
}
}
Expand Down Expand Up @@ -336,7 +361,7 @@ public void createCatalog(String catalogName, String connectorName, Map<String,

// wait for all nodes to announce the new catalog
start = nanoTime();
while (!isConnectionVisibleToAllNodes(connectorId)) {
while (!isConnectorVisibleToAllNodes(connectorId)) {
Assertions.assertLessThan(nanosSince(start), new Duration(100, SECONDS), "waiting for connector " + connectorId + " to be initialized in every node");
try {
MILLISECONDS.sleep(10);
Expand Down Expand Up @@ -390,8 +415,12 @@ public void createTestFunctionNamespace(String catalogName, String schemaName)
testFunctionNamespacesHandle.get().execute("INSERT INTO function_namespaces SELECT ?, ?", catalogName, schemaName);
}

private boolean isConnectionVisibleToAllNodes(ConnectorId connectorId)
private boolean isConnectorVisibleToAllNodes(ConnectorId connectorId)
{
if (!externalWorkers.isEmpty()) {
return true;
}

for (TestingPrestoServer server : servers) {
server.refreshNodes();
Set<InternalNode> activeNodesWithConnector = server.getActiveNodesWithConnector(connectorId);
Expand Down Expand Up @@ -540,6 +569,7 @@ public static class Builder
private SqlParserOptions parserOptions = DEFAULT_SQL_PARSER_OPTIONS;
private String environment = ENVIRONMENT;
private Optional<Path> baseDataDir = Optional.empty();
private Optional<BiFunction<Integer, URI, Process>> externalWorkerLauncher = Optional.empty();

protected Builder(Session defaultSession)
{
Expand Down Expand Up @@ -609,10 +639,16 @@ public Builder setBaseDataDir(Optional<Path> baseDataDir)
return this;
}

public Builder setExternalWorkerLauncher(Optional<BiFunction<Integer, URI, Process>> externalWorkerLauncher)
{
this.externalWorkerLauncher = requireNonNull(externalWorkerLauncher, "externalWorkerLauncher is null");
return this;
}

public DistributedQueryRunner build()
throws Exception
{
return new DistributedQueryRunner(defaultSession, nodeCount, extraProperties, coordinatorProperties, parserOptions, environment, baseDataDir);
return new DistributedQueryRunner(defaultSession, nodeCount, extraProperties, coordinatorProperties, parserOptions, environment, baseDataDir, externalWorkerLauncher);
}
}
}

0 comments on commit f3a43a8

Please sign in to comment.