Skip to content

Commit

Permalink
Merge pull request #217 from iberezovskiy/master
Browse files Browse the repository at this point in the history
Implement retries for MongoDB shell commands
  • Loading branch information
hunner committed Aug 20, 2015
2 parents 3cf6f32 + 7dcb230 commit 6fad8b4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
18 changes: 17 additions & 1 deletion lib/puppet/provider/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,27 @@ def self.get_conn_string

# Mongo Command Wrapper
def self.mongo_eval(cmd, db = 'admin')
retry_count = 10
retry_sleep = 3
if mongorc_file
cmd = mongorc_file + cmd
end

out = mongo([db, '--quiet', '--host', get_conn_string, '--eval', cmd])
out = nil
retry_count.times do |n|
begin
out = mongo([db, '--quiet', '--host', get_conn_string, '--eval', cmd])
rescue => e
debug "Request failed: '#{e.message}' Retry: '#{n}'"
sleep retry_sleep
next
end
break
end

if !out
fail "Could not evalute MongoDB shell command: #{cmd}"
end

out.gsub!(/ObjectId\(([^)]*)\)/, '\1')
out
Expand Down
28 changes: 22 additions & 6 deletions lib/puppet/provider/mongodb_replset/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ def rs_add_arbiter(host, master)
mongo_command("rs.addArb(\"#{host}\")", master)
end

def auth_enabled
@resource[:auth_enabled]
end

def master_host(hosts)
hosts.each do |host|
status = db_ismaster(host)
Expand Down Expand Up @@ -135,31 +139,36 @@ def self.get_replset_properties
end

def alive_members(hosts)
alive = []
hosts.select do |host|
begin
Puppet.debug "Checking replicaset member #{host} ..."
status = rs_status(host)
if status.has_key?('errmsg') and status['errmsg'] == 'not running with --replSet'
raise Puppet::Error, "Can't configure replicaset #{self.name}, host #{host} is not supposed to be part of a replicaset."
end

if auth_enabled and status.has_key?('errmsg') and (status['errmsg'].include? "unauthorized" or status['errmsg'].include? "not authorized")
Puppet.warning "Host #{host} is available, but you are unauthorized because of authentication is enabled: #{auth_enabled}"
alive.push(host)
end

if status.has_key?('set')
if status['set'] != self.name
raise Puppet::Error, "Can't configure replicaset #{self.name}, host #{host} is already part of another replicaset."
end

# This node is alive and supposed to be a member of our set
Puppet.debug "Host #{host} is available for replset #{status['set']}"
true
alive.push(host)
elsif status.has_key?('info')
Puppet.debug "Host #{host} is alive but unconfigured: #{status['info']}"
true
end
rescue Puppet::ExecutionFailure
Puppet.warning "Can't connect to replicaset member #{host}."

false
end
end
return alive
end

def set_members
Expand Down Expand Up @@ -225,10 +234,17 @@ def set_members
end

def mongo_command(command, host, retries=4)
self.class.mongo_command(command,host,retries)
self.class.mongo_command(command,host,retries,auth_enabled)
end

def self.mongo_command(command, host=nil, retries=4)
def self.mongo_command(command, host=nil, retries=4, auth_enabled=false)
if auth_enabled and command =~ 'rs.initiate'
# We can't setup replica from any hosts except localhost
# if authentication is enabled
# User can't be created before replica set initialization
# So we can't use user credentials for auth
host = '127.0.0.1'
end
# Allow waiting for mongod to become ready
# Wait for 2 seconds initially and double the delay at each retry
wait = 2
Expand Down
5 changes: 5 additions & 0 deletions lib/puppet/type/mongodb_replset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
desc "The name of the replicaSet"
end

newparam(:auth_enabled) do
desc "Check authentication enabled"
defaultto false
end

newparam(:arbiter) do
desc "The replicaSet arbiter"
end
Expand Down

0 comments on commit 6fad8b4

Please sign in to comment.