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

Orphaned Amps Fix #282

Closed
mustard57 opened this issue Sep 26, 2014 · 6 comments
Closed

Orphaned Amps Fix #282

mustard57 opened this issue Sep 26, 2014 · 6 comments

Comments

@mustard57
Copy link

Hello

If you do a bootstrap using a modules db, then switch your config to use 'filesystem' as the modules db ( bootstrap after doing this ) , then do a wipe, any amps belonging to the original modules db will get orphanned - you can see this in the 'Amps' page as the amp in question will have a number as the 'database'.

If this happens, you will get an error in setup:get-amps when you next bootstrap near this line

xdmp:database-name($amp/sec:database)

as the db no longer exists. The manual fix is to delete the offending amp.

This is the error you will get

Bootstrapping your project into MarkLogic on localhost...
ERROR: [{"qid":null, "type":"string", "result":"XDMP-NODB: xdmp:database-name(sec:id(\"510626068934067422\")) -- No database with identifier 510626068934067422\nSee MarkLogic Server error log for more details."}]
ERROR: ... Bootstrap FAILED

The code fix ( below ) is to ignore it.

diff --git a/deploy/lib/xquery/setup.xqy b/deploy/lib/xquery/setup.xqy
index 8e58cc6..440306f 100644
--- a/deploy/lib/xquery/setup.xqy
+++ b/deploy/lib/xquery/setup.xqy
@@ -4259,6 +4259,8 @@ declare function setup:get-amps($ids as xs:unsignedLong*)
     <amps xmlns="http://marklogic.com/xdmp/security">
     {
       for $amp in $amps
+      (: Avoid errors if amp is 'orphanned' :)
+      where $amp/sec:database = xdmp:databases()
       return
         element sec:amp
         {

Note if you try to re-create the error you may get SEC-AMPEXISTS after you have reproduced then added the patch - this is a consequence of the fact that the original error does not get rolled back. However if you undo what you got when trying to recreate the error, then try and recreate the error having applied the patch, the patch fixes. That may or may not be clear.

@dmcassel
Copy link
Collaborator

I think rather than a where clause, an if/then with the same check could produce a more informative error instead of quietly letting it go.

@grtjn
Copy link
Contributor

grtjn commented Feb 23, 2015

I suspect this error also appears if you create an amp with Roxy, and then remove the amp from the ml-config, without doing a wipe first. Then, if you wipe the entire project the amp will get left behind, but with an invalid database ref..

@grtjn
Copy link
Contributor

grtjn commented Feb 23, 2015

I agree letting it go quietly is a missed opportunity to report something. Not sure throwing an error is the best solution though. The amp might be unrelated to the project that you try to bootstrap, bit hard to tell. Easiest to tell by db-name, and that is precisely what is missing. I'll push a change that will log a message in the ErrorLog. That is at least something..

paxtonhare added a commit that referenced this issue Mar 10, 2015
Fixed #282: catching and logging orphaned amps
@mustard57
Copy link
Author

I wrote this for getting rid of dead amps, if that's at all helpful.

This is largely the problem here I think.

Ken

@grtjn
Copy link
Contributor

grtjn commented Mar 12, 2015

Applied formatting to code from Ken:

let $script :=
'xquery version "1.0-ml";

declare namespace sec="http://marklogic.com/xdmp/security";

declare function local:db-exists($id) as xs:boolean
{
  if($id = 0) then fn:true()
  else
  try{
    xdmp:database-name($id)[0],fn:true()
  }
  catch($ex){
    fn:false()
  }
};

let $dead-amps := /sec:amp[fn:not(local:db-exists(./sec:database))]
return
(
for $dead-amp in $dead-amps
return
xdmp:document-delete($dead-amp)
,
xdmp:log(fn:count($dead-amps)||" dead amps removed")
)
'
return
xdmp:eval($script,(),<options
xmlns="xdmp:eval"><database>{xdmp:database("Security")}</database></options>)

@grtjn
Copy link
Contributor

grtjn commented Mar 12, 2015

I think it could be worth looking if this could be integrated into wipe. I'll open a new ticket since this ticket was about bootstrapping..

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants