Search in sources :

Example 1 with IActiveEventSubscriber

use of org.apache.asterix.active.IActiveEventSubscriber in project asterixdb by apache.

the class QueryTranslator method handleStopFeedStatement.

private void handleStopFeedStatement(MetadataProvider metadataProvider, Statement stmt) throws Exception {
    StopFeedStatement sfst = (StopFeedStatement) stmt;
    String dataverseName = getActiveDataverse(sfst.getDataverseName());
    String feedName = sfst.getFeedName().getValue();
    EntityId feedId = new EntityId(Feed.EXTENSION_NAME, dataverseName, feedName);
    ActiveLifecycleListener activeListener = (ActiveLifecycleListener) appCtx.getActiveLifecycleListener();
    ActiveJobNotificationHandler activeEventHandler = activeListener.getNotificationHandler();
    // Obtain runtime info from ActiveListener
    FeedEventsListener listener = (FeedEventsListener) activeEventHandler.getActiveEntityListener(feedId);
    if (listener == null) {
        throw new AlgebricksException("Feed " + feedName + " is not started.");
    }
    IActiveEventSubscriber eventSubscriber = listener.subscribe(ActivityState.STOPPED);
    // Transaction
    MetadataTransactionContext mdTxnCtx = MetadataManager.INSTANCE.beginTransaction();
    metadataProvider.setMetadataTxnContext(mdTxnCtx);
    MetadataLockManager.INSTANCE.stopFeedBegin(metadataProvider.getLocks(), dataverseName, feedName);
    try {
        // validate
        FeedMetadataUtil.validateIfFeedExists(dataverseName, feedName, mdTxnCtx);
        // Construct ActiveMessage
        for (int i = 0; i < listener.getSources().length; i++) {
            String intakeLocation = listener.getSources()[i];
            FeedOperations.SendStopMessageToNode(appCtx, feedId, intakeLocation, i);
        }
        eventSubscriber.sync();
    } catch (Exception e) {
        abort(e, e, mdTxnCtx);
        throw e;
    } finally {
        metadataProvider.getLocks().unlock();
    }
}
Also used : EntityId(org.apache.asterix.active.EntityId) ActiveLifecycleListener(org.apache.asterix.active.ActiveLifecycleListener) FeedEventsListener(org.apache.asterix.external.feed.management.FeedEventsListener) IActiveEventSubscriber(org.apache.asterix.active.IActiveEventSubscriber) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) MetadataTransactionContext(org.apache.asterix.metadata.MetadataTransactionContext) StopFeedStatement(org.apache.asterix.lang.common.statement.StopFeedStatement) ActiveJobNotificationHandler(org.apache.asterix.active.ActiveJobNotificationHandler) DatasetNodegroupCardinalityHint(org.apache.asterix.metadata.dataset.hints.DatasetHints.DatasetNodegroupCardinalityHint) AlgebricksAbsolutePartitionConstraint(org.apache.hyracks.algebricks.common.constraints.AlgebricksAbsolutePartitionConstraint) 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 2 with IActiveEventSubscriber

use of org.apache.asterix.active.IActiveEventSubscriber in project asterixdb by apache.

the class QueryTranslator method handleStartFeedStatement.

private void handleStartFeedStatement(MetadataProvider metadataProvider, Statement stmt, IHyracksClientConnection hcc) throws Exception {
    StartFeedStatement sfs = (StartFeedStatement) stmt;
    String dataverseName = getActiveDataverse(sfs.getDataverseName());
    String feedName = sfs.getFeedName().getValue();
    // Transcation handler
    MetadataTransactionContext mdTxnCtx = MetadataManager.INSTANCE.beginTransaction();
    metadataProvider.setMetadataTxnContext(mdTxnCtx);
    // Runtime handler
    EntityId entityId = new EntityId(Feed.EXTENSION_NAME, dataverseName, feedName);
    // Feed & Feed Connections
    Feed feed = FeedMetadataUtil.validateIfFeedExists(dataverseName, feedName, metadataProvider.getMetadataTxnContext());
    List<FeedConnection> feedConnections = MetadataManager.INSTANCE.getFeedConections(metadataProvider.getMetadataTxnContext(), dataverseName, feedName);
    ILangCompilationProvider compilationProvider = new AqlCompilationProvider();
    IStorageComponentProvider storageComponentProvider = new StorageComponentProvider();
    DefaultStatementExecutorFactory qtFactory = new DefaultStatementExecutorFactory();
    ActiveLifecycleListener activeListener = (ActiveLifecycleListener) appCtx.getActiveLifecycleListener();
    ActiveJobNotificationHandler activeEventHandler = activeListener.getNotificationHandler();
    FeedEventsListener listener = (FeedEventsListener) activeEventHandler.getActiveEntityListener(entityId);
    if (listener != null) {
        throw new AlgebricksException("Feed " + feedName + " is started already.");
    }
    // Start
    MetadataLockManager.INSTANCE.startFeedBegin(metadataProvider.getLocks(), dataverseName, dataverseName + "." + feedName, feedConnections);
    try {
        // Prepare policy
        List<IDataset> datasets = new ArrayList<>();
        for (FeedConnection connection : feedConnections) {
            Dataset ds = metadataProvider.findDataset(connection.getDataverseName(), connection.getDatasetName());
            datasets.add(ds);
        }
        org.apache.commons.lang3.tuple.Pair<JobSpecification, AlgebricksAbsolutePartitionConstraint> jobInfo = FeedOperations.buildStartFeedJob(sessionOutput, metadataProvider, feed, feedConnections, compilationProvider, storageComponentProvider, qtFactory, hcc);
        JobSpecification feedJob = jobInfo.getLeft();
        listener = new FeedEventsListener(appCtx, entityId, datasets, jobInfo.getRight().getLocations());
        activeEventHandler.registerListener(listener);
        IActiveEventSubscriber eventSubscriber = listener.subscribe(ActivityState.STARTED);
        feedJob.setProperty(ActiveJobNotificationHandler.ACTIVE_ENTITY_PROPERTY_NAME, entityId);
        JobUtils.runJob(hcc, feedJob, Boolean.valueOf(metadataProvider.getConfig().get(StartFeedStatement.WAIT_FOR_COMPLETION)));
        eventSubscriber.sync();
        LOGGER.log(Level.INFO, "Submitted");
    } catch (Exception e) {
        abort(e, e, mdTxnCtx);
        if (listener != null) {
            activeEventHandler.unregisterListener(listener);
        }
        throw e;
    } finally {
        metadataProvider.getLocks().unlock();
    }
}
Also used : FeedEventsListener(org.apache.asterix.external.feed.management.FeedEventsListener) ArrayList(java.util.ArrayList) MetadataTransactionContext(org.apache.asterix.metadata.MetadataTransactionContext) ActiveLifecycleListener(org.apache.asterix.active.ActiveLifecycleListener) ILangCompilationProvider(org.apache.asterix.compiler.provider.ILangCompilationProvider) IDataset(org.apache.asterix.common.metadata.IDataset) JobSpecification(org.apache.hyracks.api.job.JobSpecification) ActiveJobNotificationHandler(org.apache.asterix.active.ActiveJobNotificationHandler) Feed(org.apache.asterix.metadata.entities.Feed) AqlCompilationProvider(org.apache.asterix.compiler.provider.AqlCompilationProvider) IStorageComponentProvider(org.apache.asterix.common.context.IStorageComponentProvider) FeedConnection(org.apache.asterix.metadata.entities.FeedConnection) IHyracksDataset(org.apache.hyracks.api.dataset.IHyracksDataset) IDataset(org.apache.asterix.common.metadata.IDataset) Dataset(org.apache.asterix.metadata.entities.Dataset) IActiveEventSubscriber(org.apache.asterix.active.IActiveEventSubscriber) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) IStorageComponentProvider(org.apache.asterix.common.context.IStorageComponentProvider) StorageComponentProvider(org.apache.asterix.file.StorageComponentProvider) 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) StartFeedStatement(org.apache.asterix.lang.common.statement.StartFeedStatement) AlgebricksAbsolutePartitionConstraint(org.apache.hyracks.algebricks.common.constraints.AlgebricksAbsolutePartitionConstraint)

Example 3 with IActiveEventSubscriber

use of org.apache.asterix.active.IActiveEventSubscriber in project asterixdb by apache.

the class FeedEventsListener method notifySubscribers.

private synchronized void notifySubscribers(ActiveEvent event) {
    notifyAll();
    Iterator<IActiveEventSubscriber> it = subscribers.iterator();
    while (it.hasNext()) {
        IActiveEventSubscriber subscriber = it.next();
        if (subscriber.done()) {
            it.remove();
        } else {
            subscriber.notify(event);
            if (subscriber.done()) {
                it.remove();
            }
        }
    }
}
Also used : IActiveEventSubscriber(org.apache.asterix.active.IActiveEventSubscriber)

Aggregations

IActiveEventSubscriber (org.apache.asterix.active.IActiveEventSubscriber)3 IOException (java.io.IOException)2 RemoteException (java.rmi.RemoteException)2 ActiveJobNotificationHandler (org.apache.asterix.active.ActiveJobNotificationHandler)2 ActiveLifecycleListener (org.apache.asterix.active.ActiveLifecycleListener)2 EntityId (org.apache.asterix.active.EntityId)2 ACIDException (org.apache.asterix.common.exceptions.ACIDException)2 AsterixException (org.apache.asterix.common.exceptions.AsterixException)2 CompilationException (org.apache.asterix.common.exceptions.CompilationException)2 FeedEventsListener (org.apache.asterix.external.feed.management.FeedEventsListener)2 MetadataException (org.apache.asterix.metadata.MetadataException)2 MetadataTransactionContext (org.apache.asterix.metadata.MetadataTransactionContext)2 AlgebricksAbsolutePartitionConstraint (org.apache.hyracks.algebricks.common.constraints.AlgebricksAbsolutePartitionConstraint)2 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)2 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)2 ArrayList (java.util.ArrayList)1 IStorageComponentProvider (org.apache.asterix.common.context.IStorageComponentProvider)1 IDataset (org.apache.asterix.common.metadata.IDataset)1 AqlCompilationProvider (org.apache.asterix.compiler.provider.AqlCompilationProvider)1 ILangCompilationProvider (org.apache.asterix.compiler.provider.ILangCompilationProvider)1