-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathforests.xqy
103 lines (93 loc) · 3.62 KB
/
forests.xqy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
xquery version "1.0-ml";
(:
: Copyright (c) 2011-2013 Michael Blakeley. All Rights Reserved.
:
: Licensed under the Apache License, Version 2.0 (the "License");
: you may not use this file except in compliance with the License.
: You may obtain a copy of the License at
:
: http://www.apache.org/licenses/LICENSE-2.0
:
: Unless required by applicable law or agreed to in writing, software
: distributed under the License is distributed on an "AS IS" BASIS,
: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
: See the License for the specific language governing permissions and
: limitations under the License.
:
: The use of the Apache License does not indicate that this project is
: affiliated with the Apache Software Foundation.
:
:)
import module namespace trb="com.blakeley.task-rebalancer"
at "lib-trb.xqy" ;
declare namespace fs="http://marklogic.com/xdmp/status/forest";
declare namespace hs="http://marklogic.com/xdmp/status/host" ;
declare namespace ss="http://marklogic.com/xdmp/status/server" ;
declare variable $LIMIT as xs:integer external ;
declare variable $MODULE as xs:string external ;
declare variable $RESPAWN as xs:boolean external ;
declare variable $FORESTS-MAP := trb:forests-map() ;
(: Make sure uri lexicon is enabled. :)
cts:uris((), 'limit=0'),
(: NB - cannot check trb:maybe-fatal,
: because the state is only set on the task server
:)
(: Make sure we have at least one task server thread per local forest.
: This prevents forest-uris respawning from deadlocking the task server.
:)
let $host := xdmp:host()
let $tid := xdmp:host-status($host)/hs:task-server/hs:task-server-id
let $threads := xdmp:server-status($host, $tid)/ss:max-threads/data(.)
let $assert := (
if (not($RESPAWN)) then ()
else if (count(map:keys($FORESTS-MAP)) lt $threads) then ()
else error(
(), 'TRB-TOOFEWTHREADS',
text {
'to avoid deadlocks,',
'configure the task server with at least',
1 + count(map:keys($FORESTS-MAP)), 'threads' }))
let $assert := (
if (map:count($FORESTS-MAP) gt 0) then ()
else error((), 'TRB-NOFORESTS', 'No forests to rebalance'))
(: Check the forest count for the whole database, not just this host. :)
let $assert := (
if (xdmp:database-forests(xdmp:database())[2]) then ()
else error(
(), 'TRB-TOOFEWFORESTS',
('Not enough forests to rebalance',
count(xdmp:database-forests(xdmp:database())))))
(: Clear any state if respawn is set.
: If respawn is not set, this may be a scheduled task.
:)
let $_ := xdmp:log(text { '[forests.xqy] respawn', $RESPAWN }, 'debug')
let $_ := (
if (not($RESPAWN)) then () else (
for $key in map:keys($FORESTS-MAP)
return xdmp:spawn(
'uris-start-unset.xqy',
(xs:QName('FOREST'), xdmp:forest-name(xs:unsignedLong($key))))))
(: Give larger forests a head start :)
let $keys := (
for $key in map:keys($FORESTS-MAP)
let $estimate := map:get($FORESTS-MAP, $key)
order by $estimate descending
return $key)
for $key in $keys
let $fid := xs:unsignedLong($key)
return (
(: Allow ramp-up time, 1-ms per 2000 docs but at least 2-sec.
: Do this before spawning, so the uris-start-unset has a chance.
: NB - with default time limit, this will time out around 1B docs.
: If this happens, raise the time limit.
:)
xdmp:sleep(max((2000, map:get($FORESTS-MAP, $key) idiv 2000))),
xdmp:forest-name($fid),
xdmp:spawn(
$MODULE,
(xs:QName('FOREST'), $fid,
xs:QName('LIMIT'), $LIMIT,
xs:QName('RESPAWN'), $RESPAWN),
<options xmlns="xdmp:eval"><time-limit>3600</time-limit></options>),
xdmp:log(text { '[forests.xqy] spawned forest', xdmp:forest-name($fid) }))
(: forests.xqy :)