diff --git a/README.md b/README.md index d94f025..2c23812 100644 --- a/README.md +++ b/README.md @@ -23,13 +23,16 @@ Simple example: If you're running on Amazon EC2 (or a similar environment) you might want to set the `broadcast_address` -and the `endpoint_snitch` accordingly. +and the `endpoint_snitch` accordingly. It can be also important to set proper datacenter suffix to +diffrentiate datacenters. class { 'cassandra': cluster_name => 'YourEc2CassandraCluster', seeds => [ '192.0.2.5', '192.0.2.23', '192.0.2.42', ], listen_address => $ec2_local_ipv4, broadcast_address => $ec2_public_ipv4, + dc_suffix => 'DC2', + prefer_local_ip => 'true', endpoint_snitch => 'Ec2MultiRegionSnitch', } diff --git a/manifests/config.pp b/manifests/config.pp index e78993c..6c8d1d4 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -9,6 +9,8 @@ $start_rpc, $listen_address, $broadcast_address, + $dc_suffix, + $prefer_local_ip, $rpc_address, $rpc_port, $rpc_server_type, @@ -62,4 +64,9 @@ ensure => file, content => template("${module_name}/cassandra.yaml.erb"), } + + file { "${config_path}/cassandra-rackdc.properties": + ensure => file, + content => template("${module_name}/cassandra-rackdc.properties.erb"), + } } diff --git a/manifests/init.pp b/manifests/init.pp index 25aba17..c91d722 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -19,6 +19,8 @@ $cluster_name = $cassandra::params::cluster_name, $listen_address = $cassandra::params::listen_address, $broadcast_address = $cassandra::params::broadcast_address, + $dc_suffix = $cassandra::params::dc_suffix, + $prefer_local_ip = $cassandra::params::prefer_local_ip, $start_native_transport = $cassandra::params::start_native_transport, $start_rpc = $cassandra::params::start_rpc, $rpc_address = $cassandra::params::rpc_address, @@ -89,6 +91,10 @@ fail('broadcast_address must be an IP address') } + if(!empty($prefer_local_ip)) { + validate_re($prefer_local_ip, '^(true|false)$') + } + if(!is_ip_address($rpc_address)) { fail('rpc_address must be an IP address') } @@ -147,6 +153,8 @@ start_rpc => $start_rpc, listen_address => $listen_address, broadcast_address => $broadcast_address, + dc_suffix => $dc_suffix, + prefer_local_ip => $prefer_local_ip, rpc_address => $rpc_address, rpc_port => $rpc_port, rpc_server_type => $rpc_server_type, diff --git a/manifests/params.pp b/manifests/params.pp index 6394a12..582d58d 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -130,6 +130,16 @@ default => $::cassandra_broadcast_address, } + $dc_suffix = $::cassandra_dc_suffix ? { + undef => '', + default => $::cassandra_dc_suffix, + } + + $prefer_local_ip = $::cassandra_prefer_local_ip ? { + undef => '', + default => $::cassandra_prefer_local_ip, + } + $rpc_address = $::cassandra_rpc_address ? { undef => '0.0.0.0', default => $::cassandra_rpc_address, diff --git a/spec/classes/cassandra_config_spec.rb b/spec/classes/cassandra_config_spec.rb index 9c615e7..b34bf14 100644 --- a/spec/classes/cassandra_config_spec.rb +++ b/spec/classes/cassandra_config_spec.rb @@ -17,6 +17,8 @@ :cluster_name => 'Cassandra', :listen_address => '1.2.3.4', :broadcast_address => '4.3.2.1', + :dc_suffix => 'DC2', + :prefer_local_ip => 'true', :rpc_address => '0.0.0.0', :rpc_port => 9160, :rpc_server_type => 'hsha', @@ -95,6 +97,8 @@ :cluster_name => 'Cassandra', :listen_address => '1.2.3.4', :broadcast_address => '4.3.2.1', + :dc_suffix => 'DC2', + :prefer_local_ip => 'true', :rpc_address => '0.0.0.0', :rpc_port => 9160, :rpc_server_type => 'hsha', @@ -154,6 +158,17 @@ :content => /cluster_name: 'Cassandra'/, }) end + + it 'does contain file /etc/cassandra/conf/cassandra-rackdc.properties' do + should contain_file('/etc/cassandra/conf/cassandra-rackdc.properties').with({ + :ensure => 'file', + :owner => 'cassandra', + :group => 'cassandra', + :mode => '0644', + :content => /dc_suffix=DC2/, + :content => /prefer_local=true/, + }) + end end end diff --git a/spec/classes/cassandra_spec.rb b/spec/classes/cassandra_spec.rb index 3fd9b50..735dfd3 100644 --- a/spec/classes/cassandra_spec.rb +++ b/spec/classes/cassandra_spec.rb @@ -10,7 +10,7 @@ } end - let(:params) {{ :seeds => ['1.2.3.4'], :broadcast_address => '4.3.2.1' }} + let(:params) {{ :seeds => ['1.2.3.4'], :broadcast_address => '4.3.2.1', 'dc_suffix' => 'DC2', 'prefer_local_ip' => 'true'}} context 'verify module' do @@ -90,6 +90,8 @@ :cluster_name => 'Cassandra', :listen_address => '1.2.3.4', :broadcast_address => '4.3.2.1', + :dc_suffix => 'DC2', + :prefer_local_ip => 'true', :rpc_address => '0.0.0.0', :rpc_port => 9160, :rpc_server_type => 'hsha', @@ -162,7 +164,9 @@ :data_file_directories => [[['a', 'b']], ['bozo', '']], :jmx_port => [[1, 65535], [420000, true]], :listen_address => [['1.2.3.4'], ['4.5.6']], - :broadcast_address => [['1.2.3.4'], ['1.2', 'foo']], + :broadcast_address => [['1.2.3.4'], ['1.2', 'foo']], + :dc_suffix => [['DC2'], []], + :prefer_local_ip => [['true', 'false'], ['123', 'foo']], :rpc_address => [['1.2.3.4'], ['4.5.6']], :rpc_port => [[1, 65535], [420000, true]], :storage_port => [[1, 65535], [420000, true]], diff --git a/templates/cassandra-rackdc.properties.erb b/templates/cassandra-rackdc.properties.erb new file mode 100644 index 0000000..57eb1c2 --- /dev/null +++ b/templates/cassandra-rackdc.properties.erb @@ -0,0 +1,32 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# These properties are used with GossipingPropertyFileSnitch and will +# indicate the rack and dc for this node +dc=DC1 +rack=RAC1 + +# Add a suffix to a datacenter name. Used by the Ec2Snitch and Ec2MultiRegionSnitch +# to append a string to the EC2 region name. +<% unless @dc_suffix.nil? || @dc_suffix.empty? %> +dc_suffix=<%= @dc_suffix %> +<% end %> + +# Uncomment the following line to make this snitch prefer the internal ip when possible, as the Ec2MultiRegionSnitch does. +<% unless @prefer_local_ip.nil? || @prefer_local_ip.empty? %> +prefer_local=true +<% end %> +