protected AstyanaxContext<Keyspace> createContext(Map<String, Object> config) { Map<String, Object> settings = Maps.newHashMap(); for (Map.Entry<String, Object> defaultEntry : DEFAULTS.entrySet()) { if (config.containsKey(defaultEntry.getKey())) { settings.put(defaultEntry.getKey(), config.get(defaultEntry.getKey())); } else { settings.put(defaultEntry.getKey(), defaultEntry.getValue()); } } // in the defaults case, we don't know the seed hosts until context // creation time if (settings.get(ASTYANAX_CONNECTION_POOL_CONFIGURATION) instanceof ConnectionPoolConfigurationImpl) { ConnectionPoolConfigurationImpl cpConfig = (ConnectionPoolConfigurationImpl) settings .get(ASTYANAX_CONNECTION_POOL_CONFIGURATION); cpConfig.setSeeds((String) config.get(StormCassandraConstants.CASSANDRA_HOST)); } return new AstyanaxContext.Builder() .forCluster((String) settings.get(CASSANDRA_CLUSTER_NAME)) .forKeyspace((String) config.get(StormCassandraConstants.CASSANDRA_STATE_KEYSPACE)) .withAstyanaxConfiguration((AstyanaxConfiguration) settings.get(ASTYANAX_CONFIGURATION)) .withConnectionPoolConfiguration( (ConnectionPoolConfiguration) settings.get(ASTYANAX_CONNECTION_POOL_CONFIGURATION)) .withConnectionPoolMonitor((ConnectionPoolMonitor) settings.get(ASTYANAX_CONNECTION_POOL_MONITOR)) .buildKeyspace(ThriftFamilyFactory.getInstance()); }
AstyanaxContext.Builder ctxBuilder = new AstyanaxContext.Builder(); .forCluster(clusterName) .forKeyspace(keySpaceName) .withAstyanaxConfiguration(aconf) .withConnectionPoolConfiguration(cpool) .withConnectionPoolMonitor(new CountingConnectionPoolMonitor()); try { supplier = (Supplier<List<Host>>) Class.forName(hostSupplier).newInstance(); ctxBuilder.withHostSupplier(supplier); } catch (Exception e) { log.warn("Problem with host supplier class " + hostSupplier + ", going to use default.", e);
Builder builder = new AstyanaxContext.Builder(); builder.forCluster(getClusterName()); builder.forKeyspace(getKeyspaceName()); builder.withAstyanaxConfiguration(configuration); builder.withConnectionPoolConfiguration(poolConfig); builder.withConnectionPoolMonitor(connectionPoolMonitor); builder.withConnectionPoolMonitor(new CountingConnectionPoolMonitor()); context = builder.buildKeyspace(ThriftFamilyFactory.getInstance()); context.start(); keyspace = context.getEntity();
AstyanaxContext.Builder ctxBuilder = new AstyanaxContext.Builder(); .forCluster(clusterName) .forKeyspace(keySpaceName) .withAstyanaxConfiguration(aconf) .withConnectionPoolConfiguration(cpool) .withConnectionPoolMonitor(new CountingConnectionPoolMonitor()); try { supplier = (Supplier<List<Host>>) Class.forName(hostSupplier).newInstance(); ctxBuilder.withHostSupplier(supplier); } catch (Exception e) { log.warn("Problem with host supplier class " + hostSupplier + ", going to use default.", e);
return new AstyanaxContext.Builder() .withAstyanaxConfiguration(asConfig) .withConnectionPoolConfiguration(poolConfiguration) .withConnectionPoolMonitor(poolMonitor);
return new AstyanaxContext.Builder() .withAstyanaxConfiguration(asConfig) .withConnectionPoolConfiguration(poolConfiguration) .withConnectionPoolMonitor(poolMonitor);
Collection<String> keyspaces = (Collection<String>) config.get(StormCassandraConstants.CASSANDRA_KEYSPACE); for (String keyspace : keyspaces) { returnVal.add(new AstyanaxContext.Builder() .forCluster((String) settings.get(CASSANDRA_CLUSTER_NAME)) .forKeyspace(keyspace) .withAstyanaxConfiguration((AstyanaxConfiguration) settings.get(ASTYANAX_CONFIGURATION)) .withConnectionPoolConfiguration( (ConnectionPoolConfiguration) settings .get(ASTYANAX_CONNECTION_POOL_CONFIGURATION)) .withConnectionPoolMonitor( (ConnectionPoolMonitor) settings.get(ASTYANAX_CONNECTION_POOL_MONITOR)) .buildKeyspace(ThriftFamilyFactory.getInstance()));
AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder() .forCluster(TEST_CLUSTER_NAME) .forKeyspace(keyspaceName) .withAstyanaxConfiguration( new AstyanaxConfigurationImpl() .setDiscoveryType(NodeDiscoveryType.DISCOVERY_SERVICE) .setTargetCassandraVersion("1.2") .withConnectionPoolConfiguration( new ConnectionPoolConfigurationImpl(TEST_CLUSTER_NAME + "_" + TEST_KEYSPACE_NAME) .setPort(9160) .withHostSupplier(HostSupplier) .withConnectionPoolMonitor(new CountingConnectionPoolMonitor()) .buildKeyspace(ThriftFamilyFactory.getInstance());
AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder() .forCluster(CLUSTER_NAME) .forKeyspace(k) .withAstyanaxConfiguration(new AstyanaxConfigurationImpl() .setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE) .setConnectionPoolType(ConnectionPoolType.TOKEN_AWARE) .setDefaultReadConsistencyLevel(ConsistencyLevel.valueOf("CL_"+CMBProperties.getInstance().getReadConsistencyLevel())) .setDefaultWriteConsistencyLevel(ConsistencyLevel.valueOf("CL_"+CMBProperties.getInstance().getWriteConsistencyLevel()))) .withConnectionPoolConfiguration(connectionPoolConfiguration) .withConnectionPoolMonitor(new CountingConnectionPoolMonitor()) .buildKeyspace(ThriftFamilyFactory.getInstance()); context.start(); Keyspace keyspace = context.getClient();
public void init() { logger.debug("init()"); keyspaceContext = new AstyanaxContext.Builder() .forCluster("Test Cluster") .forKeyspace(KEYSPACE_NAME) .withAstyanaxConfiguration(new AstyanaxConfigurationImpl() .setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE) .withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("MyConnectionPool") .setPort(9160) .setMaxConnsPerHost(1) .setSeeds("127.0.0.1:9160") .withAstyanaxConfiguration(new AstyanaxConfigurationImpl() .setCqlVersion("3.0.0") .setTargetCassandraVersion("1.2")) .withConnectionPoolMonitor(new CountingConnectionPoolMonitor()) .buildKeyspace(ThriftFamilyFactory.getInstance());
setAstynaxConfiguration(ConfigurationManager.getConfigInstance()); AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder() .forKeyspace(DynamicPropertyFactory.getInstance().getStringProperty(ZUUL_CASSANDRA_KEYSPACE, "zuul_scripts").get()) .withAstyanaxConfiguration(new AstyanaxConfigurationImpl() .setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE) .withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("cass_connection_pool") .setPort(DynamicPropertyFactory.getInstance().getIntProperty(ZUUL_CASSANDRA_PORT, 7102).get()) .setMaxConnsPerHost(DynamicPropertyFactory.getInstance().getIntProperty(ZUUL_CASSANDRA_MAXCONNECTIONSPERHOST, 1).get()) .withConnectionPoolMonitor(new CountingConnectionPoolMonitor()) .buildKeyspace(ThriftFamilyFactory.getInstance());
setAstynaxConfiguration(ConfigurationManager.getConfigInstance()); AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder() .forKeyspace(DynamicPropertyFactory.getInstance().getStringProperty(ZUUL_CASSANDRA_KEYSPACE, "zuul_scripts").get()) .withAstyanaxConfiguration(new AstyanaxConfigurationImpl() .setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE) .withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("cass_connection_pool") .setPort(DynamicPropertyFactory.getInstance().getIntProperty(ZUUL_CASSANDRA_PORT, 7102).get()) .setMaxConnsPerHost(DynamicPropertyFactory.getInstance().getIntProperty(ZUUL_CASSANDRA_MAXCONNECTIONSPERHOST, 1).get()) .withConnectionPoolMonitor(new CountingConnectionPoolMonitor()) .buildKeyspace(ThriftFamilyFactory.getInstance());
AstyanaxContext<Cluster> context = new AstyanaxContext.Builder() .forCluster(clusterName) .withAstyanaxConfiguration( new AstyanaxConfigurationImpl() .setDiscoveryType(NodeDiscoveryType.DISCOVERY_SERVICE) .setConnectionPoolType(ConnectionPoolType.ROUND_ROBIN) .setDiscoveryDelayInSeconds(60000)) .withConnectionPoolConfiguration( new ConnectionPoolConfigurationImpl(TEST_CLUSTER_NAME + "_" + TEST_KEYSPACE_NAME) .setPort(9160) .withHostSupplier(HostSupplier) .withConnectionPoolMonitor(new CountingConnectionPoolMonitor()) .buildCluster(ThriftFamilyFactory.getInstance());
AstyanaxContext<Cluster> context = new AstyanaxContext.Builder() .forCluster(clusterName) .withAstyanaxConfiguration( new AstyanaxConfigurationImpl() .setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE) .setDiscoveryDelayInSeconds(60000)) .withConnectionPoolConfiguration( new ConnectionPoolConfigurationImpl(TEST_CLUSTER_NAME + "_" + TEST_KEYSPACE_NAME) .setPort(9042) .withHostSupplier(HostSupplier) .withConnectionPoolMonitor(new CountingConnectionPoolMonitor()) .buildCluster(CqlFamilyFactory.getInstance());
public AstyanaxCluster cluster() { checkNotNull(_cluster, "cluster"); String metricName; ConnectionPoolConfiguration poolConfig; if (_keyspace == null) { // Use the shared pool configuration metricName = Objects.firstNonNull(_clusterMetric, _cluster); poolConfig = CassandraConfiguration.this; } else { // Use the configuration specifically for this keyspace KeyspaceConfiguration keyspaceConfig = checkNotNull(_keyspaces.get(_keyspace), "keyspaceConfig"); metricName = Objects.firstNonNull(keyspaceConfig.getKeyspaceMetric(), _keyspace); poolConfig = keyspaceConfig; } AstyanaxContext.Builder builder = newAstyanaxBuilder(_cluster, poolConfig, _metricRegistry) .forCluster(_cluster); if (!_disableClusterMetrics) { builder = builder .withTracerFactory(new InstrumentedTracerFactory(metricName, _metricRegistry)) .withConnectionPoolMonitor(new MetricConnectionPoolMonitor(metricName, _metricRegistry)); } AstyanaxContext<Cluster> astyanaxContext = builder.buildCluster(ThriftFamilyFactory.getInstance()); return new AstyanaxCluster(astyanaxContext, _cluster, _dataCenter); } }
public void init() { logger.debug("init()"); keyspaceContext = new AstyanaxContext.Builder() .forCluster("Test Cluster") .forKeyspace(KEYSPACE_NAME) .withAstyanaxConfiguration(new AstyanaxConfigurationImpl() .setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE) .withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("MyConnectionPool") .setPort(9160) .setMaxConnsPerHost(1) .setSeeds("127.0.0.1:9160") .withAstyanaxConfiguration(new AstyanaxConfigurationImpl() .setCqlVersion("3.0.0") .setTargetCassandraVersion("1.2")) .withConnectionPoolMonitor(new CountingConnectionPoolMonitor()) .buildKeyspace(ThriftFamilyFactory.getInstance());
@Nonnull @Override public Collection<Runnable> getRunnables(@Nonnull UUID jobId, int sliceId, @Nonnull UUID workerId, @Nonnull ScopedProgressClient progressClient) throws IOException { context = new AstyanaxContext.Builder().forCluster(clusterName) .forKeyspace(keyspaceName) .withAstyanaxConfiguration(new AstyanaxConfigurationImpl().setDiscoveryType(NodeDiscoveryType.NONE)) .withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("defaultConnPool") .setPort(port) .setMaxConnsPerHost(100) .setSeeds(seeds)) .withConnectionPoolMonitor(new Slf4jConnectionPoolMonitorImpl()) .buildKeyspace(ThriftFamilyFactory.getInstance()); context.start(); Keyspace keyspace = context.getEntity(); ColumnFamily<byte[], byte[]> cfDef = new ColumnFamily<>(columnFamilyName, BytesArraySerializer.get(), BytesArraySerializer.get()); ArrayList<Runnable> runnables = Lists.newArrayList(); byte[] colNameBytes = colName.getBytes(Charsets.UTF_8); for (int i = 0; i < numThreads; i++) { runnables.add(new CassandraRunnable(taskOperation, keyGeneratorFactory.getKeyGenerator(), workerId, sliceId, numQuanta, batchSize, jobId, valueGeneratorFactory.getValueGenerator(), keyspace, cfDef, colNameBytes)); } return runnables; }
/** * Builds a {@link Keyspace} from current configuration. */ public Keyspace build() { final AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder() .forCluster(clusterName) .forKeyspace(keyspaceName) .withAstyanaxConfiguration(new AstyanaxConfigurationImpl() .setDiscoveryType(RING_DESCRIBE) ) .withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("ConnectionPool") .setPort(thriftPort) .setSeeds(hosts) ) .withConnectionPoolMonitor(new CountingConnectionPoolMonitor()) .buildKeyspace(ThriftFamilyFactory.getInstance()); context.start(); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public synchronized void run() { context.shutdown(); } }); return context.getClient(); } }
public AstyanaxCluster cluster() { checkNotNull(_cluster, "cluster"); String metricName; ConnectionPoolConfiguration poolConfig; if (_keyspace == null) { // Use the shared pool configuration metricName = Objects.firstNonNull(_clusterMetric, _cluster); poolConfig = CassandraConfiguration.this; } else { // Use the configuration specifically for this keyspace KeyspaceConfiguration keyspaceConfig = checkNotNull(_keyspaces.get(_keyspace), "keyspaceConfig"); metricName = Objects.firstNonNull(keyspaceConfig.getKeyspaceMetric(), _keyspace); poolConfig = keyspaceConfig; } AstyanaxContext.Builder builder = newAstyanaxBuilder(_cluster, poolConfig, _metricRegistry) .forCluster(_cluster); if (!_disableClusterMetrics) { builder = builder .withTracerFactory(new InstrumentedTracerFactory(metricName, _metricRegistry)) .withConnectionPoolMonitor(new MetricConnectionPoolMonitor(metricName, _metricRegistry)); } AstyanaxContext<Cluster> astyanaxContext = builder.buildCluster(ThriftFamilyFactory.getInstance()); return new AstyanaxCluster(astyanaxContext, _cluster, _dataCenter); } }
private static AstyanaxContext<Keyspace> keyspaceWithJavaDriver(String keyspaceName) { final String SEEDS = "localhost"; Supplier<List<Host>> HostSupplier = new Supplier<List<Host>>() { @Override public List<Host> get() { Host host = new Host(SEEDS, -1); return Collections.singletonList(host); } }; ProtocolOptions protocolOptions = new ProtocolOptions(9042); Configuration jdConfig = Configuration.builder() .withProtocolOptions(protocolOptions) .build(); AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder() .forKeyspace(keyspaceName) .withHostSupplier(HostSupplier) .withAstyanaxConfiguration(new AstyanaxConfigurationImpl()) .withConnectionPoolConfiguration(new JavaDriverConnectionPoolConfigurationImpl(jdConfig)) .buildKeyspace(CqlFamilyFactory.getInstance()); return context; }