Search in sources :

Example 16 with RemoteFunctionRegistry

use of org.apache.drill.exec.expr.fn.registry.RemoteFunctionRegistry in project drill by apache.

the class CreateFunctionHandler method getPlan.

/**
   * Registers UDFs dynamically. Process consists of several steps:
   * <ol>
   * <li>Registering jar in jar registry to ensure that several jars with the same name is not registered.</li>
   * <li>Binary and source jars validation and back up.</li>
   * <li>Validation against local function registry.</li>
   * <li>Validation against remote function registry.</li>
   * <li>Remote function registry update.</li>
   * <li>Copying of jars to registry area and clean up.</li>
   * </ol>
   *
   * UDFs registration is allowed only if dynamic UDFs support is enabled.
   *
   * @return - Single row indicating list of registered UDFs, or error message otherwise.
   */
@Override
public PhysicalPlan getPlan(SqlNode sqlNode) throws ForemanSetupException, IOException {
    if (!context.getOption(ExecConstants.DYNAMIC_UDF_SUPPORT_ENABLED).bool_val) {
        throw UserException.validationError().message("Dynamic UDFs support is disabled.").build(logger);
    }
    RemoteFunctionRegistry remoteRegistry = context.getRemoteFunctionRegistry();
    JarManager jarManager = new JarManager(sqlNode, remoteRegistry);
    boolean inProgress = false;
    try {
        final String action = remoteRegistry.addToJars(jarManager.getBinaryName(), RemoteFunctionRegistry.Action.REGISTRATION);
        if (!(inProgress = action == null)) {
            return DirectPlan.createDirectPlan(context, false, String.format("Jar with %s name is used. Action: %s", jarManager.getBinaryName(), action));
        }
        jarManager.initRemoteBackup();
        List<String> functions = validateAgainstLocalRegistry(jarManager, context.getFunctionRegistry());
        initRemoteRegistration(functions, jarManager, remoteRegistry);
        jarManager.deleteQuietlyFromStagingArea();
        return DirectPlan.createDirectPlan(context, true, String.format("The following UDFs in jar %s have been registered:\n%s", jarManager.getBinaryName(), functions));
    } catch (Exception e) {
        logger.error("Error during UDF registration", e);
        return DirectPlan.createDirectPlan(context, false, e.getMessage());
    } finally {
        if (inProgress) {
            remoteRegistry.removeFromJars(jarManager.getBinaryName());
        }
        jarManager.cleanUp();
    }
}
Also used : RemoteFunctionRegistry(org.apache.drill.exec.expr.fn.registry.RemoteFunctionRegistry) UserException(org.apache.drill.common.exceptions.UserException) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) FunctionValidationException(org.apache.drill.exec.exception.FunctionValidationException) JarValidationException(org.apache.drill.exec.exception.JarValidationException) IOException(java.io.IOException) ForemanSetupException(org.apache.drill.exec.work.foreman.ForemanSetupException) VersionMismatchException(org.apache.drill.exec.exception.VersionMismatchException)

Aggregations

RemoteFunctionRegistry (org.apache.drill.exec.expr.fn.registry.RemoteFunctionRegistry)16 DataChangeVersion (org.apache.drill.exec.store.sys.store.DataChangeVersion)11 FunctionImplementationRegistry (org.apache.drill.exec.expr.fn.FunctionImplementationRegistry)10 Registry (org.apache.drill.exec.proto.UserBitShared.Registry)10 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)10 Test (org.junit.Test)10 Matchers.anyString (org.mockito.Matchers.anyString)10 VersionMismatchException (org.apache.drill.exec.exception.VersionMismatchException)8 LocalFunctionRegistry (org.apache.drill.exec.expr.fn.registry.LocalFunctionRegistry)8 FileSystem (org.apache.hadoop.fs.FileSystem)7 Path (org.apache.hadoop.fs.Path)6 Jar (org.apache.drill.exec.proto.UserBitShared.Jar)5 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 IOException (java.io.IOException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 UserException (org.apache.drill.common.exceptions.UserException)3 ForemanSetupException (org.apache.drill.exec.work.foreman.ForemanSetupException)3 FunctionValidationException (org.apache.drill.exec.exception.FunctionValidationException)2 JarValidationException (org.apache.drill.exec.exception.JarValidationException)2