cassandraHostConfigurator = new CassandraHostConfigurator((String)hostsRefAddr.getContent()); if ( autoDiscoverHosts != null ) { cassandraHostConfigurator.setAutoDiscoverHosts(Boolean.parseBoolean((String)autoDiscoverHosts.getContent())); if ( runAutoDiscoverAtStartup != null ) cassandraHostConfigurator.setRunAutoDiscoveryAtStartup(Boolean.parseBoolean((String)autoDiscoverHosts.getContent())); cassandraHostConfigurator.setRetryDownedHosts(false); cassandraHostConfigurator.setRetryDownedHostsDelayInSeconds(retryDelay); cassandraHostConfigurator.setMaxActive(Integer.parseInt((String)maxActiveRefAddr.getContent())); if ( maxWaitTimeWhenExhausted != null ) cassandraHostConfigurator.setMaxWaitTimeWhenExhausted(Integer.parseInt((String)maxWaitTimeWhenExhausted.getContent())); if (maxExhaustedTimeBeforeMarkingAsDown != null) { cassandraHostConfigurator.setMaxExhaustedTimeBeforeMarkingAsDown(Integer.parseInt((String) maxExhaustedTimeBeforeMarkingAsDown.getContent())); log.debug("JNDI resource created with CassandraHostConfiguration: {}", cassandraHostConfigurator.getAutoDiscoverHosts());
protected CassandraHostConfigurator getCHCForTest() { CassandraHostConfigurator chc = new CassandraHostConfigurator("127.0.0.1:9170"); chc.setMaxActive(2); return chc; } }
protected void configure(CassandraHostConfigurator configurator) { configurator.setMaxActive(1); configurator.setMaxConnectTimeMillis(3 * 1000); }
protected void configure(CassandraHostConfigurator configurator) { configurator.setMaxActive(1); configurator.setMaxLastSuccessTimeMillis(3 * 1000); }
_writeConsistency = configuration.getAttribute(WRITE_CONSISTENCY, "ONE"); final CassandraHostConfigurator config = new CassandraHostConfigurator(_hosts); config.setRetryDownedHosts(configuration.getAttribute(RETRY_DOWNED_HOSTS, Boolean.TRUE)); config.setCassandraThriftSocketTimeout(socketTimeout); config.setRetryDownedHostsDelayInSeconds(configuration.getAttribute(RETRY_DOWNED_HOSTS_DELAY_IN_SECONDS, Integer.valueOf(10))); config.setRetryDownedHostsQueueSize(configuration.getAttribute(RETRY_DOWNED_HOSTS_QUEUE_SIZE, Integer.valueOf(256))); config.setMaxWaitTimeWhenExhausted(configuration.getAttribute(MAX_WAIT_TIME_WHEN_EXHAUSTED, Integer.valueOf(0))); config.setLoadBalancingPolicy((LoadBalancingPolicy) Class.forName(lbPolicy).newInstance()); } catch (Exception ignore) {
/** * Method tries to create a Cluster instance for an existing Cassandra * cluster. If another class already called getOrCreateCluster, the factory * returns the cached instance. If the instance doesn't exist in memory, a new * ThriftCluster is created and cached. * * Example usage for a default installation of Cassandra. * * String clusterName = "Test Cluster"; String host = "localhost:9160"; * Cluster cluster = HFactory.getOrCreateCluster(clusterName, host); * * Note the host should be the hostname and port number. It is preferable to * use the hostname instead of the IP address. * * @param clusterName * The cluster name. This is an identifying string for the cluster, * e.g. "production" or "test" etc. Clusters will be created on * demand per each unique clusterName key. * @param hostIp * host:ip format string * @return */ public static Cluster getOrCreateCluster(String clusterName, String hostIp) { return getOrCreateCluster(clusterName, new CassandraHostConfigurator(hostIp)); }
private void initializeCommandRunner(CommandLine cmd) throws Exception { CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator(seedHost); cassandraHostConfigurator.setUseThriftFramedTransport(false); cassandraHostConfigurator.setMaxWaitTimeWhenExhausted(getIntValueOrExit(cmd, "max-wait")); cassandraHostConfigurator.setCassandraThriftSocketTimeout(getIntValueOrExit(cmd, "thrift-timeout")); cassandraHostConfigurator.setMaxActive(commandArgs.clients); cassandraHostConfigurator.setAutoDiscoverHosts(true); cassandraHostConfigurator.setAutoDiscoveryDelayInSeconds(getIntValueOrExit(cmd, "discovery-delay")); cassandraHostConfigurator.setRetryDownedHostsDelayInSeconds(getIntValueOrExit(cmd, "retry-delay")); cassandraHostConfigurator.setRetryDownedHosts(false);
@Test public void testConfigValuesPropogated() { CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator("localhost:9170,localhost:9171,localhost:9172"); cassandraHostConfigurator.setMaxActive(20); cassandraHostConfigurator.setCassandraThriftSocketTimeout(3000); cassandraHostConfigurator.setMaxWaitTimeWhenExhausted(4000); cassandraHostConfigurator.setMaxExhaustedTimeBeforeMarkingAsDown(5000); CassandraHost[] cassandraHosts = cassandraHostConfigurator.buildCassandraHosts(); // no need to test all, just a smattering assertEquals(20, cassandraHosts[1].getMaxActive()); assertEquals(20, cassandraHosts[0].getMaxActive()); assertEquals(4000, cassandraHosts[1].getMaxWaitTimeWhenExhausted()); assertEquals(5000, cassandraHosts[0].getMaxExhaustedTimeBeforeMarkingAsDown()); assertEquals(3000, cassandraHosts[2].getCassandraThriftSocketTimeout()); assertEquals(3000, cassandraHosts[0].getCassandraThriftSocketTimeout()); }
logger.info("event=initializing_hector"); CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator(); cassandraHostConfigurator.setHosts(CLUSTER_URL); cassandraHostConfigurator.setMaxActive(hectorPoolSize); cassandraHostConfigurator.setCassandraThriftSocketTimeout(CMBProperties.getInstance().getCassandraThriftSocketTimeOutMS()); cassandraHostConfigurator.setAutoDiscoverHosts(CMBProperties.getInstance().isHectorAutoDiscovery()); cassandraHostConfigurator.setAutoDiscoveryDelayInSeconds(CMBProperties.getInstance().getHectorAutoDiscoveryDelaySeconds()); cassandraHostConfigurator.setAutoDiscoveryDataCenter(dataCenter); cassandraHostConfigurator.setLoadBalancingPolicy(new LeastActiveBalancingPolicy()); } else if (hectorBalancingPolicy.equals("RoundRobinBalancingPolicy")) { cassandraHostConfigurator.setLoadBalancingPolicy(new RoundRobinBalancingPolicy()); //default } else if (hectorBalancingPolicy.equals("DynamicLoadBalancingPolicy")) { cassandraHostConfigurator.setLoadBalancingPolicy(new DynamicLoadBalancingPolicy());
CassandraHostConfigurator configurator = new CassandraHostConfigurator("127.0.0.1:9170"); configurator.setClientFactoryClass(TestClientFactory.class.getName()); configurator.setMaxActive(maxActive); configurator.setMaxWaitTimeWhenExhausted(50); configurator.setMaxExhaustedTimeBeforeMarkingAsDown(0); configurator.setRetryDownedHosts(false);
@Test public void testSimpleCassandraHostSetup() { CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator("localhost:9170"); CassandraHost[] cassandraHosts = cassandraHostConfigurator.buildCassandraHosts(); assertEquals(1, cassandraHosts.length); }
@Test public void testApplyConfig() { CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator("localhost:9170"); cassandraHostConfigurator.setMaxActive(15); cassandraHostConfigurator.setMaxConnectTimeMillis(30000); cassandraHostConfigurator.setMaxLastSuccessTimeMillis(40000); CassandraHost extraHost = new CassandraHost("localhost:9171"); cassandraHostConfigurator.applyConfig(extraHost); assertEquals(15, extraHost.getMaxActive()); assertEquals(30000, extraHost.getMaxConnectTimeMillis()); assertEquals(40000, extraHost.getMaxLastSuccessTimeMillis()); }
protected void init(String clustername, String hosts, String username, String password, String keyspace) { CassandraHostConfigurator hostconfig = new CassandraHostConfigurator(hosts); hostconfig.setRetryDownedHosts(true); hostconfig.setRetryDownedHostsDelayInSeconds(5); hostconfig.setRetryDownedHostsQueueSize(-1); // no bounds this.cluster = HFactory.getOrCreateCluster(clustername, hostconfig); Map<String,String> credentials = new HashMap<String, String>(); if (username != null && username.length() > 0) { credentials.put("username", username); credentials.put("password", password); } this.keyspace = HFactory.createKeyspace( keyspace, cluster, new AllOneConsistencyLevelPolicy(), FailoverPolicy.ON_FAIL_TRY_ALL_AVAILABLE); }
@Test public void testOnHostRestored() throws InterruptedException { cassandraHostConfigurator = new CassandraHostConfigurator("127.0.0.1:9170"); cassandraHostConfigurator.setRetryDownedHostsDelayInSeconds(1); cassandraHostConfigurator.setRetryDownedHosts(true); connectionManager = new HConnectionManager(clusterName,cassandraHostConfigurator); final CassandraHost host = new CassandraHost("127.0.0.1", 9170); final boolean[] eventFired = {false}; connectionManager.addListener(listenerName, new ConnectionManagerEmptyListener() { @Override public void onHostRestored(CassandraHost cassandraHost) { assertEquals(cassandraHost, host); eventFired[0] = true; } }); connectionManager.markHostAsDown(host); Thread.sleep(1100); assertTrue(eventFired[0]); }
@Test public void testNonExistingKeyspaceWithHostAutoDiscover() { hThriftClient.open(); // this keyspace won't exist String ksname = "test_ks_2_" + Thread.currentThread().getName() + Thread.currentThread().getId(); Exception caughtException = null; try { hThriftClient.getCassandra(ksname); } catch (Exception e) { caughtException = e; } assertTrue("if you try to access a non-existent ks, hclient should throw exception", (caughtException != null) && (caughtException instanceof HInvalidRequestException) ); // now create the ks KeyspaceDefinition ksdef = HFactory.createKeyspaceDefinition(ksname); CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator("127.0.0.1"); cassandraHostConfigurator.setPort(9170); cassandraHostConfigurator.setRunAutoDiscoveryAtStartup(true); cassandraHostConfigurator.setAutoDiscoverHosts(true); Cluster cluster = HFactory.getOrCreateCluster(clusterName, cassandraHostConfigurator); cluster.addKeyspace(ksdef); // now it should work hThriftClient.getCassandra(ksname); assertTrue(hThriftClient.isOpen()); } }
private static CassandraHostConfigurator createCassandraHostConfigurator() { ClusterConfiguration configuration = ClusterConfigurationFactory.create(loadConfigXML()); String carbonCassandraRPCPort = null; carbonCassandraRPCPort = System.getProperty("cassandra.rpcport"); String cassandraHosts = null; int cassandraDefaultPort = 0; if (carbonCassandraRPCPort != null) { cassandraHosts = LOCAL_HOST_NAME + ":" + carbonCassandraRPCPort; cassandraDefaultPort = Integer.parseInt(carbonCassandraRPCPort); } else { cassandraHosts = configuration.getNodesString(); } if (cassandraHosts == null || "".equals(cassandraHosts)) { cassandraHosts = DEFAULT_HOST; } CassandraHostConfigurator configurator = new CassandraHostConfigurator(cassandraHosts); configurator.setAutoDiscoverHosts(configuration.isAutoDiscovery()); configurator.setAutoDiscoveryDelayInSeconds(configuration.getAutoDiscoveryDelay()); if (cassandraDefaultPort > 0 && cassandraDefaultPort < 65536) { configurator.setPort(cassandraDefaultPort); } else { configurator.setPort(configuration.getDefaultPort()); } return configurator; }
/** * Initializes the factory */ public synchronized void init() throws Exception { super.init(); log.debug("initializing factory"); //favor an existing cassandraHostConfigurator if (cassandraHostConfigurator == null) { cassandraHostConfigurator = new CassandraHostConfigurator(); cassandraHostConfigurator.setPort(getThriftPort()); cassandraHostConfigurator.setHosts(StringUtils.join(getContactNodes(), ',')); cassandraHostConfigurator.setAutoDiscoverHosts(autoDiscoverHosts); } cluster = HFactory.getOrCreateCluster(clusterName, cassandraHostConfigurator, credentials); initializeTypeConverters(); initializeKeyspaceDefinitions(); initializeSchema(); log.debug("factory initialized"); }
@Test public void testConfiguratorPort() { CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator("localhost"); cassandraHostConfigurator.setPort(9177); CassandraHost[] cassandraHosts = cassandraHostConfigurator.buildCassandraHosts(); assertEquals(9177, cassandraHosts[0].getPort()); }
@Test public void testConfiguratorClockResolution() { // Define my own clock resolution. @SuppressWarnings("serial") class SequentialClockResolution implements ClockResolution { @Override public long createClock() { return System.currentTimeMillis() * -1; } } CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator("localhost"); cassandraHostConfigurator.setClockResolution(new SequentialClockResolution()); assertNotSame(CassandraHostConfigurator.DEF_CLOCK_RESOLUTION, cassandraHostConfigurator.getClockResolution()); }
@Test public void testMarkHostDownWithNoRetry() { cassandraHostConfigurator = new CassandraHostConfigurator("127.0.0.1:9170"); cassandraHostConfigurator.setRetryDownedHosts(false); connectionManager = new HConnectionManager(clusterName, cassandraHostConfigurator); new CassandraHost("127.0.0.1", 9170); HClient client = connectionManager.borrowClient(); connectionManager.markHostAsDown(client.getCassandraHost()); assertEquals(0,connectionManager.getActivePools().size()); }