List of usage examples for org.apache.cassandra.config DatabaseDescriptor getClusterName
public static String getClusterName()
From source file:org.elassandra.cluster.InternalCassandraClusterService.java
License:Apache License
@Override public MetaData readMetaDataAsRow(ConsistencyLevel cl) throws NoPersistedMetaDataException { UntypedResultSet result;/*from w w w.j a v a 2 s. co m*/ try { result = process(cl, selectMetadataQuery, DatabaseDescriptor.getClusterName()); Row row = result.one(); if (row != null && row.has("metadata")) { return parseMetaDataString(row.getString("metadata")); } } catch (UnavailableException e) { logger.warn("Cannot read metadata with consistency=" + cl, e); return null; } catch (Exception e) { throw new NoPersistedMetaDataException("Unexpected error", e); } throw new NoPersistedMetaDataException("Unexpected error"); }
From source file:org.elassandra.cluster.InternalCassandraClusterService.java
License:Apache License
/** * Create or update elastic_admin keyspace. * @throws IOException /*from www. j a v a2 s. c om*/ */ @Override public void createOrUpdateElasticAdminKeyspace() { UntypedResultSet result = QueryProcessor.executeInternal(String.format((Locale) null, "SELECT strategy_class,strategy_options FROM system.schema_keyspaces WHERE keyspace_name='%s'", elasticAdminKeyspaceName)); logger.info(" elasticAdminMetadata exist={}", !result.isEmpty()); if (result.isEmpty()) { MetaData metadata = state().metaData(); try { String metaDataString = MetaData.Builder.toXContent(metadata); JSONObject replication = new JSONObject(); replication.put("class", NetworkTopologyStrategy.class.getName()); replication.put(DatabaseDescriptor.getLocalDataCenter(), Integer.toString(getLocalDataCenterSize())); String createKeyspace = String.format((Locale) null, "CREATE KEYSPACE IF NOT EXISTS \"%s\" WITH replication = %s;", elasticAdminKeyspaceName, replication.toJSONString().replaceAll("\"", "'")); logger.info(createKeyspace); process(ConsistencyLevel.LOCAL_ONE, createKeyspace); String createTable = String.format((Locale) null, "CREATE TABLE IF NOT EXISTS \"%s\".%s ( cluster_name text PRIMARY KEY, owner uuid, version bigint, metadata text) WITH comment='%s';", elasticAdminKeyspaceName, ELASTIC_ADMIN_METADATA_TABLE, MetaData.Builder.toXContent(metadata)); logger.info(createTable); process(ConsistencyLevel.LOCAL_ONE, createTable); // initialize a first row if needed process(ConsistencyLevel.LOCAL_ONE, insertMetadataQuery, DatabaseDescriptor.getClusterName(), UUID.fromString(StorageService.instance.getLocalHostId()), metadata.version(), metaDataString); logger.info("Succefully initialize {}.{} = {}", elasticAdminKeyspaceName, ELASTIC_ADMIN_METADATA_TABLE, metaDataString); writeMetaDataAsComment(metaDataString); } catch (Throwable e) { logger.error("Failed to initialize table {}.{}", e, elasticAdminKeyspaceName, ELASTIC_ADMIN_METADATA_TABLE); } } else { Row row = result.one(); if (!NetworkTopologyStrategy.class.getName().equals(row.getString("strategy_class"))) { throw new ConfigurationException("Keyspace [" + this.elasticAdminKeyspaceName + "] should use " + NetworkTopologyStrategy.class.getName() + " replication strategy"); } JSONObject replication; try { replication = (JSONObject) new JSONParser().parse(row.getString("strategy_options")); int currentRF = -1; if (replication.get(DatabaseDescriptor.getLocalDataCenter()) != null) { currentRF = Integer .valueOf(replication.get(DatabaseDescriptor.getLocalDataCenter()).toString()); } int targetRF = getLocalDataCenterSize(); if (targetRF != currentRF) { replication.put(DatabaseDescriptor.getLocalDataCenter(), Integer.toString(targetRF)); replication.put("class", NetworkTopologyStrategy.class.getName()); try { String query = String.format((Locale) null, "ALTER KEYSPACE \"%s\" WITH replication = %s", elasticAdminKeyspaceName, replication.toJSONString().replaceAll("\"", "'")); process(ConsistencyLevel.LOCAL_ONE, query); logger.info(query); } catch (Throwable e) { logger.error("Failed to alter keyspace [{}]", e, this.elasticAdminKeyspaceName); throw e; } } else { logger.info("Keep unchanged keyspace={} datacenter={} RF={}", elasticAdminKeyspaceName, DatabaseDescriptor.getLocalDataCenter(), targetRF); } } catch (ParseException e1) { throw new ConfigurationException("Failed to update " + elasticAdminKeyspaceName, e1); } } }
From source file:org.elassandra.cluster.InternalCassandraClusterService.java
License:Apache License
@Override public void persistMetaData(MetaData oldMetaData, MetaData newMetaData, String source) throws IOException, InvalidRequestException, RequestExecutionException, RequestValidationException { if (!newMetaData.uuid().equals(localNode().id())) { logger.error("should not push metadata updated from another node {}/{}", newMetaData.uuid(), newMetaData.version());/*from w ww . j ava2 s .c o m*/ return; } if (newMetaData.uuid().equals(state().metaData().uuid()) && newMetaData.version() < state().metaData().version()) { logger.warn("don't push obsolete metadata uuid={} version {} < {}", newMetaData.uuid(), newMetaData.version(), state().metaData().version()); return; } String metaDataString = MetaData.Builder.toXContent(newMetaData); UUID owner = UUID.fromString(localNode().id()); boolean applied = processConditional(this.metadataWriteCL, this.metadataSerialCL, updateMetaDataQuery, new Object[] { owner, newMetaData.version(), metaDataString, DatabaseDescriptor.getClusterName(), newMetaData.version() }); if (applied) { logger.debug("PAXOS Succefully update metadata source={} newMetaData={} in cluster {}", source, metaDataString, DatabaseDescriptor.getClusterName()); writeMetaDataAsComment(metaDataString); return; } else { logger.warn("PAXOS Failed to update metadata oldMetadata={}/{} currentMetaData={}/{} in cluster {}", oldMetaData.uuid(), oldMetaData.version(), localNode().id(), newMetaData.version(), DatabaseDescriptor.getClusterName()); throw new ConcurrentMetaDataUpdateException(owner, newMetaData.version()); } }
From source file:org.elassandra.SnapshotTests.java
License:Apache License
public void restoreLucenceFiles(String dataLocation, String indexName, String snapshot) throws IOException { Path sourceDir = PathUtils.get(dataLocation + "/" + DatabaseDescriptor.getClusterName() + "/nodes/0/snapshots/" + indexName + "/" + snapshot + "/"); Path targetDir = PathUtils.get(dataLocation + "/" + DatabaseDescriptor.getClusterName() + "/nodes/0/indices/" + indexName + "/0/index/"); try (DirectoryStream<Path> stream = Files.newDirectoryStream(targetDir, "{_*.*,segments*}")) { for (Path segmentFile : stream) Files.delete(segmentFile); }/*from w w w . ja v a2 s .c om*/ try (DirectoryStream<Path> stream = Files.newDirectoryStream(sourceDir, "{_*.*,segments*}")) { for (Path f : stream) Files.copy(f, PathUtils.get(targetDir.toString(), f.getFileName().toString()), StandardCopyOption.COPY_ATTRIBUTES); } }
From source file:org.elasticsearch.test.InternalTestCluster.java
License:Apache License
@Override public String getClusterName() { return DatabaseDescriptor.getClusterName(); }
From source file:org.springframework.cassandra.test.integration.EmbeddedCassandraServerHelper.java
License:Apache License
/** * Get the embedded cassandra cluster name * * @return the cluster name// w w w.jav a 2s . com */ public static String getClusterName() { return DatabaseDescriptor.getClusterName(); }