Search in sources :

Example 1 with NotificationFeedInfo

use of co.cask.cdap.proto.notification.NotificationFeedInfo in project cdap by caskdata.

the class NotificationFeedHttpHandlerTest method testFeedsValidFlows.

@Test
public void testFeedsValidFlows() throws Exception {
    // no feeds initially
    HttpResponse response = listFeeds(NAMESPACE);
    assertResponseCode(200, response);
    List<NotificationFeedInfo> feeds = readListResponse(response);
    Assert.assertEquals(0, feeds.size());
    try {
        // create and verify
        response = createFeed(FEED_VALID);
        assertResponseCode(200, response);
        response = listFeeds(NAMESPACE);
        feeds = readListResponse(response);
        Assert.assertEquals(1, feeds.size());
        Assert.assertEquals(FEED_VALID, feeds.get(0));
    } finally {
        // cleanup
        response = deleteFeed(NAMESPACE, CATEGORY, NAME);
        assertResponseCode(200, response);
        response = listFeeds(NAMESPACE);
        feeds = readListResponse(response);
        Assert.assertEquals(0, feeds.size());
    }
}
Also used : HttpResponse(org.apache.http.HttpResponse) NotificationFeedInfo(co.cask.cdap.proto.notification.NotificationFeedInfo) Test(org.junit.Test)

Example 2 with NotificationFeedInfo

use of co.cask.cdap.proto.notification.NotificationFeedInfo in project cdap by caskdata.

the class NotificationFeedHttpHandlerTest method testCreateDuplicate.

@Test
public void testCreateDuplicate() throws Exception {
    try {
        // prepare - create feed
        HttpResponse response = createFeed(FEED_VALID);
        assertResponseCode(200, response);
        response = getFeed(NAMESPACE, CATEGORY, NAME);
        NotificationFeedInfo feed = readGetResponse(response);
        Assert.assertEquals(FEED_VALID, feed);
        // create again with the same name
        response = createFeed(FEED_EMPTY_DESCRIPTION);
        assertResponseCode(200, response);
        // check that no updates happened
        response = getFeed(NAMESPACE, CATEGORY, NAME);
        feed = readGetResponse(response);
        Assert.assertEquals(FEED_EMPTY_DESCRIPTION, feed);
    } finally {
        // cleanup
        HttpResponse response = deleteFeed(NAMESPACE, CATEGORY, NAME);
        assertResponseCode(200, response);
    }
}
Also used : HttpResponse(org.apache.http.HttpResponse) NotificationFeedInfo(co.cask.cdap.proto.notification.NotificationFeedInfo) Test(org.junit.Test)

Example 3 with NotificationFeedInfo

use of co.cask.cdap.proto.notification.NotificationFeedInfo in project cdap by caskdata.

the class NotificationFeedHttpHandlerTest method testCreateMissingOrEmptyDescription.

@Test
public void testCreateMissingOrEmptyDescription() throws Exception {
    // create with missing description
    HttpResponse response = createFeed(FEED_MISSING_DESCRIPTION);
    assertResponseCode(200, response);
    try {
        // verify
        response = getFeed(NAMESPACE, CATEGORY, NAME);
        NotificationFeedInfo feed = readGetResponse(response);
        Assert.assertEquals(FEED_MISSING_DESCRIPTION, feed);
        // cleanup
        response = deleteFeed(NAMESPACE, CATEGORY, NAME);
        assertResponseCode(200, response);
        // create with empty description
        response = createFeed(FEED_EMPTY_DESCRIPTION);
        assertResponseCode(200, response);
        // verify
        response = getFeed(NAMESPACE, CATEGORY, NAME);
        feed = readGetResponse(response);
        Assert.assertEquals(FEED_EMPTY_DESCRIPTION, feed);
    } finally {
        // cleanup
        response = deleteFeed(NAMESPACE, CATEGORY, NAME);
        assertResponseCode(200, response);
    }
    response = createFeed(NAMESPACE, CATEGORY, NAME, "");
    assertResponseCode(200, response);
}
Also used : HttpResponse(org.apache.http.HttpResponse) NotificationFeedInfo(co.cask.cdap.proto.notification.NotificationFeedInfo) Test(org.junit.Test)

Example 4 with NotificationFeedInfo

use of co.cask.cdap.proto.notification.NotificationFeedInfo 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 5 with NotificationFeedInfo

use of co.cask.cdap.proto.notification.NotificationFeedInfo in project cdap by caskdata.

the class NotificationTest method testPubSub.

/**
   * Testing publishers/subscribers interaction.
   *
   * @param pubFeeds set of feeds to publish to
   * @param publishersPerFeed number of publishers doing concurrent publishing for each feed
   * @param messagesPerPublisher number of messages being published by each publisher
   * @param subFeeds set of feeds to subscribe to
   * @param subscribersPerFeed number of subscribers for each feed
   * @param payloadType Class reprenseting the data type of the payload of the notification
   * @param payloadFunction a function that transform {@link SimpleNotification} type to the payload type
   * @param <T> type of the payload
   */
private <T> void testPubSub(Set<NotificationFeedId> pubFeeds, int publishersPerFeed, final int messagesPerPublisher, Set<NotificationFeedId> subFeeds, int subscribersPerFeed, final Class<T> payloadType, final Function<SimpleNotification, T> payloadFunction) throws Exception {
    for (NotificationFeedId feedId : Sets.union(pubFeeds, subFeeds)) {
        NotificationFeedInfo feedInfo = new NotificationFeedInfo(feedId.getNamespace(), feedId.getCategory(), feedId.getFeed(), "");
        Assert.assertTrue(feedManager.createFeed(feedInfo));
    }
    try {
        int totalMessages = subFeeds.size() * publishersPerFeed * messagesPerPublisher * subscribersPerFeed;
        final CountDownLatch latch = new CountDownLatch(totalMessages);
        final Queue<T> receivedMessages = new ConcurrentLinkedQueue<>();
        List<Cancellable> cancellables = Lists.newArrayList();
        try {
            for (NotificationFeedId feedId : subFeeds) {
                for (int i = 0; i < subscribersPerFeed; i++) {
                    Cancellable cancellable = notificationService.subscribe(feedId, new NotificationHandler<T>() {

                        @Override
                        public Type getNotificationType() {
                            return payloadType;
                        }

                        @Override
                        public void received(T notification, NotificationContext notificationContext) {
                            LOG.debug("Received notification payload: {}", notification);
                            receivedMessages.offer(notification);
                            latch.countDown();
                        }
                    });
                    cancellables.add(cancellable);
                }
            }
            // Give the subscriber some time to prepare for published messages before starting the publisher
            TimeUnit.MILLISECONDS.sleep(500);
            // Starts publishers
            final Map<NotificationFeedId, Queue<T>> publishedMessages = new ConcurrentHashMap<>();
            ExecutorService executor = Executors.newFixedThreadPool(pubFeeds.size() * publishersPerFeed);
            try {
                for (final NotificationFeedId feedId : pubFeeds) {
                    final Queue<T> publishedQueue = new ConcurrentLinkedQueue<>();
                    publishedMessages.put(feedId, publishedQueue);
                    // Let all publishers start together
                    final CyclicBarrier publisherBarrier = new CyclicBarrier(publishersPerFeed);
                    for (int i = 0; i < publishersPerFeed; i++) {
                        final int publisherId = i;
                        executor.submit(new Callable<Void>() {

                            @Override
                            public Void call() throws Exception {
                                publisherBarrier.await();
                                for (int i = 0; i < messagesPerPublisher; i++) {
                                    T notification = payloadFunction.apply(new SimpleNotification(publisherId, String.format("%s-%d", feedId, i)));
                                    notificationService.publish(feedId, notification);
                                    publishedQueue.add(notification);
                                    TimeUnit.MILLISECONDS.sleep(10);
                                }
                                return null;
                            }
                        });
                    }
                }
                // Wait for subscriptions getting all messages
                Assert.assertTrue(latch.await(5000, TimeUnit.SECONDS));
            } finally {
                executor.shutdown();
            }
            // Verify the result.
            Multiset<T> received = HashMultiset.create(receivedMessages);
            Assert.assertEquals(totalMessages, received.size());
            // there should be (publisher per feed * subscriber per feed) of them
            for (NotificationFeedId feedId : subFeeds) {
                for (T notification : ImmutableMultiset.copyOf(publishedMessages.get(feedId)).elementSet()) {
                    Assert.assertEquals(publishersPerFeed * subscribersPerFeed, received.count(notification));
                }
            }
        } finally {
            for (Cancellable cancellable : cancellables) {
                cancellable.cancel();
            }
        }
    } finally {
        for (NotificationFeedId feedId : Sets.union(pubFeeds, subFeeds)) {
            feedManager.deleteFeed(feedId);
        }
    }
}
Also used : NotificationFeedId(co.cask.cdap.proto.id.NotificationFeedId) Cancellable(org.apache.twill.common.Cancellable) CountDownLatch(java.util.concurrent.CountDownLatch) NotificationFeedNotFoundException(co.cask.cdap.notifications.feeds.NotificationFeedNotFoundException) CyclicBarrier(java.util.concurrent.CyclicBarrier) NotificationContext(co.cask.cdap.notifications.service.NotificationContext) Type(java.lang.reflect.Type) ExecutorService(java.util.concurrent.ExecutorService) NotificationFeedInfo(co.cask.cdap.proto.notification.NotificationFeedInfo) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Queue(java.util.Queue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue)

Aggregations

NotificationFeedInfo (co.cask.cdap.proto.notification.NotificationFeedInfo)13 NotificationFeedException (co.cask.cdap.notifications.feeds.NotificationFeedException)6 Test (org.junit.Test)4 NotificationFeedNotFoundException (co.cask.cdap.notifications.feeds.NotificationFeedNotFoundException)3 HttpResponse (org.apache.http.HttpResponse)3 NotificationFeedId (co.cask.cdap.proto.id.NotificationFeedId)2 HttpResponse (co.cask.common.http.HttpResponse)2 Path (javax.ws.rs.Path)2 RetryableException (co.cask.cdap.api.retry.RetryableException)1 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)1 NotificationContext (co.cask.cdap.notifications.service.NotificationContext)1 NamespaceId (co.cask.cdap.proto.id.NamespaceId)1 TypeToken (com.google.common.reflect.TypeToken)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 IOException (java.io.IOException)1 Type (java.lang.reflect.Type)1