Search in sources :

Example 66 with DBException

use of org.iq80.leveldb.DBException in project tez by apache.

the class ShuffleHandler method removeJobShuffleInfo.

private void removeJobShuffleInfo(JobID jobId) throws IOException {
    String jobIdStr = jobId.toString();
    secretManager.removeTokenForJob(jobIdStr);
    userRsrc.remove(jobIdStr);
    if (stateDb != null) {
        try {
            stateDb.delete(bytes(jobIdStr));
        } catch (DBException e) {
            throw new IOException("Unable to remove " + jobId + " from state store", e);
        }
    }
}
Also used : DBException(org.iq80.leveldb.DBException) JniDBFactory.asString(org.fusesource.leveldbjni.JniDBFactory.asString) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException)

Example 67 with DBException

use of org.iq80.leveldb.DBException in project cdap by caskdata.

the class LevelDBMessageTable 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();
            MessageTable.Entry messageTableEntry = new ImmutableMessageTableEntry(entry.getKey(), null, null);
            int dataGeneration = messageTableEntry.getGeneration();
            int currGeneration = topicMetadata.getGeneration();
            if (MessagingUtils.isOlderGeneration(dataGeneration, currGeneration)) {
                writeBatch.delete(entry.getKey());
                continue;
            }
            if ((dataGeneration == Math.abs(currGeneration)) && ((currentTime - messageTableEntry.getPublishTimestamp()) > 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) IOException(java.io.IOException) ImmutableMessageTableEntry(co.cask.cdap.messaging.store.ImmutableMessageTableEntry) RawMessageTableEntry(co.cask.cdap.messaging.store.RawMessageTableEntry) AbstractMessageTable(co.cask.cdap.messaging.store.AbstractMessageTable) MessageTable(co.cask.cdap.messaging.store.MessageTable) WriteBatch(org.iq80.leveldb.WriteBatch) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMessageTableEntry(co.cask.cdap.messaging.store.ImmutableMessageTableEntry)

Example 68 with DBException

use of org.iq80.leveldb.DBException 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)

Example 69 with DBException

use of org.iq80.leveldb.DBException in project cdap by caskdata.

the class LevelDBMetadataTable method deleteTopic.

@Override
public void deleteTopic(TopicId topicId) throws TopicNotFoundException, IOException {
    byte[] rowKey = MessagingUtils.toMetadataRowKey(topicId);
    try {
        synchronized (this) {
            byte[] tableValue = levelDB.get(rowKey);
            if (tableValue == null) {
                throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
            }
            Map<String, String> oldProperties = GSON.fromJson(Bytes.toString(tableValue), MAP_TYPE);
            TopicMetadata metadata = new TopicMetadata(topicId, oldProperties);
            if (!metadata.exists()) {
                throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
            }
            // Mark the topic as deleted
            TreeMap<String, String> newProperties = new TreeMap<>(metadata.getProperties());
            newProperties.put(TopicMetadata.GENERATION_KEY, Integer.toString(-1 * metadata.getGeneration()));
            levelDB.put(rowKey, Bytes.toBytes(GSON.toJson(newProperties, MAP_TYPE)), WRITE_OPTIONS);
        }
    } catch (DBException e) {
        throw new IOException(e);
    }
}
Also used : DBException(org.iq80.leveldb.DBException) TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) IOException(java.io.IOException) TreeMap(java.util.TreeMap) TopicMetadata(co.cask.cdap.messaging.TopicMetadata)

Example 70 with DBException

use of org.iq80.leveldb.DBException in project aion by aionnetwork.

the class LevelDB method putBatchInternal.

@Override
public void putBatchInternal(Map<byte[], byte[]> input) {
    // try-with-resources will automatically close the batch object
    try (WriteBatch batch = db.createWriteBatch()) {
        // add put and delete operations to batch
        for (Map.Entry<byte[], byte[]> e : input.entrySet()) {
            byte[] key = e.getKey();
            byte[] value = e.getValue();
            batch.put(key, value);
        }
        // bulk atomic update
        db.write(batch);
    } catch (DBException e) {
        LOG.error("Unable to execute batch put/update operation on " + this.toString() + ".", e);
    } catch (IOException e) {
        LOG.error("Unable to close WriteBatch object in " + this.toString() + ".", e);
    }
}
Also used : DBException(org.iq80.leveldb.DBException) IOException(java.io.IOException) WriteBatch(org.iq80.leveldb.WriteBatch) Map(java.util.Map)

Aggregations

IOException (java.io.IOException)70 DBException (org.iq80.leveldb.DBException)70 JniDBFactory.asString (org.fusesource.leveldbjni.JniDBFactory.asString)39 LeveldbIterator (org.apache.hadoop.yarn.server.utils.LeveldbIterator)19 WriteBatch (org.iq80.leveldb.WriteBatch)18 Path (org.apache.hadoop.fs.Path)8 ByteString (com.google.protobuf.ByteString)6 File (java.io.File)6 Map (java.util.Map)6 NativeDB (org.fusesource.leveldbjni.internal.NativeDB)6 Options (org.iq80.leveldb.Options)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 DataOutputStream (java.io.DataOutputStream)5 TopicMetadata (co.cask.cdap.messaging.TopicMetadata)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4 HashMap (java.util.HashMap)4 TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)3 RawMessageTableEntry (co.cask.cdap.messaging.store.RawMessageTableEntry)3 TreeMap (java.util.TreeMap)3 ImmutableMessageTableEntry (co.cask.cdap.messaging.store.ImmutableMessageTableEntry)2