
com.thinkaurelius.titan.diskstorage.cassandra.astyanax.AstyanaxStoreManager Maven / Gradle / Ivy
The newest version!
package com.thinkaurelius.titan.diskstorage.cassandra.astyanax;
import com.google.common.base.Preconditions;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableMap;
import com.netflix.astyanax.AstyanaxContext;
import com.netflix.astyanax.Cluster;
import com.netflix.astyanax.ColumnListMutation;
import com.netflix.astyanax.Keyspace;
import com.netflix.astyanax.MutationBatch;
import com.netflix.astyanax.connectionpool.Host;
import com.netflix.astyanax.connectionpool.NodeDiscoveryType;
import com.netflix.astyanax.connectionpool.RetryBackoffStrategy;
import com.netflix.astyanax.connectionpool.SSLConnectionContext;
import com.netflix.astyanax.connectionpool.exceptions.ConnectionException;
import com.netflix.astyanax.connectionpool.impl.ConnectionPoolConfigurationImpl;
import com.netflix.astyanax.connectionpool.impl.ConnectionPoolType;
import com.netflix.astyanax.connectionpool.impl.CountingConnectionPoolMonitor;
import com.netflix.astyanax.connectionpool.impl.ExponentialRetryBackoffStrategy;
import com.netflix.astyanax.connectionpool.impl.SimpleAuthenticationCredentials;
import com.netflix.astyanax.ddl.ColumnFamilyDefinition;
import com.netflix.astyanax.ddl.KeyspaceDefinition;
import com.netflix.astyanax.impl.AstyanaxConfigurationImpl;
import com.netflix.astyanax.model.ColumnFamily;
import com.netflix.astyanax.retry.RetryPolicy;
import com.netflix.astyanax.thrift.ThriftFamilyFactory;
import com.thinkaurelius.titan.diskstorage.BackendException;
import com.thinkaurelius.titan.diskstorage.Entry;
import com.thinkaurelius.titan.diskstorage.EntryMetaData;
import com.thinkaurelius.titan.diskstorage.PermanentBackendException;
import com.thinkaurelius.titan.diskstorage.StaticBuffer;
import com.thinkaurelius.titan.diskstorage.StoreMetaData;
import com.thinkaurelius.titan.diskstorage.TemporaryBackendException;
import com.thinkaurelius.titan.diskstorage.cassandra.AbstractCassandraStoreManager;
import com.thinkaurelius.titan.diskstorage.configuration.ConfigNamespace;
import com.thinkaurelius.titan.diskstorage.configuration.ConfigOption;
import com.thinkaurelius.titan.diskstorage.configuration.Configuration;
import com.thinkaurelius.titan.diskstorage.keycolumnvalue.KCVMutation;
import com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeyRange;
import com.thinkaurelius.titan.diskstorage.keycolumnvalue.StoreTransaction;
import com.thinkaurelius.titan.graphdb.configuration.PreInitializeConfigOptions;
import org.apache.cassandra.dht.IPartitioner;
import org.apache.cassandra.dht.Token;
import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.cassandra.utils.FBUtilities;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Constructor;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static com.thinkaurelius.titan.diskstorage.cassandra.CassandraTransaction.getTx;
@PreInitializeConfigOptions
public class AstyanaxStoreManager extends AbstractCassandraStoreManager {
private static final Logger log = LoggerFactory.getLogger(AstyanaxStoreManager.class);
//################### ASTYANAX SPECIFIC CONFIGURATION OPTIONS ######################
public static final ConfigNamespace ASTYANAX_NS =
new ConfigNamespace(CASSANDRA_NS, "astyanax", "Astyanax-specific Cassandra options");
/**
* Default name for the Cassandra cluster
*
*/
public static final ConfigOption CLUSTER_NAME =
new ConfigOption(ASTYANAX_NS, "cluster-name",
"Default name for the Cassandra cluster",
ConfigOption.Type.MASKABLE, "Titan Cluster");
/**
* Maximum pooled connections per host.
*
*/
public static final ConfigOption MAX_CONNECTIONS_PER_HOST =
new ConfigOption(ASTYANAX_NS, "max-connections-per-host",
"Maximum pooled connections per host",
ConfigOption.Type.MASKABLE, 32);
/**
* Maximum open connections allowed in the pool (counting all hosts).
*
*/
public static final ConfigOption MAX_CONNECTIONS =
new ConfigOption(ASTYANAX_NS, "max-connections",
"Maximum open connections allowed in the pool (counting all hosts)",
ConfigOption.Type.MASKABLE, -1);
/**
* Maximum number of operations allowed per connection before the connection is closed.
*
*/
public static final ConfigOption MAX_OPERATIONS_PER_CONNECTION =
new ConfigOption(ASTYANAX_NS, "max-operations-per-connection",
"Maximum number of operations allowed per connection before the connection is closed",
ConfigOption.Type.MASKABLE, 100 * 1000);
/**
* Maximum pooled "cluster" connections per host.
*
* These connections are mostly idle and only used for DDL operations
* (like creating keyspaces). Titan doesn't need many of these connections
* in ordinary operation.
*/
public static final ConfigOption MAX_CLUSTER_CONNECTIONS_PER_HOST =
new ConfigOption(ASTYANAX_NS, "max-cluster-connections-per-host",
"Maximum pooled \"cluster\" connections per host",
ConfigOption.Type.MASKABLE, 3);
/**
* How Astyanax discovers Cassandra cluster nodes. This must be one of the
* values of the Astyanax NodeDiscoveryType enum.
*
*/
public static final ConfigOption NODE_DISCOVERY_TYPE =
new ConfigOption(ASTYANAX_NS, "node-discovery-type",
"How Astyanax discovers Cassandra cluster nodes",
ConfigOption.Type.MASKABLE, "RING_DESCRIBE");
/**
* Astyanax specific host supplier useful only when discovery type set to DISCOVERY_SERVICE or TOKEN_AWARE.
* Excepts fully qualified class name which extends google.common.base.Supplier>.
*/
public static final ConfigOption HOST_SUPPLIER =
new ConfigOption(ASTYANAX_NS, "host-supplier",
"Host supplier to use when discovery type is set to DISCOVERY_SERVICE or TOKEN_AWARE",
ConfigOption.Type.MASKABLE, String.class);
/**
* Astyanax's connection pooler implementation. This must be one of the
* values of the Astyanax ConnectionPoolType enum.
*
*/
public static final ConfigOption CONNECTION_POOL_TYPE =
new ConfigOption(ASTYANAX_NS, "connection-pool-type",
"Astyanax's connection pooler implementation",
ConfigOption.Type.MASKABLE, "TOKEN_AWARE");
/**
* In Astyanax, RetryPolicy and RetryBackoffStrategy sound and look similar
* but are used for distinct purposes. RetryPolicy is for retrying failed
* operations. RetryBackoffStrategy is for retrying attempts to talk to
* uncommunicative hosts. This config option controls RetryPolicy.
*/
public static final ConfigOption RETRY_POLICY =
new ConfigOption(ASTYANAX_NS, "retry-policy",
"Astyanax's retry policy implementation with configuration parameters",
ConfigOption.Type.MASKABLE, "com.netflix.astyanax.retry.BoundedExponentialBackoff,100,25000,8");
/**
* If non-null, this must be the fully-qualified classname (i.e. the
* complete package name, a dot, and then the class name) of an
* implementation of Astyanax's RetryBackoffStrategy interface. This string
* may be followed by a sequence of integers, separated from the full
* classname and from each other by commas; in this case, the integers are
* cast to native Java ints and passed to the class constructor as
* arguments. Here's an example setting that would instantiate an Astyanax
* FixedRetryBackoffStrategy with an delay interval of 1s and suspend time
* of 5s:
*
*
* com.netflix.astyanax.connectionpool.impl.FixedRetryBackoffStrategy,1000,5000
*
*
* If null, then Astyanax uses its default strategy, which is an
* ExponentialRetryBackoffStrategy instance. The instance parameters take
* Astyanax's built-in default values, which can be overridden via the
* following config keys:
*
* - {@link #RETRY_DELAY_SLICE}
* - {@link #RETRY_MAX_DELAY_SLICE}
* - {@link #RETRY_SUSPEND_WINDOW}
*
*
* In Astyanax, RetryPolicy and RetryBackoffStrategy sound and look similar
* but are used for distinct purposes. RetryPolicy is for retrying failed
* operations. RetryBackoffStrategy is for retrying attempts to talk to
* uncommunicative hosts. This config option controls RetryBackoffStrategy.
*/
public static final ConfigOption RETRY_BACKOFF_STRATEGY =
new ConfigOption(ASTYANAX_NS, "retry-backoff-strategy",
"Astyanax's retry backoff strategy with configuration parameters",
ConfigOption.Type.MASKABLE, "com.netflix.astyanax.connectionpool.impl.FixedRetryBackoffStrategy,1000,5000");
/**
* Controls the retryDelaySlice parameter on Astyanax
* ConnectionPoolConfigurationImpl objects, which is in turn used by
* ExponentialRetryBackoffStrategy. See the code for
* {@link ConnectionPoolConfigurationImpl},
* {@link ExponentialRetryBackoffStrategy}, and the javadoc for
* {@link #RETRY_BACKOFF_STRATEGY} for more information.
*
* This parameter is not meaningful for and has no effect on
* FixedRetryBackoffStrategy.
*/
public static final ConfigOption RETRY_DELAY_SLICE =
new ConfigOption(ASTYANAX_NS, "retry-delay-slice",
"Astyanax's connection pool \"retryDelaySlice\" parameter",
ConfigOption.Type.MASKABLE, ConnectionPoolConfigurationImpl.DEFAULT_RETRY_DELAY_SLICE);
/**
* Controls the retryMaxDelaySlice parameter on Astyanax
* ConnectionPoolConfigurationImpl objects, which is in turn used by
* ExponentialRetryBackoffStrategy. See the code for
* {@link ConnectionPoolConfigurationImpl},
* {@link ExponentialRetryBackoffStrategy}, and the javadoc for
* {@link #RETRY_BACKOFF_STRATEGY} for more information.
*
* This parameter is not meaningful for and has no effect on
* FixedRetryBackoffStrategy.
*/
public static final ConfigOption RETRY_MAX_DELAY_SLICE =
new ConfigOption(ASTYANAX_NS, "retry-max-delay-slice",
"Astyanax's connection pool \"retryMaxDelaySlice\" parameter",
ConfigOption.Type.MASKABLE, ConnectionPoolConfigurationImpl.DEFAULT_RETRY_MAX_DELAY_SLICE);
/**
* Controls the retrySuspendWindow parameter on Astyanax
* ConnectionPoolConfigurationImpl objects, which is in turn used by
* ExponentialRetryBackoffStrategy. See the code for
* {@link ConnectionPoolConfigurationImpl},
* {@link ExponentialRetryBackoffStrategy}, and the javadoc for
* {@link #RETRY_BACKOFF_STRATEGY} for more information.
*
* This parameter is not meaningful for and has no effect on
* FixedRetryBackoffStrategy.
*/
public static final ConfigOption RETRY_SUSPEND_WINDOW =
new ConfigOption(ASTYANAX_NS, "retry-suspend-window",
"Astyanax's connection pool \"retryMaxDelaySlice\" parameter",
ConfigOption.Type.MASKABLE, ConnectionPoolConfigurationImpl.DEFAULT_RETRY_SUSPEND_WINDOW);
/**
* Controls the frame size of thrift sockets created by Astyanax.
*/
public static final ConfigOption THRIFT_FRAME_SIZE =
new ConfigOption(ASTYANAX_NS, "frame-size",
"The thrift frame size in mega bytes", ConfigOption.Type.MASKABLE, 15);
public static final ConfigOption LOCAL_DATACENTER =
new ConfigOption(ASTYANAX_NS, "local-datacenter",
"The name of the local or closest Cassandra datacenter. When set and not whitespace, " +
"this value will be passed into ConnectionPoolConfigurationImpl.setLocalDatacenter. " +
"When unset or set to whitespace, setLocalDatacenter will not be invoked.",
/* It's between either LOCAL or MASKABLE. MASKABLE could be useful for cases where
all the Titan instances are closest to the same Cassandra DC. */
ConfigOption.Type.MASKABLE, String.class);
private final String clusterName;
private final AstyanaxContext keyspaceContext;
private final AstyanaxContext clusterContext;
private final RetryPolicy retryPolicy;
private final int retryDelaySlice;
private final int retryMaxDelaySlice;
private final int retrySuspendWindow;
private final RetryBackoffStrategy retryBackoffStrategy;
private final String localDatacenter;
private final Map openStores;
public AstyanaxStoreManager(Configuration config) throws BackendException {
super(config);
this.clusterName = config.get(CLUSTER_NAME);
retryDelaySlice = config.get(RETRY_DELAY_SLICE);
retryMaxDelaySlice = config.get(RETRY_MAX_DELAY_SLICE);
retrySuspendWindow = config.get(RETRY_SUSPEND_WINDOW);
retryBackoffStrategy = getRetryBackoffStrategy(config.get(RETRY_BACKOFF_STRATEGY));
retryPolicy = getRetryPolicy(config.get(RETRY_POLICY));
localDatacenter = config.has(LOCAL_DATACENTER) ?
config.get(LOCAL_DATACENTER) : "";
final int maxConnsPerHost = config.get(MAX_CONNECTIONS_PER_HOST);
final int maxClusterConnsPerHost = config.get(MAX_CLUSTER_CONNECTIONS_PER_HOST);
this.clusterContext = createCluster(getContextBuilder(config, maxClusterConnsPerHost, "Cluster"));
ensureKeyspaceExists(clusterContext.getClient());
this.keyspaceContext = getContextBuilder(config, maxConnsPerHost, "Keyspace").buildKeyspace(ThriftFamilyFactory.getInstance());
this.keyspaceContext.start();
openStores = new HashMap(8);
}
@Override
public Deployment getDeployment() {
return Deployment.REMOTE; // TODO
}
@Override
@SuppressWarnings("unchecked")
public IPartitioner getCassandraPartitioner() throws BackendException {
Cluster cl = clusterContext.getClient();
try {
return FBUtilities.newPartitioner(cl.describePartitioner());
} catch (ConnectionException e) {
throw new TemporaryBackendException(e);
} catch (ConfigurationException e) {
throw new PermanentBackendException(e);
}
}
@Override
public String toString() {
return "astyanax" + super.toString();
}
@Override
public void close() {
// Shutdown the Astyanax contexts
openStores.clear();
keyspaceContext.shutdown();
clusterContext.shutdown();
}
@Override
public synchronized AstyanaxKeyColumnValueStore openDatabase(String name, StoreMetaData.Container metaData) throws BackendException {
if (openStores.containsKey(name)) return openStores.get(name);
else {
ensureColumnFamilyExists(name);
AstyanaxKeyColumnValueStore store = new AstyanaxKeyColumnValueStore(name, keyspaceContext.getClient(), this, retryPolicy);
openStores.put(name, store);
return store;
}
}
@Override
public void mutateMany(Map> batch, StoreTransaction txh) throws BackendException {
MutationBatch m = keyspaceContext.getClient().prepareMutationBatch().withAtomicBatch(atomicBatch)
.setConsistencyLevel(getTx(txh).getWriteConsistencyLevel().getAstyanax())
.withRetryPolicy(retryPolicy.duplicate());
final MaskedTimestamp commitTime = new MaskedTimestamp(txh);
for (Map.Entry> batchentry : batch.entrySet()) {
String storeName = batchentry.getKey();
Preconditions.checkArgument(openStores.containsKey(storeName), "Store cannot be found: " + storeName);
ColumnFamily columnFamily = openStores.get(storeName).getColumnFamily();
Map mutations = batchentry.getValue();
for (Map.Entry ent : mutations.entrySet()) {
// The CLMs for additions and deletions are separated because
// Astyanax's operation timestamp cannot be set on a per-delete
// or per-addition basis.
KCVMutation titanMutation = ent.getValue();
ByteBuffer key = ent.getKey().asByteBuffer();
if (titanMutation.hasDeletions()) {
ColumnListMutation dels = m.withRow(columnFamily, key);
dels.setTimestamp(commitTime.getDeletionTime(times));
for (StaticBuffer b : titanMutation.getDeletions())
dels.deleteColumn(b.as(StaticBuffer.BB_FACTORY));
}
if (titanMutation.hasAdditions()) {
ColumnListMutation upds = m.withRow(columnFamily, key);
upds.setTimestamp(commitTime.getAdditionTime(times));
for (Entry e : titanMutation.getAdditions()) {
Integer ttl = (Integer) e.getMetaData().get(EntryMetaData.TTL);
if (null != ttl && ttl > 0) {
upds.putColumn(e.getColumnAs(StaticBuffer.BB_FACTORY), e.getValueAs(StaticBuffer.BB_FACTORY), ttl);
} else {
upds.putColumn(e.getColumnAs(StaticBuffer.BB_FACTORY), e.getValueAs(StaticBuffer.BB_FACTORY));
}
}
}
}
}
try {
m.execute();
} catch (ConnectionException e) {
throw new TemporaryBackendException(e);
}
sleepAfterWrite(txh, commitTime);
}
@Override
public List getLocalKeyPartition() throws BackendException {
throw new UnsupportedOperationException();
}
@Override
public void clearStorage() throws BackendException {
try {
Cluster cluster = clusterContext.getClient();
Keyspace ks = cluster.getKeyspace(keySpaceName);
// Not a big deal if Keyspace doesn't not exist (dropped manually by user or tests).
// This is called on per test setup basis to make sure that previous test cleaned
// everything up, so first invocation would always fail as Keyspace doesn't yet exist.
if (ks == null)
return;
for (ColumnFamilyDefinition cf : cluster.describeKeyspace(keySpaceName).getColumnFamilyList()) {
ks.truncateColumnFamily(new ColumnFamily
© 2015 - 2025 Weber Informatics LLC | Privacy Policy