Search in sources :

Example 6 with Function

use of org.apache.asterix.metadata.entities.Function in project asterixdb by apache.

the class MetadataManager method getFunction.

@Override
public Function getFunction(MetadataTransactionContext ctx, FunctionSignature functionSignature) throws MetadataException {
    // First look in the context to see if this transaction created the
    // requested dataset itself (but the dataset is still uncommitted).
    Function function = ctx.getFunction(functionSignature);
    if (function != null) {
        // uncommitted.
        return function;
    }
    if (ctx.functionIsDropped(functionSignature)) {
        // in the cache.
        return null;
    }
    if (ctx.getDataverse(functionSignature.getNamespace()) != null) {
        // dataverse.
        return null;
    }
    function = cache.getFunction(functionSignature);
    if (function != null) {
        // Function is already in the cache, don't add it again.
        return function;
    }
    try {
        function = metadataNode.getFunction(ctx.getJobId(), functionSignature);
    } catch (RemoteException e) {
        throw new MetadataException(e);
    }
    // when this transaction commits.
    if (function != null) {
        ctx.addFunction(function);
    }
    return function;
}
Also used : Function(org.apache.asterix.metadata.entities.Function) RemoteException(java.rmi.RemoteException)

Example 7 with Function

use of org.apache.asterix.metadata.entities.Function in project asterixdb by apache.

the class QueryTranslator method handleDisconnectFeedStatement.

protected void handleDisconnectFeedStatement(MetadataProvider metadataProvider, Statement stmt) throws Exception {
    DisconnectFeedStatement cfs = (DisconnectFeedStatement) stmt;
    String dataverseName = getActiveDataverse(cfs.getDataverseName());
    String datasetName = cfs.getDatasetName().getValue();
    String feedName = cfs.getFeedName().getValue();
    MetadataTransactionContext mdTxnCtx = MetadataManager.INSTANCE.beginTransaction();
    metadataProvider.setMetadataTxnContext(mdTxnCtx);
    ActiveLifecycleListener activeListener = (ActiveLifecycleListener) appCtx.getActiveLifecycleListener();
    ActiveJobNotificationHandler activeEventHandler = activeListener.getNotificationHandler();
    // Check whether feed is alive
    if (activeEventHandler.getActiveEntityListener(new EntityId(Feed.EXTENSION_NAME, dataverseName, feedName)) != null) {
        throw new CompilationException(ErrorCode.FEED_CHANGE_FEED_CONNECTIVITY_ON_ALIVE_FEED, feedName);
    }
    MetadataLockManager.INSTANCE.disconnectFeedBegin(metadataProvider.getLocks(), dataverseName, dataverseName + "." + datasetName, dataverseName + "." + cfs.getFeedName());
    try {
        FeedMetadataUtil.validateIfDatasetExists(metadataProvider, dataverseName, cfs.getDatasetName().getValue(), mdTxnCtx);
        FeedMetadataUtil.validateIfFeedExists(dataverseName, cfs.getFeedName().getValue(), mdTxnCtx);
        FeedConnection fc = MetadataManager.INSTANCE.getFeedConnection(metadataProvider.getMetadataTxnContext(), dataverseName, feedName, datasetName);
        if (fc == null) {
            throw new CompilationException("Feed " + feedName + " is currently not connected to " + cfs.getDatasetName().getValue() + ". Invalid operation!");
        }
        MetadataManager.INSTANCE.dropFeedConnection(mdTxnCtx, dataverseName, feedName, datasetName);
        for (FunctionSignature functionSignature : fc.getAppliedFunctions()) {
            Function function = MetadataManager.INSTANCE.getFunction(mdTxnCtx, functionSignature);
            function.dereference();
            MetadataManager.INSTANCE.updateFunction(mdTxnCtx, function);
        }
        MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
    } catch (Exception e) {
        abort(e, e, mdTxnCtx);
        throw e;
    } finally {
        metadataProvider.getLocks().unlock();
    }
}
Also used : EntityId(org.apache.asterix.active.EntityId) CompilationException(org.apache.asterix.common.exceptions.CompilationException) Function(org.apache.asterix.metadata.entities.Function) ActiveLifecycleListener(org.apache.asterix.active.ActiveLifecycleListener) FeedConnection(org.apache.asterix.metadata.entities.FeedConnection) DisconnectFeedStatement(org.apache.asterix.lang.common.statement.DisconnectFeedStatement) MetadataTransactionContext(org.apache.asterix.metadata.MetadataTransactionContext) ActiveJobNotificationHandler(org.apache.asterix.active.ActiveJobNotificationHandler) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) ACIDException(org.apache.asterix.common.exceptions.ACIDException) MetadataException(org.apache.asterix.metadata.MetadataException) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) CompilationException(org.apache.asterix.common.exceptions.CompilationException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) AsterixException(org.apache.asterix.common.exceptions.AsterixException)

Example 8 with Function

use of org.apache.asterix.metadata.entities.Function in project asterixdb by apache.

the class QueryTranslator method handleConnectFeedStatement.

private void handleConnectFeedStatement(MetadataProvider metadataProvider, Statement stmt) throws Exception {
    FeedConnection fc;
    ConnectFeedStatement cfs = (ConnectFeedStatement) stmt;
    String dataverseName = getActiveDataverse(cfs.getDataverseName());
    String feedName = cfs.getFeedName();
    String datasetName = cfs.getDatasetName().getValue();
    String policyName = cfs.getPolicy();
    MetadataTransactionContext mdTxnCtx = MetadataManager.INSTANCE.beginTransaction();
    metadataProvider.setMetadataTxnContext(mdTxnCtx);
    // Check whether feed is alive
    ActiveLifecycleListener activeListener = (ActiveLifecycleListener) appCtx.getActiveLifecycleListener();
    ActiveJobNotificationHandler activeEventHandler = activeListener.getNotificationHandler();
    if (activeEventHandler.getActiveEntityListener(new EntityId(Feed.EXTENSION_NAME, dataverseName, feedName)) != null) {
        throw new CompilationException(ErrorCode.FEED_CHANGE_FEED_CONNECTIVITY_ON_ALIVE_FEED, feedName);
    }
    // Transaction handling
    MetadataLockManager.INSTANCE.connectFeedBegin(metadataProvider.getLocks(), dataverseName, dataverseName + "." + datasetName, dataverseName + "." + feedName);
    try {
        // validation
        FeedMetadataUtil.validateIfDatasetExists(metadataProvider, dataverseName, datasetName, mdTxnCtx);
        Feed feed = FeedMetadataUtil.validateIfFeedExists(dataverseName, feedName, metadataProvider.getMetadataTxnContext());
        ARecordType outputType = FeedMetadataUtil.getOutputType(feed, feed.getAdapterConfiguration(), ExternalDataConstants.KEY_TYPE_NAME);
        List<FunctionSignature> appliedFunctions = cfs.getAppliedFunctions();
        for (FunctionSignature func : appliedFunctions) {
            if (MetadataManager.INSTANCE.getFunction(mdTxnCtx, func) == null) {
                throw new CompilationException(ErrorCode.FEED_CONNECT_FEED_APPLIED_INVALID_FUNCTION, func.getName());
            }
        }
        fc = MetadataManager.INSTANCE.getFeedConnection(metadataProvider.getMetadataTxnContext(), dataverseName, feedName, datasetName);
        if (fc != null) {
            throw new AlgebricksException("Feed" + feedName + " is already connected dataset " + datasetName);
        }
        fc = new FeedConnection(dataverseName, feedName, datasetName, appliedFunctions, policyName, outputType.toString());
        MetadataManager.INSTANCE.addFeedConnection(metadataProvider.getMetadataTxnContext(), fc);
        // Increase function reference count.
        for (FunctionSignature funcSig : appliedFunctions) {
            // The function should be cached in Metadata manager, so this operation is not that expensive.
            Function func = MetadataManager.INSTANCE.getFunction(mdTxnCtx, funcSig);
            func.reference();
            MetadataManager.INSTANCE.updateFunction(mdTxnCtx, func);
        }
        MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
    } catch (Exception e) {
        abort(e, e, mdTxnCtx);
        throw e;
    } finally {
        metadataProvider.getLocks().unlock();
    }
}
Also used : CompilationException(org.apache.asterix.common.exceptions.CompilationException) FeedConnection(org.apache.asterix.metadata.entities.FeedConnection) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) MetadataTransactionContext(org.apache.asterix.metadata.MetadataTransactionContext) ConnectFeedStatement(org.apache.asterix.lang.common.statement.ConnectFeedStatement) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) ACIDException(org.apache.asterix.common.exceptions.ACIDException) MetadataException(org.apache.asterix.metadata.MetadataException) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) CompilationException(org.apache.asterix.common.exceptions.CompilationException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) AsterixException(org.apache.asterix.common.exceptions.AsterixException) EntityId(org.apache.asterix.active.EntityId) Function(org.apache.asterix.metadata.entities.Function) ActiveLifecycleListener(org.apache.asterix.active.ActiveLifecycleListener) ActiveJobNotificationHandler(org.apache.asterix.active.ActiveJobNotificationHandler) ARecordType(org.apache.asterix.om.types.ARecordType) Feed(org.apache.asterix.metadata.entities.Feed)

Example 9 with Function

use of org.apache.asterix.metadata.entities.Function in project asterixdb by apache.

the class ExternalLibraryUtils method installLibraryIfNeeded.

/**
     * Each element of a library is installed as part of a transaction. Any
     * failure in installing an element does not effect installation of other
     * libraries.
     */
protected static void installLibraryIfNeeded(String dataverse, final File libraryDir, Map<String, List<String>> uninstalledLibs) throws Exception {
    String libraryName = libraryDir.getName().trim();
    List<String> uninstalledLibsInDv = uninstalledLibs.get(dataverse);
    // was this library just un-installed?
    boolean wasUninstalled = uninstalledLibsInDv != null && uninstalledLibsInDv.contains(libraryName);
    MetadataTransactionContext mdTxnCtx = null;
    try {
        mdTxnCtx = MetadataManager.INSTANCE.beginTransaction();
        Library libraryInMetadata = MetadataManager.INSTANCE.getLibrary(mdTxnCtx, dataverse, libraryName);
        if (libraryInMetadata != null && !wasUninstalled) {
            // exists in metadata and was not un-installed, we return.
            // Another place which shows that our metadata transactions are broken
            // (we didn't call commit before!!!)
            MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
            return;
        }
        // Add library
        MetadataManager.INSTANCE.addLibrary(mdTxnCtx, new Library(dataverse, libraryName));
        if (LOGGER.isLoggable(Level.INFO)) {
            LOGGER.info("Added library " + libraryName + " to Metadata");
        }
        // Get the descriptor
        String[] libraryDescriptors = libraryDir.list((dir, name) -> name.endsWith(".xml"));
        if (libraryDescriptors == null) {
            throw new IOException("Unable to list files in directory " + libraryDir);
        }
        if (libraryDescriptors.length == 0) {
            // should be fine. library was installed but its content was not added to metadata
            MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
            return;
        } else if (libraryDescriptors.length > 1) {
            throw new IllegalStateException("More than 1 library descriptors defined");
        }
        ExternalLibrary library = getLibrary(new File(libraryDir + File.separator + libraryDescriptors[0]));
        // Get the dataverse
        Dataverse dv = MetadataManager.INSTANCE.getDataverse(mdTxnCtx, dataverse);
        if (dv == null) {
            MetadataManager.INSTANCE.addDataverse(mdTxnCtx, new Dataverse(dataverse, NonTaggedDataFormat.NON_TAGGED_DATA_FORMAT, MetadataUtil.PENDING_NO_OP));
        }
        // Add functions
        if (library.getLibraryFunctions() != null) {
            for (LibraryFunction function : library.getLibraryFunctions().getLibraryFunction()) {
                String[] fargs = function.getArguments().trim().split(",");
                List<String> args = new ArrayList<>();
                for (String arg : fargs) {
                    args.add(arg);
                }
                Function f = new Function(dataverse, libraryName + "#" + function.getName().trim(), args.size(), args, function.getReturnType().trim(), function.getDefinition().trim(), library.getLanguage().trim(), function.getFunctionType().trim(), 0);
                MetadataManager.INSTANCE.addFunction(mdTxnCtx, f);
                if (LOGGER.isLoggable(Level.INFO)) {
                    LOGGER.info("Installed function: " + libraryName + "#" + function.getName().trim());
                }
            }
        }
        if (LOGGER.isLoggable(Level.INFO)) {
            LOGGER.info("Installed functions in library :" + libraryName);
        }
        // Add adapters
        if (library.getLibraryAdapters() != null) {
            for (LibraryAdapter adapter : library.getLibraryAdapters().getLibraryAdapter()) {
                String adapterFactoryClass = adapter.getFactoryClass().trim();
                String adapterName = libraryName + "#" + adapter.getName().trim();
                AdapterIdentifier aid = new AdapterIdentifier(dataverse, adapterName);
                DatasourceAdapter dsa = new DatasourceAdapter(aid, adapterFactoryClass, IDataSourceAdapter.AdapterType.EXTERNAL);
                MetadataManager.INSTANCE.addAdapter(mdTxnCtx, dsa);
                if (LOGGER.isLoggable(Level.INFO)) {
                    LOGGER.info("Installed adapter: " + adapterName);
                }
            }
        }
        if (LOGGER.isLoggable(Level.INFO)) {
            LOGGER.info("Installed adapters in library :" + libraryName);
        }
        MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
    } catch (Exception e) {
        if (LOGGER.isLoggable(Level.SEVERE)) {
            LOGGER.log(Level.SEVERE, "Exception in installing library " + libraryName, e);
        }
        MetadataManager.INSTANCE.abortTransaction(mdTxnCtx);
    }
}
Also used : DatasourceAdapter(org.apache.asterix.metadata.entities.DatasourceAdapter) ArrayList(java.util.ArrayList) MetadataTransactionContext(org.apache.asterix.metadata.MetadataTransactionContext) IOException(java.io.IOException) Dataverse(org.apache.asterix.metadata.entities.Dataverse) LibraryFunction(org.apache.asterix.external.library.LibraryFunction) AsterixException(org.apache.asterix.common.exceptions.AsterixException) ACIDException(org.apache.asterix.common.exceptions.ACIDException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) Function(org.apache.asterix.metadata.entities.Function) LibraryFunction(org.apache.asterix.external.library.LibraryFunction) ExternalLibrary(org.apache.asterix.external.library.ExternalLibrary) AdapterIdentifier(org.apache.asterix.external.dataset.adapter.AdapterIdentifier) Library(org.apache.asterix.metadata.entities.Library) ExternalLibrary(org.apache.asterix.external.library.ExternalLibrary) LibraryAdapter(org.apache.asterix.external.library.LibraryAdapter) File(java.io.File)

Example 10 with Function

use of org.apache.asterix.metadata.entities.Function in project asterixdb by apache.

the class LangExpressionToPlanTranslator method lookupUserDefinedFunction.

private AbstractFunctionCallExpression lookupUserDefinedFunction(FunctionSignature signature, List<Mutable<ILogicalExpression>> args) throws MetadataException {
    if (signature.getNamespace() == null) {
        return null;
    }
    Function function = MetadataManager.INSTANCE.getFunction(metadataProvider.getMetadataTxnContext(), signature);
    if (function == null) {
        return null;
    }
    AbstractFunctionCallExpression f;
    if (function.getLanguage().equalsIgnoreCase(Function.LANGUAGE_JAVA)) {
        IFunctionInfo finfo = ExternalFunctionCompilerUtil.getExternalFunctionInfo(metadataProvider.getMetadataTxnContext(), function);
        f = new ScalarFunctionCallExpression(finfo, args);
    } else if (function.getLanguage().equalsIgnoreCase(Function.LANGUAGE_AQL)) {
        IFunctionInfo finfo = FunctionUtil.getFunctionInfo(signature);
        f = new ScalarFunctionCallExpression(finfo, args);
    } else {
        throw new MetadataException(" User defined functions written in " + function.getLanguage() + " are not supported");
    }
    return f;
}
Also used : Function(org.apache.asterix.metadata.entities.Function) IFunctionInfo(org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) MetadataException(org.apache.asterix.metadata.MetadataException) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)

Aggregations

Function (org.apache.asterix.metadata.entities.Function)18 FunctionSignature (org.apache.asterix.common.functions.FunctionSignature)9 ACIDException (org.apache.asterix.common.exceptions.ACIDException)8 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)8 RemoteException (java.rmi.RemoteException)7 IOException (java.io.IOException)6 AsterixException (org.apache.asterix.common.exceptions.AsterixException)6 CompilationException (org.apache.asterix.common.exceptions.CompilationException)6 MetadataException (org.apache.asterix.metadata.MetadataException)6 MetadataTransactionContext (org.apache.asterix.metadata.MetadataTransactionContext)6 ArrayList (java.util.ArrayList)4 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)4 ITupleReference (org.apache.hyracks.dataflow.common.data.accessors.ITupleReference)4 EntityId (org.apache.asterix.active.EntityId)3 DatasourceAdapter (org.apache.asterix.metadata.entities.DatasourceAdapter)3 Dataverse (org.apache.asterix.metadata.entities.Dataverse)3 Feed (org.apache.asterix.metadata.entities.Feed)3 FeedConnection (org.apache.asterix.metadata.entities.FeedConnection)3 ActiveJobNotificationHandler (org.apache.asterix.active.ActiveJobNotificationHandler)2 ActiveLifecycleListener (org.apache.asterix.active.ActiveLifecycleListener)2