Class: Redis::Populator

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/redis_migrator/redis_populator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#parse_redis_url, #to_redis_proto

Constructor Details

#initialize(redis_hosts) ⇒ Populator

Returns a new instance of Populator.



10
11
12
# File 'lib/redis_migrator/redis_populator.rb', line 10

def initialize(redis_hosts)
  @redis = Redis::Distributed.new(redis_hosts)
end

Instance Attribute Details

#redisObject

Returns the value of attribute redis.



8
9
10
# File 'lib/redis_migrator/redis_populator.rb', line 8

def redis
  @redis
end

Instance Method Details

#generate_keys(num) ⇒ Object

Generate sets’ keys to populate redis cluster

Parameters:

  • num

    size [Integer] the number of sets that have to be created

Returns:

  • hash of keys grouped by redis node



17
18
19
20
21
22
23
24
25
26
# File 'lib/redis_migrator/redis_populator.rb', line 17

def generate_keys(num)
  num.times.inject({}) do |acc, i| 
    key = ::Digest::MD5.hexdigest(i.to_s)
    node = redis.node_for(key).client
    hash_key = "redis://#{node.host}:#{node.port}/#{node.db}"
    acc[hash_key] = [] if acc[hash_key].nil?
    acc[hash_key] << key
    acc
  end
end

#populate_cluster(keys_num, num) ⇒ Object

Populates redis cluster

Parameters:

  • keys_num (Integer)

    amount of redis sets that need to be populated

  • num (Integer)

    number of members in each set



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/redis_migrator/redis_populator.rb', line 46

def populate_cluster(keys_num, num)
  redis.flushdb

  nodes = generate_keys(keys_num)
  threads = []

  nodes.keys.each do |node_url|
    node = parse_redis_url(node_url)

    threads << Thread.new(node, nodes[node_url], num) {|node, keys, size|
      populate_keys(node, keys, size)
    }
  end

  threads.each{|t| t.join}
end

#populate_keys(node, keys, size) ⇒ Object

Populates sets with the given amount of members

Parameters:

  • node (Hash)

    a parsed redis_url

  • keys (Array)

    an array of sets’ keys that need to be populated



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/redis_migrator/redis_populator.rb', line 31

def populate_keys(node, keys, size)
  f = IO.popen("redis-cli -h #{node[:host]} -p #{node[:port]} -n #{node[:db]} --pipe", IO::RDWR)

  keys.each do |key|
    size.times.map do |x| 
      f << to_redis_proto('SADD', key, ::Digest::MD5.hexdigest('f' + x.to_s))
    end
  end

  f.close
end