-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathJobVerificationEngine.scala
596 lines (546 loc) · 20.9 KB
/
JobVerificationEngine.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 edu.berkeley.cs.rise.opaque
import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.Map
import scala.collection.mutable.Set
import scala.collection.mutable.Queue
import org.apache.spark.sql.DataFrame
import org.apache.spark.sql.execution.SparkPlan
// Wraps Crumb data specific to graph vertices and provides graph methods.
// Represents a recursive ecall DAG node.
class JobNode(val inputMacs: ArrayBuffer[ArrayBuffer[Byte]] = ArrayBuffer[ArrayBuffer[Byte]](),
val numInputMacs: Int = 0,
val allOutputsMac: ArrayBuffer[Byte] = ArrayBuffer[Byte](),
var ecall: Int = 0) {
var outgoingNeighbors: ArrayBuffer[JobNode] = ArrayBuffer[JobNode]()
var logMacs: ArrayBuffer[ArrayBuffer[Byte]] = ArrayBuffer[ArrayBuffer[Byte]]()
var isSource: Boolean = false
var isSink: Boolean = false
def addOutgoingNeighbor(neighbor: JobNode) = {
this.outgoingNeighbors.append(neighbor)
}
def addLogMac(logMac: ArrayBuffer[Byte]) = {
this.logMacs.append(logMac)
}
def setEcall(ecall: Int) = {
this.ecall = ecall
}
def setSource() = {
this.isSource = true
}
def setSink() = {
this.isSink = true
}
// Compute and return a list of paths from this node to a sink node.
// Used in naive DAG comparison.
def pathsToSink(): ArrayBuffer[List[Seq[Int]]] = {
val retval = ArrayBuffer[List[Seq[Int]]]()
if (this.isSink) {
return retval
}
if (this.outgoingNeighbors.length == 0 && !this.isSink) {
throw new Exception("DAG is not well formed - non sink node has 0 outgoing neighbors.")
}
// This node is directly before the sink and has exactly one path to it
// (the edge from this node to the sink).
if (this.outgoingNeighbors.length == 1 && this.outgoingNeighbors(0).isSink) {
return ArrayBuffer(List(Seq(this.ecall, 0)))
}
// Each neighbor has a list of paths to the sink -
// For every path that exists, prepend the edge from this node to the neighbor.
// Return all paths collected from all neighbors.
for (neighbor <- this.outgoingNeighbors) {
val pred = Seq(this.ecall, neighbor.ecall)
val restPaths = neighbor.pathsToSink()
for (restPath <- restPaths) {
retval.append(pred +: restPath)
}
}
return retval
}
// Returns if this DAG is empty
def graphIsEmpty(): Boolean = {
return this.isSource && this.outgoingNeighbors.isEmpty
}
// Checks if JobNodeData originates from same partition (?)
override def equals(that: Any): Boolean = {
that match {
case that: JobNode => {
inputMacs == that.inputMacs &&
numInputMacs == that.numInputMacs &&
allOutputsMac == that.allOutputsMac &&
ecall == that.ecall
}
case _ => false
}
}
override def hashCode(): Int = {
inputMacs.hashCode ^ allOutputsMac.hashCode
}
}
// Used in construction of expected DAG.
// Represents a recursive Operator DAG node.
class OperatorNode(val operatorName: String = "") {
var children: ArrayBuffer[OperatorNode] = ArrayBuffer[OperatorNode]()
var parents: ArrayBuffer[OperatorNode] = ArrayBuffer[OperatorNode]()
// Contains numPartitions * numEcalls job nodes.
// numPartitions rows (outer array), numEcalls columns (inner array)
var jobNodes: ArrayBuffer[ArrayBuffer[JobNode]] = ArrayBuffer[ArrayBuffer[JobNode]]()
def addChild(child: OperatorNode) = {
this.children.append(child)
}
def setChildren(children: ArrayBuffer[OperatorNode]) = {
this.children = children
}
def addParent(parent: OperatorNode) = {
this.parents.append(parent)
}
def setParents(parents: ArrayBuffer[OperatorNode]) = {
this.parents = parents
}
def isOrphan(): Boolean = {
return this.parents.isEmpty
}
}
object JobVerificationEngine {
// An LogEntryChain object from each partition
var logEntryChains = ArrayBuffer[tuix.LogEntryChain]()
val ecallId = Map(
1 -> "project",
2 -> "filter",
3 -> "sample",
4 -> "findRangeBounds",
5 -> "partitionForSort",
6 -> "externalSort",
7 -> "scanCollectLastPrimary",
8 -> "nonObliviousSortMergeJoin",
9 -> "nonObliviousAggregate",
10 -> "countRowsPerPartition",
11 -> "computeNumRowsPerPartition",
12 -> "localLimit",
13 -> "limitReturnRows",
14 -> "broadcastNestedLoopJoin"
).withDefaultValue("unknown")
val possibleSparkOperators = Seq[String]("EncryptedProject",
"EncryptedSort",
"EncryptedSortMergeJoin",
"EncryptedFilter",
"EncryptedAggregate",
"EncryptedGlobalLimit",
"EncryptedLocalLimit",
"EncryptedBroadcastNestedLoopJoin")
def addLogEntryChain(logEntryChain: tuix.LogEntryChain): Unit = {
logEntryChains += logEntryChain
}
def resetForNextJob(): Unit = {
logEntryChains.clear
}
/********************************
Graph construction helper methods
********************************/
// Check if operator node is supported by Job Verification Engine.
// Should be in `possibleSparkOperators` list.
def isValidOperatorNode(node: OperatorNode): Boolean = {
for (targetSubstring <- possibleSparkOperators) {
if (node.operatorName contains targetSubstring) {
return true
}
}
return false
}
// Compares paths returned from pathsToSink Job Node method.
// Used in naive DAG comparison.
def pathsEqual(executedPaths: ArrayBuffer[List[Seq[Int]]],
expectedPaths: ArrayBuffer[List[Seq[Int]]]): Boolean = {
// Executed paths might contain extraneous paths from
// MACs matching across ecalls if a block is unchanged from ecall to ecall (?)
return expectedPaths.toSet.subsetOf(executedPaths.toSet)
}
// operatorDAGFromPlan helper - recursively convert SparkPlan objects to OperatorNode object.
def sparkNodesToOperatorNodes(plan: SparkPlan): OperatorNode = {
var operatorName = ""
val firstLine = plan.toString.split("\n")(0)
for (sparkOperator <- possibleSparkOperators) {
if (firstLine contains sparkOperator) {
operatorName = sparkOperator
}
}
val operatorNode = new OperatorNode(operatorName)
for (child <- plan.children) {
val parentOperatorNode = sparkNodesToOperatorNodes(child)
operatorNode.addParent(parentOperatorNode)
}
return operatorNode
}
// Returns true if every OperatorNode in this list is "valid", or supported by JobVerificationEngine.
def allValidOperators(operators: ArrayBuffer[OperatorNode]): Boolean = {
for (operator <- operators) {
if (!isValidOperatorNode(operator)) {
return false
}
}
return true
}
// operatorDAGFromPlan helper - recursively prunes non valid nodes from an OperatorNode tree, bottom up.
def fixOperatorTree(root: OperatorNode): Unit = {
if (root.isOrphan) {
return
}
while (!allValidOperators(root.parents)) {
val newParents = new ArrayBuffer[OperatorNode]()
for (parent <- root.parents) {
if (isValidOperatorNode(parent)) {
newParents.append(parent)
} else {
for (grandparent <- parent.parents) {
newParents.append(grandparent)
}
}
}
root.setParents(newParents)
}
for (parent <- root.parents) {
fixOperatorTree(parent)
}
}
// Given operators with correctly set parents, correctly set the children pointers.
def setChildrenDag(operators: ArrayBuffer[OperatorNode]): Unit = {
for (operator <- operators) {
operator.setChildren(ArrayBuffer[OperatorNode]())
}
for (operator <- operators) {
for (parent <- operator.parents) {
parent.addChild(operator)
}
}
}
// Uses BFS to put all nodes in an OperatorNode tree into a list.
def treeToList(root: OperatorNode): ArrayBuffer[OperatorNode] = {
val retval = ArrayBuffer[OperatorNode]()
val queue = new Queue[OperatorNode]()
val visited = Set[OperatorNode]()
queue.enqueue(root)
while (!queue.isEmpty) {
val curr = queue.dequeue
if (!visited.contains(curr)) {
visited.add(curr)
retval.append(curr)
for (parent <- curr.parents) {
queue.enqueue(parent)
}
}
}
return retval
}
// Converts a SparkPlan into a DAG of OperatorNode objects.
// Returns a list of all the nodes in the DAG.
def operatorDAGFromPlan(executedPlan: SparkPlan): ArrayBuffer[OperatorNode] = {
// Convert SparkPlan tree to OperatorNode tree
val leafOperatorNode = sparkNodesToOperatorNodes(executedPlan)
// Enlist the tree
val allOperatorNodes = treeToList(leafOperatorNode)
// Attach a sink to the tree and prune invalid OperatorNodes starting from the sink.
val sinkNode = new OperatorNode("sink")
for (operatorNode <- allOperatorNodes) {
if (operatorNode.children.isEmpty) {
operatorNode.addChild(sinkNode)
sinkNode.addParent(operatorNode)
}
}
fixOperatorTree(sinkNode)
// Enlist the fixed tree.
val fixedOperatorNodes = treeToList(sinkNode)
fixedOperatorNodes -= sinkNode
for (sinkParents <- sinkNode.parents) {
sinkParents.setChildren(ArrayBuffer[OperatorNode]())
}
setChildrenDag(fixedOperatorNodes)
return fixedOperatorNodes
}
// expectedDAGFromOperatorDAG helper - links parent ecall partitions to child ecall partitions.
def linkEcalls(parentEcalls: ArrayBuffer[JobNode], childEcalls: ArrayBuffer[JobNode]): Unit = {
if (parentEcalls.length != childEcalls.length) {
println("Ecall lengths don't match! (linkEcalls)")
}
val numPartitions = parentEcalls.length
val ecall = parentEcalls(0).ecall
// println("Linking ecall " + ecall + " to ecall " + childEcalls(0).ecall)
// project
if (ecall == 1) {
for (i <- 0 until numPartitions) {
parentEcalls(i).addOutgoingNeighbor(childEcalls(i))
}
// filter
} else if (ecall == 2) {
for (i <- 0 until numPartitions) {
parentEcalls(i).addOutgoingNeighbor(childEcalls(i))
}
// externalSort
} else if (ecall == 6) {
for (i <- 0 until numPartitions) {
parentEcalls(i).addOutgoingNeighbor(childEcalls(i))
}
// sample
} else if (ecall == 3) {
for (i <- 0 until numPartitions) {
parentEcalls(i).addOutgoingNeighbor(childEcalls(0))
}
// findRangeBounds
} else if (ecall == 4) {
for (i <- 0 until numPartitions) {
parentEcalls(0).addOutgoingNeighbor(childEcalls(i))
}
// partitionForSort
} else if (ecall == 5) {
// All to all shuffle
for (i <- 0 until numPartitions) {
for (j <- 0 until numPartitions) {
parentEcalls(i).addOutgoingNeighbor(childEcalls(j))
}
}
// nonObliviousAggregate
} else if (ecall == 9) {
for (i <- 0 until numPartitions) {
parentEcalls(i).addOutgoingNeighbor(childEcalls(0))
}
// nonObliviousSortMergeJoin
} else if (ecall == 8) {
for (i <- 0 until numPartitions) {
parentEcalls(i).addOutgoingNeighbor(childEcalls(i))
}
// countRowsPerPartition
} else if (ecall == 10) {
// Send from all partitions to partition 0
for (i <- 0 until numPartitions) {
parentEcalls(i).addOutgoingNeighbor(childEcalls(0))
}
// computeNumRowsPerPartition
} else if (ecall == 11) {
// Broadcast from one partition (assumed to be partition 0) to all partitions
for (i <- 0 until numPartitions) {
parentEcalls(0).addOutgoingNeighbor(childEcalls(i))
}
// limitReturnRows
} else if (ecall == 13) {
for (i <- 0 until numPartitions) {
parentEcalls(i).addOutgoingNeighbor(childEcalls(i))
}
} else if (ecall == 14) {
for (i <- 0 until numPartitions) {
parentEcalls(i).addOutgoingNeighbor(childEcalls(i))
}
} else {
throw new Exception("Job Verification Error creating expected DAG: "
+ "ecall not supported - " + ecall)
}
}
// expectedDAGFromOperatorDAG helper - generates a matrix of job nodes for each operator node.
def generateJobNodes(numPartitions: Int, operatorName: String): ArrayBuffer[ArrayBuffer[JobNode]] = {
val jobNodes = ArrayBuffer[ArrayBuffer[JobNode]]()
val expectedEcalls = ArrayBuffer[Int]()
// println("generating job nodes for " + operatorName + " with " + numPartitions + " partitions.")
if (operatorName == "EncryptedSort" && numPartitions == 1) {
// ("externalSort")
expectedEcalls.append(6)
} else if (operatorName == "EncryptedSort" && numPartitions > 1) {
// ("sample", "findRangeBounds", "partitionForSort", "externalSort")
expectedEcalls.append(3, 4, 5, 6)
} else if (operatorName == "EncryptedProject") {
// ("project")
expectedEcalls.append(1)
} else if (operatorName == "EncryptedFilter") {
// ("filter")
expectedEcalls.append(2)
} else if (operatorName == "EncryptedAggregate") {
// ("nonObliviousAggregate")
expectedEcalls.append(9)
} else if (operatorName == "EncryptedSortMergeJoin") {
// ("nonObliviousSortMergeJoin")
expectedEcalls.append(8)
} else if (operatorName == "EncryptedLocalLimit") {
// ("limitReturnRows")
expectedEcalls.append(13)
} else if (operatorName == "EncryptedGlobalLimit") {
// ("countRowsPerPartition", "computeNumRowsPerPartition", "limitReturnRows")
expectedEcalls.append(10, 11, 13)
} else if (operatorName == "EncryptedBroadcastNestedLoopJoin") {
// ("broadcastNestedLoopJoin")
expectedEcalls.append(14)
} else {
throw new Exception("Executed unknown operator: " + operatorName)
}
// println("Expected ecalls for " + operatorName + ": " + expectedEcalls)
for (ecallIdx <- 0 until expectedEcalls.length) {
val ecall = expectedEcalls(ecallIdx)
val ecallJobNodes = ArrayBuffer[JobNode]()
jobNodes.append(ecallJobNodes)
// println("Creating job nodes for ecall " + ecall)
for (partitionIdx <- 0 until numPartitions) {
val jobNode = new JobNode()
jobNode.setEcall(ecall)
ecallJobNodes.append(jobNode)
}
}
return jobNodes
}
// expectedDAGFromPlan helper - converts a DAG of Spark operators to a DAG of ecalls and partitions.
def expectedDAGFromOperatorDAG(operatorNodes: ArrayBuffer[OperatorNode]): JobNode = {
val source = new JobNode()
val sink = new JobNode()
source.setSource
sink.setSink
// For each node, create numPartitions * numEcalls jobnodes.
for (node <- operatorNodes) {
node.jobNodes = generateJobNodes(logEntryChains.size, node.operatorName)
}
// println("Job node generation finished.")
// Link all ecalls.
for (node <- operatorNodes) {
// println("Linking ecalls for operator " + node.operatorName + " with num ecalls = " + node.jobNodes.length)
for (ecallIdx <- 0 until node.jobNodes.length) {
if (ecallIdx == node.jobNodes.length - 1) {
// last ecall of this operator, link to child operators if one exists.
for (child <- node.children) {
linkEcalls(node.jobNodes(ecallIdx), child.jobNodes(0))
}
} else {
linkEcalls(node.jobNodes(ecallIdx), node.jobNodes(ecallIdx + 1))
}
}
}
// Set source and sink
for (node <- operatorNodes) {
if (node.isOrphan) {
for (jobNode <- node.jobNodes(0)) {
source.addOutgoingNeighbor(jobNode)
}
}
if (node.children.isEmpty) {
for (jobNode <- node.jobNodes(node.jobNodes.length - 1)) {
jobNode.addOutgoingNeighbor(sink)
}
}
}
return source
}
// verify helper - generates an expected DAG of ecalls and partitions from a dataframe's SparkPlan object.
def expectedDAGFromPlan(executedPlan: SparkPlan): JobNode = {
val operatorDAGList = operatorDAGFromPlan(executedPlan)
expectedDAGFromOperatorDAG(operatorDAGList)
}
/***********************
Main verification method
***********************/
// Verify that the executed flow of information from ecall partition to ecall partition
// matches what is expected for a given Spark dataframe.
// This function should be the one called from the rest of the client to do job verification.
def verify(df: DataFrame): Boolean = {
// Get expected DAG.
val expectedSourceNode = expectedDAGFromPlan(df.queryExecution.executedPlan)
// Quit if graph is empty.
if (expectedSourceNode.graphIsEmpty) {
println("Expected graph empty")
return true
}
// Construct executed DAG.
val OE_HMAC_SIZE = 32
// Keep a set of nodes, since right now, the last nodes won't have outputs.
val nodeSet = Set[JobNode]()
// Set up map from allOutputsMAC --> JobNode.
val outputsMap = Map[ArrayBuffer[Byte], JobNode]()
for (logEntryChain <- logEntryChains) {
// Create job node for last ecall.
val logEntry = logEntryChain.currEntries(0)
val inputMacs = ArrayBuffer[ArrayBuffer[Byte]]()
val allOutputsMac = ArrayBuffer[Byte]()
// (TODO): add logMac and allOutputsMac to last crumb.
for (j <- 0 until logEntry.numInputMacs) {
inputMacs.append(ArrayBuffer[Byte]())
for (k <- 0 until OE_HMAC_SIZE) {
inputMacs(j).append(logEntry.inputMacs(j * OE_HMAC_SIZE + k).toByte)
}
}
val lastJobNode = new JobNode(inputMacs, logEntry.numInputMacs,
allOutputsMac, logEntry.ecall)
nodeSet.add(lastJobNode)
// Create job nodes for all ecalls before last for this partition.
for (i <- 0 until logEntryChain.pastEntriesLength) {
val pastEntry = logEntryChain.pastEntries(i)
// Copy byte buffers
val inputMacs = ArrayBuffer[ArrayBuffer[Byte]]()
val logMac = ArrayBuffer[Byte]()
val allOutputsMac = ArrayBuffer[Byte]()
for (j <- 0 until pastEntry.numInputMacs) {
inputMacs.append(ArrayBuffer[Byte]())
for (k <- 0 until OE_HMAC_SIZE) {
inputMacs(j).append(pastEntry.inputMacs(j * OE_HMAC_SIZE + k).toByte)
}
}
for (j <- 0 until pastEntry.logMacLength) {
logMac += pastEntry.logMac(i).toByte
}
for (j <- 0 until pastEntry.allOutputsMacLength) {
allOutputsMac += pastEntry.allOutputsMac(j).toByte
}
// Create or update job node.
if (!(outputsMap contains allOutputsMac)) {
outputsMap(allOutputsMac) = new JobNode(inputMacs, pastEntry.numInputMacs,
allOutputsMac, pastEntry.ecall)
}
val jobNode = outputsMap(allOutputsMac)
jobNode.addLogMac(logMac)
nodeSet.add(jobNode)
}
}
// For each node, check that allOutputsMac is computed correctly.
for (node <- nodeSet) {
// assert (node.allOutputsMac == mac(concat(node.logMacs)))
// Unclear what order to arrange log_macs to get the all_outputs_mac
// Doing numEcalls * (numPartitions!) arrangements seems very bad.
// See if we can do it more efficiently.
}
// Construct executed DAG by setting parent JobNodes for each node.
val executedSourceNode = new JobNode()
executedSourceNode.setSource
val executedSinkNode = new JobNode()
executedSinkNode.setSink
// Iterate through all nodes, matching `all_outputs_mac` to `input_macs`.
for (node <- nodeSet) {
if (node.inputMacs == ArrayBuffer[ArrayBuffer[Byte]]()) {
executedSourceNode.addOutgoingNeighbor(node)
} else {
for (i <- 0 until node.numInputMacs) {
val parentNode = outputsMap(node.inputMacs(i))
parentNode.addOutgoingNeighbor(node)
}
}
}
for (node <- nodeSet) {
if (node.outgoingNeighbors.length == 0) {
node.addOutgoingNeighbor(executedSinkNode)
}
}
val executedPathsToSink = executedSourceNode.pathsToSink
val expectedPathsToSink = expectedSourceNode.pathsToSink
val arePathsEqual = pathsEqual(executedPathsToSink, expectedPathsToSink)
if (!arePathsEqual) {
println("===========DAGS NOT EQUAL===========")
}
return true
}
}