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);
}
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);
}
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();
}
};
}
Aggregations