Search in sources :

Example 46 with TopicId

use of co.cask.cdap.proto.id.TopicId in project cdap by caskdata.

the class MessagingUtilsTest method testTopicConversion.

@Test
public void testTopicConversion() throws Exception {
    TopicId id = new TopicId("n1", "t1");
    byte[] topicBytes = MessagingUtils.toMetadataRowKey(id);
    TopicId topicId = MessagingUtils.toTopicId(topicBytes);
    Assert.assertEquals(id, topicId);
}
Also used : TopicId(co.cask.cdap.proto.id.TopicId) Test(org.junit.Test)

Example 47 with TopicId

use of co.cask.cdap.proto.id.TopicId in project cdap by caskdata.

the class MessagingHttpServiceTest method testChunkConsume.

@Test
public void testChunkConsume() throws Exception {
    // This test is to verify the message fetching body producer works correctly
    TopicId topicId = new NamespaceId("ns1").topic("testChunkConsume");
    client.createTopic(new TopicMetadata(topicId));
    // Publish 10 messages, each payload is half the size of the chunk size
    int payloadSize = cConf.getInt(Constants.MessagingSystem.HTTP_SERVER_CONSUME_CHUNK_SIZE) / 2;
    for (int i = 0; i < 10; i++) {
        String payload = Strings.repeat(Integer.toString(i), payloadSize);
        client.publish(StoreRequestBuilder.of(topicId).addPayloads(payload).build());
    }
    // Fetch messages. All of them should be fetched correctly
    List<RawMessage> messages = new ArrayList<>();
    try (CloseableIterator<RawMessage> iterator = client.prepareFetch(topicId).fetch()) {
        Iterators.addAll(messages, iterator);
    }
    Assert.assertEquals(10, messages.size());
    for (int i = 0; i < 10; i++) {
        RawMessage message = messages.get(i);
        Assert.assertEquals(payloadSize, message.getPayload().length);
        String payload = Strings.repeat(Integer.toString(i), payloadSize);
        Assert.assertEquals(payload, Bytes.toString(message.getPayload()));
    }
    client.deleteTopic(topicId);
}
Also used : ArrayList(java.util.ArrayList) TopicId(co.cask.cdap.proto.id.TopicId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) RawMessage(co.cask.cdap.messaging.data.RawMessage) TopicMetadata(co.cask.cdap.messaging.TopicMetadata) Test(org.junit.Test)

Example 48 with TopicId

use of co.cask.cdap.proto.id.TopicId in project cdap by caskdata.

the class BasicMapReduceTaskContext method getMessagingContext.

@Override
protected MessagingContext getMessagingContext() {
    // Override to have transactional publisher use "store" instead of "payload"
    // since a task is executed with long transaction.
    // The actual publish will be done in the MR driver.
    final MessagingContext context = super.getMessagingContext();
    // TODO: CDAP-7807 Make it available for any topic
    final TopicId allowedTopic = NamespaceId.SYSTEM.topic(cConf.get(Constants.Dataset.DATA_EVENT_TOPIC));
    return new MessagingContext() {

        @Override
        public MessagePublisher getMessagePublisher() {
            return new AbstractMessagePublisher() {

                @Override
                protected void publish(TopicId topicId, Iterator<byte[]> payloads) throws IOException, TopicNotFoundException {
                    if (!allowedTopic.equals(topicId)) {
                        throw new UnsupportedOperationException("Publish to topic '" + topicId.getTopic() + "' is not supported");
                    }
                    // Use storePayload
                    getMessagingService().storePayload(StoreRequestBuilder.of(topicId).setTransaction(transaction.getWritePointer()).addPayloads(payloads).build());
                }
            };
        }

        @Override
        public MessagePublisher getDirectMessagePublisher() {
            return context.getDirectMessagePublisher();
        }

        @Override
        public MessageFetcher getMessageFetcher() {
            return context.getMessageFetcher();
        }
    };
}
Also used : Iterator(java.util.Iterator) MessagingContext(co.cask.cdap.api.messaging.MessagingContext) TopicId(co.cask.cdap.proto.id.TopicId) AbstractMessagePublisher(co.cask.cdap.internal.app.runtime.messaging.AbstractMessagePublisher)

Aggregations

TopicId (co.cask.cdap.proto.id.TopicId)48 TopicMetadata (co.cask.cdap.messaging.TopicMetadata)29 Test (org.junit.Test)23 NamespaceId (co.cask.cdap.proto.id.NamespaceId)19 ArrayList (java.util.ArrayList)18 RawMessage (co.cask.cdap.messaging.data.RawMessage)11 IOException (java.io.IOException)11 TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)9 MessageId (co.cask.cdap.messaging.data.MessageId)8 Path (javax.ws.rs.Path)8 TopicAlreadyExistsException (co.cask.cdap.api.messaging.TopicAlreadyExistsException)6 TimeProvider (co.cask.cdap.common.utils.TimeProvider)4 RollbackDetail (co.cask.cdap.messaging.RollbackDetail)4 HttpRequest (co.cask.common.http.HttpRequest)4 HttpResponse (co.cask.common.http.HttpResponse)4 TreeMap (java.util.TreeMap)4 BadRequestException (co.cask.cdap.common.BadRequestException)3 StoreRequest (co.cask.cdap.messaging.StoreRequest)3 POST (javax.ws.rs.POST)3 GenericRecord (org.apache.avro.generic.GenericRecord)3