-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,009 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...ers/src/main/java/com/facebook/presto/functionNamespace/execution/rest/ForRestClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.functionNamespace.execution.rest; | ||
|
||
import com.google.inject.BindingAnnotation; | ||
|
||
import java.lang.annotation.Retention; | ||
|
||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Retention(RUNTIME) | ||
@BindingAnnotation | ||
public @interface ForRestClient | ||
{ | ||
} |
29 changes: 29 additions & 0 deletions
29
...in/java/com/facebook/presto/functionNamespace/execution/rest/RestComminicationModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* 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.functionNamespace.execution.rest; | ||
|
||
import com.google.inject.Binder; | ||
import com.google.inject.Module; | ||
|
||
import static com.facebook.airlift.http.client.HttpClientBinder.httpClientBinder; | ||
|
||
public class RestComminicationModule | ||
implements Module | ||
{ | ||
@Override | ||
public void configure(Binder binder) | ||
{ | ||
httpClientBinder(binder).bindHttpClient("rest", ForRestClient.class); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
.../com/facebook/presto/functionNamespace/execution/rest/RestSqlFunctionExecutionModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* 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.functionNamespace.execution.rest; | ||
|
||
import com.facebook.presto.functionNamespace.execution.SqlFunctionExecutionModule; | ||
import com.facebook.presto.spi.function.SqlFunctionExecutor; | ||
import com.google.inject.Binder; | ||
import com.google.inject.Scopes; | ||
|
||
public class RestSqlFunctionExecutionModule | ||
extends SqlFunctionExecutionModule | ||
{ | ||
@Override | ||
protected void setup(Binder binder) | ||
{ | ||
binder.bind(SqlFunctionExecutor.class).to(RestSqlFunctionExecutor.class).in(Scopes.SINGLETON); | ||
} | ||
} |
185 changes: 185 additions & 0 deletions
185
...in/java/com/facebook/presto/functionNamespace/execution/rest/RestSqlFunctionExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/* | ||
* 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.functionNamespace.execution.rest; | ||
|
||
import com.facebook.airlift.http.client.HttpClient; | ||
import com.facebook.airlift.http.client.HttpStatus; | ||
import com.facebook.airlift.http.client.HttpUriBuilder; | ||
import com.facebook.airlift.http.client.Request; | ||
import com.facebook.airlift.http.client.Response; | ||
import com.facebook.airlift.http.client.ResponseHandler; | ||
import com.facebook.presto.common.Page; | ||
import com.facebook.presto.common.block.BlockEncodingSerde; | ||
import com.facebook.presto.common.function.SqlFunctionResult; | ||
import com.facebook.presto.common.type.Type; | ||
import com.facebook.presto.common.type.TypeSignature; | ||
import com.facebook.presto.spi.NodeManager; | ||
import com.facebook.presto.spi.PrestoException; | ||
import com.facebook.presto.spi.function.FunctionImplementationType; | ||
import com.facebook.presto.spi.function.RemoteScalarFunctionImplementation; | ||
import com.facebook.presto.spi.function.SqlFunctionExecutor; | ||
import com.facebook.presto.spi.function.SqlFunctionHandle; | ||
import com.facebook.presto.spi.function.SqlFunctionId; | ||
import com.facebook.presto.spi.page.PagesSerde; | ||
import com.facebook.presto.spi.page.SerializedPage; | ||
import com.google.common.util.concurrent.FutureCallback; | ||
import com.google.common.util.concurrent.Futures; | ||
import io.airlift.slice.DynamicSliceOutput; | ||
import io.airlift.slice.InputStreamSliceInput; | ||
import io.airlift.slice.SliceInput; | ||
|
||
import javax.inject.Inject; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import static com.facebook.airlift.concurrent.MoreFutures.toCompletableFuture; | ||
import static com.facebook.airlift.http.client.HttpUriBuilder.uriBuilderFrom; | ||
import static com.facebook.airlift.http.client.Request.Builder.preparePost; | ||
import static com.facebook.airlift.http.client.StaticBodyGenerator.createStaticBodyGenerator; | ||
import static com.facebook.presto.spi.StandardErrorCode.FUNCTION_SERVER_FAILURE; | ||
import static com.facebook.presto.spi.StandardErrorCode.NOT_FOUND; | ||
import static com.facebook.presto.spi.function.FunctionImplementationType.REST; | ||
import static com.facebook.presto.spi.page.PagesSerdeUtil.readSerializedPage; | ||
import static com.facebook.presto.spi.page.PagesSerdeUtil.writeSerializedPage; | ||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static com.google.common.base.Preconditions.checkState; | ||
import static com.google.common.collect.ImmutableList.toImmutableList; | ||
import static com.google.common.net.HttpHeaders.ACCEPT; | ||
import static com.google.common.net.HttpHeaders.CONTENT_TYPE; | ||
import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8; | ||
import static com.google.common.util.concurrent.MoreExecutors.directExecutor; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public class RestSqlFunctionExecutor | ||
implements SqlFunctionExecutor | ||
{ | ||
private BlockEncodingSerde blockEncodingSerde; | ||
private static PagesSerde pageSerde; | ||
private HttpClient httpClient; | ||
private final NodeManager nodeManager; | ||
private final RestURIServerConfig restURIServerConfig; | ||
|
||
@Inject | ||
public RestSqlFunctionExecutor(NodeManager nodeManager, RestURIServerConfig restURIServerConfig, @ForRestClient HttpClient httpClient) | ||
{ | ||
this.nodeManager = requireNonNull(nodeManager, "nodeManager is null"); | ||
this.restURIServerConfig = requireNonNull(restURIServerConfig, "restURIServerConfig is null"); | ||
this.httpClient = requireNonNull(httpClient, "httpClient is null"); | ||
} | ||
|
||
@Override | ||
public FunctionImplementationType getImplementationType() | ||
{ | ||
return REST; | ||
} | ||
|
||
@Override | ||
public void setBlockEncodingSerde(BlockEncodingSerde blockEncodingSerde) | ||
{ | ||
checkState(this.blockEncodingSerde == null, "blockEncodingSerde already set"); | ||
this.blockEncodingSerde = requireNonNull(blockEncodingSerde, "blockEncodingSerde is null"); | ||
this.pageSerde = new PagesSerde(blockEncodingSerde, Optional.empty(), Optional.empty(), Optional.empty()); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<SqlFunctionResult> executeFunction( | ||
String source, | ||
RemoteScalarFunctionImplementation functionImplementation, | ||
Page input, | ||
List<Integer> channels, | ||
List<Type> argumentTypes, | ||
Type returnType) | ||
{ | ||
SqlFunctionHandle functionHandle = functionImplementation.getFunctionHandle(); | ||
SqlFunctionId functionId = functionHandle.getFunctionId(); | ||
DynamicSliceOutput sliceOutput = new DynamicSliceOutput((int) input.getRetainedSizeInBytes()); | ||
writeSerializedPage(sliceOutput, pageSerde.serialize(input)); | ||
try { | ||
Request request = preparePost() | ||
.setUri(getNativeWorkerUri(functionId, returnType)) | ||
.setBodyGenerator(createStaticBodyGenerator(sliceOutput.slice().byteArray())) | ||
.setHeader(CONTENT_TYPE, PLAIN_TEXT_UTF_8.toString()) | ||
.setHeader(ACCEPT, PLAIN_TEXT_UTF_8.toString()) | ||
.build(); | ||
HttpClient.HttpResponseFuture<SqlFunctionResult> future = httpClient.executeAsync(request, new SqlFunctionResultResponseHandler()); | ||
Futures.addCallback(future, new FutureCallback<SqlFunctionResult>() { | ||
@Override | ||
public void onSuccess(SqlFunctionResult result) | ||
{ | ||
result.getResult(); | ||
} | ||
|
||
@Override | ||
public void onFailure(Throwable t) | ||
{ | ||
throw new PrestoException(FUNCTION_SERVER_FAILURE, "Failed with message " + t.getMessage()); | ||
} | ||
}, directExecutor()); | ||
return toCompletableFuture(future); | ||
} | ||
catch (Exception e) { | ||
throw new IllegalStateException("Failed to get function definitions for REST server/ Native worker, " + e.getMessage()); | ||
} | ||
} | ||
|
||
private URI getNativeWorkerUri(SqlFunctionId functionId, Type returnType) | ||
{ | ||
List<String> functionArgumentTypes = functionId.getArgumentTypes().stream().map(TypeSignature::toString).collect(toImmutableList()); | ||
if (restURIServerConfig.getRestUri() == null) { | ||
throw new PrestoException(NOT_FOUND, "Failed to find native node !"); | ||
} | ||
|
||
HttpUriBuilder uri = uriBuilderFrom(URI.create(restURIServerConfig.getRestUri())) | ||
.appendPath("/v1/functions/" + functionId.getFunctionName().getObjectName()) | ||
.addParameter("returnType", returnType.toString()) | ||
.addParameter("numArgs", "" + functionArgumentTypes.size()); | ||
for (int i = 1; i <= functionArgumentTypes.size(); i++) { | ||
uri.addParameter("argType" + i, functionArgumentTypes.get(i - 1)); | ||
} | ||
return uri.build(); | ||
} | ||
|
||
public static class SqlFunctionResultResponseHandler | ||
implements ResponseHandler<SqlFunctionResult, RuntimeException> | ||
{ | ||
@Override | ||
public SqlFunctionResult handleException(Request request, Exception exception) | ||
{ | ||
throw new PrestoException(FUNCTION_SERVER_FAILURE, "Failed to get response for rest function call from function server, with exception" + exception.getMessage()); | ||
} | ||
|
||
@Override | ||
public SqlFunctionResult handle(Request request, Response response) | ||
{ | ||
if (response.getStatusCode() != 200 || response.getStatusCode() != HttpStatus.OK.code()) { | ||
throw new PrestoException(FUNCTION_SERVER_FAILURE, "Failed to get response for rest function call from function server. Response code: " + response.getStatusCode()); | ||
} | ||
try { | ||
SliceInput input = new InputStreamSliceInput(response.getInputStream()); | ||
SerializedPage serializedPage = readSerializedPage(input); | ||
Page page = pageSerde.deserialize(serializedPage); | ||
checkArgument(page.getChannelCount() == 1, "Expected only one channel in the function output"); | ||
SqlFunctionResult output = new SqlFunctionResult(page.getBlock(0), 1); | ||
return output; | ||
} | ||
catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...c/main/java/com/facebook/presto/functionNamespace/execution/rest/RestURIServerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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.functionNamespace.execution.rest; | ||
|
||
import com.facebook.airlift.configuration.Config; | ||
|
||
public class RestURIServerConfig | ||
{ | ||
private String restUri; | ||
|
||
public String getRestUri() | ||
{ | ||
return restUri; | ||
} | ||
|
||
@Config("rest-uri") | ||
public RestURIServerConfig setRestUri(String restUri) | ||
{ | ||
this.restUri = restUri; | ||
return this; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
.../presto/functionNamespace/execution/rest/SimpleAddressRestSqlFunctionExecutorsModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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.functionNamespace.execution.rest; | ||
|
||
import com.facebook.airlift.configuration.AbstractConfigurationAwareModule; | ||
import com.facebook.presto.functionNamespace.execution.SqlFunctionExecutionModule; | ||
import com.facebook.presto.functionNamespace.execution.SqlFunctionExecutors; | ||
import com.facebook.presto.spi.function.FunctionImplementationType; | ||
import com.facebook.presto.spi.function.RoutineCharacteristics.Language; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.inject.Binder; | ||
import com.google.inject.TypeLiteral; | ||
|
||
import java.util.Map; | ||
|
||
import static com.facebook.airlift.configuration.ConfigBinder.configBinder; | ||
import static com.facebook.presto.spi.function.FunctionImplementationType.REST; | ||
import static com.google.inject.Scopes.SINGLETON; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public class SimpleAddressRestSqlFunctionExecutorsModule | ||
extends AbstractConfigurationAwareModule | ||
{ | ||
private final SqlFunctionExecutionModule sqlFunctionExecutorModule; | ||
|
||
public SimpleAddressRestSqlFunctionExecutorsModule() | ||
{ | ||
this(new RestSqlFunctionExecutionModule()); | ||
} | ||
|
||
public SimpleAddressRestSqlFunctionExecutorsModule(SqlFunctionExecutionModule sqlFunctionExecutorModule) | ||
{ | ||
this.sqlFunctionExecutorModule = requireNonNull(sqlFunctionExecutorModule, "sqlFunctionExecutorModule is null"); | ||
} | ||
|
||
@Override | ||
protected void setup(Binder binder) | ||
{ | ||
configBinder(binder).bindConfig(RestURIServerConfig.class); | ||
binder.bind(RestSqlFunctionExecutor.class).in(SINGLETON); | ||
Map<Language, FunctionImplementationType> languageImplementationTypeMap = ImmutableMap.of(new Language("CPP"), REST); | ||
Map<String, FunctionImplementationType> supportedLanguages = ImmutableMap.of("CPP", REST); | ||
// for SqlFunctionExecutor | ||
sqlFunctionExecutorModule.setSupportedLanguages(supportedLanguages); | ||
install(sqlFunctionExecutorModule); | ||
// for SqlFunctionExecutors | ||
binder.bind(SqlFunctionExecutors.class).in(SINGLETON); | ||
binder.bind(new TypeLiteral<Map<Language, FunctionImplementationType>>() {}).toInstance(languageImplementationTypeMap); | ||
} | ||
} |
Oops, something went wrong.