Module: RedisFailover::Util

Extended by:
Util
Included in:
Client, FailoverStrategy, Node, NodeManager, NodeStrategy, NodeWatcher, Util
Defined in:
lib/redis_failover/util.rb

Overview

Common utiilty methods and constants.

Constant Summary collapse

REDIS_READ_OPS =

Redis read operations that are automatically dispatched to slaves. Any operation not listed here will be dispatched to the master.

Set[
  :echo,
  :exists,
  :get,
  :getbit,
  :getrange,
  :hexists,
  :hget,
  :hgetall,
  :hkeys,
  :hlen,
  :hmget,
  :hvals,
  :keys,
  :lindex,
  :llen,
  :lrange,
  :mapped_hmget,
  :mapped_mget,
  :mget,
  :scard,
  :sdiff,
  :sinter,
  :sismember,
  :smembers,
  :srandmember,
  :strlen,
  :sunion,
  :type,
  :zcard,
  :zcount,
  :zrange,
  :zrangebyscore,
  :zrank,
  :zrevrange,
  :zrevrangebyscore,
  :zrevrank,
  :zscore
].freeze
UNSUPPORTED_OPS =

Unsupported Redis operations. These don't make sense in a client that abstracts the master/slave servers.

Set[:select, :dbsize].freeze
DEFAULT_ROOT_ZNODE_PATH =

Default root node in ZK used for redis_failover.

'/redis_failover'.freeze
REDIS_ERRORS =

Connectivity errors that the redis (<3.x) client raises.

Errno.constants.map { |c| Errno.const_get(c) }
ZK_ERRORS =

ZK Errors

[
  ZK::Exceptions::LockAssertionFailedError,
  ZK::Exceptions::InterruptedSession,
  ZK::Exceptions::Retryable,
  Zookeeper::Exceptions::ContinuationTimeoutError
].freeze
CONNECTIVITY_ERRORS =

Full set of errors related to connectivity.

[
  RedisFailover::Error,
  REDIS_ERRORS,
  ZK_ERRORS
].flatten.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.loggerLogger

Returns the logger instance to use.

Returns:

  • (Logger)

    the logger instance to use



97
98
99
100
101
102
103
104
105
106
# File 'lib/redis_failover/util.rb', line 97

def self.logger
  @logger ||= begin
    logger = Logger.new(STDOUT)
    logger.level = Logger::INFO
    logger.formatter = proc do |severity, datetime, progname, msg|
      "#{datetime.utc} RedisFailover #{Process.pid} #{severity}: #{msg}\n"
    end
    logger
  end
end

.logger=(logger) ⇒ Object

Sets a new logger to use.

Parameters:

  • logger (Logger)

    a new logger to use



111
112
113
# File 'lib/redis_failover/util.rb', line 111

def self.logger=(logger)
  @logger = logger
end

Instance Method Details

#decode(data) ⇒ Object

Decodes the specified JSON data.

Parameters:

  • data (String)

    the JSON data to decode

Returns:

  • (Object)

    the decoded data



132
133
134
135
# File 'lib/redis_failover/util.rb', line 132

def decode(data)
  return unless data
  MultiJson.decode(data)
end

#different?(ary_a, ary_b) ⇒ Boolean

Determines if two arrays are different.

Parameters:

  • ary_a (Array)

    the first array

  • ary_b (Array)

    the second array

Returns:

  • (Boolean)

    true if arrays are different, false otherwise



92
93
94
# File 'lib/redis_failover/util.rb', line 92

def different?(ary_a, ary_b)
  ((ary_a | ary_b) - (ary_a & ary_b)).size > 0
end

#encode(data) ⇒ String

Encodes the specified data in JSON format.

Parameters:

  • data (Object)

    the data to encode

Returns:

  • (String)

    the JSON-encoded data



124
125
126
# File 'lib/redis_failover/util.rb', line 124

def encode(data)
  MultiJson.encode(data)
end

#loggerLogger

Returns the logger instance to use.

Returns:

  • (Logger)

    the logger instance to use



116
117
118
# File 'lib/redis_failover/util.rb', line 116

def logger
  Util.logger
end

#symbolize_keys(hash) ⇒ Hash

Symbolizes the keys of the specified hash.

Parameters:

  • hash (Hash)

    a hash for which keys should be symbolized

Returns:

  • (Hash)

    a new hash with symbolized keys



83
84
85
# File 'lib/redis_failover/util.rb', line 83

def symbolize_keys(hash)
  Hash[hash.map { |k, v| [k.to_sym, v] }]
end