Search in sources :

Example 1 with StorageException

use of com.thinkaurelius.titan.diskstorage.StorageException in project titan by thinkaurelius.

the class AstyanaxStoreManager method getRetryPolicy.

private static RetryPolicy getRetryPolicy(String serializedRetryPolicy) throws StorageException {
    String[] tokens = serializedRetryPolicy.split(",");
    String policyClassName = tokens[0];
    int argCount = tokens.length - 1;
    Integer[] args = new Integer[argCount];
    for (int i = 1; i < tokens.length; i++) {
        args[i - 1] = Integer.valueOf(tokens[i]);
    }
    try {
        RetryPolicy rp = instantiate(policyClassName, args, serializedRetryPolicy);
        log.debug("Instantiated RetryPolicy object {} from config string \"{}\"", rp, serializedRetryPolicy);
        return rp;
    } catch (Exception e) {
        throw new PermanentStorageException("Failed to instantiate Astyanax Retry Policy class", e);
    }
}
Also used : PermanentStorageException(com.thinkaurelius.titan.diskstorage.PermanentStorageException) RetryPolicy(com.netflix.astyanax.retry.RetryPolicy) PermanentStorageException(com.thinkaurelius.titan.diskstorage.PermanentStorageException) ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) ConnectionException(com.netflix.astyanax.connectionpool.exceptions.ConnectionException) TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException) StorageException(com.thinkaurelius.titan.diskstorage.StorageException)

Example 2 with StorageException

use of com.thinkaurelius.titan.diskstorage.StorageException in project titan by thinkaurelius.

the class BerkeleyJEKeyValueStore method getSlice.

@Override
public RecordIterator<KeyValueEntry> getSlice(StaticBuffer keyStart, StaticBuffer keyEnd, KeySelector selector, StoreTransaction txh) throws StorageException {
    log.trace("Get slice query");
    Transaction tx = getTransaction(txh);
    Cursor cursor = null;
    final List<KeyValueEntry> result = new ArrayList<KeyValueEntry>();
    try {
        DatabaseEntry foundKey = keyStart.as(ENTRY_FACTORY);
        DatabaseEntry foundData = new DatabaseEntry();
        cursor = db.openCursor(tx, null);
        OperationStatus status = cursor.getSearchKeyRange(foundKey, foundData, LockMode.DEFAULT);
        // Iterate until given condition is satisfied or end of records
        while (status == OperationStatus.SUCCESS) {
            StaticBuffer key = getBuffer(foundKey);
            if (key.compareTo(keyEnd) >= 0)
                break;
            if (selector.include(key)) {
                result.add(new KeyValueEntry(key, getBuffer(foundData)));
            }
            if (selector.reachedLimit())
                break;
            status = cursor.getNext(foundKey, foundData, LockMode.DEFAULT);
        }
        log.trace("Retrieved: {}", result.size());
        return new RecordIterator<KeyValueEntry>() {

            private final Iterator<KeyValueEntry> entries = result.iterator();

            @Override
            public boolean hasNext() {
                return entries.hasNext();
            }

            @Override
            public KeyValueEntry next() {
                return entries.next();
            }

            @Override
            public void close() {
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    } catch (Exception e) {
        throw new PermanentStorageException(e);
    } finally {
        try {
            if (cursor != null)
                cursor.close();
        } catch (Exception e) {
            throw new PermanentStorageException(e);
        }
    }
}
Also used : RecordIterator(com.thinkaurelius.titan.diskstorage.util.RecordIterator) PermanentStorageException(com.thinkaurelius.titan.diskstorage.PermanentStorageException) ArrayList(java.util.ArrayList) StorageException(com.thinkaurelius.titan.diskstorage.StorageException) PermanentStorageException(com.thinkaurelius.titan.diskstorage.PermanentStorageException) NoSuchElementException(java.util.NoSuchElementException) StoreTransaction(com.thinkaurelius.titan.diskstorage.keycolumnvalue.StoreTransaction) RecordIterator(com.thinkaurelius.titan.diskstorage.util.RecordIterator) Iterator(java.util.Iterator) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) KeyValueEntry(com.thinkaurelius.titan.diskstorage.keycolumnvalue.keyvalue.KeyValueEntry)

Example 3 with StorageException

use of com.thinkaurelius.titan.diskstorage.StorageException in project titan by thinkaurelius.

the class ElasticSearchIndex method clearStorage.

@Override
public void clearStorage() throws StorageException {
    try {
        try {
            client.admin().indices().delete(new DeleteIndexRequest(indexName)).actionGet();
            // We wait for one second to let ES delete the river
            Thread.sleep(1000);
        } catch (IndexMissingException e) {
        // Index does not exist... Fine
        }
    } catch (Exception e) {
        throw new PermanentStorageException("Could not delete index " + indexName, e);
    } finally {
        close();
    }
}
Also used : PermanentStorageException(com.thinkaurelius.titan.diskstorage.PermanentStorageException) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) IndexMissingException(org.elasticsearch.indices.IndexMissingException) ElasticSearchInterruptedException(org.elasticsearch.ElasticSearchInterruptedException) PermanentStorageException(com.thinkaurelius.titan.diskstorage.PermanentStorageException) TitanException(com.thinkaurelius.titan.core.TitanException) TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException) IndexMissingException(org.elasticsearch.indices.IndexMissingException) StorageException(com.thinkaurelius.titan.diskstorage.StorageException) IOException(java.io.IOException)

Example 4 with StorageException

use of com.thinkaurelius.titan.diskstorage.StorageException in project titan by thinkaurelius.

the class ElasticSearchIndex method mutate.

@Override
public void mutate(Map<String, Map<String, IndexMutation>> mutations, KeyInformation.IndexRetriever informations, TransactionHandle tx) throws StorageException {
    BulkRequestBuilder brb = client.prepareBulk();
    int bulkrequests = 0;
    try {
        for (Map.Entry<String, Map<String, IndexMutation>> stores : mutations.entrySet()) {
            String storename = stores.getKey();
            for (Map.Entry<String, IndexMutation> entry : stores.getValue().entrySet()) {
                String docid = entry.getKey();
                IndexMutation mutation = entry.getValue();
                Preconditions.checkArgument(!(mutation.isNew() && mutation.isDeleted()));
                Preconditions.checkArgument(!mutation.isNew() || !mutation.hasDeletions());
                Preconditions.checkArgument(!mutation.isDeleted() || !mutation.hasAdditions());
                // Deletions first
                if (mutation.hasDeletions()) {
                    if (mutation.isDeleted()) {
                        log.trace("Deleting entire document {}", docid);
                        brb.add(new DeleteRequest(indexName, storename, docid));
                        bulkrequests++;
                    } else {
                        Set<String> deletions = Sets.newHashSet(mutation.getDeletions());
                        if (mutation.hasAdditions()) {
                            for (IndexEntry ie : mutation.getAdditions()) {
                                deletions.remove(ie.key);
                            }
                        }
                        if (!deletions.isEmpty()) {
                            // TODO make part of batch mutation if/when possible
                            StringBuilder script = new StringBuilder();
                            for (String key : deletions) {
                                script.append("ctx._source.remove(\"" + key + "\"); ");
                            }
                            log.trace("Deleting individual fields [{}] for document {}", deletions, docid);
                            client.prepareUpdate(indexName, storename, docid).setScript(script.toString()).execute().actionGet();
                        }
                    }
                }
                if (mutation.hasAdditions()) {
                    if (mutation.isNew()) {
                        // Index
                        log.trace("Adding entire document {}", docid);
                        brb.add(new IndexRequest(indexName, storename, docid).source(getContent(mutation.getAdditions())));
                        bulkrequests++;
                    } else {
                        // Update: TODO make part of batch mutation if/when possible
                        boolean needUpsert = !mutation.hasDeletions();
                        XContentBuilder builder = getContent(mutation.getAdditions());
                        UpdateRequestBuilder update = client.prepareUpdate(indexName, storename, docid).setDoc(builder);
                        if (needUpsert)
                            update.setUpsert(builder);
                        log.trace("Updating document {} with upsert {}", docid, needUpsert);
                        update.execute().actionGet();
                    }
                }
            }
        }
        if (bulkrequests > 0)
            brb.execute().actionGet();
    } catch (Exception e) {
        throw convert(e);
    }
}
Also used : UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) ElasticSearchInterruptedException(org.elasticsearch.ElasticSearchInterruptedException) PermanentStorageException(com.thinkaurelius.titan.diskstorage.PermanentStorageException) TitanException(com.thinkaurelius.titan.core.TitanException) TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException) IndexMissingException(org.elasticsearch.indices.IndexMissingException) StorageException(com.thinkaurelius.titan.diskstorage.StorageException) IOException(java.io.IOException) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) Map(java.util.Map) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 5 with StorageException

use of com.thinkaurelius.titan.diskstorage.StorageException in project titan by thinkaurelius.

the class CassandraThriftStoreManager method ensureColumnFamilyExists.

private void ensureColumnFamilyExists(String ksName, String cfName, String comparator) throws StorageException {
    CTConnection conn = null;
    try {
        KsDef keyspaceDef = ensureKeyspaceExists(ksName);
        conn = pool.borrowObject(ksName);
        Cassandra.Client client = conn.getClient();
        log.debug("Looking up metadata on keyspace {}...", ksName);
        boolean foundColumnFamily = false;
        for (CfDef cfDef : keyspaceDef.getCf_defs()) {
            String curCfName = cfDef.getName();
            if (curCfName.equals(cfName))
                foundColumnFamily = true;
        }
        if (!foundColumnFamily) {
            createColumnFamily(client, ksName, cfName, comparator);
        } else {
            log.debug("Keyspace {} and ColumnFamily {} were found.", ksName, cfName);
        }
    } catch (SchemaDisagreementException e) {
        throw new TemporaryStorageException(e);
    } catch (Exception e) {
        throw new PermanentStorageException(e);
    } finally {
        pool.returnObjectUnsafe(ksName, conn);
    }
}
Also used : CTConnection(com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection) TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException) Cassandra(org.apache.cassandra.thrift.Cassandra) PermanentStorageException(com.thinkaurelius.titan.diskstorage.PermanentStorageException) KsDef(org.apache.cassandra.thrift.KsDef) SchemaDisagreementException(org.apache.cassandra.thrift.SchemaDisagreementException) CfDef(org.apache.cassandra.thrift.CfDef) NotFoundException(org.apache.cassandra.thrift.NotFoundException) PermanentStorageException(com.thinkaurelius.titan.diskstorage.PermanentStorageException) InvalidRequestException(org.apache.cassandra.thrift.InvalidRequestException) TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException) TException(org.apache.thrift.TException) StorageException(com.thinkaurelius.titan.diskstorage.StorageException) SchemaDisagreementException(org.apache.cassandra.thrift.SchemaDisagreementException)

Aggregations

StorageException (com.thinkaurelius.titan.diskstorage.StorageException)29 TemporaryStorageException (com.thinkaurelius.titan.diskstorage.TemporaryStorageException)22 PermanentStorageException (com.thinkaurelius.titan.diskstorage.PermanentStorageException)20 StaticBuffer (com.thinkaurelius.titan.diskstorage.StaticBuffer)9 InvalidRequestException (org.apache.cassandra.thrift.InvalidRequestException)9 TException (org.apache.thrift.TException)9 CTConnection (com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection)8 ArrayList (java.util.ArrayList)7 Cassandra (org.apache.cassandra.thrift.Cassandra)7 TitanException (com.thinkaurelius.titan.core.TitanException)6 NotFoundException (org.apache.cassandra.thrift.NotFoundException)6 SchemaDisagreementException (org.apache.cassandra.thrift.SchemaDisagreementException)6 SlicePredicate (org.apache.cassandra.thrift.SlicePredicate)5 IOException (java.io.IOException)4 List (java.util.List)4 NoSuchElementException (java.util.NoSuchElementException)4 CfDef (org.apache.cassandra.thrift.CfDef)4 ColumnParent (org.apache.cassandra.thrift.ColumnParent)4 KsDef (org.apache.cassandra.thrift.KsDef)4 SliceRange (org.apache.cassandra.thrift.SliceRange)4