use of io.cdap.cdap.proto.Notification in project cdap by cdapio.
the class MessagingProgramStatePublisher method publish.
public void publish(Notification.Type notificationType, Map<String, String> properties) {
// ProgramRunId is always required in a notification
Notification programStatusNotification = new Notification(notificationType, properties);
int failureCount = 0;
long startTime = -1L;
boolean done = false;
// This should be refactored into a common class for publishing to TMS with a retry strategy
while (!done) {
try {
messagingService.publish(StoreRequestBuilder.of(topicId).addPayload(GSON.toJson(programStatusNotification)).build());
LOG.trace("Published program status notification: {}", programStatusNotification);
done = true;
} catch (IOException | AccessException e) {
throw Throwables.propagate(e);
} catch (TopicNotFoundException | ServiceUnavailableException e) {
// These exceptions are retry-able due to TMS not completely started
if (startTime < 0) {
startTime = System.currentTimeMillis();
}
long retryMillis = retryStrategy.nextRetry(++failureCount, startTime);
if (retryMillis < 0) {
LOG.error("Failed to publish messages to TMS and exceeded retry limit.", e);
throw Throwables.propagate(e);
}
LOG.debug("Failed to publish messages to TMS due to {}. Will be retried in {} ms.", e.getMessage(), retryMillis);
try {
TimeUnit.MILLISECONDS.sleep(retryMillis);
} catch (InterruptedException e1) {
// Something explicitly stopping this thread. Simply just break and reset the interrupt flag.
LOG.warn("Publishing message to TMS interrupted.");
Thread.currentThread().interrupt();
done = true;
}
}
}
}
use of io.cdap.cdap.proto.Notification in project cdap by cdapio.
the class RuntimeProgramStatusSubscriberService method processMessages.
@Override
protected void processMessages(StructuredTableContext context, Iterator<ImmutablePair<String, Notification>> messages) throws Exception {
while (messages.hasNext()) {
ImmutablePair<String, Notification> pair = messages.next();
Notification notification = pair.getSecond();
if (notification.getNotificationType() != Notification.Type.PROGRAM_STATUS) {
continue;
}
processNotification(pair.getFirst().getBytes(StandardCharsets.UTF_8), notification, getAppMetadataStore(context));
}
}
use of io.cdap.cdap.proto.Notification in project cdap by cdapio.
the class ProgramStatusEventPublisherTest method provideMockedMessages.
private Iterator<ImmutablePair<String, Notification>> provideMockedMessages() {
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
InputStream notificationIS = classLoader.getResourceAsStream(MOCKED_NOTIFICATION_FILENAME);
Assert.assertNotNull(notificationIS);
String notificationJson = new BufferedReader(new InputStreamReader(notificationIS)).lines().collect(Collectors.joining(System.lineSeparator()));
Notification notification = GSON.fromJson(notificationJson, Notification.class);
ImmutablePair<String, Notification> message = new ImmutablePair<>("test", notification);
List<ImmutablePair<String, Notification>> messageList = new ArrayList<>();
messageList.add(message);
return messageList.iterator();
}
use of io.cdap.cdap.proto.Notification in project cdap by cdapio.
the class RuntimeClientServiceTest method testBasicRelay.
@Test
public void testBasicRelay() throws Exception {
// Send some messages to multiple topics in the client side TMS, they should get replicated to the server side TMS.
MessagingContext messagingContext = new MultiThreadMessagingContext(clientMessagingService);
MessagePublisher messagePublisher = messagingContext.getDirectMessagePublisher();
ProgramStateWriter programStateWriter = new MessagingProgramStateWriter(clientCConf, clientMessagingService);
for (Map.Entry<String, String> entry : topicConfigs.entrySet()) {
// the RuntimeClientService will decode it to watch for program termination
if (entry.getKey().equals(Constants.AppFabric.PROGRAM_STATUS_EVENT_TOPIC)) {
// Write a non-terminal state to test basic relaying
programStateWriter.running(PROGRAM_RUN_ID, null);
} else {
messagePublisher.publish(NamespaceId.SYSTEM.getNamespace(), entry.getValue(), entry.getKey(), entry.getKey());
}
}
MessagingContext serverMessagingContext = new MultiThreadMessagingContext(messagingService);
for (Map.Entry<String, String> entry : topicConfigs.entrySet()) {
if (entry.getKey().equals(Constants.AppFabric.PROGRAM_STATUS_EVENT_TOPIC)) {
// Extract the program run status from the Notification
Tasks.waitFor(Collections.singletonList(ProgramRunStatus.RUNNING), () -> fetchMessages(serverMessagingContext, entry.getValue(), 10, null).stream().map(Message::getPayloadAsString).map(s -> GSON.fromJson(s, Notification.class)).map(n -> n.getProperties().get(ProgramOptionConstants.PROGRAM_STATUS)).map(ProgramRunStatus::valueOf).collect(Collectors.toList()), 5, TimeUnit.SECONDS);
} else {
Tasks.waitFor(Arrays.asList(entry.getKey(), entry.getKey()), () -> fetchMessages(serverMessagingContext, entry.getValue(), 10, null).stream().map(Message::getPayloadAsString).collect(Collectors.toList()), 5, TimeUnit.SECONDS);
}
}
// Writes a program terminate message to unblock stopping of the client service
programStateWriter.completed(PROGRAM_RUN_ID);
}
use of io.cdap.cdap.proto.Notification in project cdap by cdapio.
the class PartitionTrigger method getPartitionsCount.
private int getPartitionsCount(List<Notification> notifications) {
int partitionsCount = 0;
for (Notification notification : notifications) {
if (!notification.getNotificationType().equals(Notification.Type.PARTITION)) {
continue;
}
String datasetId = notification.getProperties().get(Notification.DATASET_ID);
if (!dataset.toString().equals(datasetId)) {
continue;
}
String numPartitionsString = notification.getProperties().get(Notification.NUM_PARTITIONS);
if (numPartitionsString != null) {
partitionsCount += Integer.parseInt(numPartitionsString);
}
}
return partitionsCount;
}
Aggregations