-
Notifications
You must be signed in to change notification settings - Fork 509
/
Supervisor.scala
297 lines (264 loc) · 10.4 KB
/
Supervisor.scala
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Copyright 2020-2022 Typelevel
*
* 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.
*/
package cats.effect.std
import cats.effect.kernel._
import cats.effect.kernel.implicits._
import cats.syntax.all._
import scala.collection.mutable.ListBuffer
import java.util.concurrent.ConcurrentHashMap
/**
* A fiber-based supervisor that monitors the lifecycle of all fibers that are started via its
* interface. The supervisor is managed by a singular fiber to which the lifecycles of all
* spawned fibers are bound.
*
* Whereas [[cats.effect.kernel.GenSpawn.background]] links the lifecycle of the spawned fiber
* to the calling fiber, starting a fiber via a [[Supervisor]] links the lifecycle of the
* spawned fiber to the supervisor fiber. This is useful when the scope of some fiber must
* survive the spawner, but should still be confined within some "larger" scope.
*
* The fibers started via the supervisor are guaranteed to be terminated when the supervisor
* fiber is terminated. When a supervisor fiber is canceled, all active and queued fibers will
* be safely finalized before finalization of the supervisor is complete.
*
* The following diagrams illustrate the lifecycle of a fiber spawned via
* [[cats.effect.kernel.GenSpawn.start]], [[cats.effect.kernel.GenSpawn.background]], and
* [[Supervisor]]. In each example, some fiber A is spawning another fiber B. Each box
* represents the lifecycle of a fiber. If a box is enclosed within another box, it means that
* the lifecycle of the former is confined within the lifecycle of the latter. In other words,
* if an outer fiber terminates, the inner fibers are guaranteed to be terminated as well.
*
* start:
* {{{
* Fiber A lifecycle
* +---------------------+
* | | |
* +-----------------|---+
* |
* |A starts B
* Fiber B lifecycle |
* +-----------------|---+
* | + |
* +---------------------+
* }}}
*
* background:
* {{{
* Fiber A lifecycle
* +------------------------+
* | | |
* | Fiber B lifecycle |A starts B
* | +------------------|-+ |
* | | | | |
* | +--------------------+ |
* +------------------------+
* }}}
*
* Supervisor:
* {{{
* Supervisor lifecycle
* +---------------------+
* | Fiber B lifecycle |
* | +-----------------+ |
* | | + | |
* | +---------------|-+ |
* +-----------------|---+
* |
* | A starts B
* Fiber A lifecycle |
* +-----------------|---+
* | | |
* +---------------------+
* }}}
*
* [[Supervisor]] should be used when fire-and-forget semantics are desired.
*/
trait Supervisor[F[_]] {
/**
* Starts the supplied effect `fa` on the supervisor.
*
* @return
* a [[cats.effect.kernel.Fiber]] that represents a handle to the started fiber.
*/
def supervise[A](fa: F[A]): F[Fiber[F, Throwable, A]]
}
object Supervisor {
/**
* Creates a [[cats.effect.kernel.Resource]] scope within which fibers can be monitored. When
* this scope exits, all supervised fibers will be finalized.
*
* @note
* if an effect that never completes, is supervised by a `Supervisor` with awaiting
* termination policy, the termination of the `Supervisor` is indefinitely suspended
* {{{
* val io: IO[Unit] = // never completes
* Supervisor[IO](await = true).use { supervisor =>
* supervisor.supervise(IO.never).void
* }
* }}}
*
* @param await
* the termination policy
* - true - wait for the completion of the active fibers
* - false - cancel the active fibers
*/
def apply[F[_]](await: Boolean)(implicit F: Concurrent[F]): Resource[F, Supervisor[F]] =
apply[F](await, None)(F)
private[std] def apply[F[_]](
await: Boolean,
checkRestart: Option[Outcome[F, Throwable, _] => Boolean] = None)(
implicit F: Concurrent[F]): Resource[F, Supervisor[F]] = {
F match {
case asyncF: Async[F] => applyForAsync(await, checkRestart)(asyncF)
case _ => applyForConcurrent(await, checkRestart)
}
}
def apply[F[_]: Concurrent]: Resource[F, Supervisor[F]] =
apply[F](
false,
None
) // TODO we have to do this for now because Scala 3 doesn't like it (lampepfl/dotty#15546)
private trait State[F[_]] {
def remove(token: Unique.Token): F[Unit]
def add(token: Unique.Token, fiber: Fiber[F, Throwable, _]): F[Unit]
// run all the finalizers
val joinAll: F[Unit]
val cancelAll: F[Unit]
}
private def supervisor[F[_]](
mkState: F[State[F]],
await: Boolean,
checkRestart: Option[Outcome[F, Throwable, _] => Boolean])(
implicit F: Concurrent[F]): Resource[F, Supervisor[F]] = {
// It would have preferable to use Scope here but explicit cancelation is
// intertwined with resource management
for {
doneR <- Resource.eval(F.ref(false))
state <- Resource.makeCase(mkState) {
case (st, Resource.ExitCase.Succeeded) if await => doneR.set(true) >> st.joinAll
case (st, _) =>
doneR.set(true) >> { /*println("canceling all!");*/
st.cancelAll
}
}
} yield new Supervisor[F] {
def supervise[A](fa: F[A]): F[Fiber[F, Throwable, A]] =
F.uncancelable { _ =>
val monitor: (F[A], F[Unit]) => F[Fiber[F, Throwable, A]] = checkRestart match {
case Some(restart) => { (fa, fin) =>
F.deferred[Outcome[F, Throwable, A]] flatMap { resultR =>
F.ref(false) flatMap { canceledR =>
F.deferred[Ref[F, Fiber[F, Throwable, A]]] flatMap { currentR =>
lazy val action: F[Unit] = F uncancelable { _ =>
val started = F start {
fa guaranteeCase { oc =>
canceledR.get flatMap { canceled =>
doneR.get flatMap { done =>
if (!canceled && !done && restart(oc))
action.void
else
fin.guarantee(resultR.complete(oc).void)
}
}
}
}
started flatMap { f =>
lazy val loop: F[Unit] = currentR.tryGet flatMap {
case Some(inner) =>
inner.set(f)
case None =>
F.ref(f)
.flatMap(inner => currentR.complete(inner).ifM(F.unit, loop))
}
loop
}
}
action map { _ =>
new Fiber[F, Throwable, A] {
private[this] val delegateF = currentR.get.flatMap(_.get)
val cancel: F[Unit] = F uncancelable { _ =>
canceledR.set(true) >> delegateF flatMap { fiber =>
fiber.cancel >> fiber.join flatMap {
case Outcome.Canceled() =>
resultR.complete(Outcome.Canceled()).void
case _ =>
resultR.tryGet.map(_.isDefined).ifM(F.unit, cancel)
}
}
}
val join = resultR.get
}
}
}
}
}
}
case None => (fa, fin) => F.start(fa.guarantee(fin))
}
for {
done <- F.ref(false)
token <- F.unique
cleanup = state.remove(token)
fiber <- monitor(fa, done.set(true) >> cleanup)
_ <- state.add(token, fiber)
_ <- done.get.ifM(cleanup, F.unit)
} yield fiber
}
}
}
private[effect] def applyForConcurrent[F[_]](
await: Boolean,
checkRestart: Option[Outcome[F, Throwable, _] => Boolean])(
implicit F: Concurrent[F]): Resource[F, Supervisor[F]] = {
val mkState = F.ref[Map[Unique.Token, Fiber[F, Throwable, _]]](Map.empty).map { stateRef =>
new State[F] {
def remove(token: Unique.Token): F[Unit] = stateRef.update(_ - token)
def add(token: Unique.Token, fiber: Fiber[F, Throwable, _]): F[Unit] =
stateRef.update(_ + (token -> fiber))
private[this] val allFibers: F[List[Fiber[F, Throwable, _]]] =
stateRef.get.map(_.values.toList)
val joinAll: F[Unit] = allFibers.flatMap(_.traverse_(_.join.void))
val cancelAll: F[Unit] = allFibers.flatMap(_.parUnorderedTraverse(_.cancel).void)
}
}
supervisor(mkState, await, checkRestart)
}
private[effect] def applyForAsync[F[_]](
await: Boolean,
checkRestart: Option[Outcome[F, Throwable, _] => Boolean])(
implicit F: Async[F]): Resource[F, Supervisor[F]] = {
val mkState = F.delay {
val state = new ConcurrentHashMap[Unique.Token, Fiber[F, Throwable, _]]
new State[F] {
def remove(token: Unique.Token): F[Unit] = F.delay(state.remove(token)).void
def add(token: Unique.Token, fiber: Fiber[F, Throwable, _]): F[Unit] =
F.delay(state.put(token, fiber)).void
private[this] val allFibers: F[List[Fiber[F, Throwable, _]]] =
F delay {
val fibersToCancel = ListBuffer.empty[Fiber[F, Throwable, _]]
fibersToCancel.sizeHint(state.size())
val values = state.values().iterator()
while (values.hasNext) {
fibersToCancel += values.next()
}
fibersToCancel.result()
}
val joinAll: F[Unit] = allFibers.flatMap(_.traverse_(_.join.void))
val cancelAll: F[Unit] = allFibers.flatMap(_.parUnorderedTraverse(_.cancel).void)
}
}
supervisor(mkState, await, checkRestart)
}
}