use of co.cask.cdap.proto.id.TopicId in project cdap by caskdata.
the class MessagingHttpServiceTest method testMetadataEndpoints.
@Test
public void testMetadataEndpoints() throws Exception {
NamespaceId nsId = new NamespaceId("metadata");
TopicId topic1 = nsId.topic("t1");
TopicId topic2 = nsId.topic("t2");
// Get a non exist topic should fail
try {
client.getTopic(topic1);
Assert.fail("Expected TopicNotFoundException");
} catch (TopicNotFoundException e) {
// Expected
}
// Create the topic t1
client.createTopic(new TopicMetadata(topic1));
// Create an existing topic should fail
try {
client.createTopic(new TopicMetadata(topic1));
Assert.fail("Expect TopicAlreadyExistsException");
} catch (TopicAlreadyExistsException e) {
// Expected
}
// Get the topic properties. Verify TTL is the same as the default one
Assert.assertEquals(cConf.getInt(Constants.MessagingSystem.TOPIC_DEFAULT_TTL_SECONDS), client.getTopic(topic1).getTTL());
// Update the topic t1 with new TTL
client.updateTopic(new TopicMetadata(topic1, "ttl", "5"));
// Get the topic t1 properties. Verify TTL is updated
Assert.assertEquals(5, client.getTopic(topic1).getTTL());
// Try to add another topic t2 with invalid ttl, it should fail
try {
client.createTopic(new TopicMetadata(topic2, "ttl", "xyz"));
Assert.fail("Expect BadRequestException");
} catch (IllegalArgumentException e) {
// Expected
}
// Add topic t2 with valid ttl
client.createTopic(new TopicMetadata(topic2, "ttl", "5"));
// Get the topic t2 properties. It should have TTL set based on what provided
Assert.assertEquals(5, client.getTopic(topic2).getTTL());
// Listing topics under namespace ns1
List<TopicId> topics = client.listTopics(nsId);
Assert.assertEquals(Arrays.asList(topic1, topic2), topics);
// Delete both topics
client.deleteTopic(topic1);
client.deleteTopic(topic2);
// Delete a non exist topic should fail
try {
client.deleteTopic(topic1);
Assert.fail("Expect TopicNotFoundException");
} catch (TopicNotFoundException e) {
// Expected
}
// Update a non exist topic should fail
try {
client.updateTopic(new TopicMetadata(topic1));
Assert.fail("Expect TopicNotFoundException");
} catch (TopicNotFoundException e) {
// Expected
}
// Listing topics under namespace ns1 again, it should be empty
Assert.assertTrue(client.listTopics(nsId).isEmpty());
}
use of co.cask.cdap.proto.id.TopicId in project cdap by caskdata.
the class MessagingHttpServiceTest method testGeMetadata.
@Test
public void testGeMetadata() throws Exception {
TopicId topicId = new NamespaceId("ns2").topic("d");
TopicMetadata metadata = new TopicMetadata(topicId, "ttl", "100");
for (int i = 1; i <= 5; i++) {
client.createTopic(metadata);
TopicMetadata topicMetadata = client.getTopic(topicId);
Assert.assertEquals(100, topicMetadata.getTTL());
Assert.assertEquals(i, topicMetadata.getGeneration());
client.deleteTopic(topicId);
}
}
use of co.cask.cdap.proto.id.TopicId in project cdap by caskdata.
the class MessagingHttpServiceTest method testTxMaxLifeTime.
@Test
public void testTxMaxLifeTime() throws Exception {
NamespaceId nsId = new NamespaceId("txCheck");
TopicId topic1 = nsId.topic("t1");
// Create a topic
client.createTopic(new TopicMetadata(topic1));
final RollbackDetail rollbackDetail = client.publish(StoreRequestBuilder.of(topic1).setTransaction(1L).addPayloads("a", "b").build());
try {
client.publish(StoreRequestBuilder.of(topic1).setTransaction(-Long.MAX_VALUE).addPayloads("c", "d").build());
Assert.fail("Expected IOException");
} catch (IOException ex) {
// expected
}
Set<String> msgs = new HashSet<>();
CloseableIterator<RawMessage> messages = client.prepareFetch(topic1).fetch();
while (messages.hasNext()) {
RawMessage message = messages.next();
msgs.add(Bytes.toString(message.getPayload()));
}
Assert.assertEquals(2, msgs.size());
Assert.assertTrue(msgs.contains("a"));
Assert.assertTrue(msgs.contains("b"));
messages.close();
client.rollback(topic1, rollbackDetail);
client.deleteTopic(topic1);
}
use of co.cask.cdap.proto.id.TopicId in project cdap by caskdata.
the class DataCleanupTest method testOldGenCleanup.
@Test
public void testOldGenCleanup() throws Exception {
try (MetadataTable metadataTable = getMetadataTable();
MessageTable messageTable = getMessageTable();
PayloadTable payloadTable = getPayloadTable()) {
int txWritePtr = 100;
TopicId topicId = NamespaceId.DEFAULT.topic("oldGenCleanup");
TopicMetadata topic = new TopicMetadata(topicId, TopicMetadata.TTL_KEY, "100000", TopicMetadata.GENERATION_KEY, Integer.toString(GENERATION));
metadataTable.createTopic(topic);
List<MessageTable.Entry> entries = new ArrayList<>();
List<PayloadTable.Entry> pentries = new ArrayList<>();
byte[] messageId = new byte[MessageId.RAW_ID_SIZE];
MessageId.putRawId(0L, (short) 0, 0L, (short) 0, messageId, 0);
entries.add(new TestMessageEntry(topicId, GENERATION, "data", txWritePtr, (short) 0));
pentries.add(new TestPayloadEntry(topicId, GENERATION, "data", txWritePtr, (short) 0));
messageTable.store(entries.iterator());
payloadTable.store(pentries.iterator());
// Fetch the entries and make sure we are able to read it
try (CloseableIterator<MessageTable.Entry> iterator = messageTable.fetch(topic, 0, Integer.MAX_VALUE, null)) {
checkMessageEntry(iterator, txWritePtr);
}
try (CloseableIterator<PayloadTable.Entry> iterator = payloadTable.fetch(topic, txWritePtr, new MessageId(messageId), true, 100)) {
checkPayloadEntry(iterator, txWritePtr);
}
// Now run full compaction
forceFlushAndCompact(Table.MESSAGE);
forceFlushAndCompact(Table.PAYLOAD);
// Fetch the entries and make sure we are able to read it
try (CloseableIterator<MessageTable.Entry> iterator = messageTable.fetch(topic, 0, Integer.MAX_VALUE, null)) {
checkMessageEntry(iterator, txWritePtr);
}
try (CloseableIterator<PayloadTable.Entry> iterator = payloadTable.fetch(topic, txWritePtr, new MessageId(messageId), true, 100)) {
checkPayloadEntry(iterator, txWritePtr);
}
metadataTable.deleteTopic(topicId);
// Sleep so that the metadata cache in coprocessor expires
TimeUnit.SECONDS.sleep(3 * METADATA_CACHE_EXPIRY);
forceFlushAndCompact(Table.MESSAGE);
forceFlushAndCompact(Table.PAYLOAD);
try (CloseableIterator<MessageTable.Entry> iterator = messageTable.fetch(topic, 0, Integer.MAX_VALUE, null)) {
Assert.assertFalse(iterator.hasNext());
}
try (CloseableIterator<PayloadTable.Entry> iterator = payloadTable.fetch(topic, txWritePtr, new MessageId(messageId), true, 100)) {
Assert.assertFalse(iterator.hasNext());
}
}
}
use of co.cask.cdap.proto.id.TopicId in project cdap by caskdata.
the class LevelDBMetadataTableTest method testScanTopics.
@Test
public void testScanTopics() throws Exception {
try (MetadataTable metadataTable = createMetadataTable()) {
LevelDBMetadataTable table = (LevelDBMetadataTable) metadataTable;
TopicMetadata t1 = new TopicMetadata(NamespaceId.CDAP.topic("t1"), ImmutableMap.of(TopicMetadata.TTL_KEY, "10", TopicMetadata.GENERATION_KEY, "1"));
TopicMetadata t2 = new TopicMetadata(NamespaceId.SYSTEM.topic("t2"), ImmutableMap.of(TopicMetadata.TTL_KEY, "20", TopicMetadata.GENERATION_KEY, "1"));
metadataTable.createTopic(t1);
metadataTable.createTopic(t2);
List<TopicId> allTopics = table.listTopics();
Assert.assertEquals(2, allTopics.size());
List<TopicMetadata> metadatas = new ArrayList<>();
Iterators.addAll(metadatas, table.scanTopics());
Assert.assertEquals(2, metadatas.size());
allTopics = table.listTopics(NamespaceId.CDAP);
Assert.assertEquals(1, allTopics.size());
allTopics = table.listTopics(NamespaceId.SYSTEM);
Assert.assertEquals(1, allTopics.size());
metadataTable.deleteTopic(t1.getTopicId());
metadatas.clear();
Iterators.addAll(metadatas, table.scanTopics());
Assert.assertEquals(2, metadatas.size());
Assert.assertEquals(1, metadataTable.listTopics().size());
Assert.assertEquals(1, metadataTable.listTopics(NamespaceId.SYSTEM).size());
Assert.assertTrue(metadataTable.listTopics(NamespaceId.CDAP).isEmpty());
metadataTable.deleteTopic(t2.getTopicId());
metadatas.clear();
Iterators.addAll(metadatas, table.scanTopics());
for (TopicMetadata metadata : metadatas) {
Assert.assertEquals(-1, metadata.getGeneration());
}
Assert.assertTrue(metadataTable.listTopics().isEmpty());
}
}
Aggregations