Search in sources :

Example 36 with TopicMetadata

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();
    }
}
Also used : ArrayList(java.util.ArrayList) TopicId(co.cask.cdap.proto.id.TopicId) TopicMetadata(co.cask.cdap.messaging.TopicMetadata) Test(org.junit.Test)

Example 37 with TopicMetadata

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);
    }
}
Also used : ArrayList(java.util.ArrayList) TopicMetadata(co.cask.cdap.messaging.TopicMetadata) TopicId(co.cask.cdap.proto.id.TopicId) MessageId(co.cask.cdap.messaging.data.MessageId) Test(org.junit.Test)

Example 38 with TopicMetadata

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());
    }
}
Also used : ArrayList(java.util.ArrayList) TopicId(co.cask.cdap.proto.id.TopicId) TopicMetadata(co.cask.cdap.messaging.TopicMetadata) Test(org.junit.Test)

Example 39 with TopicMetadata

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
            }
        }
    }
}
Also used : TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) TopicId(co.cask.cdap.proto.id.TopicId) TopicMetadata(co.cask.cdap.messaging.TopicMetadata) Test(org.junit.Test)

Example 40 with TopicMetadata

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
        }
    }
}
Also used : TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) TopicId(co.cask.cdap.proto.id.TopicId) TopicAlreadyExistsException(co.cask.cdap.api.messaging.TopicAlreadyExistsException) TopicMetadata(co.cask.cdap.messaging.TopicMetadata) Test(org.junit.Test)

Aggregations

TopicMetadata (co.cask.cdap.messaging.TopicMetadata)42 TopicId (co.cask.cdap.proto.id.TopicId)29 Test (org.junit.Test)23 ArrayList (java.util.ArrayList)17 IOException (java.io.IOException)14 NamespaceId (co.cask.cdap.proto.id.NamespaceId)13 TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)12 MessageId (co.cask.cdap.messaging.data.MessageId)9 RawMessage (co.cask.cdap.messaging.data.RawMessage)9 TreeMap (java.util.TreeMap)7 TopicAlreadyExistsException (co.cask.cdap.api.messaging.TopicAlreadyExistsException)6 TimeProvider (co.cask.cdap.common.utils.TimeProvider)5 MetadataTable (co.cask.cdap.messaging.store.MetadataTable)4 Transaction (org.apache.tephra.Transaction)4 DBException (org.iq80.leveldb.DBException)4 RollbackDetail (co.cask.cdap.messaging.RollbackDetail)3 MessageTable (co.cask.cdap.messaging.store.MessageTable)3 Path (javax.ws.rs.Path)3 Result (org.apache.hadoop.hbase.client.Result)3 HashSet (java.util.HashSet)2