use of co.cask.cdap.messaging.TopicMetadata in project cdap by caskdata.
the class DataCleanupTest method testMessageTTLCleanup.
@Test
public void testMessageTTLCleanup() throws Exception {
try (MetadataTable metadataTable = getMetadataTable();
MessageTable messageTable = getMessageTable();
PayloadTable payloadTable = getPayloadTable()) {
Assert.assertNotNull(payloadTable);
TopicId topicId = NamespaceId.DEFAULT.topic("t1");
TopicMetadata topic = new TopicMetadata(topicId, "ttl", "3", TopicMetadata.GENERATION_KEY, Integer.toString(GENERATION));
metadataTable.createTopic(topic);
List<MessageTable.Entry> entries = new ArrayList<>();
entries.add(new TestMessageEntry(topicId, GENERATION, "data", 100, (short) 0));
messageTable.store(entries.iterator());
// Fetch the entries and make sure we are able to read it
CloseableIterator<MessageTable.Entry> iterator = messageTable.fetch(topic, 0, Integer.MAX_VALUE, null);
Assert.assertTrue(iterator.hasNext());
MessageTable.Entry entry = iterator.next();
Assert.assertFalse(iterator.hasNext());
Assert.assertEquals(100, entry.getTransactionWritePointer());
Assert.assertEquals("data", Bytes.toString(entry.getPayload()));
iterator.close();
forceFlushAndCompact(Table.MESSAGE);
// Entry should still be there since the ttl has not expired
iterator = messageTable.fetch(topic, 0, Integer.MAX_VALUE, null);
entry = iterator.next();
Assert.assertFalse(iterator.hasNext());
Assert.assertEquals(100, entry.getTransactionWritePointer());
Assert.assertEquals("data", Bytes.toString(entry.getPayload()));
iterator.close();
TimeUnit.SECONDS.sleep(3 * METADATA_CACHE_EXPIRY);
forceFlushAndCompact(Table.MESSAGE);
iterator = messageTable.fetch(topic, 0, Integer.MAX_VALUE, null);
Assert.assertFalse(iterator.hasNext());
iterator.close();
}
}
use of co.cask.cdap.messaging.TopicMetadata in project cdap by caskdata.
the class DataCleanupTest method testPayloadTTLCleanup.
@Test
public void testPayloadTTLCleanup() throws Exception {
try (MetadataTable metadataTable = getMetadataTable();
PayloadTable payloadTable = getPayloadTable();
MessageTable messageTable = getMessageTable()) {
Assert.assertNotNull(messageTable);
TopicId topicId = NamespaceId.DEFAULT.topic("t2");
TopicMetadata topic = new TopicMetadata(topicId, "ttl", "3", TopicMetadata.GENERATION_KEY, Integer.toString(GENERATION));
metadataTable.createTopic(topic);
List<PayloadTable.Entry> entries = new ArrayList<>();
entries.add(new TestPayloadEntry(topicId, GENERATION, "payloaddata", 101, (short) 0));
payloadTable.store(entries.iterator());
byte[] messageId = new byte[MessageId.RAW_ID_SIZE];
MessageId.putRawId(0L, (short) 0, 0L, (short) 0, messageId, 0);
CloseableIterator<PayloadTable.Entry> iterator = payloadTable.fetch(topic, 101L, new MessageId(messageId), true, Integer.MAX_VALUE);
Assert.assertTrue(iterator.hasNext());
PayloadTable.Entry entry = iterator.next();
Assert.assertFalse(iterator.hasNext());
Assert.assertEquals("payloaddata", Bytes.toString(entry.getPayload()));
Assert.assertEquals(101L, entry.getTransactionWritePointer());
iterator.close();
forceFlushAndCompact(Table.PAYLOAD);
//Entry should still be there since ttl has not expired
iterator = payloadTable.fetch(topic, 101L, new MessageId(messageId), true, Integer.MAX_VALUE);
Assert.assertTrue(iterator.hasNext());
entry = iterator.next();
Assert.assertFalse(iterator.hasNext());
Assert.assertEquals("payloaddata", Bytes.toString(entry.getPayload()));
Assert.assertEquals(101L, entry.getTransactionWritePointer());
iterator.close();
TimeUnit.SECONDS.sleep(3 * METADATA_CACHE_EXPIRY);
forceFlushAndCompact(Table.PAYLOAD);
iterator = payloadTable.fetch(topic, 101L, new MessageId(messageId), true, Integer.MAX_VALUE);
Assert.assertFalse(iterator.hasNext());
iterator.close();
metadataTable.deleteTopic(topicId);
}
}
use of co.cask.cdap.messaging.TopicMetadata in project cdap by caskdata.
the class MessageTableTest method testEmptyPayload.
@Test
public void testEmptyPayload() throws Exception {
TopicId topicId = NamespaceId.DEFAULT.topic("testEmptyPayload");
TopicMetadata metadata = new TopicMetadata(topicId, DEFAULT_PROPERTY);
// stores only a reference to the payload table
try (MessageTable table = getMessageTable();
MetadataTable metadataTable = getMetadataTable()) {
metadataTable.createTopic(metadata);
try {
table.store(Collections.singleton(new TestMessageEntry(topicId, GENERATION, 1L, 0, null, null)).iterator());
Assert.fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
// Expected as non-transactional message cannot have null payload
}
// For transactional message, ok to have null payload
table.store(Collections.singleton(new TestMessageEntry(topicId, GENERATION, 1L, 0, 2L, null)).iterator());
// Fetch the entry to validate
List<MessageTable.Entry> entries = new ArrayList<>();
try (CloseableIterator<MessageTable.Entry> iterator = table.fetch(metadata, 0L, Integer.MAX_VALUE, null)) {
Iterators.addAll(entries, iterator);
}
Assert.assertEquals(1, entries.size());
MessageTable.Entry entry = entries.get(0);
Assert.assertEquals(1L, entry.getPublishTimestamp());
Assert.assertEquals(0, entry.getSequenceId());
Assert.assertTrue(entry.isTransactional());
Assert.assertEquals(2L, entry.getTransactionWritePointer());
Assert.assertNull(entry.getPayload());
Assert.assertTrue(entry.isPayloadReference());
}
}
use of co.cask.cdap.messaging.TopicMetadata in project cdap by caskdata.
the class MetadataTableTest method testGenerations.
@Test
public void testGenerations() throws Exception {
try (MetadataTable table = createMetadataTable()) {
TopicId topicId = NamespaceId.DEFAULT.topic("gtopic");
for (int i = 1; i <= 50; i++) {
table.createTopic(new TopicMetadata(topicId, "ttl", 1));
TopicMetadata metadata = table.getMetadata(topicId);
Assert.assertEquals(i, metadata.getGeneration());
Assert.assertEquals(1, metadata.getTTL());
table.deleteTopic(topicId);
try {
table.getMetadata(topicId);
Assert.fail("Expected TopicNotFoundException");
} catch (TopicNotFoundException ex) {
// Expected
}
}
}
}
use of co.cask.cdap.messaging.TopicMetadata in project cdap by caskdata.
the class MetadataTableTest method testCRUD.
@Test
public void testCRUD() throws Exception {
try (MetadataTable table = createMetadataTable()) {
TopicId topicId = NamespaceId.DEFAULT.topic("topic");
// Update a non-existing topic should fail.
try {
table.updateTopic(new TopicMetadata(topicId, "ttl", 10));
Assert.fail("Expected TopicNotFoundException");
} catch (TopicNotFoundException e) {
// Expected
}
// Create a topic and validate
table.createTopic(new TopicMetadata(topicId, "ttl", 10));
Assert.assertEquals(10, table.getMetadata(topicId).getTTL());
// Update the property and validate
table.updateTopic(new TopicMetadata(topicId, "ttl", 30));
Assert.assertEquals(30, table.getMetadata(topicId).getTTL());
// Create the same topic again should fail
try {
table.createTopic(new TopicMetadata(topicId, "ttl", 10));
Assert.fail("Expected TopicAlreadyExistsException");
} catch (TopicAlreadyExistsException e) {
// Expected
}
// It shouldn't affect the topic at all if creation failed
Assert.assertEquals(30, table.getMetadata(topicId).getTTL());
// Delete the topic
table.deleteTopic(topicId);
try {
table.getMetadata(topicId);
Assert.fail("Expected TopicNotFoundException");
} catch (TopicNotFoundException e) {
// Expected
}
// Delete again should raise a TopicNotFoundException
try {
table.deleteTopic(topicId);
Assert.fail("Expected TopicNotFoundException");
} catch (TopicNotFoundException e) {
// Expected
}
}
}
Aggregations