Search in sources :

Example 36 with DBException

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

the class LevelDBPayloadTable method persist.

@Override
public void persist(Iterator<RawPayloadTableEntry> entries) throws IOException {
    try (WriteBatch writeBatch = levelDB.createWriteBatch()) {
        while (entries.hasNext()) {
            RawPayloadTableEntry entry = entries.next();
            byte[] key = entry.getKey();
            byte[] value = entry.getValue();
            // LevelDB doesn't make copies, and since we reuse RawPayloadTableEntry object, we need to create copies.
            writeBatch.put(Arrays.copyOf(key, key.length), Arrays.copyOf(value, value.length));
        }
        levelDB.write(writeBatch, WRITE_OPTIONS);
    } catch (DBException ex) {
        throw new IOException(ex);
    }
}
Also used : DBException(org.iq80.leveldb.DBException) RawPayloadTableEntry(co.cask.cdap.messaging.store.RawPayloadTableEntry) IOException(java.io.IOException) WriteBatch(org.iq80.leveldb.WriteBatch)

Example 37 with DBException

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

the class LevelDBMetadataTable method createTopic.

@Override
public void createTopic(TopicMetadata topicMetadata) throws TopicAlreadyExistsException, IOException {
    try {
        TopicId topicId = topicMetadata.getTopicId();
        byte[] key = MessagingUtils.toMetadataRowKey(topicId);
        TreeMap<String, String> properties = new TreeMap<>(topicMetadata.getProperties());
        properties.put(TopicMetadata.GENERATION_KEY, MessagingUtils.Constants.DEFAULT_GENERATION);
        synchronized (this) {
            byte[] tableValue = levelDB.get(key);
            if (tableValue != null) {
                Map<String, String> oldProperties = GSON.fromJson(Bytes.toString(tableValue), MAP_TYPE);
                TopicMetadata metadata = new TopicMetadata(topicId, oldProperties);
                if (metadata.exists()) {
                    throw new TopicAlreadyExistsException(topicId.getNamespace(), topicId.getTopic());
                }
                int newGenerationId = (metadata.getGeneration() * -1) + 1;
                properties.put(TopicMetadata.GENERATION_KEY, Integer.toString(newGenerationId));
            }
            byte[] value = Bytes.toBytes(GSON.toJson(properties, MAP_TYPE));
            levelDB.put(key, value, WRITE_OPTIONS);
        }
    } catch (DBException e) {
        throw new IOException(e);
    }
}
Also used : DBException(org.iq80.leveldb.DBException) TopicId(co.cask.cdap.proto.id.TopicId) IOException(java.io.IOException) TreeMap(java.util.TreeMap) TopicAlreadyExistsException(co.cask.cdap.api.messaging.TopicAlreadyExistsException) TopicMetadata(co.cask.cdap.messaging.TopicMetadata)

Example 38 with DBException

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

the class LevelDBMetadataTable method getMetadata.

@Override
public TopicMetadata getMetadata(TopicId topicId) throws IOException, TopicNotFoundException {
    try {
        byte[] value = levelDB.get(MessagingUtils.toMetadataRowKey(topicId));
        if (value == null) {
            throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
        }
        Map<String, String> properties = GSON.fromJson(Bytes.toString(value), MAP_TYPE);
        TopicMetadata topicMetadata = new TopicMetadata(topicId, properties);
        if (!topicMetadata.exists()) {
            throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
        }
        return topicMetadata;
    } catch (DBException e) {
        // DBException is a RuntimeException. Turn it to IOException so that it forces caller to handle it.
        throw new IOException(e);
    }
}
Also used : DBException(org.iq80.leveldb.DBException) TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) IOException(java.io.IOException) TopicMetadata(co.cask.cdap.messaging.TopicMetadata)

Example 39 with DBException

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

the class LevelDBMetadataTable method updateTopic.

@Override
public void updateTopic(TopicMetadata topicMetadata) throws TopicNotFoundException, IOException {
    try {
        TopicId topicId = topicMetadata.getTopicId();
        byte[] key = MessagingUtils.toMetadataRowKey(topicId);
        synchronized (this) {
            byte[] tableValue = levelDB.get(key);
            if (tableValue == null) {
                throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
            }
            Map<String, String> oldProperties = GSON.fromJson(Bytes.toString(tableValue), MAP_TYPE);
            TopicMetadata oldMetadata = new TopicMetadata(topicId, oldProperties);
            if (!oldMetadata.exists()) {
                throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic());
            }
            TreeMap<String, String> newProperties = new TreeMap<>(topicMetadata.getProperties());
            newProperties.put(TopicMetadata.GENERATION_KEY, Integer.toString(oldMetadata.getGeneration()));
            levelDB.put(key, 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) TopicId(co.cask.cdap.proto.id.TopicId) IOException(java.io.IOException) TreeMap(java.util.TreeMap) TopicMetadata(co.cask.cdap.messaging.TopicMetadata)

Example 40 with DBException

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

the class ShuffleHandler method startStore.

private void startStore(Path recoveryRoot) throws IOException {
    Options options = new Options();
    options.createIfMissing(false);
    options.logger(new LevelDBLogger());
    Path dbPath = new Path(recoveryRoot, STATE_DB_NAME);
    LOG.info("Using state database at " + dbPath + " for recovery");
    File dbfile = new File(dbPath.toString());
    try {
        stateDb = JniDBFactory.factory.open(dbfile, options);
    } catch (NativeDB.DBException e) {
        if (e.isNotFound() || e.getMessage().contains(" does not exist ")) {
            LOG.info("Creating state database at " + dbfile);
            options.createIfMissing(true);
            try {
                stateDb = JniDBFactory.factory.open(dbfile, options);
                storeVersion();
            } catch (DBException dbExc) {
                throw new IOException("Unable to create state store", dbExc);
            }
        } else {
            throw e;
        }
    }
    checkVersion();
}
Also used : Path(org.apache.hadoop.fs.Path) Options(org.iq80.leveldb.Options) DBException(org.iq80.leveldb.DBException) IOException(java.io.IOException) NativeDB(org.fusesource.leveldbjni.internal.NativeDB) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

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