Class: Cassandra::Cluster
- Inherits:
-
Object
- Object
- Cassandra::Cluster
- Extended by:
- Forwardable
- Defined in:
- lib/cassandra/cluster.rb,
lib/cassandra/cluster/client.rb,
lib/cassandra/cluster/schema.rb,
lib/cassandra/cluster/options.rb,
lib/cassandra/cluster/metadata.rb,
lib/cassandra/cluster/registry.rb,
lib/cassandra/cluster/connector.rb,
lib/cassandra/cluster/connection_pool.rb,
lib/cassandra/cluster/failed_connection.rb,
lib/cassandra/cluster/control_connection.rb,
lib/cassandra/cluster/schema/type_parser.rb,
lib/cassandra/cluster/schema/partitioners/random.rb,
lib/cassandra/cluster/schema/partitioners/murmur3.rb,
lib/cassandra/cluster/schema/partitioners/ordered.rb,
lib/cassandra/cluster/schema/replication_strategies/none.rb,
lib/cassandra/cluster/schema/replication_strategies/simple.rb,
lib/cassandra/cluster/schema/replication_strategies/network_topology.rb
Overview
Cluster represents a cassandra cluster. It serves as a session factory factory and a collection of metadata.
Instance Method Summary collapse
-
#close ⇒ self
Synchronously closes all sessions managed by this cluster.
-
#close_async ⇒ Cassandra::Future<Cassandra::Cluster>
Asynchronously closes all sessions managed by this cluster.
-
#connect(keyspace = nil) ⇒ Cassandra::Session
Synchronously create a new session, optionally scoped to a keyspace.
-
#connect_async(keyspace = nil) ⇒ Cassandra::Future<Cassandra::Session>
Asynchronously create a new session, optionally scoped to a keyspace.
-
#each_host(&block) ⇒ Object
(also: #hosts)
Yield or enumerate each member of this cluster.
-
#each_keyspace(&block) ⇒ Object
(also: #keyspaces)
Yield or enumerate each keyspace defined in this cluster.
-
#find_replicas(keyspace, statement) ⇒ Array<Cassandra::Host>
Return replicas for a given statement and keyspace.
-
#has_host?(address) ⇒ Boolean
Determine if a host by a given address exists.
-
#has_keyspace?(name) ⇒ Boolean
Determine if a keyspace by a given name exists.
-
#host(address) ⇒ Cassandra::Host?
Find a host by its address.
-
#inspect ⇒ String
A CLI-friendly cluster representation.
-
#keyspace(name) ⇒ Cassandra::Keyspace?
Find a keyspace by name.
-
#name ⇒ String
Return cluster's name.
-
#refresh_schema ⇒ nil
Synchronously refresh schema metadata.
-
#refresh_schema_async ⇒ Cassandra::Future<nil>
Trigger an asynchronous schema metadata refresh.
-
#register(listener) ⇒ self
Register a cluster state listener.
-
#unregister(listener) ⇒ self
Unregister a cluster state listener.
Instance Method Details
#close ⇒ self
Synchronously closes all sessions managed by this cluster
232 233 234 |
# File 'lib/cassandra/cluster.rb', line 232 def close close_async.get end |
#close_async ⇒ Cassandra::Future<Cassandra::Cluster>
Asynchronously closes all sessions managed by this cluster
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/cassandra/cluster.rb', line 211 def close_async promise = @futures.promise @control_connection.close_async.on_complete do |f| if f.resolved? promise.fulfill(self) else f.on_failure {|e| promise.break(e)} end @executor.shutdown end promise.future end |
#connect(keyspace = nil) ⇒ Cassandra::Session
Synchronously create a new session, optionally scoped to a keyspace
203 204 205 |
# File 'lib/cassandra/cluster.rb', line 203 def connect(keyspace = nil) connect_async(keyspace).get end |
#connect_async(keyspace = nil) ⇒ Cassandra::Future<Cassandra::Session>
Asynchronously create a new session, optionally scoped to a keyspace
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/cassandra/cluster.rb', line 162 def connect_async(keyspace = nil) if !keyspace.nil? && !keyspace.is_a?(::String) return @futures.error(::ArgumentError.new("keyspace must be a string, #{keyspace.inspect} given")) end client = Client.new(@logger, @registry, @schema, @io_reactor, @connector, @load_balancing_policy, @reconnection_policy, @retry_policy, @address_resolver, @connection_options, @futures) session = Session.new(client, @execution_options, @futures) promise = @futures.promise client.connect.on_complete do |f| if f.resolved? if keyspace f = session.execute_async("USE #{Util.escape_name(keyspace)}") f.on_success {promise.fulfill(session)} f.on_failure {|e| promise.break(e)} else promise.fulfill(session) end else f.on_failure {|e| promise.break(e)} end end promise.future end |
#each_host {|host| ... } ⇒ Cassandra::Cluster #each_host ⇒ Array<Cassandra::Host> Also known as: hosts
Yield or enumerate each member of this cluster
93 94 95 96 97 |
# File 'lib/cassandra/cluster.rb', line 93 def each_host(&block) r = @registry.each_host(&block) return self if r == @registry r end |
#each_keyspace {|keyspace| ... } ⇒ Cassandra::Cluster #each_keyspace ⇒ Array<Cassandra::Keyspace> Also known as: keyspaces
Yield or enumerate each keyspace defined in this cluster
117 118 119 120 121 |
# File 'lib/cassandra/cluster.rb', line 117 def each_keyspace(&block) r = @schema.each_keyspace(&block) return self if r == @schema r end |
#find_replicas(keyspace, statement) ⇒ Array<Cassandra::Host>
an empty list is returned when statement/keyspace information is not enough to determine replica list.
Return replicas for a given statement and keyspace
63 |
# File 'lib/cassandra/cluster.rb', line 63 def_delegators :@metadata, :name, :find_replicas |
#has_host?(address) ⇒ Boolean
Determine if a host by a given address exists
109 |
# File 'lib/cassandra/cluster.rb', line 109 def_delegators :@registry, :host, :has_host? |
#has_keyspace?(name) ⇒ Boolean
Determine if a keyspace by a given name exists
133 |
# File 'lib/cassandra/cluster.rb', line 133 def_delegators :@schema, :keyspace, :has_keyspace? |
#host(address) ⇒ Cassandra::Host?
Find a host by its address
109 |
# File 'lib/cassandra/cluster.rb', line 109 def_delegators :@registry, :host, :has_host? |
#inspect ⇒ String
Returns a CLI-friendly cluster representation.
237 238 239 |
# File 'lib/cassandra/cluster.rb', line 237 def inspect "#<#{self.class.name}:0x#{self.object_id.to_s(16)}>" end |
#keyspace(name) ⇒ Cassandra::Keyspace?
Find a keyspace by name
133 |
# File 'lib/cassandra/cluster.rb', line 133 def_delegators :@schema, :keyspace, :has_keyspace? |
#name ⇒ String
Return cluster's name
63 |
# File 'lib/cassandra/cluster.rb', line 63 def_delegators :@metadata, :name, :find_replicas |
#refresh_schema ⇒ nil
Synchronously refresh schema metadata
149 150 151 |
# File 'lib/cassandra/cluster.rb', line 149 def refresh_schema refresh_schema_async.get end |
#refresh_schema_async ⇒ Cassandra::Future<nil>
Trigger an asynchronous schema metadata refresh
139 140 |
# File 'lib/cassandra/cluster.rb', line 139 def_delegator :@control_connection, :refresh_schema_async_maybe_retry, \ :refresh_schema_async |
#register(listener) ⇒ self
Register a cluster state listener. State listener will start receiving notifications about topology and schema changes
70 71 72 73 74 |
# File 'lib/cassandra/cluster.rb', line 70 def register(listener) @registry.add_listener(listener) @schema.add_listener(listener) self end |
#unregister(listener) ⇒ self
Unregister a cluster state listener. State listener will stop receiving notifications about topology and schema changes
81 82 83 84 85 |
# File 'lib/cassandra/cluster.rb', line 81 def unregister(listener) @registry.remove_listener(listener) @schema.remove_listener(listener) self end |