-
Notifications
You must be signed in to change notification settings - Fork 91
/
MetadataCollectingFilterSpec.scala
334 lines (280 loc) · 10.3 KB
/
MetadataCollectingFilterSpec.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
/*
* Copyright 2021 ABSA Group Limited
*
* 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 za.co.absa.spline.harvester.postprocessing.extra
import org.apache.commons.configuration.BaseConfiguration
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import org.scalatestplus.mockito.MockitoSugar
import za.co.absa.commons.scalatest.EnvFixture
import za.co.absa.spline.harvester.postprocessing.metadata.MetadataCollectingFilter._
import za.co.absa.spline.harvester.postprocessing.metadata.{BaseNodeName, MetadataCollectingFilter}
import za.co.absa.spline.harvester.{HarvestingContext, IdGeneratorsBundle}
import za.co.absa.spline.producer.model._
import java.util.UUID
class MetadataCollectingFilterSpec extends AnyFlatSpec with EnvFixture with Matchers with MockitoSugar {
private val logicalPlan = mock[LogicalPlan]
private val sparkSession = SparkSession.builder
.master("local")
.appName("testApp")
.config("spark.driver.host", "localhost")
.config("spark.ui.enabled", "false")
.config("k", "nice")
.getOrCreate()
private val idGenerators = mock[IdGeneratorsBundle]
private val harvestingContext = new HarvestingContext("", logicalPlan, None, sparkSession, idGenerators)
private val wop = WriteOperation("foo", append = false, "42", "", Seq.empty, Map.empty, Map.empty)
private val systemInfo = NameAndVersion("foo", "bar")
private val agentInfo = NameAndVersion("test", "0.0.0")
private val emptyExpressions = Expressions(Seq.empty, Seq.empty)
private val defaultExtra = Map("ttt" -> 777)
private val ops = Operations(wop, Seq.empty, Seq.empty)
private val ep =
ExecutionPlan(None, "pn", None, Map.empty, ops, Seq.empty, emptyExpressions, systemInfo, agentInfo, defaultExtra)
private val eventExtra = Map("foo" -> "a", "bar" -> false, "baz" -> Seq(1, 2, 3))
private val ee = ExecutionEvent(UUID.randomUUID(), Map.empty, 66L, None, None, None, eventExtra)
behavior of "ExtraMetadataCollectingFilter"
it should "parse and replace all variables with values" in {
val configString =
"""
|{
| "executionPlan": {
| "extra": {
| "qux": 42,
| "seq": [ "aaa", "bbb", "ccc" ],
| "foo": { "$js": "executionPlan.name()" },
| "bar": { "$env": "BAR_HOME" },
| "baz": { "$jvm": "some.jvm.prop" },
| "daz": { "$js": "session.conf().get('k')" },
| "appName1": { "$js": "session.conf().get('spark.app.name')" },
| "appName2": { "$js":"session.sparkContext().appName()" }
| }
| }
|}
|""".stripMargin
System.setProperty("some.jvm.prop", "123")
setEnv("BAR_HOME", "rabbit")
val config = new BaseConfiguration {
addPropertyDirect(InjectRulesKey, configString)
}
val filter = new MetadataCollectingFilter(config)
val processedPlan = filter.processExecutionPlan(ep, harvestingContext)
val extra = processedPlan.extraInfo
extra("ttt") shouldBe 777
extra("qux") shouldBe 42
extra("seq") shouldBe Seq("aaa", "bbb", "ccc")
extra("foo") shouldBe "pn"
extra("bar") shouldBe "rabbit"
extra("baz") shouldBe "123"
extra("daz") shouldBe "nice"
extra("appName1") shouldBe "testApp"
extra("appName2") shouldBe "testApp"
}
it should "support extra in Write" in {
val configString =
"""
|{
| "write": {
| "extra": {
| "foo": "extra-value",
| "appName": { "$js":"session.sparkContext().appName()" }
| }
| }
|}
|""".stripMargin
val config = new BaseConfiguration {
addPropertyDirect(InjectRulesKey, configString)
}
val filter = new MetadataCollectingFilter(config)
val processedWrite = filter.processWriteOperation(wop, harvestingContext)
val extra = processedWrite.extra
extra("foo") shouldBe "extra-value"
extra("appName") shouldBe "testApp"
}
it should "support labels" in {
val configString =
"""
|{
| "executionPlan": {
| "labels": {
| "qux": 42,
| "tags": [ "aaa", "bbb", null, "ccc" ],
| "foo1": null,
| "foo2": [],
| "foo3": [null],
| "notFlat": ["a", ["b", "c"]],
| "bar": { "$env": "BAR_HOME" },
| "baz": { "$jvm": "some.jvm.prop" },
| "daz": { "$js": "session.conf().get('k')" }
| }
| },
| "executionEvent": {
| "labels": {
| "qux": 42
| }
| }
|}
|""".stripMargin
System.setProperty("some.jvm.prop", "123")
setEnv("BAR_HOME", "rabbit")
val config = new BaseConfiguration {
addPropertyDirect(InjectRulesKey, configString)
}
val filter = new MetadataCollectingFilter(config)
val processedPlan = filter.processExecutionPlan(ep, harvestingContext)
val processedEvent = filter.processExecutionEvent(ee, harvestingContext)
processedPlan.labels shouldEqual Map(
"qux" -> Seq("42"),
"tags" -> Seq("aaa", "bbb", "ccc"),
"notFlat" -> Seq("a", "b", "c"),
"bar" -> Seq("rabbit"),
"baz" -> Seq("123"),
"daz" -> Seq("nice")
)
processedEvent.labels shouldEqual Map(
"qux" -> Seq("42")
)
}
it should "handle missing JSON property" in {
val filter = new MetadataCollectingFilter(Map.empty[BaseNodeName.Type, Seq[RuleDef]])
filter.processExecutionPlan(mock[ExecutionPlan], mock[HarvestingContext]) should not be null
}
it should "handle json nesting" in {
val configString =
"""
|{
| "executionEvent": {
| "extra": {
| "tux": {
| "qux": 42,
| "qax": {
| "tax": { "$js": "session.conf().get('k')" }
| }
| }
| }
| }
|}
|""".stripMargin
val config = new BaseConfiguration {
addPropertyDirect(InjectRulesKey, configString)
}
val filter = new MetadataCollectingFilter(config)
val processedEvent = filter.processExecutionEvent(ee, harvestingContext)
val extra = processedEvent.extra
val tux = extra("tux").asInstanceOf[Map[String, Any]]
tux("qux") shouldBe 42
val qax = tux("qax").asInstanceOf[Map[String, Any]]
qax("tax") shouldBe "nice"
}
it should "merge nested extra" in {
val configString =
"""
|{
| "executionEvent": {
| "extra": {
| "tux": {
| "qux": 42,
| "qax": ["ta", "pa"]
| }
| }
| },
| "executionEvent[true]": {
| "extra": {
| "tux": {
| "qax": ["da", "ta"],
| "fax": 33
| }
| }
| }
|}
|""".stripMargin
val config = new BaseConfiguration {
addPropertyDirect(InjectRulesKey, configString)
}
val filter = new MetadataCollectingFilter(config)
val processedEvent = filter.processExecutionEvent(ee, harvestingContext)
val extra = processedEvent.extra
val tux = extra("tux").asInstanceOf[Map[String, Any]]
tux("qux") shouldBe 42
tux("qax") shouldBe Seq("ta", "pa", "da")
tux("fax") shouldBe 33
}
it should "apply defined predicates" in {
val configString =
"""
|{
| "executionEvent[@.timestamp > 65]": {
| "extra": { "tux": 1 }
| },
| "executionEvent[@.extra['foo'] == 'a' && @.extra['bar'] == 'x']": {
| "extra": { "bux": 2 }
| },
| "executionEvent[@.extra['foo'] == 'a' && !@.extra['bar']]": {
| "extra": { "dux": 3 }
| },
| "executionEvent[@.extra['baz'][2] >= 3]": {
| "extra": { "mux": 4 }
| },
| "executionEvent[@.extra['baz'][2] < 3]": {
| "extra": { "fux": 5 }
| }
|}
|""".stripMargin
val config = new BaseConfiguration {
addPropertyDirect(InjectRulesKey, configString)
}
val filter = new MetadataCollectingFilter(config)
val processedEvent = filter.processExecutionEvent(ee, harvestingContext)
val extra = processedEvent.extra
extra("tux") shouldBe 1
extra.get("bux") shouldBe None
extra("dux") shouldBe 3
extra("mux") shouldBe 4
extra.get("fux") shouldBe None
}
it should "allow predicates to access sparkContext config" in {
val configString =
"""
|{
| "executionEvent[session.sparkContext.conf['spark.ui.enabled'] == 'false']": {
| "extra": { "tux": 1 }
| }
|}
|""".stripMargin
val config = new BaseConfiguration {
addPropertyDirect(InjectRulesKey, configString)
}
val filter = new MetadataCollectingFilter(config)
val processedEvent = filter.processExecutionEvent(ee, harvestingContext)
val extra = processedEvent.extra
extra("tux") shouldBe 1
}
it should "throw an exception when parsing labels on unexpected places" in {
val configString =
"""
|{
| "operation": {
| "labels": { "tux": 1 }
| }
|}
|""".stripMargin
val config = new BaseConfiguration {
addPropertyDirect(InjectRulesKey, configString)
}
(the[IllegalArgumentException] thrownBy new MetadataCollectingFilter(config)).getMessage should include("Labels are not supported")
}
}