List of usage examples for org.apache.cassandra.config DatabaseDescriptor getLocalDataCenter
public static String getLocalDataCenter()
From source file:org.elassandra.cluster.InternalCassandraClusterService.java
License:Apache License
/** * Don't use QueryProcessor.executeInternal, we need to propagate this on * all nodes./*from w w w . j a v a2 s . co m*/ * * @see org.elasticsearch.cassandra.ElasticSchemaService#createIndexKeyspace(java.lang.String, * int) **/ @Override public void createIndexKeyspace(final String ksname, final int replicationFactor) throws IOException { try { Keyspace ks = Keyspace.open(ksname); if (ks != null && !(ks.getReplicationStrategy() instanceof NetworkTopologyStrategy)) { throw new IOException( "Cannot create index, underlying keyspace requires the NetworkTopologyStrategy."); } } catch (AssertionError | NullPointerException e) { } try { QueryProcessor.process(String.format((Locale) null, "CREATE KEYSPACE IF NOT EXISTS \"%s\" WITH replication = {'class':'NetworkTopologyStrategy', '%s':'%d' };", ksname, DatabaseDescriptor.getLocalDataCenter(), replicationFactor), ConsistencyLevel.LOCAL_ONE); } catch (Throwable e) { throw new IOException(e.getMessage(), e); } }
From source file:org.elassandra.cluster.InternalCassandraClusterService.java
License:Apache License
public ClusterState updateNumberOfShards(ClusterState currentState) { int numberOfNodes = currentState.nodes().size(); assert numberOfNodes > 0; MetaData.Builder metaDataBuilder = MetaData.builder(currentState.metaData()); for (Iterator<IndexMetaData> it = currentState.metaData().iterator(); it.hasNext();) { IndexMetaData indexMetaData = it.next(); IndexMetaData.Builder indexMetaDataBuilder = IndexMetaData.builder(indexMetaData); indexMetaDataBuilder.numberOfShards(numberOfNodes); String keyspace = indexMetaData.keyspace(); if (Schema.instance != null && Schema.instance.getKeyspaceInstance(keyspace) != null) { AbstractReplicationStrategy replicationStrategy = Schema.instance.getKeyspaceInstance(keyspace) .getReplicationStrategy(); int rf = replicationStrategy.getReplicationFactor(); if (replicationStrategy instanceof NetworkTopologyStrategy) { rf = ((NetworkTopologyStrategy) replicationStrategy) .getReplicationFactor(DatabaseDescriptor.getLocalDataCenter()); }/*from ww w .j a va2 s . c o m*/ indexMetaDataBuilder.numberOfReplicas(Math.max(0, rf - 1)); } else { indexMetaDataBuilder.numberOfReplicas(0); } metaDataBuilder.put(indexMetaDataBuilder.build(), false); } return ClusterState.builder(currentState).metaData(metaDataBuilder.build()).build(); }
From source file:org.elassandra.cluster.InternalCassandraClusterService.java
License:Apache License
/** * Return number of node including me.//from w w w .j av a 2s . c o m * @param dc * @return */ private int getLocalDataCenterSize() { int size = 1; Set<InetAddress> ringMembers = StorageService.instance.getLiveRingMembers(); UntypedResultSet results = QueryProcessor.executeInternal("SELECT peer, data_center FROM system.peers"); for (Row row : results) { if (DatabaseDescriptor.getLocalDataCenter().equals(row.getString(1)) && ringMembers.contains(row.getInetAddress(0))) size++; } return size; }
From source file:org.elassandra.cluster.InternalCassandraClusterService.java
License:Apache License
/** * Create or update elastic_admin keyspace. * @throws IOException /* w w w. j av a2s. co m*/ */ @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 ShardInfo shardInfo(String index, ConsistencyLevel cl) { Keyspace keyspace = Schema.instance.getKeyspaceInstance(state().metaData().index(index).keyspace()); AbstractReplicationStrategy replicationStrategy = keyspace.getReplicationStrategy(); int rf = replicationStrategy.getReplicationFactor(); if (replicationStrategy instanceof NetworkTopologyStrategy) rf = ((NetworkTopologyStrategy) replicationStrategy) .getReplicationFactor(DatabaseDescriptor.getLocalDataCenter()); return new ShardInfo(rf, cl.blockFor(keyspace)); }
From source file:org.elassandra.PartitionedIndexTests.java
License:Apache License
@Test public void basicPartitionFunctionTest() throws Exception { process(ConsistencyLevel.ONE,/* w ww . ja v a2 s .c o m*/ String.format(Locale.ROOT, "CREATE KEYSPACE ks WITH replication = {'class': 'NetworkTopologyStrategy', '%s': '1'}", DatabaseDescriptor.getLocalDataCenter())); process(ConsistencyLevel.ONE, "CREATE TABLE ks.t1 ( name text, age int, primary key (name))"); XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("t1") .field("discover", ".*").endObject().endObject(); for (long i = 20; i < 30; i++) { createIndex("ks_" + i, Settings.builder().put("index.keyspace", "ks") .put("index.partition_function", "byage ks_{0,number,##} age").build(), "t1", mapping); ensureGreen("ks_" + i); } for (long i = 20; i < 30; i++) { for (int j = 0; j < i; j++) process(ConsistencyLevel.ONE, String.format(Locale.ROOT, "INSERT INTO ks.t1 (name, age) VALUES ('name%d-%d', %d)", i, j, i)); } for (long i = 20; i < 30; i++) assertThat( client().prepareSearch().setIndices("ks_" + i).setTypes("t1") .setQuery(QueryBuilders.queryStringQuery("*:*")).get().getHits().getTotalHits(), equalTo(i)); }
From source file:org.elassandra.PartitionedIndexTests.java
License:Apache License
@Test public void basicStringPartitionFunctionTest() throws Exception { process(ConsistencyLevel.ONE,/*from w ww. jav a2s .c o m*/ String.format(Locale.ROOT, "CREATE KEYSPACE ks WITH replication = {'class': 'NetworkTopologyStrategy', '%s': '1'}", DatabaseDescriptor.getLocalDataCenter())); process(ConsistencyLevel.ONE, "CREATE TABLE ks.t1 ( name text, age int, primary key (name))"); XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("t1") .field("discover", ".*").endObject().endObject(); for (long i = 20; i < 30; i++) { createIndex("ks_" + i, Settings.builder().put("index.keyspace", "ks") .put("index.partition_function", "byage ks_%d age") .put("index.partition_function_class", "org.elassandra.index.StringPartitionFunction").build(), "t1", mapping); ensureGreen("ks_" + i); } for (long i = 20; i < 30; i++) { for (int j = 0; j < i; j++) process(ConsistencyLevel.ONE, String.format(Locale.ROOT, "INSERT INTO ks.t1 (name, age) VALUES ('name%d-%d', %d)", i, j, i)); } for (long i = 20; i < 30; i++) assertThat( client().prepareSearch().setIndices("ks_" + i).setTypes("t1") .setQuery(QueryBuilders.queryStringQuery("*:*")).get().getHits().getTotalHits(), equalTo(i)); }
From source file:org.elassandra.SnapshotTests.java
License:Apache License
@Test public void basicSnapshotTest() throws Exception { process(ConsistencyLevel.ONE,/*w w w. j a v a2s . c o m*/ String.format(Locale.ROOT, "CREATE KEYSPACE ks WITH replication = {'class': 'NetworkTopologyStrategy', '%s': '1'}", DatabaseDescriptor.getLocalDataCenter())); process(ConsistencyLevel.ONE, "CREATE TABLE ks.t1 ( name text, age int, primary key (name))"); XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("t1") .field("discover", ".*").endObject().endObject(); createIndex("ks", Settings.builder().put("index.snapshot_with_sstable", true).build(), "t1", mapping); ensureGreen("ks"); for (long i = 0; i < 1000; i++) process(ConsistencyLevel.ONE, String.format(Locale.ROOT, "INSERT INTO ks.t1 (name, age) VALUES ('name%d', %d)", i, i)); assertThat( client().prepareSearch().setIndices("ks").setTypes("t1") .setQuery(QueryBuilders.queryStringQuery("*:*")).get().getHits().getTotalHits(), equalTo(1000L)); // take snaphot StorageService.instance.takeSnapshot("snap1", "ks"); // drop all process(ConsistencyLevel.ONE, "TRUNCATE ks.t1"); assertThat( client().prepareSearch().setIndices("ks").setTypes("t1") .setQuery(QueryBuilders.queryStringQuery("*:*")).get().getHits().getTotalHits(), equalTo(0L)); // close index and restore SSTable+Lucene files assertAcked(client().admin().indices().prepareClose("ks").get()); String dataLocation = DatabaseDescriptor.getAllDataFileLocations()[0]; restoreSSTable(dataLocation, "ks", "t1", Schema.instance.getCFMetaData("ks", "t1").cfId, "snap1"); restoreLucenceFiles(dataLocation, "ks", "snap1"); // refresh SSTables and repopen index StorageService.instance.loadNewSSTables("ks", "t1"); assertAcked(client().admin().indices().prepareOpen("ks").get()); ensureGreen("ks"); assertThat( client().prepareSearch().setIndices("ks").setTypes("t1") .setQuery(QueryBuilders.queryStringQuery("*:*")).get().getHits().getTotalHits(), equalTo(1000L)); }
From source file:org.elassandra.SnapshotTests.java
License:Apache License
@Test //mvn test -Pdev -pl com.strapdata.elasticsearch:elasticsearch -Dtests.seed=622A2B0618CE4676 -Dtests.class=org.elassandra.SnapshotTests -Dtests.method="onDropSnapshotTest" -Des.logger.level=ERROR -Dtests.assertion.disabled=false -Dtests.security.manager=false -Dtests.heap.size=1024m -Dtests.locale=ro-RO -Dtests.timezone=America/Toronto public void onDropSnapshotTest() throws Exception { process(ConsistencyLevel.ONE,/*from ww w . j a v a 2 s.co m*/ String.format(Locale.ROOT, "CREATE KEYSPACE ks WITH replication = {'class': 'NetworkTopologyStrategy', '%s': '1'}", DatabaseDescriptor.getLocalDataCenter())); process(ConsistencyLevel.ONE, "CREATE TABLE ks.t1 ( name text, age int, primary key (name))"); XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("t1") .field("discover", ".*").endObject().endObject(); createIndex("ks", Settings.builder().put("index.snapshot_with_sstable", true).build(), "t1", mapping); ensureGreen("ks"); for (long i = 0; i < 1000; i++) process(ConsistencyLevel.ONE, String.format(Locale.ROOT, "INSERT INTO ks.t1 (name, age) VALUES ('name%d', %d)", i, i)); assertThat( client().prepareSearch().setIndices("ks").setTypes("t1") .setQuery(QueryBuilders.queryStringQuery("*:*")).get().getHits().getTotalHits(), equalTo(1000L)); UUID cfId = Schema.instance.getCFMetaData("ks", "t1").cfId; String id = cfId.toString().replaceAll("\\-", ""); if (!DatabaseDescriptor.isAutoSnapshot()) StorageService.instance.takeTableSnapshot("ks", "t1", Long.toString(new Date().getTime())); // drop index + keyspace (C* snapshot before drop => flush before snapshot => ES flush before delete) assertAcked(client().admin().indices().prepareDelete("ks").get()); // recreate schema and mapping process(ConsistencyLevel.ONE, String.format(Locale.ROOT, "CREATE KEYSPACE ks WITH replication = {'class': 'NetworkTopologyStrategy', '%s': '1'}", DatabaseDescriptor.getLocalDataCenter())); process(ConsistencyLevel.ONE, "CREATE TABLE ks.t1 ( name text, age int, primary key (name))"); createIndex("ks", Settings.builder().put("index.snapshot_with_sstable", true).build(), "t1", mapping); ensureGreen("ks"); assertThat( client().prepareSearch().setIndices("ks").setTypes("t1") .setQuery(QueryBuilders.queryStringQuery("*:*")).get().getHits().getTotalHits(), equalTo(0L)); // close index and restore SSTable+Lucene files assertAcked(client().admin().indices().prepareClose("ks").get()); String dataLocation = DatabaseDescriptor.getAllDataFileLocations()[0]; DirectoryStream<Path> stream = Files .newDirectoryStream(PathUtils.get(dataLocation + "/ks/t1-" + id + "/snapshots/")); Path snapshot = stream.iterator().next(); String snap = snapshot.getFileName().toString(); System.out.println("snapshot name=" + snap); stream.close(); restoreSSTable(dataLocation, "ks", "t1", cfId, snap); restoreLucenceFiles(dataLocation, "ks", snap); // refresh SSTables and repopen index StorageService.instance.loadNewSSTables("ks", "t1"); assertAcked(client().admin().indices().prepareOpen("ks").get()); ensureGreen("ks"); assertThat( client().prepareSearch().setIndices("ks").setTypes("t1") .setQuery(QueryBuilders.queryStringQuery("*:*")).get().getHits().getTotalHits(), equalTo(1000L)); }
From source file:org.elasticsearch.cassandra.cluster.InternalCassandraClusterService.java
License:Apache License
/** * Don't use QueryProcessor.executeInternal, we need to propagate this on * all nodes./*w w w .ja v a 2s . com*/ * * @see org.elasticsearch.cassandra.ElasticSchemaService#createIndexKeyspace(java.lang.String, * int) **/ @Override public void createIndexKeyspace(final String index, final int replicationFactor) throws IOException { try { QueryProcessor.process(String.format( "CREATE KEYSPACE IF NOT EXISTS \"%s\" WITH replication = {'class':'NetworkTopologyStrategy', '%s':'%d' };", index, DatabaseDescriptor.getLocalDataCenter(), replicationFactor), ConsistencyLevel.LOCAL_ONE); } catch (Throwable e) { throw new IOException(e.getMessage(), e); } }