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);
}
}
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);
}
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);
}
}
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();
}
}
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");
}
}
Aggregations