-
Notifications
You must be signed in to change notification settings - Fork 3
/
Planner.scala
407 lines (350 loc) · 14.4 KB
/
Planner.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
* Copyright 2023 Valdemar Grange
*
* 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 gql
import cats.implicits._
import cats.data._
import gql.PreparedQuery._
import scala.collection.immutable.TreeSet
import cats._
import scala.io.AnsiColor
import cats.mtl.Stateful
import gql.resolver.Step
import scala.collection.immutable.SortedMap
trait Planner[F[_]] { self =>
def plan(naive: Planner.NodeTree): F[Planner.NodeTree]
def mapK[G[_]](fk: F ~> G): Planner[G] =
new Planner[G] {
def plan(naive: Planner.NodeTree): G[Planner.NodeTree] = fk(self.plan(naive))
}
}
object Planner {
final case class BatchRef[K, V](
batcherId: gql.resolver.Step.BatchKey[K, V],
uniqueNodeId: PreparedQuery.UniqueBatchInstance[K, V]
)
final case class NodeId(id: Int) extends AnyVal
object NodeId {
implicit val ord: Order[NodeId] = Order.by[NodeId, Int](_.id)
}
final case class Node(
id: NodeId,
name: String,
//end: Double,
cost: Double,
elemCost: Double,
//children: List[Node],
parents: Set[NodeId],
batchId: Option[BatchRef[?, ?]]
) //{
//lazy val start = end - cost
//}
final case class TraversalState(
id: Int,
parents: Set[NodeId],
nodes: Chain[Node]
)
def getId[F[_]: Applicative](implicit S: Stateful[F, TraversalState]): F[NodeId] =
S.inspect(x => NodeId(x.id)) <* S.modify(s => s.copy(id = s.id + 1))
def setParents[F[_]](parents: Set[NodeId])(implicit S: Stateful[F, TraversalState]): F[Unit] =
S.modify(s => s.copy(parents = parents))
def addNodes[F[_]](nodes: Chain[Node])(implicit S: Stateful[F, TraversalState]): F[Unit] =
S.modify(s => s.copy(nodes = s.nodes ++ nodes))
def addNode[F[_]](node: Node)(implicit S: Stateful[F, TraversalState]): F[Unit] =
S.modify(s => s.copy(nodes = s.nodes :+ node))
def costForStep[F[_], G[_]](step: PreparedStep[G, ?, ?])(implicit
stats: Statistics[F],
F: Monad[F],
S: Stateful[F, TraversalState]
): F[Unit] = {
def goParallel(l: PreparedStep[G, ?, ?], r: PreparedStep[G, ?, ?]): F[Unit] = {
// A parallel op is disjunctive so the parent must be the same for both branches
// This creates a diamond shape in the graph
// Parent -> Left -> Child
// Parent -> Right -> Child
S.inspect(_.parents).flatMap { ps =>
val setParents = S.modify(s => s.copy(parents = ps))
val left = setParents *> costForStep(l) *> S.inspect(_.parents)
val right = setParents *> costForStep(r) *> S.inspect(_.parents)
(left, right).flatMapN { case (lp, rp) =>
val parents = lp ++ rp
S.modify(s => s.copy(parents = parents))
}
}
}
import PreparedStep._
step match {
case Lift(_) | EmbedError() | GetMeta(_) => F.unit
case Compose(l, r) => costForStep[F, G](l) *> costForStep[F, G](r)
case alg: Choose[G, ?, ?, ?, ?] => goParallel(alg.fac, alg.fbc)
case alg: First[G, ?, ?, ?] => costForStep[F, G](alg.step)
case Batch(_, _) | EmbedEffect(_) | EmbedStream(_, _) =>
val name = step match {
case Batch(id, _) => s"batch_$id"
case EmbedEffect(cursor) => cursor.asString
case EmbedStream(_, cursor) => cursor.asString
case _ => ???
}
val costF = stats
.getStatsOpt(name)
.map(_.getOrElse(Statistics.Stats(100d, 5d)))
costF.flatMap { cost =>
getId[F].flatMap { id =>
S.inspect(_.parents).flatMap { parentIds =>
addNode {
Node(
id,
name,
cost.initialCost,
cost.extraElementCost,
parentIds,
step match {
case Batch(batcherId, uniqueNodeId) => Some(BatchRef(batcherId, uniqueNodeId))
case _ => None
}
)
}
}
}
}
}
}
def costForFields[F[_], G[_]](prepared: NonEmptyList[PreparedQuery.PreparedField[G, ?]])(implicit
F: Monad[F],
stats: Statistics[F],
S: Stateful[F, TraversalState]
): F[Unit] = {
prepared.toList.traverse_ { pf =>
pf match {
case PreparedDataField(_, _, cont) => costForCont[F, G](cont.edges, cont.cont)
case PreparedSpecification(_, _, selection) => costForFields[F, G](selection)
}
}
}
def costForPrepared[F[_]: Statistics, G[_]](p: Prepared[G, ?])(implicit
F: Monad[F],
S: Stateful[F, TraversalState]
): F[Unit] =
p match {
case PreparedLeaf(_, _) => F.unit
case Selection(fields) => costForFields[F, G](fields)
case l: PreparedList[G, ?, ?, ?] => costForCont[F, G](l.of.edges, l.of.cont)
case o: PreparedOption[G, ?, ?] => costForCont[F, G](o.of.edges, o.of.cont)
}
def costForCont[F[_]: Statistics: Monad, G[_]](
edges: PreparedStep[G, ?, ?],
cont: Prepared[G, ?]
)(implicit S: Stateful[F, TraversalState]): F[Unit] =
costForStep[F, G](edges) *> costForPrepared[F, G](cont)
type H[F[_], A] = StateT[F, TraversalState, A]
def liftStatistics[F[_]: Applicative](stats: Statistics[F]): Statistics[H[F, *]] =
stats.mapK(StateT.liftK[F, TraversalState])
def runCostAnalysisFor[F[_]: Monad, A](f: Statistics[H[F, *]] => H[F, A])(implicit stats: Statistics[F]): F[A] =
f(liftStatistics[F](stats)).runA(TraversalState(1, Set.empty, Chain.empty))
def runCostAnalysis[F[_]: Monad: Statistics, A](f: Statistics[H[F, *]] => H[F, A]): F[NodeTree] =
runCostAnalysisFor[F, List[Node]](s => f(s).get.map(_.nodes.toList)).map(NodeTree.simple(_))
def apply[F[_]](implicit F: Applicative[F]) = new Planner[F] {
def plan(tree: NodeTree): F[NodeTree] = {
tree.roots.toNel match {
case None => F.pure(tree.copy(source = Some(tree)))
case Some(_) =>
val baseEnds = tree.endTimes
val childrenV = tree.childrenLookup
val lookupV = tree.lookup
val maxEnd = baseEnds.values.maxOption.get
// Move as far down as we can
def moveDown(id: NodeId): State[EndTimes, Double] =
State.inspect((a: EndTimes) => a.get(id)).flatMap {
case Some(d) => State.pure(d)
case None =>
val children = childrenV.get(id).toList.flatMap(_.toList)
// Find the minimum start time of all children
// That is as far down as we can go
children
.traverse(n => moveDown(n).map(newEnd => newEnd - lookupV(n).cost))
.map(_.minOption.getOrElse(maxEnd))
.flatMap { newEnd =>
State.modify[EndTimes](_ + (id -> newEnd)) as newEnd
}
}
val movedDown = tree.roots.map(_.id).traverse_(moveDown).runS(Map.empty).value
// Run though orderd by end time (smallest first)
// Then move up to furthest batchable neighbour
// If no such neighbour, move up as far as possible
def moveUp(ns: List[NodeId]): Map[NodeId, Double] =
ns.mapAccumulate(
(
// Map of node id to NEW end time
Map.empty[NodeId, Double],
// When a node has been visited and is batchable, it is added here
Map.empty[Step.BatchKey[?, ?], TreeSet[Double]]
)
) { case ((ends, batchMap), id) =>
val n = lookupV(id)
// Our min end is the maximum of all parents + our cost
val minEnd = n.parents.map(ends(_)).maxOption.getOrElse(0d) + n.cost
val (newEnd, newMap) = n.batchId match {
// No batching here, move up as far as possible
case None => (minEnd, batchMap)
case Some(bn) =>
batchMap.get(bn.batcherId) match {
case None => (minEnd, batchMap + (bn.batcherId -> TreeSet(minEnd)))
case Some(s) =>
// This is a batch possibility
val o = if (s.contains(minEnd)) Some(minEnd) else s.minAfter(minEnd)
// If a batch is found, then we can move up to the batch and don't need to modify the set
// If no batch is found, then we move up to the minimum end and add this to the set
o match {
case None => (minEnd, batchMap + (bn.batcherId -> (s + minEnd)))
case Some(x) => (x, batchMap)
}
}
}
val newEnds = ends + (id -> newEnd)
((newEnds, newMap), (n.id, newEnd))
}._2
.toMap
val ordered = movedDown.toList.sortBy { case (_, d) => d }.map { case (id, _) => id }
val movedUp = moveUp(ordered)
F.pure(tree.set(movedUp))
}
}
}
type EndTimes = Map[NodeId, Double]
final case class NodeTree(
all: List[Node],
endTimes: EndTimes,
source: Option[NodeTree] = None
) {
def set(endTimes: EndTimes): NodeTree =
NodeTree(all, endTimes, Some(this))
lazy val lookup: Map[NodeId, Node] =
all.map(n => n.id -> n).toMap
lazy val childrenLookup: SortedMap[NodeId, NonEmptyChain[NodeId]] =
all
.flatMap(n => n.parents.map(p => p -> n.id))
.groupByNec { case (p, _) => p }
.fmap(_.map { case (_, c) => c })
lazy val roots = all.filter(_.parents.isEmpty)
// If a node has children, it is not a leaf
lazy val leaves = {
val childrenV = childrenLookup
all.filter(n => !childrenV.contains(n.id))
}
lazy val batches: List[(Step.BatchKey[?, ?], NonEmptyChain[BatchRef[?, ?]])] = {
val endTimesV = endTimes
all
.map(n => (n.batchId, n))
.collect { case (Some(batcherKey), node) => (batcherKey, node) }
.groupByNec { case (_, node) => endTimesV(node.id) }
.toList
.flatMap { case (_, endGroup) =>
endGroup.toList
.groupBy { case (batcherKey, _) => batcherKey.batcherId }
.map { case (batcherKey, batch) => batcherKey -> NonEmptyChain.fromSeq(batch.map { case (br, _) => br }) }
.collect { case (k, Some(vs)) => k -> vs }
}
}
lazy val totalCost: Double = {
val thisFlat = all
val thisBatches = batches.filter { case (_, edges) => edges.size > 1 }
val batchCostMap: Map[Step.BatchKey[?, ?], Double] =
thisFlat.mapFilter(n => n.batchId.map(br => br.batcherId -> n.cost)).toMap
val naiveCost = thisFlat.map(_.cost).sum
val batchSubtraction = thisBatches.map { case (_, xs) =>
// cost * (size - 1 )
batchCostMap(xs.head.batcherId) * (xs.size - 1)
}.sum
naiveCost - batchSubtraction
}
def show(showImprovement: Boolean = false, ansiColors: Boolean = false) = {
val (default, displaced) =
if (showImprovement)
source match {
case Some(x) => (x, Some(this))
case None => (this, None)
}
else (this, None)
val nt = displaced.getOrElse(default)
val maxEnd = nt.all.map(n => nt.endTimes(n.id)).maxOption.getOrElse(0d)
val (red, green, blue, reset) =
if (ansiColors) (AnsiColor.RED_B, AnsiColor.GREEN_B, AnsiColor.BLUE_B, AnsiColor.RESET)
else ("", "", "", "")
val prefix =
if (ansiColors)
displaced.as {
s"""|
|${red}old field schedule$reset
|${green}new field offset (deferral of execution)$reset
|""".stripMargin
}.mkString
else ""
val per = math.max((maxEnd / 40d), 1)
def go(nodes: List[NodeId]): String = {
nodes
.sortBy(_.id)
.map { n0 =>
val n = lookup(n0)
val nEnd = endTimes(n.id)
val nStart = nEnd - n.cost
val disp = nt.lookup.get(n.id)
val basePrefix = " " * (nStart / per).toInt
val showDisp = disp
.filter(d => nt.endTimes(d.id) != nEnd.toInt)
.map { d =>
val dEnd = nt.endTimes(d.id)
val dStart = dEnd - d.cost
val pushedPrefix = blue + ">" * ((dStart - nStart) / per).toInt + green
s"$basePrefix${pushedPrefix}name: ${d.name}, cost: ${d.cost}, end: ${dEnd}$reset"
}
val showHere =
s"$basePrefix${if (showDisp.isDefined) red else ""}name: ${n.name}, cost: ${n.cost}, end: ${nEnd}$reset"
val all = showHere + showDisp.map("\n" + _).mkString
val children = go(childrenLookup.get(n.id).map(_.toList).getOrElse(Nil))
all + "\n" + children
}
.mkString("")
}
prefix + go(default.roots.map(_.id))
}
}
object NodeTree {
def computeNewEndTimes(nt: NodeTree) = {
// lazy val optimization
val lookupV = nt.lookup
// we start from the leaves since that fixes the diamond problem
def getEndTime(n: Node): State[EndTimes, Double] =
State.inspect((a: EndTimes) => a.get(n.id)).flatMap {
case Some(d) => State.pure(d)
case None =>
// find the max end time of all parents and add the cost
n.parents.toList
.traverse(p => getEndTime(lookupV(p)))
.map(_.maxOption.getOrElse(0d))
.flatMap { parentEnd =>
val end = parentEnd + n.cost
State.modify[EndTimes](_ + (n.id -> end)) as end
}
}
nt.leaves.traverse_(getEndTime).runS(Map.empty).value
}
def simple(nodes: List[Node]): NodeTree = {
val base = NodeTree(nodes, Map.empty)
val ends = computeNewEndTimes(base)
NodeTree(nodes, ends)
}
implicit lazy val showForNodeTree: Show[NodeTree] = Show.show[NodeTree](_.show(showImprovement = true))
}
}