@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 @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); } }
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); } }
@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<Object, Object>(cf.getName(), null, null)); } } catch (ConnectionException e) { throw new PermanentBackendException(e); } }
@Override public Map<String, String> getCompressionOptions(String cf) throws BackendException { try { Keyspace k = keyspaceContext.getClient(); KeyspaceDefinition kdef = k.describeKeyspace(); if (null == kdef) { throw new PermanentBackendException("Keyspace " + k.getKeyspaceName() + " is undefined"); } ColumnFamilyDefinition cfdef = kdef.getColumnFamily(cf); if (null == cfdef) { throw new PermanentBackendException("Column family " + cf + " is undefined"); } return cfdef.getCompressionOptions(); } catch (ConnectionException e) { throw new PermanentBackendException(e); } } }
@Override public void mutateMany(Map<String, Map<StaticBuffer, KCVMutation>> batch, StoreTransaction txh) throws BackendException { MutationBatch m = keyspaceContext.getClient().prepareMutationBatch().withAtomicBatch(atomicBatch) .setConsistencyLevel(getTx(txh).getWriteConsistencyLevel().getAstyanax()) .withRetryPolicy(retryPolicy.duplicate());
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); }
/** * @deprecated This should be called getClient * @return */ public T getEntity() { return getClient(); }
public CassandraGatewayProd(AstyanaxContext<Keyspace> context) { keyspace = context.getClient(); }
public CassandraGatewayProd(AstyanaxContext<Keyspace> context) { keyspace = context.getClient(); }
@Override public Keyspace connect(String keyspaceName) { try { return _astyanaxContext.getClient().getKeyspace(keyspaceName); } catch (ConnectionException e) { throw Throwables.propagate(e); } } }
@Override public void init() { ks = ksContext.getClient(); ksContext.start(); }
@Override public Keyspace connect(String keyspaceName) { try { return _astyanaxContext.getClient().getKeyspace(keyspaceName); } catch (ConnectionException e) { throw Throwables.propagate(e); } } }
@Override public synchronized AstyanaxKeyColumnValueStore openDatabase(String name) 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 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 Keyspace createKeyspaceIfNotExists() { // Don't do in production; better to create from cqlsh to avoid parallel issues from eventual consistency. Keyspace ks = null; try { ks = keyspaceContext.getClient(); Properties props = new Properties(); props.setProperty("name", KEYSPACE_NAME); props.setProperty("strategy_class", "SimpleStrategy"); props.setProperty("strategy_options.replication_factor", "1"); ks.createKeyspaceIfNotExists(props); KeyspaceDefinition ksDef = ks.describeKeyspace(); } catch (Exception e) { logger.info("Didn't (re)create keyspace, message={}", e.getMessage()); } return ks; }
@Override @SuppressWarnings("unchecked") public IPartitioner<? extends Token<?>> 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 @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); } }