Search in sources :

Example 1 with RuntimeProgramContext

use of io.cdap.cdap.data.RuntimeProgramContext in project cdap by caskdata.

the class AbstractContext method createRuntimeProgramContext.

/**
 * Creates a new instance of {@link RuntimeProgramContext} to be
 * provided to {@link RuntimeProgramContextAware} dataset.
 */
private RuntimeProgramContext createRuntimeProgramContext(final DatasetId datasetId) {
    return new RuntimeProgramContext() {

        @Override
        public void notifyNewPartitions(Collection<? extends PartitionKey> partitionKeys) throws IOException {
            String topic = cConf.get(Constants.Dataset.DATA_EVENT_TOPIC);
            if (Strings.isNullOrEmpty(topic)) {
                // Don't publish if there is no data event topic
                return;
            }
            TopicId dataEventTopic = NamespaceId.SYSTEM.topic(topic);
            MessagePublisher publisher = getMessagingContext().getMessagePublisher();
            byte[] payload = Bytes.toBytes(GSON.toJson(Notification.forPartitions(datasetId, partitionKeys)));
            int failure = 0;
            long startTime = System.currentTimeMillis();
            while (true) {
                try {
                    publisher.publish(dataEventTopic.getNamespace(), dataEventTopic.getTopic(), payload);
                    return;
                } catch (TopicNotFoundException e) {
                    // this shouldn't happen since the TMS creates the data event topic on startup.
                    throw new IOException("Unexpected exception due to missing topic '" + dataEventTopic + "'", e);
                } catch (AccessException e) {
                    throw new IOException("Unexpected access exception during publishing notification to '" + dataEventTopic + "'", e);
                } catch (IOException e) {
                    long sleepTime = retryStrategy.nextRetry(++failure, startTime);
                    if (sleepTime < 0) {
                        throw e;
                    }
                    try {
                        TimeUnit.MILLISECONDS.sleep(sleepTime);
                    } catch (InterruptedException ex) {
                        // If interrupted during sleep, just reset the interrupt flag and return
                        Thread.currentThread().interrupt();
                        return;
                    }
                }
            }
        }

        @Override
        public ProgramRunId getProgramRunId() {
            return programRunId;
        }

        @Nullable
        @Override
        public NamespacedEntityId getComponentId() {
            return AbstractContext.this.getComponentId();
        }
    };
}
Also used : RuntimeProgramContext(io.cdap.cdap.data.RuntimeProgramContext) MessagePublisher(io.cdap.cdap.api.messaging.MessagePublisher) TopicNotFoundException(io.cdap.cdap.api.messaging.TopicNotFoundException) IOException(java.io.IOException) AccessException(io.cdap.cdap.api.security.AccessException) Collection(java.util.Collection) PartitionKey(io.cdap.cdap.api.dataset.lib.PartitionKey) TopicId(io.cdap.cdap.proto.id.TopicId)

Aggregations

PartitionKey (io.cdap.cdap.api.dataset.lib.PartitionKey)1 MessagePublisher (io.cdap.cdap.api.messaging.MessagePublisher)1 TopicNotFoundException (io.cdap.cdap.api.messaging.TopicNotFoundException)1 AccessException (io.cdap.cdap.api.security.AccessException)1 RuntimeProgramContext (io.cdap.cdap.data.RuntimeProgramContext)1 TopicId (io.cdap.cdap.proto.id.TopicId)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1