Module: DatastaxRails::Connection::ClassMethods
- Defined in:
- lib/datastax_rails/connection.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ servers: '127.0.0.1:9160', thrift: {}, cql_version: '3.0.0' }
Instance Method Summary collapse
-
#current_server ⇒ String
Returns the current server that we are talking to.
-
#establish_connection(spec) ⇒ Object
Establish a Cassandra connection to DSE.
-
#establish_solr_connection ⇒ RSolr::Client
Similar to
establish_connection
, this method creates a connection object for Solr. -
#solr_base_url ⇒ String
Returns the base portion of the URL for connecting to SOLR based on the current Cassandra server.
-
#solr_connection(reconnect = false) ⇒ DatastaxRails::RSolrClientWrapper
Wraps and caches a solr connection object.
Instance Method Details
#current_server ⇒ String
Returns the current server that we are talking to. This is useful when you are talking to a cluster, and we want to know which server specifically we are connected to.
Used by Relation to calculate the SOLR URL so that it follows the Cassandra connection.
31 32 33 |
# File 'lib/datastax_rails/connection.rb', line 31 def current_server connection.current_connection.instance_variable_get(:@connection).host end |
#establish_connection(spec) ⇒ Object
Establish a Cassandra connection to DSE. datastax.yml will be read and the current environment’s settings passed to this method.
The following is an example production configuration document. Assume that your setup consists of three datacenters each with three servers and RF=3 (i.e., you’re storing your data 9 times)
servers: ["10.1.2.5"]
port: 9042
keyspace: "datastax_rails_production"
strategy_class: "org.apache.cassandra.locator.NetworkTopologyStrategy"
strategy_options: {"DS1": "3", "DS2": "3", "DS3": "3"}
connection_options:
timeout: 10
solr:
port: 8983
path: /solr
ssl:
use_ssl: true
cert: config/datastax_rails.crt
key: config/datastax_rails.key
keypass: changeme
The servers
entry should be a list of all seed nodes for servers you wish to connect to. DSR will automatically connect to all nodes in the cluster or in the datacenter if you are using multiple datacenters. You can safely just list all nodes in a particular datacenter if you would like.
The port to connect to, this port will be used for all nodes. Because the ‘system.peers` table does not contain the port that the nodes are listening on, the port must be the same for all nodes.
Since we’re using the NetworkTopologyStrategy for our locator, it is important that you configure cassandra-topology.properties. See the DSE documentation at www.datastax.com for more information.
strategy_options lets us specify what our topology looks like. In this case, we have RF=3 in all three of our datacenters (DS1, DS2, and DS3).
connection_options are the options that are passed to the thrift layer for the connection to cassandra.
-
retries - Number of times a request will be retried. Should likely be the number of servers - 1. Defaults to 0.
-
server_retry_period - Amount of time to wait before retrying a down server. Defaults to 1.
-
server_max_requests - Number of requests to make to a server before moving to the next one (helps keep load balanced). Default to nil which means cycling does not take place.
-
retry_overrides - Overrides retries option for individual exceptions.
-
connect_timeout - The connection timeout on the Thrift socket. Defaults to 0.1.
-
timeout - The timeout for the transport layer. Defaults to 1.
-
timeout_overrides - Overrides the timeout value for specific methods (advanced).
-
exception_classes - List of exceptions for which Thrift will automatically retry a new server in the cluster (up to retry limit). Defaults to [IOError, Thrift::Exception, Thrift::ApplicationException, Thrift::TransportException].
-
exception_class_overrides - List of exceptions which will never cause a retry. Defaults to [CassandraCQL::Thrift::InvalidRequestException].
-
wrapped_exception_options - List of exceptions that will be automatically wrapped in an exception provided by client class with the same name (advanced). Defaults to [Thrift::ApplicationException, Thrift::TransportException].
-
raise - Whether to raise exceptions or default calls that cause an error (advanced). Defaults to true (raise exceptions).
-
defaults - When raise is false and an error is encountered, these methods are called to default the return value (advanced). Should be a hash of method names to values.
-
protocol - The thrift protocol to use (advanced). Defaults to Thrift::BinaryProtocol.
-
protocol_extra_params - Any extra parameters to send to the protocol (advanced).
-
transport - The thrift transport to use (advanced). Defaults to Thrift::Socket.
-
transport_wrapper - The thrift transport wrapper to use (advanced). Defaults to Thrift::FramedTransport.
See solr_connection
for a description of the solr options in datastax.yml
100 101 102 103 104 105 106 |
# File 'lib/datastax_rails/connection.rb', line 100 def establish_connection(spec) DatastaxRails::Base.config = spec.with_indifferent_access spec.reverse_merge!(DEFAULT_OPTIONS) self.connection = ::Cql::Client.connect(hosts: spec[:servers], keyspace: spec[:keyspace], connection_timeout: spec[:connection_options][:timeout]) end |
#establish_solr_connection ⇒ RSolr::Client
Similar to establish_connection
, this method creates a connection object for Solr. Since HTTP is stateless, this doesn’t actually launch the connection, but it gets everything set up so that RSolr can do its work. It’s important to note that unlike the cassandra connection which is global to all of DSR, each model will have its own solr_connection.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/datastax_rails/connection.rb', line 136 def establish_solr_connection opts = { url: "#{solr_base_url}/#{DatastaxRails::Base.connection.keyspace}.#{column_family}" } if DatastaxRails::Base.config[:solr].key?(:ssl) && DatastaxRails::Base.config[:solr][:ssl].key?(:cert) && DatastaxRails::Base.config[:solr][:ssl][:use_ssl] cert = Pathname.new(DatastaxRails::Base.config[:solr][:ssl][:cert]) key = Pathname.new(DatastaxRails::Base.config[:solr][:ssl][:key]) pass = DatastaxRails::Base.config[:solr][:ssl][:keypass] cert = Rails.root.join(cert) unless cert.absolute? key = Rails.root.join(key) unless key.absolute? opts[:ssl_cert_file] = cert.to_s opts[:ssl_key_file] = key.to_s opts[:ssl_key_pass] = pass if pass RSolr::ClientCert.connect opts else RSolr.connect opts end end |
#solr_base_url ⇒ String
Returns the base portion of the URL for connecting to SOLR based on the current Cassandra server.
111 112 113 114 115 116 117 |
# File 'lib/datastax_rails/connection.rb', line 111 def solr_base_url DatastaxRails::Base.establish_connection unless connection port = DatastaxRails::Base.config[:solr][:port] path = DatastaxRails::Base.config[:solr][:path] protocol = DatastaxRails::Base.config[:solr].key?(:ssl) && DatastaxRails::Base.config[:solr][:ssl][:use_ssl] ? 'https' : 'http' # rubocop:disable LineLength "#{protocol}://#{current_server}:#{port}#{path}" end |
#solr_connection(reconnect = false) ⇒ DatastaxRails::RSolrClientWrapper
Wraps and caches a solr connection object
123 124 125 126 127 128 |
# File 'lib/datastax_rails/connection.rb', line 123 def solr_connection(reconnect = false) if !@rsolr || reconnect @rsolr = DatastaxRails::RSolrClientWrapper.new(establish_solr_connection, self) end @rsolr end |