Module: XBar::Mapper

Extended by:
ClassMethods
Included in:
Proxy
Defined in:
lib/xbar/mapper.rb

Overview

This module holds the current configuration. It is read from a JSON document and always represents exactly the state of that document. The configuration should not be ‘tweaked’ by changing the state of in-memory structures. The approved way to change the configuration is to call:

XBar::Mapper.reset(:xbar_env => "<config file>",
  :app_env => "<application environment>")

This loads a new configuration file from the XBar ‘config’ directory. This file may contain multiple ‘environments’. Only the specified environment is used. Both arguments are required. Changes to the configuration via this API cause all Proxies and their Shards to be reset. This is, all Proxies are found and ‘cleaned’, and the Shard references held by each Proxy are dropped and new Shards are allocated.

No thread-specific state is kept in the Mapper structure. In fact, the state it encapsulates is shared by all threads. In contrast, thread-specific state is kept in instances of the Proxy class, or instances of the Shard class. Thread specific state is handled as follows:

Thread.current references an instance of Proxy. In turn, an an instance of XBar::Proxy references a list of instances of XBar::Shard.

Each Shard is considered to be an array of replicas, even if the configuration JSON specifies a single Connection (as a String literal). In this case, the Shard is an array of one replica. At any point in time, each Shard has one replica designated as the master. Thus the complete Shard description always an array of Connections and the first Connection in the array is considered to be the master.

This module is included in the XBar::Proxy class. This adds instance methods to instances of XBar::Proxy that allow the configuration state to be read.

The following data structures are maintained:

@@connections -- a Hash, the key is the connection name and the value
  is a connection pool.
@@shards -- an array of Hashes.  For each hash the key is the shard name
  and the value is a connection pool.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

app_env, config, config_file_name, config_from_file, connection_file_name, disconnect_all!, environments, initialize_options, reset, shards=, unregister, xbar_env

Class Method Details

.exportsObject



47
48
49
50
# File 'lib/xbar/mapper.rb', line 47

def self.exports
  # omit master_config
  %w(connections shards adapters options app_env proxies).map(&:to_sym)
end

Instance Method Details

#registerObject



345
346
347
# File 'lib/xbar/mapper.rb', line 345

def register
  Mapper.register(self)
end

#reset_config(options = {}) ⇒ Object



341
342
343
# File 'lib/xbar/mapper.rb', line 341

def reset_config(options = {})
  Mapper.reset(options)
end