Search in sources :

Example 1 with TestMessageEntry

use of io.cdap.cdap.messaging.store.TestMessageEntry in project cdap by caskdata.

the class LevelDBMessageTableTest method testMultiPartitionReadWrite.

@Test
public void testMultiPartitionReadWrite() throws Exception {
    TopicId topicId = new TopicId("default", "multipart");
    int generation = 1;
    TopicMetadata topicMetadata = new TopicMetadata(topicId, Collections.singletonMap(TopicMetadata.GENERATION_KEY, String.valueOf(generation)));
    try (MessageTable table = tableFactory.createMessageTable(topicMetadata)) {
        List<MessageTable.Entry> writes = new ArrayList<>();
        Map<Long, Byte> expected = new HashMap<>();
        for (int i = 0; i < 10 * PARTITION_SECONDS; i++) {
            long publishTime = i * 1000;
            expected.put(publishTime, (byte) i);
            writes.add(new TestMessageEntry(topicId, generation, publishTime, 0, null, new byte[] { (byte) i }));
        }
        table.store(writes.iterator());
        Map<Long, Byte> actual = new HashMap<>();
        byte[] messageId = new byte[MessageId.RAW_ID_SIZE];
        MessageId.putRawId(0L, (short) 0, 0L, (short) 0, messageId, 0);
        try (CloseableIterator<MessageTable.Entry> iter = table.fetch(topicMetadata, new MessageId(messageId), true, 100, null)) {
            while (iter.hasNext()) {
                MessageTable.Entry entry = iter.next();
                actual.put(entry.getPublishTimestamp(), entry.getPayload()[0]);
            }
        }
        Assert.assertEquals(expected, actual);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TopicMetadata(io.cdap.cdap.messaging.TopicMetadata) TestMessageEntry(io.cdap.cdap.messaging.store.TestMessageEntry) MessageTable(io.cdap.cdap.messaging.store.MessageTable) TopicId(io.cdap.cdap.proto.id.TopicId) TestMessageEntry(io.cdap.cdap.messaging.store.TestMessageEntry) MessageId(io.cdap.cdap.messaging.data.MessageId) Test(org.junit.Test) MessageTableTest(io.cdap.cdap.messaging.store.MessageTableTest)

Example 2 with TestMessageEntry

use of io.cdap.cdap.messaging.store.TestMessageEntry in project cdap by caskdata.

the class LevelDBMessageTableTest method testUpgrade.

@Test
public void testUpgrade() throws Exception {
    File baseDir = tmpFolder.newFolder();
    String tableName = "tms.messages";
    CConfiguration cConf = CConfiguration.create();
    cConf.set(Constants.MessagingSystem.LOCAL_DATA_DIR, baseDir.getAbsolutePath());
    cConf.set(Constants.MessagingSystem.MESSAGE_TABLE_NAME, tableName);
    cConf.set(Constants.MessagingSystem.LOCAL_DATA_PARTITION_SECONDS, Integer.toString(1));
    LevelDBTableFactory tableFactory = new LevelDBTableFactory(cConf);
    TopicId topicId = new TopicId("default", "preview");
    int generation = 1;
    TopicMetadata topicMetadata = new TopicMetadata(topicId, Collections.singletonMap(TopicMetadata.GENERATION_KEY, String.valueOf(generation)));
    // write a message to a table, then rename the underlying directory to the old format
    long publishTime = 1000;
    try (MessageTable table = tableFactory.createMessageTable(topicMetadata)) {
        List<MessageTable.Entry> writes = new ArrayList<>();
        writes.add(new TestMessageEntry(topicId, generation, publishTime, 0, null, new byte[] { 0 }));
        table.store(writes.iterator());
    }
    tableFactory.close();
    File topicDir = LevelDBTableFactory.getMessageTablePath(baseDir, topicId, generation, tableName);
    File partitionDir = LevelDBPartitionManager.getPartitionDir(topicDir, 0, publishTime + 1000);
    File oldDir = new File(baseDir, topicDir.getName().substring(LevelDBTableFactory.MESSAGE_TABLE_VERSION.length() + 1));
    Files.move(partitionDir.toPath(), oldDir.toPath());
    Files.delete(topicDir.toPath());
    // now run the upgrade and make sure the table is readable.
    tableFactory = new LevelDBTableFactory(cConf);
    tableFactory.init();
    try (MessageTable table = tableFactory.createMessageTable(topicMetadata)) {
        byte[] messageId = new byte[MessageId.RAW_ID_SIZE];
        MessageId.putRawId(0L, (short) 0, 0L, (short) 0, messageId, 0);
        try (CloseableIterator<MessageTable.Entry> iter = table.fetch(topicMetadata, new MessageId(messageId), true, 100, null)) {
            Assert.assertTrue(iter.hasNext());
            MessageTable.Entry entry = iter.next();
            Assert.assertEquals(publishTime, entry.getPublishTimestamp());
            Assert.assertFalse(iter.hasNext());
        }
    }
}
Also used : ArrayList(java.util.ArrayList) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) TopicMetadata(io.cdap.cdap.messaging.TopicMetadata) TestMessageEntry(io.cdap.cdap.messaging.store.TestMessageEntry) MessageTable(io.cdap.cdap.messaging.store.MessageTable) TopicId(io.cdap.cdap.proto.id.TopicId) File(java.io.File) TestMessageEntry(io.cdap.cdap.messaging.store.TestMessageEntry) MessageId(io.cdap.cdap.messaging.data.MessageId) Test(org.junit.Test) MessageTableTest(io.cdap.cdap.messaging.store.MessageTableTest)

Aggregations

TopicMetadata (io.cdap.cdap.messaging.TopicMetadata)2 MessageId (io.cdap.cdap.messaging.data.MessageId)2 MessageTable (io.cdap.cdap.messaging.store.MessageTable)2 MessageTableTest (io.cdap.cdap.messaging.store.MessageTableTest)2 TestMessageEntry (io.cdap.cdap.messaging.store.TestMessageEntry)2 TopicId (io.cdap.cdap.proto.id.TopicId)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)1 File (java.io.File)1 HashMap (java.util.HashMap)1