Search in sources :

Example 11 with TopicNotFoundException

use of co.cask.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.

the class DefaultAuditPublisher method publish.

@Override
public void publish(EntityId entityId, AuditType auditType, AuditPayload auditPayload) {
    String userId = Objects.firstNonNull(SecurityRequestContext.getUserId(), "");
    AuditMessage auditMessage = new AuditMessage(System.currentTimeMillis(), entityId, userId, auditType, auditPayload);
    LOG.trace("Publishing audit message {}", auditMessage);
    try {
        MessagingServices.publishWithRetry(messagingService, auditTopic, retryStrategy, GSON.toJson(auditMessage).getBytes(StandardCharsets.UTF_8));
    } catch (TopicNotFoundException e) {
        LOG.error("Missing topic for audit publish: {}", auditTopic);
    } catch (Exception e) {
        LOG.error("Got exception publishing audit message {}. Exception:", auditMessage, e);
    }
}
Also used : AuditMessage(co.cask.cdap.proto.audit.AuditMessage) TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException)

Example 12 with TopicNotFoundException

use of co.cask.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.

the class MessagingAppTestRun method testWithWorker.

@Test
public void testWithWorker() throws Exception {
    ApplicationManager appManager = deployWithArtifact(NAMESPACE, MessagingApp.class, artifactJar);
    final WorkerManager workerManager = appManager.getWorkerManager(MessagingApp.MessagingWorker.class.getSimpleName()).start();
    MessagingContext messagingContext = getMessagingContext();
    final MessagingAdmin messagingAdmin = getMessagingAdmin(NAMESPACE);
    // Wait for the worker to create the topic
    Tasks.waitFor(true, new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            try {
                messagingAdmin.getTopicProperties(MessagingApp.TOPIC);
                return true;
            } catch (TopicNotFoundException e) {
                return false;
            }
        }
    }, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    // Publish a message
    String message = "message";
    MessagePublisher messagePublisher = messagingContext.getMessagePublisher();
    messagePublisher.publish(NAMESPACE.getNamespace(), MessagingApp.TOPIC, message);
    // The worker will publish back a message with payload as concat(message, message)
    final MessageFetcher messageFetcher = messagingContext.getMessageFetcher();
    Tasks.waitFor(message + message, new Callable<String>() {

        @Override
        public String call() throws Exception {
            try (CloseableIterator<Message> iterator = messageFetcher.fetch(NAMESPACE.getNamespace(), MessagingApp.TOPIC, Integer.MAX_VALUE, 0L)) {
                Message message = Iterators.getLast(iterator, null);
                return message == null ? null : message.getPayloadAsString();
            }
        }
    }, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    // Publish concat(message + message) to the app
    messagePublisher.publish(NAMESPACE.getNamespace(), MessagingApp.TOPIC, message + message);
    // timeout.
    try {
        Tasks.waitFor(message + message + message + message, new Callable<String>() {

            @Override
            public String call() throws Exception {
                try (CloseableIterator<Message> iterator = messageFetcher.fetch(NAMESPACE.getNamespace(), MessagingApp.TOPIC, Integer.MAX_VALUE, 0L)) {
                    Message message = Iterators.getLast(iterator, null);
                    return message == null ? null : message.getPayloadAsString();
                }
            }
        }, 2, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
        Assert.fail("Expected timeout exception");
    } catch (TimeoutException e) {
    // expected
    }
    // Now publish a message to the control topic, to unblock the transaction block.
    messagePublisher.publish(NAMESPACE.getNamespace(), MessagingApp.CONTROL_TOPIC, message);
    // Should expect a new message as concat(message, message, message, message)
    Tasks.waitFor(message + message + message + message, new Callable<String>() {

        @Override
        public String call() throws Exception {
            try (CloseableIterator<Message> iterator = messageFetcher.fetch(NAMESPACE.getNamespace(), MessagingApp.TOPIC, Integer.MAX_VALUE, 0L)) {
                Message message = Iterators.getLast(iterator, null);
                return message == null ? null : message.getPayloadAsString();
            }
        }
    }, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    // Wait for the worker to finish and verify that it completes successfully.
    workerManager.waitForRun(ProgramRunStatus.COMPLETED, 5, TimeUnit.SECONDS);
}
Also used : ApplicationManager(co.cask.cdap.test.ApplicationManager) MessageFetcher(co.cask.cdap.api.messaging.MessageFetcher) CloseableIterator(co.cask.cdap.api.dataset.lib.CloseableIterator) Message(co.cask.cdap.api.messaging.Message) TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) MessagePublisher(co.cask.cdap.api.messaging.MessagePublisher) TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) TimeoutException(java.util.concurrent.TimeoutException) WorkerManager(co.cask.cdap.test.WorkerManager) MessagingAdmin(co.cask.cdap.api.messaging.MessagingAdmin) MessagingContext(co.cask.cdap.api.messaging.MessagingContext) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 13 with TopicNotFoundException

use of co.cask.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.

the class CoreMessagingService method storePayload.

@Override
public void storePayload(StoreRequest request) throws TopicNotFoundException, IOException {
    try {
        TopicMetadata metadata = topicCache.get(request.getTopicId());
        payloadTableWriterCache.get(request.getTopicId()).persist(request, metadata);
    } catch (ExecutionException e) {
        Throwable cause = Objects.firstNonNull(e.getCause(), e);
        Throwables.propagateIfPossible(cause, TopicNotFoundException.class, IOException.class);
        throw Throwables.propagate(e);
    }
}
Also used : TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TopicMetadata(co.cask.cdap.messaging.TopicMetadata)

Example 14 with TopicNotFoundException

use of co.cask.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.

the class HBaseMetadataTable method getMetadata.

@Override
public TopicMetadata getMetadata(TopicId topicId) throws IOException, TopicNotFoundException {
    Get get = tableUtil.buildGet(MessagingUtils.toMetadataRowKey(topicId)).addFamily(columnFamily).build();
    try {
        Result result = hTable.get(get);
        byte[] value = result.getValue(columnFamily, COL);
        if (value == null) {
            throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
        }
        Map<String, String> properties = GSON.fromJson(Bytes.toString(value), MAP_TYPE);
        TopicMetadata topicMetadata = new TopicMetadata(topicId, properties);
        if (!topicMetadata.exists()) {
            throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
        }
        return topicMetadata;
    } catch (IOException e) {
        throw exceptionHandler.handle(e);
    }
}
Also used : TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) Get(org.apache.hadoop.hbase.client.Get) IOException(java.io.IOException) Result(org.apache.hadoop.hbase.client.Result) TopicMetadata(co.cask.cdap.messaging.TopicMetadata)

Example 15 with TopicNotFoundException

use of co.cask.cdap.api.messaging.TopicNotFoundException in project cdap by caskdata.

the class ClientMessagingService method performWriteRequest.

/**
   * Makes a request to the server for writing to the messaging system
   *
   * @param request contains information about what to write
   * @param publish {@code true} to make publish call, {@code false} to make store call.
   * @return the response from the server
   * @throws IOException if failed to perform the write operation
   * @throws TopicNotFoundException if the topic to write to does not exist
   */
private HttpResponse performWriteRequest(StoreRequest request, boolean publish) throws IOException, TopicNotFoundException {
    GenericRecord record = new GenericData.Record(Schemas.V1.PublishRequest.SCHEMA);
    if (request.isTransactional()) {
        record.put("transactionWritePointer", request.getTransactionWritePointer());
    }
    record.put("messages", convertPayloads(request));
    // Encode the request as avro
    ExposedByteArrayOutputStream os = new ExposedByteArrayOutputStream();
    Encoder encoder = EncoderFactory.get().directBinaryEncoder(os, null);
    DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(Schemas.V1.PublishRequest.SCHEMA);
    datumWriter.write(record, encoder);
    // Make the publish request
    String writeType = publish ? "publish" : "store";
    TopicId topicId = request.getTopicId();
    HttpRequest httpRequest = remoteClient.requestBuilder(HttpMethod.POST, createTopicPath(topicId) + "/" + writeType).addHeader(HttpHeaders.CONTENT_TYPE, "avro/binary").withBody(os.toByteBuffer()).build();
    HttpResponse response = remoteClient.execute(httpRequest);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
    }
    handleError(response, "Failed to " + writeType + " message to topic " + topicId);
    return response;
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) Encoder(org.apache.avro.io.Encoder) TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) HttpResponse(co.cask.common.http.HttpResponse) GenericRecord(org.apache.avro.generic.GenericRecord) TopicId(co.cask.cdap.proto.id.TopicId) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) GenericRecord(org.apache.avro.generic.GenericRecord)

Aggregations

TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)20 TopicMetadata (co.cask.cdap.messaging.TopicMetadata)12 TopicId (co.cask.cdap.proto.id.TopicId)8 IOException (java.io.IOException)7 Test (org.junit.Test)7 HttpRequest (co.cask.common.http.HttpRequest)5 HttpResponse (co.cask.common.http.HttpResponse)5 TopicAlreadyExistsException (co.cask.cdap.api.messaging.TopicAlreadyExistsException)3 DBException (org.iq80.leveldb.DBException)3 CloseableIterator (co.cask.cdap.api.dataset.lib.CloseableIterator)2 Message (co.cask.cdap.api.messaging.Message)2 MessageFetcher (co.cask.cdap.api.messaging.MessageFetcher)2 MessagePublisher (co.cask.cdap.api.messaging.MessagePublisher)2 MessagingAdmin (co.cask.cdap.api.messaging.MessagingAdmin)2 NamespaceId (co.cask.cdap.proto.id.NamespaceId)2 ApplicationManager (co.cask.cdap.test.ApplicationManager)2 TreeMap (java.util.TreeMap)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 PartitionKey (co.cask.cdap.api.dataset.lib.PartitionKey)1