com.netflix.astyanax.Cluster Java Examples

The following examples show how to use com.netflix.astyanax.Cluster. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AstyanaxStoreManager.java    From titan1withtp3.1 with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example #2
Source File: AstyanaxSupport.java    From brooklyn-library with Apache License 2.0 6 votes vote down vote up
public AstyanaxContext<Cluster> newAstyanaxContextForCluster() {
    AstyanaxContext<Cluster> context = new AstyanaxContext.Builder()
            .forCluster(clusterName)
            .withAstyanaxConfiguration(new AstyanaxConfigurationImpl()
                    .setDiscoveryType(NodeDiscoveryType.NONE))
            .withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("BrooklynPool")
                    .setPort(thriftPort)
                    .setMaxConnsPerHost(1)
                    .setConnectTimeout(5000) // 10s
                    .setSeeds(String.format("%s:%d", hostname, thriftPort)))
            .withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
            .buildCluster(ThriftFamilyFactory.getInstance());

    context.start();
    return context;
}
 
Example #3
Source File: DefaultAstyanaxClusterClientProvider.java    From staash with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized Cluster acquireCluster(ClusterKey clusterKey) {
    String clusterName = clusterKey.getClusterName().toLowerCase();
    Preconditions.checkNotNull(clusterName, "Invalid cluster name 'null'");
    
    ClusterContextHolder holder = contextMap.get(clusterName);
    if (holder == null) {
        HostSupplierProvider hostSupplierProvider = hostSupplierProviders.get(clusterKey.getDiscoveryType());
        Preconditions.checkNotNull(hostSupplierProvider, String.format("Unknown host supplier provider '%s' for cluster '%s'", clusterKey.getDiscoveryType(), clusterName));
        
        AstyanaxContext<Cluster> context = new AstyanaxContext.Builder()
            .forCluster(clusterName)
            .withAstyanaxConfiguration(configurationProvider.get(clusterName))
            .withConnectionPoolConfiguration(cpProvider.get(clusterName))
            .withConnectionPoolMonitor(monitorProvider.get(clusterName))
            .withHostSupplier(hostSupplierProvider.getSupplier(clusterName))
            .buildCluster(ThriftFamilyFactory.getInstance());
        
        holder = new ClusterContextHolder(context);
        holder.start();
    }
    holder.addRef();
    
    return holder.getKeyspace();
}
 
Example #4
Source File: CassandraConfiguration.java    From emodb with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #5
Source File: AstyanaxSupport.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example #6
Source File: CassandraDatacenterLiveTest.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example #7
Source File: CassandraClusterImpl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
private AstyanaxContext<Cluster> getCluster ( final String clusterName,
                                              final AstyanaxConfiguration astyanaxConfiguration,
                                              final ConnectionPoolConfiguration poolConfig ) {

    return new AstyanaxContext.Builder().forCluster( clusterName )
        .withAstyanaxConfiguration( astyanaxConfiguration )
        .withConnectionPoolConfiguration( poolConfig )
        .withConnectionPoolMonitor( new Slf4jConnectionPoolMonitorImpl())
        .buildCluster( ThriftFamilyFactory.getInstance() );

}
 
Example #8
Source File: CassandraConfiguration.java    From emodb with Apache License 2.0 4 votes vote down vote up
private com.datastax.driver.core.Cluster.Builder newCqlDriverBuilder(ConnectionPoolConfiguration poolConfig,
                                                                     MetricRegistry metricRegistry) {
    performHostDiscovery(metricRegistry);

    String[] seeds = _seeds.split(",");
    List<String> contactPoints = Lists.newArrayListWithCapacity(seeds.length);

    // Each seed may be a host name or a host name and port (e.g.; "1.2.3.4" or "1.2.3.4:9160").  These need
    // to be converted into host names only.
    for (String seed : seeds) {
        HostAndPort hostAndPort = HostAndPort.fromString(seed);
        seed = hostAndPort.getHostText();
        if (hostAndPort.hasPort()) {
            if (hostAndPort.getPort() == _thriftPort) {
                _log.debug("Seed {} found using RPC port; swapping for native port {}", seed, _cqlPort);
            } else if (hostAndPort.getPort() != _cqlPort) {
                throw new IllegalArgumentException(String.format(
                        "Seed %s found with invalid port %s.  The port must match either the RPC (thrift) port %s " +
                        "or the native (CQL) port %s", seed, hostAndPort.getPort(), _thriftPort, _cqlPort));
            }
        }

        contactPoints.add(seed);
    }

    PoolingOptions poolingOptions = new PoolingOptions();
    if (poolConfig.getMaxConnectionsPerHost().or(getMaxConnectionsPerHost()).isPresent()) {
        poolingOptions.setMaxConnectionsPerHost(HostDistance.LOCAL, poolConfig.getMaxConnectionsPerHost().or(getMaxConnectionsPerHost()).get());
    }
    if (poolConfig.getCoreConnectionsPerHost().or(getCoreConnectionsPerHost()).isPresent()) {
        poolingOptions.setCoreConnectionsPerHost(HostDistance.LOCAL, poolConfig.getCoreConnectionsPerHost().or(getCoreConnectionsPerHost()).get());
    }

    SocketOptions socketOptions = new SocketOptions();
    if (poolConfig.getConnectTimeout().or(getConnectTimeout()).isPresent()) {
        socketOptions.setConnectTimeoutMillis(poolConfig.getConnectTimeout().or(getConnectTimeout()).get());
    }
    if (poolConfig.getSocketTimeout().or(getSocketTimeout()).isPresent()) {
        socketOptions.setReadTimeoutMillis(poolConfig.getSocketTimeout().or(getSocketTimeout()).get());
    }

    AuthProvider authProvider = _authenticationCredentials != null
            ? new PlainTextAuthProvider(_authenticationCredentials.getUsername(), _authenticationCredentials.getPassword())
            : AuthProvider.NONE;

    return com.datastax.driver.core.Cluster.builder()
            .addContactPoints(contactPoints.toArray(new String[contactPoints.size()]))
            .withPort(_cqlPort)
            .withPoolingOptions(poolingOptions)
            .withSocketOptions(socketOptions)
            .withRetryPolicy(Policies.defaultRetryPolicy())
            .withAuthProvider(authProvider);
}
 
Example #9
Source File: AstyanaxCluster.java    From emodb with Apache License 2.0 4 votes vote down vote up
public AstyanaxCluster(AstyanaxContext<Cluster> astyanaxContext, String clusterName, String dataCenter) {
    super(clusterName, dataCenter);
    _astyanaxContext = astyanaxContext;
}
 
Example #10
Source File: AstyanaxStoreManager.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
private static AstyanaxContext<Cluster> createCluster(AstyanaxContext.Builder cb) {
    AstyanaxContext<Cluster> clusterCtx = cb.buildCluster(ThriftFamilyFactory.getInstance());
    clusterCtx.start();

    return clusterCtx;
}
 
Example #11
Source File: DefaultAstyanaxClusterClientProvider.java    From staash with Apache License 2.0 4 votes vote down vote up
public ClusterContextHolder(AstyanaxContext<Cluster> context) {
    this.context = context;
}
 
Example #12
Source File: DefaultAstyanaxClusterClientProvider.java    From staash with Apache License 2.0 4 votes vote down vote up
public Cluster getKeyspace() {
    return context.getClient();
}
 
Example #13
Source File: LocalClusterClientProvider.java    From staash with Apache License 2.0 4 votes vote down vote up
@Override
public Cluster acquireCluster(ClusterKey clusterName) {
    
    return null;
}
 
Example #14
Source File: CassandraClusterImpl.java    From usergrid with Apache License 2.0 3 votes vote down vote up
private void addKeyspace (Cluster cluster, String keyspaceName) throws Exception {

        try {
            keyspaces.put( keyspaceName, cluster.getKeyspace( keyspaceName ) );
        } catch (ConnectionException e) {

            logger.error("Unable to get keyspace client for keyspace: {}, detail: {}",
                keyspaceName,
                e.getMessage());

            throw e;
        }


    }
 
Example #15
Source File: ClusterClientProvider.java    From staash with Apache License 2.0 2 votes vote down vote up
/**
 * Acquire a cassandra cluster by name.  Must call releaseCluster once done.
 * The concrete provider must implement it's own reference counting and
 * garbage collection to shutdown Cluster clients that are not longer in use.
 * 
 * @param clusterName
 */
public Cluster acquireCluster(ClusterKey clusterName);