Search in sources :

Example 41 with Notification

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;
            }
        }
    }
}
Also used : AccessException(io.cdap.cdap.api.security.AccessException) TopicNotFoundException(io.cdap.cdap.api.messaging.TopicNotFoundException) IOException(java.io.IOException) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) Notification(io.cdap.cdap.proto.Notification)

Example 42 with Notification

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));
    }
}
Also used : Notification(io.cdap.cdap.proto.Notification)

Example 43 with Notification

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();
}
Also used : InputStreamReader(java.io.InputStreamReader) ImmutablePair(io.cdap.cdap.common.utils.ImmutablePair) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) Notification(io.cdap.cdap.proto.Notification)

Example 44 with Notification

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);
}
Also used : MessagingServerRuntimeModule(io.cdap.cdap.messaging.guice.MessagingServerRuntimeModule) Arrays(java.util.Arrays) ConfigModule(io.cdap.cdap.common.guice.ConfigModule) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Notification(io.cdap.cdap.proto.Notification) Spliterators(java.util.Spliterators) TimeoutException(java.util.concurrent.TimeoutException) MessageFetcher(io.cdap.cdap.api.messaging.MessageFetcher) ProgramStateWriter(io.cdap.cdap.app.runtime.ProgramStateWriter) Gson(com.google.gson.Gson) RuntimeServerModule(io.cdap.cdap.app.guice.RuntimeServerModule) After(org.junit.After) Map(java.util.Map) ClassRule(org.junit.ClassRule) NoOpMetricsCollectionService(io.cdap.cdap.common.metrics.NoOpMetricsCollectionService) Tasks(io.cdap.cdap.common.utils.Tasks) MessagingService(io.cdap.cdap.messaging.MessagingService) ProgramRunStatus(io.cdap.cdap.proto.ProgramRunStatus) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) MultiThreadMessagingContext(io.cdap.cdap.messaging.context.MultiThreadMessagingContext) Constants(io.cdap.cdap.common.conf.Constants) MessagingContext(io.cdap.cdap.api.messaging.MessagingContext) ProgramOptionConstants(io.cdap.cdap.internal.app.runtime.ProgramOptionConstants) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) InMemoryDiscoveryService(org.apache.twill.discovery.InMemoryDiscoveryService) LocalLocationModule(io.cdap.cdap.common.guice.LocalLocationModule) MessagingProgramStateWriter(io.cdap.cdap.internal.app.program.MessagingProgramStateWriter) RemoteAuthenticatorModules(io.cdap.cdap.common.guice.RemoteAuthenticatorModules) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) StreamSupport(java.util.stream.StreamSupport) Nullable(javax.annotation.Nullable) Before(org.junit.Before) DiscoveryService(org.apache.twill.discovery.DiscoveryService) Message(io.cdap.cdap.api.messaging.Message) RunIds(io.cdap.cdap.common.app.RunIds) Test(org.junit.Test) IOException(java.io.IOException) CloseableIterator(io.cdap.cdap.api.dataset.lib.CloseableIterator) TopicNotFoundException(io.cdap.cdap.api.messaging.TopicNotFoundException) Service(com.google.common.util.concurrent.Service) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) Injector(com.google.inject.Injector) TimeUnit(java.util.concurrent.TimeUnit) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) MessagePublisher(io.cdap.cdap.api.messaging.MessagePublisher) Guice(com.google.inject.Guice) Assert(org.junit.Assert) Collections(java.util.Collections) AuthenticationContextModules(io.cdap.cdap.security.auth.context.AuthenticationContextModules) TemporaryFolder(org.junit.rules.TemporaryFolder) AbstractModule(com.google.inject.AbstractModule) ProgramRunStatus(io.cdap.cdap.proto.ProgramRunStatus) Message(io.cdap.cdap.api.messaging.Message) ProgramStateWriter(io.cdap.cdap.app.runtime.ProgramStateWriter) MessagingProgramStateWriter(io.cdap.cdap.internal.app.program.MessagingProgramStateWriter) MessagePublisher(io.cdap.cdap.api.messaging.MessagePublisher) MultiThreadMessagingContext(io.cdap.cdap.messaging.context.MultiThreadMessagingContext) MessagingContext(io.cdap.cdap.api.messaging.MessagingContext) MultiThreadMessagingContext(io.cdap.cdap.messaging.context.MultiThreadMessagingContext) MessagingProgramStateWriter(io.cdap.cdap.internal.app.program.MessagingProgramStateWriter) Map(java.util.Map) Notification(io.cdap.cdap.proto.Notification) Test(org.junit.Test)

Example 45 with Notification

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;
}
Also used : Notification(io.cdap.cdap.proto.Notification)

Aggregations

Notification (io.cdap.cdap.proto.Notification)52 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)14 IOException (java.io.IOException)14 Map (java.util.Map)14 Test (org.junit.Test)14 MessagingService (io.cdap.cdap.messaging.MessagingService)12 ProgramRunStatus (io.cdap.cdap.proto.ProgramRunStatus)12 Gson (com.google.gson.Gson)10 ProgramStateWriter (io.cdap.cdap.app.runtime.ProgramStateWriter)10 RunIds (io.cdap.cdap.common.app.RunIds)10 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)10 Constants (io.cdap.cdap.common.conf.Constants)10 MessagingProgramStateWriter (io.cdap.cdap.internal.app.program.MessagingProgramStateWriter)10 ProgramOptionConstants (io.cdap.cdap.internal.app.runtime.ProgramOptionConstants)10 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)10 ArrayList (java.util.ArrayList)10 Collections (java.util.Collections)10 List (java.util.List)10 TimeUnit (java.util.concurrent.TimeUnit)10 Nullable (javax.annotation.Nullable)10