Search in sources :

Example 1 with NotificationFeedNotFoundException

use of co.cask.cdap.notifications.feeds.NotificationFeedNotFoundException in project cdap by caskdata.

the class MessagingNotificationService method subscribe.

@Override
public <N> Cancellable subscribe(NotificationFeedId feed, NotificationHandler<N> handler, Executor executor) throws NotificationFeedNotFoundException, NotificationFeedException {
    Cancellable subscribeCancellable = super.subscribe(feed, handler, executor);
    // If already has a thread fetching, just return the cancellable.
    if (!needFetch.compareAndSet(false, true)) {
        return subscribeCancellable;
    }
    // Start fetching
    subscribeExecutor.execute(new Runnable() {

        private final long startTime = System.currentTimeMillis();

        private final RetryStrategy scheduleStrategy = RetryStrategies.exponentialDelay(100, 3000, TimeUnit.MILLISECONDS);

        private byte[] messageId;

        private int emptyFetchCount;

        @Override
        public void run() {
            try {
                MessageFetcher fetcher = messagingService.prepareFetch(notificationTopic);
                if (messageId == null) {
                    fetcher.setStartTime(startTime);
                } else {
                    fetcher.setStartMessage(messageId, false);
                }
                emptyFetchCount++;
                try (CloseableIterator<RawMessage> iterator = fetcher.fetch()) {
                    while (iterator.hasNext()) {
                        emptyFetchCount = 0;
                        RawMessage rawMessage = iterator.next();
                        NotificationMessage message = GSON.fromJson(new String(rawMessage.getPayload(), StandardCharsets.UTF_8), NotificationMessage.class);
                        try {
                            LOG.trace("Decoded notification: {}", message);
                            notificationReceived(message.getFeedId(), message.getNotificationJson());
                        } catch (Throwable t) {
                            LOG.warn("Error while processing notification {} with handler {}", message, t);
                        }
                        messageId = rawMessage.getId();
                    }
                }
            } catch (Exception e) {
                LOG.error("Failed to get notification", e);
            }
            // Back-off if it was empty fetch.
            if (emptyFetchCount > 0) {
                // Schedule the next fetch. Exponential strategy doesn't use the time component,
                // so doesn't matter what we passed in
                subscribeExecutor.schedule(this, scheduleStrategy.nextRetry(emptyFetchCount, startTime), TimeUnit.MILLISECONDS);
            } else {
                subscribeExecutor.execute(this);
            }
        }
    });
    return subscribeCancellable;
}
Also used : MessageFetcher(co.cask.cdap.messaging.MessageFetcher) CloseableIterator(co.cask.cdap.api.dataset.lib.CloseableIterator) Cancellable(org.apache.twill.common.Cancellable) NotificationFeedNotFoundException(co.cask.cdap.notifications.feeds.NotificationFeedNotFoundException) NotificationException(co.cask.cdap.notifications.service.NotificationException) NotificationFeedException(co.cask.cdap.notifications.feeds.NotificationFeedException) RawMessage(co.cask.cdap.messaging.data.RawMessage) RetryStrategy(co.cask.cdap.common.service.RetryStrategy)

Example 2 with NotificationFeedNotFoundException

use of co.cask.cdap.notifications.feeds.NotificationFeedNotFoundException in project cdap by caskdata.

the class RemoteNotificationFeedManager method deleteFeed.

@Override
public void deleteFeed(NotificationFeedId feed) throws NotificationFeedNotFoundException, NotificationFeedException {
    String path = String.format("namespaces/%s/feeds/categories/%s/names/%s", feed.getNamespace(), feed.getCategory(), feed.getFeed());
    HttpResponse response = execute(remoteClient.requestBuilder(HttpMethod.DELETE, path).build());
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotificationFeedNotFoundException(feed);
    } else if (response.getResponseCode() != HttpURLConnection.HTTP_OK) {
        throw new NotificationFeedException("Cannot delete notification feed. Reason: " + response);
    }
}
Also used : NotificationFeedException(co.cask.cdap.notifications.feeds.NotificationFeedException) NotificationFeedNotFoundException(co.cask.cdap.notifications.feeds.NotificationFeedNotFoundException) HttpResponse(co.cask.common.http.HttpResponse)

Example 3 with NotificationFeedNotFoundException

use of co.cask.cdap.notifications.feeds.NotificationFeedNotFoundException in project cdap by caskdata.

the class DistributedStreamService method createHeartbeatsFeed.

/**
 * Create Notification feed for stream's heartbeats, if it does not already exist.
 */
private void createHeartbeatsFeed() throws NotificationFeedException {
    NotificationFeedInfo streamHeartbeatsFeed = new NotificationFeedInfo(NamespaceId.SYSTEM.getEntityName(), Constants.Notification.Stream.STREAM_INTERNAL_FEED_CATEGORY, Constants.Notification.Stream.STREAM_HEARTBEAT_FEED_NAME, "Stream heartbeats feed.");
    LOG.debug("Ensuring Stream HeartbeatsFeed exists.");
    boolean isRetry = false;
    while (true) {
        try {
            feedManager.getFeed(streamHeartbeatsFeed);
            LOG.debug("Stream HeartbeatsFeed exists.");
            return;
        } catch (NotificationFeedNotFoundException notFoundException) {
            if (!isRetry) {
                LOG.debug("Creating Stream HeartbeatsFeed.");
            }
            feedManager.createFeed(streamHeartbeatsFeed);
            LOG.info("Stream HeartbeatsFeed created.");
            return;
        } catch (NotificationFeedException | RetryableException e) {
            if (!isRetry) {
                LOG.warn("Could not ensure existence of HeartbeatsFeed. Will retry until successful. " + "Retry failures will be logged at debug level.", e);
            } else {
                LOG.debug("Could not ensure existence of HeartbeatsFeed. Will retry until successful.", e);
            }
            isRetry = true;
            waitBeforeRetryHeartbeatsFeedOperation();
        }
    }
}
Also used : NotificationFeedException(co.cask.cdap.notifications.feeds.NotificationFeedException) RetryableException(co.cask.cdap.api.retry.RetryableException) NotificationFeedNotFoundException(co.cask.cdap.notifications.feeds.NotificationFeedNotFoundException) NotificationFeedInfo(co.cask.cdap.proto.notification.NotificationFeedInfo)

Example 4 with NotificationFeedNotFoundException

use of co.cask.cdap.notifications.feeds.NotificationFeedNotFoundException in project cdap by caskdata.

the class NotificationFeedHttpHandler method deleteFeed.

@DELETE
@Path("/feeds/categories/{feed-category}/names/{feed-name}")
public void deleteFeed(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("feed-category") String category, @PathParam("feed-name") String name) {
    try {
        NotificationFeedId feed;
        try {
            feed = new NotificationFeedId(namespaceId, category, name);
        } catch (IllegalArgumentException e) {
            responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
            return;
        }
        feedManager.deleteFeed(feed);
        responder.sendStatus(HttpResponseStatus.OK);
    } catch (NotificationFeedNotFoundException e) {
        responder.sendStatus(HttpResponseStatus.NOT_FOUND);
    } catch (NotificationFeedException e) {
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, String.format("Could not delete Notification Feed. %s", e.getMessage()));
    } catch (Throwable t) {
        LOG.debug("Error in deleting notification feed.", t);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, t.getMessage());
    }
}
Also used : NotificationFeedException(co.cask.cdap.notifications.feeds.NotificationFeedException) NotificationFeedId(co.cask.cdap.proto.id.NotificationFeedId) NotificationFeedNotFoundException(co.cask.cdap.notifications.feeds.NotificationFeedNotFoundException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 5 with NotificationFeedNotFoundException

use of co.cask.cdap.notifications.feeds.NotificationFeedNotFoundException in project cdap by caskdata.

the class RemoteNotificationFeedManager method getFeed.

@Override
public NotificationFeedInfo getFeed(NotificationFeedId feed) throws NotificationFeedNotFoundException, NotificationFeedException {
    String path = String.format("namespaces/%s/feeds/categories/%s/names/%s", feed.getNamespace(), feed.getCategory(), feed.getFeed());
    HttpResponse response = execute(remoteClient.requestBuilder(HttpMethod.GET, path).build());
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotificationFeedNotFoundException(feed);
    } else if (response.getResponseCode() != HttpURLConnection.HTTP_OK) {
        throw new NotificationFeedException("Cannot get notification feed. Reason: " + response);
    }
    return ObjectResponse.fromJsonBody(response, NotificationFeedInfo.class).getResponseObject();
}
Also used : NotificationFeedException(co.cask.cdap.notifications.feeds.NotificationFeedException) NotificationFeedNotFoundException(co.cask.cdap.notifications.feeds.NotificationFeedNotFoundException) HttpResponse(co.cask.common.http.HttpResponse) NotificationFeedInfo(co.cask.cdap.proto.notification.NotificationFeedInfo)

Aggregations

NotificationFeedException (co.cask.cdap.notifications.feeds.NotificationFeedException)6 NotificationFeedNotFoundException (co.cask.cdap.notifications.feeds.NotificationFeedNotFoundException)6 NotificationFeedId (co.cask.cdap.proto.id.NotificationFeedId)2 NotificationFeedInfo (co.cask.cdap.proto.notification.NotificationFeedInfo)2 HttpResponse (co.cask.common.http.HttpResponse)2 Path (javax.ws.rs.Path)2 CloseableIterator (co.cask.cdap.api.dataset.lib.CloseableIterator)1 RetryableException (co.cask.cdap.api.retry.RetryableException)1 RetryStrategy (co.cask.cdap.common.service.RetryStrategy)1 MessageFetcher (co.cask.cdap.messaging.MessageFetcher)1 RawMessage (co.cask.cdap.messaging.data.RawMessage)1 NotificationException (co.cask.cdap.notifications.service.NotificationException)1 DELETE (javax.ws.rs.DELETE)1 GET (javax.ws.rs.GET)1 Cancellable (org.apache.twill.common.Cancellable)1