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<String, AstyanaxKeyColumnValueStore>(8); }
public CassandraStorage(CassandraDataSegmentConfig config) { this.astyanaxContext = new AstyanaxContext.Builder() .forCluster(CLUSTER_NAME) .forKeyspace(config.getKeyspace()) .withAstyanaxConfiguration(new AstyanaxConfigurationImpl().setDiscoveryType(NodeDiscoveryType.NONE)) .withConnectionPoolConfiguration( new ConnectionPoolConfigurationImpl("MyConnectionPool").setMaxConnsPerHost(10) .setSeeds(config.getHost())).withConnectionPoolMonitor(new CountingConnectionPoolMonitor()) .buildKeyspace(ThriftFamilyFactory.getInstance()); this.astyanaxContext.start(); this.keyspace = this.astyanaxContext.getEntity(); this.config = config; indexStorage = new CassandraChunkedStorageProvider(keyspace, INDEX_TABLE_NAME); descriptorStorage = new ColumnFamily<String, String>(DESCRIPTOR_TABLE_NAME, StringSerializer.get(), StringSerializer.get()); } }
@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; } }
private static AstyanaxContext<Cluster> createCluster(AstyanaxContext.Builder cb) { AstyanaxContext<Cluster> clusterCtx = cb.buildCluster(ThriftFamilyFactory.getInstance()); clusterCtx.start(); return clusterCtx; }
protected static Boolean areVersionsConsistent(CassandraNode node) { AstyanaxContext<Cluster> context = null; try { context = new AstyanaxSample(node).newAstyanaxContextForCluster(); Map<String, List<String>> v = context.getEntity().describeSchemaVersions(); return v.size() == 1; } catch (Exception e) { return null; } finally { if (context != null) context.shutdown(); } }
public void start(Map<String, Object> config) { try { List<AstyanaxContext<Keyspace>> contexts = createContext(config); for (AstyanaxContext<Keyspace> context : contexts) { this.addAstyanaxContext(context.getKeyspaceName(), context); this.getAstyanaxContext(context.getKeyspaceName()).start(); // test the connection this.getKeyspace(context.getKeyspaceName()).describeKeyspace(); } } catch (Throwable e) { LOG.warn("Astyanax initialization failed.", e); throw new IllegalStateException("Failed to prepare Astyanax", e); } }
public Keyspace getKeyspace() { return this.getAstyanaxContext().getEntity(); }
@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 void start() throws Exception { _astyanaxContext.start(); }
public static void main(String[] args) throws Exception { AstyanaxSample support = new AstyanaxSample("ignored", "ec2-79-125-32-2.eu-west-1.compute.amazonaws.com", 9160); AstyanaxContext<Cluster> context = support.newAstyanaxContextForCluster(); try { System.out.println(context.getEntity().describeSchemaVersions()); } finally { context.shutdown(); } } }
public Keyspace getKeyspace(String keyspace) { return this.getAstyanaxContext(keyspace).getEntity(); }
getCluster( cassandraFig.getClusterName(), config, sharedPoolConfig ); sharedClusterContext.start(); Cluster sharedCluster = sharedClusterContext.getClient(); applicationClusterContext.start(); locksClusterContext.start(); Cluster applicationCluster = applicationClusterContext.getClient(); Cluster locksCluster = locksClusterContext.getClient();
private void ensureColumnFamilyExists(String name, String comparator) throws BackendException { Cluster cl = clusterContext.getClient(); try { KeyspaceDefinition ksDef = cl.describeKeyspace(keySpaceName); boolean found = false; if (null != ksDef) { for (ColumnFamilyDefinition cfDef : ksDef.getColumnFamilyList()) { found |= cfDef.getName().equals(name); } } if (!found) { ColumnFamilyDefinition cfDef = cl.makeColumnFamilyDefinition() .setName(name) .setKeyspace(keySpaceName) .setComparatorType(comparator); ImmutableMap.Builder<String, String> compressionOptions = new ImmutableMap.Builder<String, String>(); if (compressionEnabled) { compressionOptions.put("sstable_compression", compressionClass) .put("chunk_length_kb", Integer.toString(compressionChunkSizeKB)); } cl.addColumnFamily(cfDef.setCompressionOptions(compressionOptions.build())); } } catch (ConnectionException e) { throw new TemporaryBackendException(e); } }
@SuppressWarnings({ "rawtypes", "unchecked" }) public CassandraMapState(Options<T> options, Map conf) { this.options = options; this.serializer = options.serializer; AstyanaxContext<Keyspace> context = createContext((Map<String, Object>) conf.get(options.clientConfigKey)); context.start(); this.keyspace = context.getEntity(); }
@Override public void start() throws Exception { _astyanaxContext.start(); }
/** * Read from a {@link CassandraNode} using the Astyanax API. * @throws ConnectionException */ public void readData(String keyspaceName) throws ConnectionException { // Create context AstyanaxContext<Keyspace> context = newAstyanaxContextForKeyspace(keyspaceName); try { Keyspace keyspace = context.getEntity(); // Query data OperationResult<ColumnList<String>> query = keyspace.prepareQuery(sampleColumnFamily) .getKey("one") .execute(); assertEquals(query.getHost().getHostName(), hostname); assertTrue(query.getLatency() > 0L); ColumnList<String> columns = query.getResult(); assertEquals(columns.size(), 2); // Lookup columns in response by name String name = columns.getColumnByName("name").getStringValue(); assertEquals(name, "Alice"); // Iterate through the columns for (Column<String> c : columns) { assertTrue(ImmutableList.of("name", "company").contains(c.getName())); } } finally { context.shutdown(); } }