Skip to content

Commit 264ca76

Browse files
authored
CA-404013: replace Thread.delay with Delay module (#6213)
Reporter.cancel would be blocked for a long time by backoff delay when another thread is waiting for next reading, replace Thread.delay with Delay module so that Reporter.cancel will not be blocked.
2 parents dc565fd + 5efa5d0 commit 264ca76

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

ocaml/xcp-rrdd/lib/plugin/reporter.ml

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
* GNU Lesser General Public License for more details.
1313
*)
1414

15+
module Delay = Xapi_stdext_threads.Threadext.Delay
16+
1517
let with_lock = Xapi_stdext_threads.Threadext.Mutex.execute
1618

1719
(* This exception is setup to be raised on sigint by Process.initialise,
@@ -57,10 +59,20 @@ type state =
5759
| Cancelled
5860
| Stopped of [`New | `Cancelled | `Failed of exn]
5961

60-
type t = {mutable state: state; lock: Mutex.t; condition: Condition.t}
62+
type t = {
63+
mutable state: state
64+
; lock: Mutex.t
65+
; condition: Condition.t
66+
; delay: Delay.t
67+
}
6168

6269
let make () =
63-
{state= Stopped `New; lock= Mutex.create (); condition= Condition.create ()}
70+
{
71+
state= Stopped `New
72+
; lock= Mutex.create ()
73+
; condition= Condition.create ()
74+
; delay= Delay.make ()
75+
}
6476

6577
let choose_protocol = function
6678
| Rrd_interface.V1 ->
@@ -69,13 +81,20 @@ let choose_protocol = function
6981
Rrd_protocol_v2.protocol
7082

7183
let wait_until_next_reading (module D : Debug.DEBUG) ~neg_shift ~uid ~protocol
72-
~overdue_count =
84+
~overdue_count ~reporter =
7385
let next_reading = RRDD.Plugin.Local.register uid Rrd.Five_Seconds protocol in
7486
let wait_time = next_reading -. neg_shift in
7587
let wait_time = if wait_time < 0.1 then wait_time +. 5. else wait_time in
7688
(* overdue count - 0 if there is no overdue; +1 if there is overdue *)
7789
if wait_time > 0. then (
78-
Thread.delay wait_time ; 0
90+
( match reporter with
91+
| Some reporter ->
92+
let (_ : bool) = Delay.wait reporter.delay wait_time in
93+
()
94+
| None ->
95+
Thread.delay wait_time
96+
) ;
97+
0
7998
) else (
8099
if overdue_count > 1 then (
81100
(* if register returns negative more than once in a succession,
@@ -84,7 +103,12 @@ let wait_until_next_reading (module D : Debug.DEBUG) ~neg_shift ~uid ~protocol
84103
D.debug
85104
"rrdd says next reading is overdue, seems like rrdd is busy;\n\
86105
\t\t\t\tBacking off for %.1f seconds" backoff_time ;
87-
Thread.delay backoff_time
106+
match reporter with
107+
| Some reporter ->
108+
let (_ : bool) = Delay.wait reporter.delay backoff_time in
109+
()
110+
| None ->
111+
Thread.delay backoff_time
88112
) else
89113
D.debug "rrdd says next reading is overdue by %.1f seconds; not sleeping"
90114
(-.wait_time) ;
@@ -147,8 +171,10 @@ let cancel ~reporter =
147171
match reporter.state with
148172
| Running ->
149173
reporter.state <- Cancelled ;
174+
Delay.signal reporter.delay ;
150175
Condition.wait reporter.condition reporter.lock
151176
| Cancelled ->
177+
Delay.signal reporter.delay ;
152178
Condition.wait reporter.condition reporter.lock
153179
| Stopped _ ->
154180
()

ocaml/xcp-rrdd/lib/plugin/reporter_local.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ let start_local (module D : Debug.DEBUG) ~reporter ~uid ~neg_shift ~page_count
2828
overdue_count :=
2929
wait_until_next_reading
3030
(module D)
31-
~neg_shift ~uid ~protocol ~overdue_count:!overdue_count ;
31+
~neg_shift ~uid ~protocol ~overdue_count:!overdue_count ~reporter ;
3232
if page_count > 0 then
3333
let payload =
3434
Rrd_protocol.{timestamp= Utils.now (); datasources= dss_f ()}

0 commit comments

Comments
 (0)