Class: Cassandra::Cluster

Inherits:
Object
  • Object
show all
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

Instance Method Details

#closeself

Synchronously closes all sessions managed by this cluster

Returns:

  • (self)

    this cluster

See Also:



232
233
234
# File 'lib/cassandra/cluster.rb', line 232

def close
  close_async.get
end

#close_asyncCassandra::Future<Cassandra::Cluster>

Asynchronously closes all sessions managed by this cluster

Returns:



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

Parameters:

  • keyspace (String) (defaults to: nil)

    optional keyspace to scope the session to

Returns:

Raises:

See Also:



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

Parameters:

  • keyspace (String) (defaults to: nil)

    optional keyspace to scope session to

Returns:

See Also:



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_hostArray<Cassandra::Host> Also known as: hosts

Yield or enumerate each member of this cluster

Overloads:



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_keyspaceArray<Cassandra::Keyspace> Also known as: keyspaces

Yield or enumerate each keyspace defined in this cluster

Overloads:



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>

Note:

an empty list is returned when statement/keyspace information is not enough to determine replica list.

Return replicas for a given statement and keyspace

Parameters:

  • keyspace (String)

    keyspace name

  • statement (Cassandra::Statement)

    statement for which to find replicas

Returns:



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

Parameters:

  • address (IPAddr, String)

    ip address

Returns:

  • (Boolean)

    true or false



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

Parameters:

  • name (String)

    keyspace name

Returns:

  • (Boolean)

    true or false



133
# File 'lib/cassandra/cluster.rb', line 133

def_delegators :@schema, :keyspace, :has_keyspace?

#host(address) ⇒ Cassandra::Host?

Find a host by its address

Parameters:

  • address (IPAddr, String)

    ip address

Returns:



109
# File 'lib/cassandra/cluster.rb', line 109

def_delegators :@registry, :host, :has_host?

#inspectString

Returns a CLI-friendly cluster representation.

Returns:

  • (String)

    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

Parameters:

  • name (String)

    keyspace name

Returns:



133
# File 'lib/cassandra/cluster.rb', line 133

def_delegators :@schema, :keyspace, :has_keyspace?

#nameString

Return cluster's name

Returns:

  • (String)

    cluster's name



63
# File 'lib/cassandra/cluster.rb', line 63

def_delegators :@metadata, :name, :find_replicas

#refresh_schemanil

Synchronously refresh schema metadata

Returns:

  • (nil)

    nothing

Raises:

See Also:



149
150
151
# File 'lib/cassandra/cluster.rb', line 149

def refresh_schema
  refresh_schema_async.get
end

#refresh_schema_asyncCassandra::Future<nil>

Trigger an asynchronous schema metadata refresh

Returns:

  • (Cassandra::Future<nil>)

    a future that will be fulfilled when schema metadata has been refreshed



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

Parameters:

Returns:

  • (self)


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

Parameters:

Returns:

  • (self)


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