Skip to content
Draft
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
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
<module>presto-parser</module>
<module>presto-main-base</module>
<module>presto-main</module>
<module>presto-built-in-worker-function-tools</module>
<module>presto-ml</module>
<module>presto-geospatial-toolkit</module>
<module>presto-benchmark</module>
Expand Down Expand Up @@ -815,6 +816,12 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-built-in-worker-function-tools</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-main-base</artifactId>
Expand Down
57 changes: 57 additions & 0 deletions presto-built-in-worker-function-tools/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>presto-root</artifactId>
<groupId>com.facebook.presto</groupId>
<version>0.295-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>presto-built-in-worker-function-tools</artifactId>
<name>presto-built-in-worker-function-tools</name>

<properties>
<air.main.basedir>${project.parent.basedir}</air.main.basedir>
<project.build.targetJdk>17</project.build.targetJdk>
<air.check.skip-modernizer>true</air.check.skip-modernizer>
</properties>

<dependencies>
<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-spi</artifactId>
</dependency>
<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-function-namespace-managers-common</artifactId>
</dependency>
<dependency>
<groupId>com.facebook.airlift</groupId>
<artifactId>http-client</artifactId>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>com.facebook.airlift</groupId>
<artifactId>json</artifactId>
</dependency>
<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-common</artifactId>
</dependency>
<dependency>
<groupId>com.facebook.airlift</groupId>
<artifactId>log</artifactId>
</dependency>
<dependency>
<groupId>com.facebook.airlift</groupId>
<artifactId>configuration</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.builtin.tools;

import com.google.inject.BindingAnnotation;

import java.lang.annotation.Retention;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Retention(RUNTIME)
@BindingAnnotation
public @interface ForNativeFunctionRegistryInfo
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* 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.builtin.tools;

import com.facebook.airlift.http.client.HttpClient;
import com.facebook.airlift.http.client.HttpUriBuilder;
import com.facebook.airlift.http.client.JsonResponseHandler;
import com.facebook.airlift.http.client.Request;
import com.facebook.airlift.json.JsonCodec;
import com.facebook.airlift.log.Logger;
import com.facebook.presto.functionNamespace.JsonBasedUdfFunctionMetadata;
import com.facebook.presto.functionNamespace.UdfFunctionSignatureMap;
import com.facebook.presto.spi.Node;
import com.facebook.presto.spi.NodeManager;
import com.facebook.presto.spi.PrestoException;
import com.facebook.presto.spi.StandardErrorCode;
import com.facebook.presto.spi.function.SqlFunction;
import com.google.common.collect.ImmutableMap;

import java.net.URI;
import java.util.List;
import java.util.Map;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.util.Objects.requireNonNull;

public class NativeSidecarFunctionRegistryTool
implements WorkerFunctionRegistryTool
{
private final int maxRetries;
private final long retryDelayMs;
private static final Logger log = Logger.get(NativeSidecarFunctionRegistryTool.class);
private final JsonCodec<Map<String, List<JsonBasedUdfFunctionMetadata>>> nativeFunctionSignatureMapJsonCodec;
private final NodeManager nodeManager;
private final HttpClient httpClient;
private static final String FUNCTION_SIGNATURES_ENDPOINT = "/v1/functions";

public NativeSidecarFunctionRegistryTool(
HttpClient httpClient,
JsonCodec<Map<String, List<JsonBasedUdfFunctionMetadata>>> nativeFunctionSignatureMapJsonCodec,
NodeManager nodeManager,
int nativeSidecarRegistryToolNumRetries,
long nativeSidecarRegistryToolRetryDelayMs)
{
this.nativeFunctionSignatureMapJsonCodec =
requireNonNull(nativeFunctionSignatureMapJsonCodec, "nativeFunctionSignatureMapJsonCodec is null");
this.nodeManager = requireNonNull(nodeManager, "nodeManager is null");
this.httpClient = requireNonNull(httpClient, "typeManager is null");
this.maxRetries = nativeSidecarRegistryToolNumRetries;
this.retryDelayMs = nativeSidecarRegistryToolRetryDelayMs;
}

@Override
public List<? extends SqlFunction> getWorkerFunctions()
{
return getNativeFunctionSignatureMap()
.getUDFSignatureMap()
.entrySet()
.stream()
.flatMap(entry -> entry.getValue().stream()
.map(metaInfo -> WorkerFunctionUtil.createSqlInvokedFunction(entry.getKey(), metaInfo, "presto")))
.collect(toImmutableList());
}

private UdfFunctionSignatureMap getNativeFunctionSignatureMap()
{
try {
Request request = Request.Builder.prepareGet().setUri(getSidecarLocationOnStartup()).build();
Map<String, List<JsonBasedUdfFunctionMetadata>> nativeFunctionSignatureMap = httpClient.execute(request, JsonResponseHandler.createJsonResponseHandler(nativeFunctionSignatureMapJsonCodec));
return new UdfFunctionSignatureMap(ImmutableMap.copyOf(nativeFunctionSignatureMap));
}
catch (Exception e) {
throw new PrestoException(StandardErrorCode.INVALID_ARGUMENTS, "Failed to get functions from sidecar.", e);
}
}

private URI getSidecarLocationOnStartup()
{
Node sidecarNode = null;
for (int attempt = 1; attempt <= maxRetries; attempt++) {
try {
sidecarNode = nodeManager.getSidecarNode();
if (sidecarNode != null) {
break;
}
}
catch (Exception e) {
log.error("Error getting sidecar node (attempt " + attempt + "): " + e.getMessage());
if (attempt == maxRetries) {
throw new RuntimeException("Failed to get sidecar node", e);
}
else {
try {
Thread.sleep(retryDelayMs);
}
catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new RuntimeException("Retry fetching sidecar function registry interrupted", ie);
}
}
}
}

return HttpUriBuilder
.uriBuilderFrom(sidecarNode.getHttpUri())
.appendPath(FUNCTION_SIGNATURES_ENDPOINT)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.builtin.tools;

import com.facebook.airlift.configuration.Config;
import com.facebook.airlift.configuration.ConfigDescription;

import java.time.Duration;

public class NativeSidecarRegistryToolConfig
{
private int nativeSidecarRegistryToolNumRetries = 8;
private long nativeSidecarRegistryToolRetryDelayMs = Duration.ofMinutes(1).toMillis();

public int getNativeSidecarRegistryToolNumRetries()
{
return nativeSidecarRegistryToolNumRetries;
}

@Config("native-sidecar-registry-tool-num-retries")
@ConfigDescription("Max times to retry fetching sidecar node")
public NativeSidecarRegistryToolConfig setNativeSidecarRegistryToolNumRetries(int nativeSidecarRegistryToolNumRetries)
{
this.nativeSidecarRegistryToolNumRetries = nativeSidecarRegistryToolNumRetries;
return this;
}

public long getNativeSidecarRegistryToolRetryDelayMs()
{
return nativeSidecarRegistryToolRetryDelayMs;
}

@Config("native-sidecar-registry-tool-retry-delay-ms")
@ConfigDescription("Cooldown period to retry when fetching sidecar node fails")
public NativeSidecarRegistryToolConfig setNativeSidecarRegistryToolRetryDelayMs(int nativeSidecarRegistryToolRetryDelayMs)
{
this.nativeSidecarRegistryToolRetryDelayMs = nativeSidecarRegistryToolRetryDelayMs;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.builtin.tools;

import com.facebook.presto.spi.function.SqlFunction;

import java.util.List;

public interface WorkerFunctionRegistryTool
{
List<? extends SqlFunction> getWorkerFunctions();
}
Loading
Loading