Hi, We are able to write script as per the given explanation in the docs. Please see below our script and share your suggestions to make that script better.
import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy"; (: After going through documentation ( https://docs.marklogic.com/guide/database-replication/dbrep_intro#id_99224 ), we are able to write script below to rollbak replica forests to a minimal nonblocking timestamp Step 1: Retrieve database information e.g. id, name and forests ids etc.. for which the forests needs rollback Step 2: Detach all the forests from a database Step 3: Retrieve latest merge timestamp Step 4: Set merge time stamp as required. Currently we assume 10 minutes Step 5: Save configuration with latest merge timestamp Step 6: As per given in documentation, I retrieved database minimum nonblocking timestamp Step 7: Rollback all the forests to that minimal nonblocking timestamp Step 8: Set original merge time stamp in the configuration Step 9: Attach all the forests that detached before rolling back Step 10: Save configuration with original merge time stamp and attached forests :) declare variable $DATABASETABLE := <databases> <database name="TESTDR" /> </databases>; try { let $config := admin:get-configuration() for $db in $DATABASETABLE/database let $dbname := fn:string($db/@name) let $dbid := xdmp:database($dbname) (: returns the new configuration element -- use admin:save-configuration to save the changes to the configuration or pass the configuration to other Admin API functions to make other changes. :) let $attached-forests-ids-orig := xdmp:database-forests($dbid) let $_ := if (admin:database-exists($config, $dbname)) then (: detach multiple forests from a database:) for $forest-id in $attached-forests-ids-orig let $attached-forests-ids := xdmp:database-forests(xdmp:database($dbname)) return if($forest-id eq $attached-forests-ids) then (: already attached the forest :) let $nconfig := admin:database-detach-forest($config, $dbid, $forest-id) return xdmp:set($config, $nconfig) else () else () let $orig-db-merge-timestamp := admin:database-get-merge-timestamp($config, $dbid) (: A negative value indicates a timestamp relative to the time of the merge, at ten million ticks per second. For example, 6000000000 means preserve fragments deleted ten minutes before the merge. :) let $ten-mins-ago := $orig-db-merge-timestamp - 6000000000 let $nconfig := admin:database-set-merge-timestamp($config, $dbid, $ten-mins-ago) let $_ := admin:save-configuration($nconfig) let $minimum-timestamp := xdmp:database-nonblocking-timestamp($dbid) (: let $_ := xdmp:forest-rollback(xdmp:forest-open-replica(xdmp:database-forests($dbid)),$minimum-timestamp) :) let $cmd := ' declare variable $dbid external; declare variable $minimum-timestamp external; declare variable $db-forest-ids external; xdmp:log("rollback", "debug") (: xdmp:forest-rollback(xdmp:forest-open-replica($db-forest-ids),$minimum-timestamp):) ' let $_ := xdmp:eval($cmd, ( xs:QName("dbid"), $dbid, xs:QName("minimum-timestamp"), $minimum-timestamp, xs:QName("db-forest-ids"), $attached-forests-ids-orig ), <options xmlns="xdmp:eval"> <database>{xdmp:database("Modules")}</database> </options> ) let $config := admin:get-configuration() let $nconfig-orig := admin:database-set-merge-timestamp($config, $dbid, $orig-db-merge-timestamp) (: returns the new configuration element -- use admin:save-configuration to save the changes to the configuration or pass the configuration to other Admin API functions to make other changes. :) let $_ := if (admin:database-exists($nconfig-orig, $dbname)) then (: Attach multiple forests to a database:) for $forest-id in $attached-forests-ids-orig let $attached-forests-ids := xdmp:database-forests(xdmp:database($dbname)) return if($forest-id eq $attached-forests-ids) then () (: already attached the forest :) else let $nconfig := admin:database-attach-forest($nconfig-orig, $dbid, $forest-id) return xdmp:set($nconfig-orig, $nconfig) else () let $_ := admin:save-configuration($nconfig-orig) return "Rollback is done!" } catch($e){ ( $e , xdmp:log($e, "debug") ) } Thank you for your help! Regards, Indy On Tue, Oct 4, 2016 at 8:16 PM, Indrajeet Verma <[email protected]> wrote: > Hi All, > > We have configured database replication in a database in which there are 3 > forests and we want to write a script to make DR host same database to > ready for updates in case master host is down. > > As per documentation: > After you fail over your applications to a Replica database, each Replica > forest will likely have committed its last transaction at different > timestamps. If the Replica database has multiple forests and relationships > exist between those forests, this inconsistency may cause problems. In > order to return the database to a transactionally consistent state, all > forests must be rolled back to the minimum nonblocking timestamp. > > Also it says in the documentation (https://docs.marklogic.com/ > guide/database-replication/dbrep_intro#id_99224) use the > xdmp:forest-rollback function to roll back the forests to the minimum > nonblocking-timestamp returned by the xdmp:forest-status function for each > forest that is in the open or open replica state. > > However in the code sample below in the documentation is retrieving > nonblocking-timestamp from the method xdmp:database-nonblocking- > timestamp($db). > > xquery version "1.0-ml"; > let $db := xdmp:database("Documents") > let $timestamp := xdmp:database-nonblocking-timestamp($db) > let $rollback := xdmp:forest-rollback( > xdmp:forest-open-replica( > xdmp:database-forests($db)), > $timestamp) > return > "Roll back done" > > So wanted to know whether we should use xdmp:forest-status() to get > minimum nonblocking-timestamp (*retrieve nonblocking-timestamp from all > the forests and get the minimum timestamp from all three then rollback to > that timestamp*) or xdmp:database-nonblocking-timestamp() is fine. > > Thank you for sharing your thoughts! > > Regards, > Indy >
_______________________________________________ General mailing list [email protected] Manage your subscription at: http://developer.marklogic.com/mailman/listinfo/general
