![JAR search and dependency download from the Maven repository](/logo.png)
com.rackspacecloud.blueflood.io.AstyanaxIO Maven / Gradle / Ivy
/*
* Copyright 2013 Rackspace
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.rackspacecloud.blueflood.io;
import com.netflix.astyanax.AstyanaxContext;
import com.netflix.astyanax.Keyspace;
import com.netflix.astyanax.connectionpool.NodeDiscoveryType;
import com.netflix.astyanax.connectionpool.impl.ConnectionPoolConfigurationImpl;
import com.netflix.astyanax.connectionpool.impl.ConnectionPoolType;
import com.netflix.astyanax.impl.AstyanaxConfigurationImpl;
import com.netflix.astyanax.model.ColumnFamily;
import com.netflix.astyanax.retry.RetryNTimes;
import com.netflix.astyanax.serializers.LongSerializer;
import com.netflix.astyanax.serializers.StringSerializer;
import com.netflix.astyanax.thrift.ThriftFamilyFactory;
import com.rackspacecloud.blueflood.service.Configuration;
import com.rackspacecloud.blueflood.types.Locator;
import java.util.*;
public class AstyanaxIO {
private static final AstyanaxContext context;
private static final Keyspace keyspace;
protected static final ColumnFamily CF_METRICS_FULL = new ColumnFamily("metrics_full",
LocatorSerializer.get(),
LongSerializer.get());
protected static final ColumnFamily CF_METRICS_5M = new ColumnFamily("metrics_5m",
LocatorSerializer.get(),
LongSerializer.get());
protected static final ColumnFamily CF_METRICS_20M = new ColumnFamily("metrics_20m",
LocatorSerializer.get(),
LongSerializer.get());
protected static final ColumnFamily CF_METRICS_60M = new ColumnFamily("metrics_60m",
LocatorSerializer.get(),
LongSerializer.get());
protected static final ColumnFamily CF_METRICS_240M = new ColumnFamily("metrics_240m",
LocatorSerializer.get(),
LongSerializer.get());
protected static final ColumnFamily CF_METRICS_1440M = new ColumnFamily("metrics_1440m",
LocatorSerializer.get(),
LongSerializer.get());
protected static final ColumnFamily CF_METRIC_METADATA = new ColumnFamily("metrics_metadata",
LocatorSerializer.get(),
StringSerializer.get());
protected static final ColumnFamily CF_METRICS_STRING = new ColumnFamily("metrics_string",
LocatorSerializer.get(),
LongSerializer.get());
protected static final ColumnFamily CF_METRICS_LOCATOR = new ColumnFamily("metrics_locator",
LongSerializer.get(),
LocatorSerializer.get());
protected static final ColumnFamily CF_METRICS_STATE = new ColumnFamily("metrics_state",
LongSerializer.get(),
StringSerializer.get());
protected static final Map> CF_NAME_TO_CF;
static {
context = createPreferredHostContext();
context.start();
keyspace = context.getEntity();
Map> tempMap = new HashMap>();
tempMap.put("metrics_full", CF_METRICS_FULL);
tempMap.put("metrics_5m", CF_METRICS_5M);
tempMap.put("metrics_20m", CF_METRICS_20M);
tempMap.put("metrics_60m", CF_METRICS_60M);
tempMap.put("metrics_240m", CF_METRICS_240M);
tempMap.put("metrics_1440m", CF_METRICS_1440M);
CF_NAME_TO_CF = Collections.unmodifiableMap(tempMap);
}
protected AstyanaxIO() {
}
private static AstyanaxContext createCustomHostContext(AstyanaxConfigurationImpl configuration,
ConnectionPoolConfigurationImpl connectionPoolConfiguration) {
return new AstyanaxContext.Builder()
.forCluster(Configuration.getStringProperty("CLUSTER_NAME"))
.forKeyspace(Configuration.getStringProperty("ROLLUP_KEYSPACE"))
.withAstyanaxConfiguration(configuration)
.withConnectionPoolConfiguration(connectionPoolConfiguration)
.withConnectionPoolMonitor(new InstrumentedConnectionPoolMonitor())
.buildKeyspace(ThriftFamilyFactory.getInstance());
}
private static AstyanaxContext createPreferredHostContext() {
return createCustomHostContext(createPreferredAstyanaxConfiguration(), createPreferredConnectionPoolConfiguration());
}
private static AstyanaxConfigurationImpl createPreferredAstyanaxConfiguration() {
AstyanaxConfigurationImpl config = new AstyanaxConfigurationImpl()
.setDiscoveryType(NodeDiscoveryType.NONE)
.setConnectionPoolType(ConnectionPoolType.ROUND_ROBIN);
int numRetries = Configuration.getIntegerProperty("CASSANDRA_MAX_RETRIES");
if (numRetries > 0) {
config.setRetryPolicy(new RetryNTimes(numRetries));
}
return config;
}
private static ConnectionPoolConfigurationImpl createPreferredConnectionPoolConfiguration() {
int port = Configuration.getIntegerProperty("DEFAULT_CASSANDRA_PORT");
Set uniqueHosts = new HashSet();
Collections.addAll(uniqueHosts, Configuration.getStringProperty("CASSANDRA_HOSTS").split(","));
int numHosts = uniqueHosts.size();
int maxConns = Configuration.getIntegerProperty("MAX_CASSANDRA_CONNECTIONS");
int connsPerHost = maxConns / numHosts + (maxConns % numHosts == 0 ? 0 : 1);
// This timeout effectively results in waiting a maximum of (timeoutWhenExhausted / numHosts) on each Host
int timeoutWhenExhausted = Configuration.getIntegerProperty("MAX_TIMEOUT_WHEN_EXHAUSTED");
timeoutWhenExhausted = Math.max(timeoutWhenExhausted, 1 * numHosts); // Minimum of 1ms per host
final ConnectionPoolConfigurationImpl connectionPoolConfiguration = new ConnectionPoolConfigurationImpl("MyConnectionPool")
.setPort(port)
.setInitConnsPerHost(connsPerHost)
.setMaxConnsPerHost(connsPerHost)
.setMaxBlockedThreadsPerHost(5)
.setMaxTimeoutWhenExhausted(timeoutWhenExhausted)
.setInitConnsPerHost(connsPerHost / 2)
.setSeeds(Configuration.getStringProperty("CASSANDRA_HOSTS"));
return connectionPoolConfiguration;
}
protected static Keyspace getKeyspace() {
return keyspace;
}
public static Map> getColumnFamilyMapper() {
return CF_NAME_TO_CF;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy