Class: Mink::ShardingManager

Inherits:
Object
  • Object
show all
Includes:
ManagerHelper
Defined in:
lib/mink/managers/sharding_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ManagerHelper

#attempt, #get_path, #kill_existing_mongods, #kill_existing_mongos, #kill_pidlist

Constructor Details

#initialize(opts = {}) ⇒ ShardingManager

Returns a new instance of ShardingManager.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mink/managers/sharding_manager.rb', line 7

def initialize(opts={})
  @durable = opts.fetch(:durable, true)

  @mongos_port  = opts.fetch(:mongos_start_port, 50000)
  @config_port  = opts.fetch(:config_server_start_port, 40000)
  @working_dir  = opts.fetch(:working_dir, nil)
  @mongod_path  = opts.fetch(:mongod_path, "mongod")
  @mongos_path  = opts.fetch(:mongos_path, "mongos")
  @write_conf   = opts.fetch(:write_conf, false)
  @host         = opts.fetch(:host, "localhost")

  @shard_count  = opts.fetch(:shard_count, 2)
  @mongos_count = opts.fetch(:mongos_count, 1)
  @config_server_count = opts.fetch(:config_server_count, 1)
  @replica_set_config  = opts.fetch(:replica_set_config, {})

  @shard_db     = opts.fetch(:shard_database, "app")
  @shard_coll   = opts.fetch(:shard_collection, "images")
  @shard_key    = opts.fetch(:shard_key, {:tid => 1})

  if ![1, 3].include?(@config_server_count)
    raise ArgumentError, "Must specify 1 or 3 config servers."
  end

  @pidlistfile  = File.join(@working_dir, "mink.pidlist")
  @data_path    = opts.fetch(:path, File.join(@working_dir, "data"))

  @config_servers = {}
  @mongos_servers = {}
  @shards = []
  @ports  = []
  @pids   = []
end

Instance Attribute Details

#shardsObject

Returns the value of attribute shards.



5
6
7
# File 'lib/mink/managers/sharding_manager.rb', line 5

def shards
  @shards
end

Instance Method Details

#add_shardsObject



68
69
70
71
72
73
74
75
# File 'lib/mink/managers/sharding_manager.rb', line 68

def add_shards
  @shards.each do |shard|
    cmd = {:addshard => shard.shard_string}
    cmd
    p mongos['admin'].command(cmd)
  end
  p mongos['admin'].command({:listshards => 1})
end

#configure_clusterObject



49
50
51
52
53
54
55
# File 'lib/mink/managers/sharding_manager.rb', line 49

def configure_cluster
  add_shards
  enable_sharding
  if shard_collection
    STDOUT << "Shard cluster initiated!\nEnter the following to connect:\n  mongo localhost:#{@mongos_port}"
  end
end

#enable_shardingObject



57
58
59
# File 'lib/mink/managers/sharding_manager.rb', line 57

def enable_sharding
  mongos['admin'].command({:enablesharding => @shard_db})
end

#kill_randomObject



83
84
85
86
# File 'lib/mink/managers/sharding_manager.rb', line 83

def kill_random
  shard_to_kill = rand(@shard_count)
  @shards[shard_to_kill].kill_primary
end

#mongosObject



77
78
79
80
81
# File 'lib/mink/managers/sharding_manager.rb', line 77

def mongos
  attempt do
    @mongos ||= Mongo::Connection.new(@host, @mongos_servers[0]['port'])
  end
end

#restart_killedObject



88
89
90
91
92
93
94
95
# File 'lib/mink/managers/sharding_manager.rb', line 88

def restart_killed
  threads = []
  @shards.each do |k, shard|
    threads << Thread.new do
      shard.restart_killed_nodes
    end
  end
end

#shard_collectionObject



61
62
63
64
65
66
# File 'lib/mink/managers/sharding_manager.rb', line 61

def shard_collection
  cmd = BSON::OrderedHash.new
  cmd[:shardcollection] = "#{@shard_db}.#{@shard_coll}"
  cmd[:key] = {:tid => 1}
  mongos['admin'].command(cmd)
end

#start_clusterObject



41
42
43
44
45
46
47
# File 'lib/mink/managers/sharding_manager.rb', line 41

def start_cluster
  kill_existing_mongods
  kill_existing_mongos
  start_sharding_components
  start_mongos_servers
  configure_cluster
end