List of usage examples for org.apache.cassandra.config DatabaseDescriptor getListenAddress
public static InetAddress getListenAddress()
From source file:com.navercorp.pinpoint.plugin.cassandra.CassandraTestHelper.java
License:Apache License
public static String getHost() { return DatabaseDescriptor.getListenAddress().getHostAddress(); }
From source file:org.kiji.schema.cassandra.TestingCassandraFactory.java
License:Apache License
/** * Ensure that the EmbeddedCassandraService for unit tests is running. If it is not, then start * it.//from w ww . j av a 2 s. c om */ private void startEmbeddedCassandraServiceIfNotRunningAndOpenSession() throws Exception { LOG.debug("Ready to start a C* service if necessary..."); if (null != mCassandraSession) { LOG.debug("C* is already running, no need to start the service."); //Preconditions.checkNotNull(mCassandraSession); return; } LOG.debug("Starting EmbeddedCassandra!"); try { LOG.info("Starting EmbeddedCassandraService..."); // Use a custom YAML file that specifies different ports from normal for RPC and thrift. InputStream yamlStream = getClass().getResourceAsStream("/cassandra.yaml"); LOG.debug("Checking that we can load cassandra.yaml as a stream..."); Preconditions.checkNotNull(yamlStream, "Unable to load resource /cassandra.yaml as a stream"); LOG.debug("Looks good to load it as a stream!"); // Update cassandra.yaml to use available ports. String cassandraYaml = IOUtils.toString(yamlStream); final int storagePort = findOpenPort(); // Normally 7000. final int sslStoragePort = findOpenPort(); // Normally 7001. final int nativeTransportPort = findOpenPort(); // Normally 9042. final int rpcPort = findOpenPort(); // Normally 9160. cassandraYaml = updateCassandraYamlWithPort(cassandraYaml, "__STORAGE_PORT__", storagePort); cassandraYaml = updateCassandraYamlWithPort(cassandraYaml, "__SSL_STORAGE_PORT__", sslStoragePort); cassandraYaml = updateCassandraYamlWithPort(cassandraYaml, "__NATIVE_TRANSPORT_PORT__", nativeTransportPort); cassandraYaml = updateCassandraYamlWithPort(cassandraYaml, "__RPC_PORT__", rpcPort); // Write out the YAML contents to a temp file. File yamlFile = File.createTempFile("cassandra", ".yaml"); LOG.info("Writing cassandra.yaml to {}", yamlFile); final BufferedWriter bw = new BufferedWriter(new FileWriter(yamlFile)); try { bw.write(cassandraYaml); } finally { bw.close(); } Preconditions.checkArgument(yamlFile.exists()); System.setProperty("cassandra.config", "file:" + yamlFile.getAbsolutePath()); System.setProperty("cassandra-foreground", "true"); // Make sure that all of the directories for the commit log, data, and caches are empty. // Thank goodness there are methods to get this information (versus parsing the YAML // directly). ArrayList<String> directoriesToDelete = new ArrayList<String>( Arrays.asList(DatabaseDescriptor.getAllDataFileLocations())); directoriesToDelete.add(DatabaseDescriptor.getCommitLogLocation()); directoriesToDelete.add(DatabaseDescriptor.getSavedCachesLocation()); for (String dirName : directoriesToDelete) { FileUtils.deleteDirectory(new File(dirName)); } EmbeddedCassandraService embeddedCassandraService = new EmbeddedCassandraService(); embeddedCassandraService.start(); } catch (IOException ioe) { throw new KijiIOException("Cannot start embedded C* service!"); } try { // Use different port from normal here to avoid conflicts with any locally-running C* cluster. // Port settings are controlled in "cassandra.yaml" in test resources. // Also change the timeouts and retry policies. Since we have only a single thread here for // this test process, it can slow down dramatically if it has to do a compaction (see // SCHEMA-959 and SCHEMA-969 for examples of the flakiness this case cause in unit tests). // No builder for `SocketOptions`: final SocketOptions socketOptions = new SocketOptions(); // Setting this to 0 disables read timeouts. socketOptions.setReadTimeoutMillis(0); // This defaults to 5 s. Increase to a minute. socketOptions.setConnectTimeoutMillis(60 * 1000); Cluster cluster = Cluster.builder().addContactPoints(DatabaseDescriptor.getListenAddress()) .withPort(DatabaseDescriptor.getNativeTransportPort()).withSocketOptions(socketOptions) // Let's at least log all of the retries so we can see what is happening. .withRetryPolicy(new LoggingRetryPolicy(Policies.defaultRetryPolicy())) // The default reconnection policy (exponential) looks fine. .build(); mCassandraSession = cluster.connect(); } catch (Exception exc) { throw new KijiIOException("Started embedded C* service, but cannot connect to cluster. " + exc); } }
From source file:org.ms123.common.cassandra.CassandraServiceImpl.java
License:Open Source License
public synchronized Session getSession(String keyspaceName) { Session session = m_sessionCache.get(keyspaceName); if (session != null) { if (!session.isClosed()) { return session; }/*from w w w .ja v a 2 s .c o m*/ } Cluster cluster = Cluster.builder().addContactPoint(DatabaseDescriptor.getListenAddress().getHostName()) .withPort(DatabaseDescriptor.getNativeTransportPort()).build(); KeyspaceMetadata kmd = cluster.getMetadata().getKeyspace(keyspaceName); if (kmd == null) { session = cluster.connect(); String cql = "CREATE KEYSPACE " + keyspaceName + " WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1};"; info(cql + "\n"); session.execute(cql); cql = "USE " + keyspaceName + ";"; info(cql + "\n"); session.execute(cql); } else { session = cluster.connect(keyspaceName); } m_sessionCache.put(keyspaceName, session); return session; }