Search in sources :

Example 6 with TemporaryStorageException

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

the class CassandraThriftStoreManager method clearStorage.

/**
 * Connect to Cassandra via Thrift on the specified host and port and attempt to truncate the named keyspace.
 * <p/>
 * This is a utility method intended mainly for testing. It is
 * equivalent to issuing 'truncate <cf>' for each of the column families in keyspace using
 * the cassandra-cli tool.
 * <p/>
 * Using truncate is better for a number of reasons, most significantly because it doesn't
 * involve any schema modifications which can take time to propagate across the cluster such
 * leaves nodes in the inconsistent state and could result in read/write failures.
 * Any schema modifications are discouraged until there is no traffic to Keyspace or ColumnFamilies.
 *
 * @throws StorageException if any checked Thrift or UnknownHostException is thrown in the body of this method
 */
public void clearStorage() throws StorageException {
    openStores.clear();
    // "log prefix"
    final String lp = "ClearStorage: ";
    /*
         * log4j is capable of automatically writing the name of a method that
         * generated a log message, but the docs warn that "generating caller
         * location information is extremely slow and should be avoided unless
         * execution speed is not an issue."
         */
    CTConnection conn = null;
    try {
        conn = pool.borrowObject(SYSTEM_KS);
        Cassandra.Client client = conn.getClient();
        KsDef ksDef;
        try {
            client.set_keyspace(keySpaceName);
            ksDef = client.describe_keyspace(keySpaceName);
        } catch (NotFoundException e) {
            log.debug(lp + "Keyspace {} does not exist, not attempting to truncate.", keySpaceName);
            return;
        } catch (InvalidRequestException e) {
            log.debug(lp + "InvalidRequestException when attempting to describe keyspace {}, not attempting to truncate.", keySpaceName);
            return;
        }
        if (null == ksDef) {
            log.debug(lp + "Received null KsDef for keyspace {}; not truncating its CFs", keySpaceName);
            return;
        }
        List<CfDef> cfDefs = ksDef.getCf_defs();
        if (null == cfDefs) {
            log.debug(lp + "Received empty CfDef list for keyspace {}; not truncating CFs", keySpaceName);
            return;
        }
        for (CfDef cfDef : ksDef.getCf_defs()) {
            client.truncate(cfDef.name);
            log.info(lp + "Truncated CF {} in keyspace {}", cfDef.name, keySpaceName);
        }
    /*
             * Clearing the CTConnectionPool is unnecessary. This method
             * removes no keyspaces. All open Cassandra connections will
             * remain valid.
             */
    } catch (Exception e) {
        throw new TemporaryStorageException(e);
    } finally {
        if (conn != null && conn.getClient() != null) {
            try {
                conn.getClient().set_keyspace(SYSTEM_KS);
            } catch (InvalidRequestException e) {
                log.warn("Failed to reset keyspace", e);
            } catch (TException e) {
                log.warn("Failed to reset keyspace", e);
            }
        }
        pool.returnObjectUnsafe(SYSTEM_KS, conn);
    }
}
Also used : CTConnection(com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection) TException(org.apache.thrift.TException) TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException) Cassandra(org.apache.cassandra.thrift.Cassandra) NotFoundException(org.apache.cassandra.thrift.NotFoundException) InvalidRequestException(org.apache.cassandra.thrift.InvalidRequestException) KsDef(org.apache.cassandra.thrift.KsDef) 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)

Example 7 with TemporaryStorageException

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

the class AstyanaxOrderedKeyColumnValueStore method getKeys.

@Override
public KeyIterator getKeys(KeyRangeQuery query, StoreTransaction txh) throws StorageException {
    // this query could only be done when byte-ordering partitioner is used
    // because Cassandra operates on tokens internally which means that even contiguous
    // range of keys (e.g. time slice) with random partitioner could produce disjoint set of tokens
    // returning ambiguous results to the user.
    Partitioner partitioner = storeManager.getPartitioner();
    if (partitioner != Partitioner.BYTEORDER)
        throw new PermanentStorageException("getKeys(KeyRangeQuery could only be used with byte-ordering partitioner.");
    ByteBuffer start = query.getKeyStart().asByteBuffer(), end = query.getKeyEnd().asByteBuffer();
    int limit = (query.hasLimit()) ? query.getLimit() : Integer.MAX_VALUE;
    RowSliceQuery rowSlice = keyspace.prepareQuery(columnFamily).setConsistencyLevel(getTx(txh).getReadConsistencyLevel().getAstyanaxConsistency()).withRetryPolicy(retryPolicy.duplicate()).getKeyRange(start, end, null, null, Integer.MAX_VALUE);
    // Astyanax is bad at builder pattern :(
    rowSlice.withColumnRange(query.getSliceStart().asByteBuffer(), query.getSliceEnd().asByteBuffer(), false, limit);
    // Omit final the query's keyend from the result, if present in result
    final Rows<ByteBuffer, ByteBuffer> r;
    try {
        r = ((OperationResult<Rows<ByteBuffer, ByteBuffer>>) rowSlice.execute()).getResult();
    } catch (ConnectionException e) {
        throw new TemporaryStorageException(e);
    }
    Iterator<Row<ByteBuffer, ByteBuffer>> i = Iterators.filter(r.iterator(), new KeySkipPredicate(query.getKeyEnd().asByteBuffer()));
    return new RowIterator(i, query);
}
Also used : PermanentStorageException(com.thinkaurelius.titan.diskstorage.PermanentStorageException) ByteBuffer(java.nio.ByteBuffer) StaticByteBuffer(com.thinkaurelius.titan.diskstorage.util.StaticByteBuffer) RowSliceQuery(com.netflix.astyanax.query.RowSliceQuery) TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException) Partitioner(com.thinkaurelius.titan.diskstorage.cassandra.AbstractCassandraStoreManager.Partitioner) ConnectionException(com.netflix.astyanax.connectionpool.exceptions.ConnectionException)

Example 8 with TemporaryStorageException

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

the class CassandraKiller method startCassandra.

public void startCassandra() {
    try {
        final File cdd = new File(cassandraDataDir);
        if (delete) {
            if (cdd.isDirectory()) {
                log.debug("Deleting dir {}...", cassandraDataDir);
                FileUtils.deleteQuietly(new File(cassandraDataDir));
                log.debug("Deleted dir {}", cassandraDataDir);
            } else if (cdd.isFile()) {
                log.debug("Deleting file {}...", cassandraDataDir);
                cdd.delete();
                log.debug("Deleted file {}", cassandraDataDir);
            } else {
                log.debug("Cassandra data directory {} does not exist; " + "letting Cassandra startup script create it", cassandraDataDir);
            }
        }
        ProcessBuilder pb = new ProcessBuilder(cassandraCommand, "-f");
        Map<String, String> env = pb.environment();
        env.put("CASSANDRA_CONF", cassandraConfigDir);
        env.put("CASSANDRA_INCLUDE", cassandraInclude);
        pb.redirectErrorStream(true);
        // Tail Cassandra
        log.debug("Starting Cassandra process {}...", StringUtils.join(pb.command(), ' '));
        cassandraProcess = pb.start();
        log.debug("Started Cassandra process {}.", StringUtils.join(pb.command(), ' '));
        // Register a Cassandra-killer shutdown hook
        cassandraKiller = new CassandraKiller(cassandraProcess, address + ":" + port);
        Runtime.getRuntime().addShutdownHook(cassandraKiller);
        // Create Runnable to process Cassandra's stderr and stdout
        log.debug("Starting Cassandra output handler task...");
        outputReader = new CassandraOutputReader(cassandraProcess, address, port, logCassandraOutput);
        cassandraOutputLoggerFuture = cassandraOutputLogger.submit(outputReader);
        log.debug("Started Cassandra output handler task.");
        // Block in a loop until connection to the Thrift port succeeds
        long sleep = 0;
        long connectAttemptStartTime = System.currentTimeMillis();
        final long sleepGrowthIncrement = 100;
        log.debug("Attempting to connect to Cassandra's Thrift service on {}:{}...", address, port);
        while (!Thread.currentThread().isInterrupted()) {
            Socket s = new Socket();
            s.setSoTimeout(50);
            try {
                s.connect(new InetSocketAddress(address, port));
                long delay = System.currentTimeMillis() - connectAttemptStartTime;
                log.debug("Thrift connection to {}:{} succeeded " + "(about {} ms after process start)", new Object[] { address, port, delay });
                break;
            } catch (IOException e) {
                sleep += sleepGrowthIncrement;
                log.debug("Thrift connection failed; retrying in {} ms", sleep);
                Thread.sleep(sleep);
            } finally {
                if (s.isConnected()) {
                    s.close();
                    log.debug("Closed Thrift connection to {}:{}", address, port);
                }
            }
        }
        /*
			 * Check that the Cassandra process logged that it
			 * successfully bound its Thrift port.
			 *
			 * This detects
			 */
        log.debug("Waiting for Cassandra process to log successful Thrift-port bind...");
        if (!outputReader.awaitThrift(CASSANDRA_STARTUP_TIMEOUT, TimeUnit.MILLISECONDS)) {
            String msg = "Cassandra process failed to bind Thrift-port within timeout.";
            log.error(msg);
            throw new TemporaryStorageException(msg);
        }
        log.debug("Cassandra process logged successful Thrift-port bind.");
    } catch (Exception e) {
        e.printStackTrace();
        throw new TitanException(e);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) TTransportException(org.apache.thrift.transport.TTransportException) StorageException(com.thinkaurelius.titan.diskstorage.StorageException) TitanException(com.thinkaurelius.titan.core.TitanException) TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException) TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException) TitanException(com.thinkaurelius.titan.core.TitanException) Socket(java.net.Socket)

Example 9 with TemporaryStorageException

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

the class CassandraKiller method waitForClusterSize.

public void waitForClusterSize(int minSize) throws InterruptedException, StorageException {
    CTConnectionFactory f = new CTConnectionFactory(new String[] { address }, port, // username, password
    null, // username, password
    null, GraphDatabaseConfiguration.CONNECTION_TIMEOUT_DEFAULT, AbstractCassandraStoreManager.THRIFT_DEFAULT_FRAME_SIZE);
    CTConnection conn = null;
    try {
        conn = f.makeRawConnection();
        CTConnectionFactory.waitForClusterSize(conn.getClient(), minSize);
    } catch (TTransportException e) {
        throw new TemporaryStorageException(e);
    } finally {
        if (null != conn)
            if (conn.getTransport().isOpen())
                conn.getTransport().close();
    }
}
Also used : CTConnection(com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection) TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException) TTransportException(org.apache.thrift.transport.TTransportException) CTConnectionFactory(com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnectionFactory)

Example 10 with TemporaryStorageException

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

the class AbstractLocker method writeLock.

@Override
public void writeLock(KeyColumn lockID, StoreTransaction tx) throws TemporaryLockingException, PermanentLockingException {
    if (null != tx.getConfiguration().getMetricsPrefix()) {
        MetricManager.INSTANCE.getCounter(tx.getConfiguration().getMetricsPrefix(), M_LOCKS, M_WRITE, M_CALLS).inc();
    }
    if (lockState.has(tx, lockID)) {
        log.debug("Transaction {} already wrote lock on {}", tx, lockID);
        return;
    }
    if (lockLocally(lockID, tx)) {
        boolean ok = false;
        try {
            S stat = writeSingleLock(lockID, tx);
            // update local lock expiration time
            lockLocally(lockID, stat.getExpirationTimestamp(TimeUnit.NANOSECONDS), tx);
            lockState.take(tx, lockID, stat);
            ok = true;
        } catch (TemporaryStorageException tse) {
            throw new TemporaryLockingException(tse);
        } catch (AssertionError ae) {
            // Concession to ease testing with mocks & behavior verification
            ok = true;
            throw ae;
        } catch (Throwable t) {
            throw new PermanentLockingException(t);
        } finally {
            if (!ok) {
                // lockState.release(tx, lockID); // has no effect
                unlockLocally(lockID, tx);
                if (null != tx.getConfiguration().getMetricsPrefix()) {
                    MetricManager.INSTANCE.getCounter(tx.getConfiguration().getMetricsPrefix(), M_LOCKS, M_WRITE, M_EXCEPTIONS).inc();
                }
            }
        }
    } else {
        // Fail immediately with no retries on local contention
        throw new PermanentLockingException("Local lock contention");
    }
}
Also used : NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) TemporaryStorageException(com.thinkaurelius.titan.diskstorage.TemporaryStorageException)

Aggregations

TemporaryStorageException (com.thinkaurelius.titan.diskstorage.TemporaryStorageException)37 StorageException (com.thinkaurelius.titan.diskstorage.StorageException)11 PermanentStorageException (com.thinkaurelius.titan.diskstorage.PermanentStorageException)10 IOException (java.io.IOException)10 StaticBuffer (com.thinkaurelius.titan.diskstorage.StaticBuffer)9 ConnectionException (com.netflix.astyanax.connectionpool.exceptions.ConnectionException)6 Test (org.junit.Test)6 CTConnection (com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool.CTConnection)5 InvalidRequestException (org.apache.cassandra.thrift.InvalidRequestException)5 NotFoundException (org.apache.cassandra.thrift.NotFoundException)5 SchemaDisagreementException (org.apache.cassandra.thrift.SchemaDisagreementException)5 TException (org.apache.thrift.TException)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 ConsistentKeyLockStatus (com.thinkaurelius.titan.diskstorage.locking.consistentkey.ConsistentKeyLockStatus)4 ByteBuffer (java.nio.ByteBuffer)4 Cassandra (org.apache.cassandra.thrift.Cassandra)4 CfDef (org.apache.cassandra.thrift.CfDef)4 KsDef (org.apache.cassandra.thrift.KsDef)4 TitanException (com.thinkaurelius.titan.core.TitanException)3 Entry (com.thinkaurelius.titan.diskstorage.keycolumnvalue.Entry)3