Search in sources :

Example 1 with ImmutablePayloadTableEntry

use of co.cask.cdap.messaging.store.ImmutablePayloadTableEntry in project cdap by caskdata.

the class LevelDBPayloadTable method pruneMessages.

/**
 * Delete messages of a {@link TopicId} that has exceeded the TTL or if it belongs to an older generation
 *
 * @param topicMetadata {@link TopicMetadata}
 * @param currentTime current timestamp
 * @throws IOException error occurred while trying to delete a row in LevelDB
 */
public void pruneMessages(TopicMetadata topicMetadata, long currentTime) throws IOException {
    WriteBatch writeBatch = levelDB.createWriteBatch();
    long ttlInMs = TimeUnit.SECONDS.toMillis(topicMetadata.getTTL());
    byte[] startRow = MessagingUtils.toDataKeyPrefix(topicMetadata.getTopicId(), Integer.parseInt(MessagingUtils.Constants.DEFAULT_GENERATION));
    byte[] stopRow = Bytes.stopKeyForPrefix(startRow);
    try (CloseableIterator<Map.Entry<byte[], byte[]>> rowIterator = new DBScanIterator(levelDB, startRow, stopRow)) {
        while (rowIterator.hasNext()) {
            Map.Entry<byte[], byte[]> entry = rowIterator.next();
            PayloadTable.Entry payloadTableEntry = new ImmutablePayloadTableEntry(entry.getKey(), entry.getValue());
            int dataGeneration = payloadTableEntry.getGeneration();
            int currGeneration = topicMetadata.getGeneration();
            if (MessagingUtils.isOlderGeneration(dataGeneration, currGeneration)) {
                writeBatch.delete(entry.getKey());
                continue;
            }
            if ((dataGeneration == Math.abs(currGeneration)) && ((currentTime - payloadTableEntry.getPayloadWriteTimestamp()) > ttlInMs)) {
                writeBatch.delete(entry.getKey());
            } else {
                // since the entries are sorted by time.
                break;
            }
        }
    }
    try {
        levelDB.write(writeBatch, WRITE_OPTIONS);
    } catch (DBException ex) {
        throw new IOException(ex);
    }
}
Also used : DBException(org.iq80.leveldb.DBException) ImmutablePayloadTableEntry(co.cask.cdap.messaging.store.ImmutablePayloadTableEntry) IOException(java.io.IOException) PayloadTable(co.cask.cdap.messaging.store.PayloadTable) AbstractPayloadTable(co.cask.cdap.messaging.store.AbstractPayloadTable) RawPayloadTableEntry(co.cask.cdap.messaging.store.RawPayloadTableEntry) ImmutablePayloadTableEntry(co.cask.cdap.messaging.store.ImmutablePayloadTableEntry) WriteBatch(org.iq80.leveldb.WriteBatch) Map(java.util.Map)

Aggregations

AbstractPayloadTable (co.cask.cdap.messaging.store.AbstractPayloadTable)1 ImmutablePayloadTableEntry (co.cask.cdap.messaging.store.ImmutablePayloadTableEntry)1 PayloadTable (co.cask.cdap.messaging.store.PayloadTable)1 RawPayloadTableEntry (co.cask.cdap.messaging.store.RawPayloadTableEntry)1 IOException (java.io.IOException)1 Map (java.util.Map)1 DBException (org.iq80.leveldb.DBException)1 WriteBatch (org.iq80.leveldb.WriteBatch)1