Search in sources :

Example 1 with TopicNotFoundException

use of io.cdap.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.

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 2 with TopicNotFoundException

use of io.cdap.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.

the class TetheringServerHandler method connectControlChannel.

/**
 * Sends control commands to the client.
 */
@GET
@Path("/tethering/controlchannels/{peer}")
public void connectControlChannel(FullHttpRequest request, HttpResponder responder, @PathParam("peer") String peer, @QueryParam("messageId") String messageId) throws IOException, NotImplementedException, PeerNotFoundException, ForbiddenException, BadRequestException {
    checkTetheringServerEnabled();
    store.updatePeerTimestamp(peer);
    TetheringStatus tetheringStatus = store.getPeer(peer).getTetheringStatus();
    if (tetheringStatus == TetheringStatus.PENDING) {
        throw new PeerNotFoundException(String.format("Peer %s not found", peer));
    } else if (tetheringStatus == TetheringStatus.REJECTED) {
        responder.sendStatus(HttpResponseStatus.FORBIDDEN);
        throw new ForbiddenException(String.format("Peer %s is not authorized", peer));
    }
    List<TetheringControlResponse> controlResponses = new ArrayList<>();
    MessageFetcher fetcher = messagingContext.getMessageFetcher();
    TopicId topic = new TopicId(NamespaceId.SYSTEM.getNamespace(), topicPrefix + peer);
    String lastMessageId = messageId;
    try (CloseableIterator<Message> iterator = fetcher.fetch(topic.getNamespace(), topic.getTopic(), 1, messageId)) {
        while (iterator.hasNext()) {
            Message message = iterator.next();
            TetheringControlMessage controlMessage = GSON.fromJson(message.getPayloadAsString(StandardCharsets.UTF_8), TetheringControlMessage.class);
            lastMessageId = message.getId();
            controlResponses.add(new TetheringControlResponse(lastMessageId, controlMessage));
        }
    } catch (TopicNotFoundException e) {
        LOG.warn("Received control connection from peer {} that's not tethered", peer);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(String.format("Invalid message id %s", messageId));
    }
    if (controlResponses.isEmpty()) {
        controlResponses.add(new TetheringControlResponse(lastMessageId, new TetheringControlMessage(TetheringControlMessage.Type.KEEPALIVE)));
    }
    responder.sendJson(HttpResponseStatus.OK, GSON.toJson(controlResponses.toArray(new TetheringControlResponse[0]), TetheringControlResponse[].class));
}
Also used : ForbiddenException(io.cdap.cdap.common.ForbiddenException) MessageFetcher(io.cdap.cdap.api.messaging.MessageFetcher) Message(io.cdap.cdap.api.messaging.Message) TopicNotFoundException(io.cdap.cdap.api.messaging.TopicNotFoundException) ArrayList(java.util.ArrayList) BadRequestException(io.cdap.cdap.common.BadRequestException) TopicId(io.cdap.cdap.proto.id.TopicId) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 3 with TopicNotFoundException

use of io.cdap.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.

the class TetheringHandler method deleteTether.

/**
 * Deletes a tether.
 */
@DELETE
@Path("/tethering/connections/{peer}")
public void deleteTether(HttpRequest request, HttpResponder responder, @PathParam("peer") String peer) throws PeerNotFoundException, IOException {
    store.deletePeer(peer);
    // Remove per-peer tethering topic if we're running on the server
    if (cConf.getBoolean(Constants.Tethering.TETHERING_SERVER_ENABLED)) {
        TopicId topic = new TopicId(NamespaceId.SYSTEM.getNamespace(), topicPrefix + peer);
        try {
            messagingService.deleteTopic(topic);
        } catch (TopicNotFoundException e) {
            LOG.info("Topic {} was not found", topic.getTopic());
        }
    }
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : TopicNotFoundException(io.cdap.cdap.api.messaging.TopicNotFoundException) TopicId(io.cdap.cdap.proto.id.TopicId) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 4 with TopicNotFoundException

use of io.cdap.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.

the class AdminEventPublisher method publishMessage.

private void publishMessage(EntityId entityId, MetadataMessage.Type type, Object payload) {
    MetadataMessage message = new MetadataMessage(type, entityId, GSON.toJsonTree(payload));
    LOG.trace("Publishing message: {}", message);
    try {
        Retries.supplyWithRetries(() -> {
            try {
                messagingContext.getMessagePublisher().publish(NamespaceId.SYSTEM.getNamespace(), topic.getTopic(), GSON.toJson(message));
            } catch (TopicNotFoundException | ServiceUnavailableException e) {
                throw new RetryableException(e);
            } catch (IOException | AccessException e) {
                throw Throwables.propagate(e);
            }
            return null;
        }, retryStrategy);
    } catch (Exception e) {
        throw new RuntimeException(String.format("Failed to publish profile metadata request for entity id %s", entityId), e);
    }
}
Also used : RetryableException(io.cdap.cdap.api.retry.RetryableException) AccessException(io.cdap.cdap.api.security.AccessException) TopicNotFoundException(io.cdap.cdap.api.messaging.TopicNotFoundException) MetadataMessage(io.cdap.cdap.data2.metadata.writer.MetadataMessage) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) IOException(java.io.IOException) AccessException(io.cdap.cdap.api.security.AccessException) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) RetryableException(io.cdap.cdap.api.retry.RetryableException) IOException(java.io.IOException) TopicNotFoundException(io.cdap.cdap.api.messaging.TopicNotFoundException)

Example 5 with TopicNotFoundException

use of io.cdap.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.

the class ProvisionerNotifier method publish.

private void publish(Map<String, String> properties) {
    final StoreRequest storeRequest = StoreRequestBuilder.of(topic).addPayload(GSON.toJson(new Notification(Notification.Type.PROGRAM_STATUS, properties))).build();
    Retries.supplyWithRetries(() -> {
        try {
            messagingService.publish(storeRequest);
        } catch (TopicNotFoundException e) {
            throw new RetryableException(e);
        } catch (IOException | AccessException e) {
            throw Throwables.propagate(e);
        }
        return null;
    }, retryStrategy);
}
Also used : RetryableException(io.cdap.cdap.api.retry.RetryableException) AccessException(io.cdap.cdap.api.security.AccessException) TopicNotFoundException(io.cdap.cdap.api.messaging.TopicNotFoundException) StoreRequest(io.cdap.cdap.messaging.StoreRequest) IOException(java.io.IOException) Notification(io.cdap.cdap.proto.Notification)

Aggregations

TopicNotFoundException (io.cdap.cdap.api.messaging.TopicNotFoundException)29 TopicMetadata (io.cdap.cdap.messaging.TopicMetadata)13 TopicId (io.cdap.cdap.proto.id.TopicId)12 IOException (java.io.IOException)11 Test (org.junit.Test)9 Message (io.cdap.cdap.api.messaging.Message)5 HttpRequest (io.cdap.common.http.HttpRequest)5 HttpResponse (io.cdap.common.http.HttpResponse)5 MessageFetcher (io.cdap.cdap.api.messaging.MessageFetcher)4 AccessException (io.cdap.cdap.api.security.AccessException)4 TopicAlreadyExistsException (io.cdap.cdap.api.messaging.TopicAlreadyExistsException)3 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)3 ApplicationManager (io.cdap.cdap.test.ApplicationManager)3 Path (javax.ws.rs.Path)3 DBException (org.iq80.leveldb.DBException)3 CloseableIterator (io.cdap.cdap.api.dataset.lib.CloseableIterator)2 MessagePublisher (io.cdap.cdap.api.messaging.MessagePublisher)2 MessagingAdmin (io.cdap.cdap.api.messaging.MessagingAdmin)2 RetryableException (io.cdap.cdap.api.retry.RetryableException)2 BadRequestException (io.cdap.cdap.common.BadRequestException)2