Skip to content
This repository was archived by the owner on Nov 9, 2022. It is now read-only.

Commit 12b708f

Browse files
committed
Fixed #703: restart using rest api, iterate hosts to verify
1 parent 353f2ea commit 12b708f

File tree

3 files changed

+108
-60
lines changed

3 files changed

+108
-60
lines changed

deploy/lib/MLClient.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,15 @@ def build_request_params(url, verb)
8888
}
8989
end
9090

91-
def go(url, verb, headers = {}, params = nil, body = nil, xcc = false)
91+
def go(url, verb, headers = nil, params = nil, body = nil, xcc = false)
9292
logger.debug(%Q{[#{verb.upcase}]\t#{url}})
9393
password_prompt
9494
request_params = build_request_params(url, verb)
9595
# configure headers
96-
headers.each do |k, v|
97-
request_params[:request][k] = v
96+
if headers
97+
headers.each do |k, v|
98+
request_params[:request][k] = v
99+
end
98100
end
99101

100102
raise ExitException.new("Don't combine params and body. One or the other please") if (params && body)

deploy/lib/server_config.rb

Lines changed: 87 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -533,76 +533,106 @@ def execute_query(query, properties = {})
533533
return r
534534
end
535535

536-
def restart_basic
537-
@ml_username = @properties['ml.bootstrap-user'] || @properties['ml.user']
538-
if @ml_username == @properties['ml.bootstrap-user']
539-
@ml_password = @properties['ml.bootstrap-password']
540-
else
541-
@ml_password = @properties['ml.password']
542-
end
543-
544-
group = nil
545-
ARGV.each do |arg|
546-
# Exclude any argument passed from command line.
547-
if ! arg.match("^-")
548-
group = arg
549-
end
536+
def restart_group(group = nil)
537+
if ! group
538+
# Note:
539+
# Restarting partial cluster is unsafe when working with multiple groups.
540+
# Therefor restart entire cluster by default..
541+
group = "cluster"
550542
end
551543

552-
if group && group == "cluster"
544+
if group == "cluster"
553545
logger.info "Restarting MarkLogic Server cluster of #{@hostname}"
554-
elsif group
555-
logger.info "Restarting MarkLogic Server group #{group}"
546+
r = go(%Q{http://#{@properties["ml.server"]}:#{@properties["ml.bootstrap-port"]}/manage/v2?format=json}, "post", {
547+
'Content-Type' => 'application/json'
548+
}, nil, %Q{
549+
{ "operation": "restart-local-cluster" }
550+
})
556551
else
557-
# restarting partial cluster unsafe when working with multiple groups
558-
#logger.info "Restarting MarkLogic Server group of #{@hostname}"
559-
logger.info "Restarting MarkLogic Server cluster of #{@hostname}"
560-
group = "cluster"
552+
logger.info "Restarting MarkLogic Server group #{group}"
553+
r = go(%Q{http://#{@properties["ml.server"]}:#{@properties["ml.bootstrap-port"]}/manage/v2/groups/#{group}?format=json}, "post", {
554+
'Content-Type' => 'application/json'
555+
}, nil, %Q{
556+
{ "operation": "restart-group" }
557+
})
561558
end
562-
logger.debug "this: #{self}"
563-
setup = File.read ServerConfig.expand_path("#{@@path}/lib/xquery/setup.xqy")
564-
r = execute_query %Q{#{setup} setup:do-restart("#{group}")}
565-
logger.debug "code: #{r.code.to_i}"
566559

567-
r.body = parse_body(r.body)
568-
logger.info r.body
569-
return true
560+
raise ExitException.new(r.body) unless r.code.to_i == 202
561+
562+
return JSON.parse(r.body)['restart']['last-startup']
563+
end
564+
565+
def get_host_names
566+
r = go(%Q{http://#{@properties["ml.server"]}:8002/manage/v2/hosts?format=json}, "get")
567+
568+
raise ExitException.new(r.body) unless r.code.to_i == 200
569+
570+
names = {}
571+
JSON.parse(r.body)['host-default-list']['list-items']['list-item'].each do |host|
572+
names[host['idref']] = host['nameref']
573+
end
574+
575+
return names
570576
end
571577

572-
# implemented verified restart
573578
def restart
574-
verify = find_arg(['--verify'])
575-
if verify==='false'
576-
restart_basic
579+
# Default to verified restart
580+
verify = find_arg(['--verify']) != 'false'
581+
582+
group = next_arg("^[^-]")
583+
584+
@ml_username = @properties['ml.bootstrap-user'] || @properties['ml.user']
585+
if @ml_username == @properties['ml.bootstrap-user']
586+
@ml_password = @properties['ml.bootstrap-password']
587+
else
588+
@ml_password = @properties['ml.password']
589+
end
590+
591+
if ! verify
592+
restart_group(group)
577593
else
578-
# defaults to verified restart
579-
old_timestamp = go(%Q{http://#{@properties["ml.server"]}:8001/admin/v1/timestamp}, "get").body
580-
restart_basic
581-
retry_count = 0
582-
retry_max = @properties["ml.verify_retry_max"].to_i
583-
retry_interval = @properties["ml.verify_retry_interval"].to_i
584-
new_timestamp = old_timestamp
585-
while retry_count < retry_max do
586-
begin
587-
new_timestamp = go(%Q{http://#{@properties["ml.server"]}:8001/admin/v1/timestamp}, "get").body
588-
rescue
589-
logger.info 'Verifying restart ...'
590-
logger.debug 'Retry attempt ' + retry_count.to_s + ' failed'
594+
old_timestamps = restart_group(group)
595+
596+
host_names = get_host_names()
597+
598+
# Iterate until all hosts have restarted (or max is reached)
599+
old_timestamps.each do |host|
600+
host_name = host_names[host['host-id']]
601+
old_timestamp = host['value']
602+
603+
print "Verifying restart for #{host_name}"
604+
605+
# Initialize vars for repeated check
606+
retry_count = 0
607+
retry_max = @properties["ml.verify_retry_max"].to_i
608+
retry_interval = [@properties["ml.verify_retry_interval"].to_i, 10].max # 10 sec sleep at least
609+
new_timestamp = old_timestamp
610+
611+
while retry_count < retry_max do
612+
begin
613+
new_timestamp = go(%Q{http://#{host_name}:8001/admin/v1/timestamp}, "get").body
614+
rescue
615+
logger.debug 'Retry attempt ' + retry_count.to_s + ' failed'
616+
end
617+
618+
if new_timestamp != old_timestamp
619+
# Indicates that restart is confirmed successful
620+
break
621+
end
622+
623+
# Retry..
624+
print ".."
625+
sleep retry_interval
626+
retry_count += 1
591627
end
592-
if new_timestamp != old_timestamp
593-
# indicates that restart is confirmed successful
594-
break
628+
629+
if new_timestamp == old_timestamp
630+
puts ": FAILED"
631+
else
632+
puts ": OK"
595633
end
596-
logger.debug "Verifying restart..."
597-
sleep retry_interval
598-
retry_count += 1
599-
end
600-
if new_timestamp == old_timestamp
601-
logger.warn "Could not verify restart"
602-
else
603-
logger.info 'Verified restart.'
604-
logger.debug "Verified restart new #{new_timestamp} old #{old_timestamp}"
605634
end
635+
606636
end
607637
end
608638

deploy/lib/util.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ def find_arg(args = [])
3636
nil
3737
end
3838

39+
def next_arg(match)
40+
ARGV.each do |arg|
41+
# Exclude any argument passed from command line.
42+
if arg.match(match)
43+
44+
# Remove group from arguments list
45+
index = ARGV.index(arg)
46+
ARGV.slice!(index)
47+
48+
# Bail out on first valid arg
49+
return arg
50+
end
51+
end
52+
nil
53+
end
54+
3955
def load_prop_from_args(props)
4056
ARGV.each do |a|
4157
if a.match(/(^--)(ml\..*)(=)(.*)/)

0 commit comments

Comments
 (0)