use of org.apache.cassandra.thrift.KsDef in project atlasdb by palantir.
the class CassandraVerifier method sanityCheckDatacenters.
static Set<String> sanityCheckDatacenters(CassandraClient client, CassandraKeyValueServiceConfig config) throws TException {
Set<String> hosts = Sets.newHashSet();
Multimap<String, String> dataCenterToRack = HashMultimap.create();
List<String> existingKeyspaces = Lists.transform(client.describe_keyspaces(), KsDef::getName);
if (!existingKeyspaces.contains(CassandraConstants.SIMPLE_RF_TEST_KEYSPACE)) {
client.system_add_keyspace(new KsDef(CassandraConstants.SIMPLE_RF_TEST_KEYSPACE, CassandraConstants.SIMPLE_STRATEGY, ImmutableList.of()).setStrategy_options(ImmutableMap.of(CassandraConstants.REPLICATION_FACTOR_OPTION, "1")));
}
List<TokenRange> ring = client.describe_ring(CassandraConstants.SIMPLE_RF_TEST_KEYSPACE);
for (TokenRange tokenRange : ring) {
for (EndpointDetails details : tokenRange.getEndpoint_details()) {
dataCenterToRack.put(details.datacenter, details.rack);
hosts.add(details.host);
}
}
if (dataCenterToRack.size() == 1) {
String dc = dataCenterToRack.keySet().iterator().next();
String rack = dataCenterToRack.values().iterator().next();
if (dc.equals(CassandraConstants.DEFAULT_DC) && rack.equals(CassandraConstants.DEFAULT_RACK) && config.replicationFactor() > 1) {
// We don't allow greater than RF=1 because they didn't set up their node topology
logErrorOrThrow("The cassandra cluster is not set up to be datacenter and rack aware. " + "Please set up Cassandra to use NetworkTopology and add corresponding snitch information " + "before running with a replication factor higher than 1. " + "If you're running in some sort of environment where nodes have no known correlated " + "failure patterns, you can set the 'ignoreNodeTopologyChecks' KVS config option.", config.ignoreNodeTopologyChecks());
}
if (dataCenterToRack.values().size() < config.replicationFactor() && hosts.size() > config.replicationFactor()) {
logErrorOrThrow("The cassandra cluster only has one DC, " + "and is set up with less racks than the desired number of replicas, " + "and there are more hosts than the replication factor. " + "It is very likely that your rack configuration is incorrect and replicas " + "would not be placed correctly for the failure tolerance you want. " + "If you fully understand how NetworkTopology replica placement strategy will be placing " + "your replicas, feel free to set the 'ignoreNodeTopologyChecks' KVS config option.", config.ignoreNodeTopologyChecks());
}
}
return dataCenterToRack.keySet();
}
use of org.apache.cassandra.thrift.KsDef in project atlasdb by palantir.
the class CassandraVerifier method currentRfOnKeyspaceMatchesDesiredRf.
static void currentRfOnKeyspaceMatchesDesiredRf(CassandraClient client, CassandraKeyValueServiceConfig config) throws TException {
KsDef ks = client.describe_keyspace(config.getKeyspaceOrThrow());
Set<String> dcs = sanityCheckDatacenters(client, config);
sanityCheckReplicationFactor(ks, config, dcs);
}
use of org.apache.cassandra.thrift.KsDef in project atlasdb by palantir.
the class CassandraVerifier method attemptToCreateKeyspaceOnHost.
private static void attemptToCreateKeyspaceOnHost(InetSocketAddress host, CassandraKeyValueServiceConfig config) throws TException {
CassandraClient client = CassandraClientFactory.getClientInternal(host, config);
KsDef ks = new KsDef(config.getKeyspaceOrThrow(), CassandraConstants.NETWORK_STRATEGY, ImmutableList.of());
checkAndSetReplicationFactor(client, ks, true, config);
ks.setDurable_writes(true);
client.system_add_keyspace(ks);
log.info("Created keyspace: {}", UnsafeArg.of("keyspace", config.getKeyspaceOrThrow()));
CassandraKeyValueServices.waitForSchemaVersions(config, client, "(adding the initial empty keyspace)", true);
}
use of org.apache.cassandra.thrift.KsDef 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);
}
}
use of org.apache.cassandra.thrift.KsDef 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);
}
}
Aggregations