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());
}
}
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);
}
}
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);
}
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();
}
}
}
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);
}
}
}
Aggregations