use of co.cask.cdap.messaging.TopicMetadata in project cdap by caskdata.
the class CoreMessagingService method updateTopic.
@Override
public void updateTopic(TopicMetadata topicMetadata) throws TopicNotFoundException, IOException {
try (MetadataTable metadataTable = createMetadataTable()) {
Map<String, String> properties = createDefaultProperties();
properties.putAll(topicMetadata.getProperties());
metadataTable.updateTopic(new TopicMetadata(topicMetadata.getTopicId(), properties, true));
topicCache.invalidate(topicMetadata.getTopicId());
}
}
use of co.cask.cdap.messaging.TopicMetadata in project cdap by caskdata.
the class CoreMessagingService method createTopicIfNotExists.
/**
* Creates the given topic if it is not yet created.
*/
private void createTopicIfNotExists(TopicId topicId) throws IOException {
try {
createTopic(new TopicMetadata(topicId));
LOG.info("System topic created: {}", topicId);
} catch (TopicAlreadyExistsException e) {
// OK for the topic already created. Just log a debug as it happens on every restart.
LOG.debug("System topic already exists: {}", topicId);
}
}
use of co.cask.cdap.messaging.TopicMetadata in project cdap by caskdata.
the class HBaseMetadataTable method scanTopics.
/**
* Scans the HBase table to get a list of {@link TopicId}.
*/
private List<TopicId> scanTopics(ScanBuilder scanBuilder) throws IOException {
Scan scan = scanBuilder.setFilter(new FirstKeyOnlyFilter()).setCaching(scanCacheRows).build();
try {
List<TopicId> topicIds = new ArrayList<>();
try (ResultScanner resultScanner = hTable.getScanner(scan)) {
for (Result result : resultScanner) {
TopicId topicId = MessagingUtils.toTopicId(result.getRow());
byte[] value = result.getValue(columnFamily, COL);
Map<String, String> properties = GSON.fromJson(Bytes.toString(value), MAP_TYPE);
TopicMetadata metadata = new TopicMetadata(topicId, properties);
if (metadata.exists()) {
topicIds.add(topicId);
}
}
}
return topicIds;
} catch (IOException e) {
throw exceptionHandler.handle(e);
}
}
use of co.cask.cdap.messaging.TopicMetadata 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);
}
}
use of co.cask.cdap.messaging.TopicMetadata in project cdap by caskdata.
the class MessagingHttpServiceTest method testPayloadTable.
@Test
public void testPayloadTable() throws Exception {
// This test is to verify storing transaction messages to the payload table
TopicId topicId = new NamespaceId("ns1").topic("testPayloadTable");
client.createTopic(new TopicMetadata(topicId));
// Try to store to Payload table with empty iterator, expected failure
try {
client.storePayload(StoreRequestBuilder.of(topicId).setTransaction(1L).build());
Assert.fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
// Expected
}
// Store 20 payloads to the payload table, with 2 payloads per request
for (int i = 0; i < 10; i++) {
String payload = Integer.toString(i);
client.storePayload(StoreRequestBuilder.of(topicId).addPayloads(payload, payload).setTransaction(1L).build());
}
// Try to consume and there should be no messages
try (CloseableIterator<RawMessage> iterator = client.prepareFetch(topicId).fetch()) {
Assert.assertFalse(iterator.hasNext());
}
// Publish an empty payload message to the message table. This simulates a tx commit.
client.publish(StoreRequestBuilder.of(topicId).setTransaction(1L).build());
// Consume again and there should be 20 messages
List<RawMessage> messages = new ArrayList<>();
try (CloseableIterator<RawMessage> iterator = client.prepareFetch(topicId).fetch()) {
Iterators.addAll(messages, iterator);
}
Assert.assertEquals(20, messages.size());
for (int i = 0; i < 20; i += 2) {
String payload1 = Bytes.toString(messages.get(i).getPayload());
String payload2 = Bytes.toString(messages.get(i + 1).getPayload());
Assert.assertEquals(payload1, payload2);
Assert.assertEquals(Integer.toString(i / 2), payload1);
}
// Consume with a limit
messages.clear();
try (CloseableIterator<RawMessage> iterator = client.prepareFetch(topicId).setLimit(6).fetch()) {
Iterators.addAll(messages, iterator);
}
Assert.assertEquals(6, messages.size());
for (int i = 0; i < 6; i += 2) {
String payload1 = Bytes.toString(messages.get(i).getPayload());
String payload2 = Bytes.toString(messages.get(i + 1).getPayload());
Assert.assertEquals(payload1, payload2);
Assert.assertEquals(Integer.toString(i / 2), payload1);
}
client.deleteTopic(topicId);
}
Aggregations