Example usage for org.apache.cassandra.db Keyspace getReplicationStrategy

List of usage examples for org.apache.cassandra.db Keyspace getReplicationStrategy

Introduction

In this page you can find the example usage for org.apache.cassandra.db Keyspace getReplicationStrategy.

Prototype

public AbstractReplicationStrategy getReplicationStrategy() 

Source Link

Usage

From source file:org.elassandra.cluster.InternalCassandraClusterService.java

License:Apache License

/**
 * Don't use QueryProcessor.executeInternal, we need to propagate this on
 * all nodes./* w  w  w  .  java 2 s  .  com*/
 * 
 * @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

@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.elasticsearch.cluster.metadata.IndexMetaData.java

License:Apache License

private IndexMetaData(String index, long version, State state, Settings settings,
        ImmutableOpenMap<String, MappingMetaData> mappings, ImmutableOpenMap<String, AliasMetaData> aliases,
        ImmutableOpenMap<String, Custom> customs, int numberOfNodes) {
    /*// w ww .jav a 2 s . c  om
    Integer maybeNumberOfShards = settings.getAsInt(SETTING_NUMBER_OF_SHARDS, null);
    if (maybeNumberOfShards == null) {
    throw new IllegalArgumentException("must specify numberOfShards for index [" + index + "]");
    }
    int numberOfShards = maybeNumberOfShards;
    if (numberOfShards <= 0) {
    throw new IllegalArgumentException("must specify positive number of shards for index [" + index + "]");
    }
            
    Integer maybeNumberOfReplicas = settings.getAsInt(SETTING_NUMBER_OF_REPLICAS, null);
    if (maybeNumberOfReplicas == null) {
    throw new IllegalArgumentException("must specify numberOfReplicas for index [" + index + "]");
    }
    int numberOfReplicas = maybeNumberOfReplicas;
    if (numberOfReplicas < 0) {
    throw new IllegalArgumentException("must specify non-negative number of shards for index [" + index + "]");
    }
    */
    this.index = index;
    this.version = version;
    this.state = state;
    this.settings = settings;
    this.mappings = mappings;
    this.customs = customs;

    if (Schema.instance != null && Schema.instance != null
            && Schema.instance.getKeyspaceInstance(keyspace()) != null) {
        Keyspace ks = Schema.instance.getKeyspaceInstance(keyspace());
        this.replicationFactor = (ks.getReplicationStrategy() == null) ? 1
                : ks.getReplicationStrategy().getReplicationFactor();
    } else {
        this.replicationFactor = 1;
    }

    this.numberOfShards = numberOfNodes;
    this.numberOfReplicas = replicationFactor * (numberOfNodes - 1);
    this.totalNumberOfShards = replicationFactor * numberOfNodes;
    this.aliases = aliases;

    Map<String, String> requireMap = settings.getByPrefix("index.routing.allocation.require.").getAsMap();
    if (requireMap.isEmpty()) {
        requireFilters = null;
    } else {
        requireFilters = DiscoveryNodeFilters.buildFromKeyValue(AND, requireMap);
    }
    Map<String, String> includeMap = settings.getByPrefix("index.routing.allocation.include.").getAsMap();
    if (includeMap.isEmpty()) {
        includeFilters = null;
    } else {
        includeFilters = DiscoveryNodeFilters.buildFromKeyValue(OR, includeMap);
    }
    Map<String, String> excludeMap = settings.getByPrefix("index.routing.allocation.exclude.").getAsMap();
    if (excludeMap.isEmpty()) {
        excludeFilters = null;
    } else {
        excludeFilters = DiscoveryNodeFilters.buildFromKeyValue(OR, excludeMap);
    }
    indexCreatedVersion = Version.indexCreated(settings);
    indexUpgradedVersion = settings.getAsVersion(IndexMetaData.SETTING_VERSION_UPGRADED, indexCreatedVersion);
    String stringLuceneVersion = settings.get(SETTING_VERSION_MINIMUM_COMPATIBLE);
    if (stringLuceneVersion != null) {
        try {
            this.minimumCompatibleLuceneVersion = org.apache.lucene.util.Version.parse(stringLuceneVersion);
        } catch (ParseException ex) {
            throw new IllegalStateException("Cannot parse lucene version [" + stringLuceneVersion + "] in the ["
                    + SETTING_VERSION_MINIMUM_COMPATIBLE + "] setting", ex);
        }
    } else {
        this.minimumCompatibleLuceneVersion = null;
    }
    final String hashFunction = settings.get(SETTING_LEGACY_ROUTING_HASH_FUNCTION);
    if (hashFunction == null) {
        routingHashFunction = MURMUR3_HASH_FUNCTION;
    } else {
        final Class<? extends HashFunction> hashFunctionClass;
        try {
            hashFunctionClass = Class.forName(hashFunction).asSubclass(HashFunction.class);
        } catch (ClassNotFoundException | NoClassDefFoundError e) {
            throw new ElasticsearchException("failed to load custom hash function [" + hashFunction + "]", e);
        }
        try {
            routingHashFunction = hashFunctionClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new IllegalStateException("Cannot instantiate hash function", e);
        }
    }
    useTypeForRouting = settings.getAsBoolean(SETTING_LEGACY_ROUTING_USE_TYPE, false);
}