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);
}
}
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());
}
}
}
Aggregations