Skip to content

Instantly share code, notes, and snippets.

@ableasdale
Created September 11, 2013 10:45
Show Gist options
  • Save ableasdale/6521982 to your computer and use it in GitHub Desktop.
Save ableasdale/6521982 to your computer and use it in GitHub Desktop.
Flexible replication: revert failed over forests
xquery version "1.0-ml";
(: This script looks for forests configured as master on this host that
are currently acting as a replica. This implies that a failover has
occurred and a replica for the forest has taken over as master, and
also that the master is now back online but is in the role of replica.
Restarting the acting master will cause the configured master to
resume its role as master. That will allow the server node that
owns the master forest to resume handling query load for it.
This module is intended to be installed in the modules database and
run by a periodic scheduled task about once a day.
:)
declare namespace fs = "http://marklogic.com/xdmp/status/forest";
import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy";
let $replicas :=
let $me := xdmp:host()
for $db in xdmp:databases()
for $f in xdmp:database-forests($db)
let $status := xdmp:forest-status($f)
where $status/fs:state = "sync replicating"
return
for $replica in $status/fs:replica-forests/fs:replica-forest
let $replica-status := xdmp:forest-status($replica)
where ($replica-status/fs:state = ("open","open replica")) and ($replica-status/fs:host-id = $me)
return $replica
let $replica-count := fn:count ($replicas) let $msg := fn:concat (
"Restarting replica forest",
if ($replica-count gt 1) then "s" else "",
" to re-target master: xdmp:forest-restart((",
fn:string-join($replicas/xdmp:forest-name(.), ", "),
"))")
return
if ($replica-count gt 0)
then ( xdmp:log ($msg), xdmp:forest-restart ($replicas) ) else xdmp:log ("No acting-master replica forests found that need restarting", "info")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment