diff --git a/attributes/default.rb b/attributes/default.rb index edfbe5c6..a64c8fb0 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -31,6 +31,14 @@ default['cassandra']['log_dir'] = '/var/log/cassandra' default['cassandra']['rootlogger'] = 'INFO,stdout,R' +# Seed node discovery +default['cassandra']['seeds'] = node['ipaddress'] + +default['cassandra']['seed_discovery']['use_chef_search'] = false +default['cassandra']['seed_discovery']['count'] = 3 +default['cassandra']['seed_discovery']['search_role'] = 'cassandra-seed' +default['cassandra']['seed_discovery']['search_query'] = nil + default['cassandra']['jbod']['slices'] = nil default['cassandra']['jbod']['dir_name_prefix'] = 'data' @@ -129,7 +137,6 @@ default['cassandra']['xss'] = '256k' default['cassandra']['vnodes'] = true default['cassandra']['num_tokens'] = 256 -default['cassandra']['seeds'] = node['ipaddress'] default['cassandra']['enable_assertions'] = true default['cassandra']['internode_compression'] = 'all' # all, dc, none default['cassandra']['jmx_server_hostname'] = false diff --git a/libraries/config_helpers.rb b/libraries/config_helpers.rb index e180fc0a..c017c572 100644 --- a/libraries/config_helpers.rb +++ b/libraries/config_helpers.rb @@ -18,3 +18,36 @@ def cassandra_bool_config(config_val) def hash_to_yaml_string(hash) hash.to_hash.to_yaml end + +def discover_seed_nodes + # use chef search for seed nodes + if node['cassandra']['seed_discovery']['use_chef_search'] + if Chef::Config[:solo] + Chef::Log.warn("Chef Solo does not support search, provide the seed nodes via node attribute node['cassandra']['seeds']") + node['ipaddress'] + else + Chef::Log.info("Cassandra seed discovery using Chef search is enabled") + q = if search_query = node['cassandra']['seed_discovery']['search_query'] + search_query + else + "chef_environment:#{node.chef_environment} AND role:#{node['cassandra']['seed_discovery']['search_role']} AND cassandra_cluster_name:#{node['cassandra']['cluster_name']}" + end + Chef::Log.info("Will discover Cassandra seeds using query '#{q}'") + xs = search(:node, q).map(&:ipaddress).sort.uniq + Chef::Log.debug("Discovered #{xs.size} Cassandra seeds using query '#{q}'") + + if xs.empty? + node['ipaddress'] + else + xs.take(node['cassandra']['seed_discovery']['count']).join(',') + end + end + else + # user defined seed nodes + if node['cassandra']['seeds'].is_a?(Array) + node['cassandra']['seeds'].join(',') + else + node['cassandra']['seeds'] + end + end +end diff --git a/recipes/default.rb b/recipes/default.rb index f561600e..8aeff019 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -35,4 +35,6 @@ node.default['cassandra']['jamm']['sha256sum'] = '79d44f1b911a603f0a249aa59ad6ea22aac9c9b211719e86f357646cdf361a42' end +node.default['cassandra']['seeds'] = discover_seed_nodes + include_recipe "cassandra-dse::#{node['cassandra']['install_method']}" diff --git a/templates/default/cassandra.yaml.erb b/templates/default/cassandra.yaml.erb index efa54d81..4c1a5cdf 100644 --- a/templates/default/cassandra.yaml.erb +++ b/templates/default/cassandra.yaml.erb @@ -242,7 +242,7 @@ seed_provider: # seeds is actually a comma-delimited list of addresses. # Ex: ",," #- seeds: "127.0.0.1" - - seeds: "<%= if node['cassandra']['seeds'].kind_of?(Array) then node['cassandra']['seeds'].join(",") else node['cassandra']['seeds'] end %>" + - seeds: "<%= node['cassandra']['seeds'] %>" # emergency pressure valve: each time heap usage after a full (CMS) # garbage collection is above this fraction of the max, Cassandra will