Skip to content

Commit 4dbdb76

Browse files
terryyylimpyalex
andauthored
Add deadletter/read-from-source metrics to batch and stream ingestion (#1223)
* Add deadletter metrics to batch and stream Signed-off-by: Terence <terencelimxp@gmail.com> * Address comments Signed-off-by: Terence <terencelimxp@gmail.com> * generalize ingestion pipeline metrics Signed-off-by: Oleksii Moskalenko <moskalenko.alexey@gmail.com> * tests passing Signed-off-by: Oleksii Moskalenko <moskalenko.alexey@gmail.com> * increase grpc connection timeout Signed-off-by: Oleksii Moskalenko <moskalenko.alexey@gmail.com> * mvn retry in e2e tests Signed-off-by: Oleksii Moskalenko <moskalenko.alexey@gmail.com> Co-authored-by: Oleksii Moskalenko <moskalenko.alexey@gmail.com>
1 parent 6db1ec0 commit 4dbdb76

16 files changed

Lines changed: 259 additions & 78 deletions

File tree

‎infra/scripts/test-end-to-end-gcp.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22

33
export DISABLE_SERVICE_FIXTURES=1
4-
export MAVEN_OPTS="-Dmaven.repo.local=/tmp/.m2/repository -DdependencyLocationsEnabled=false"
4+
export MAVEN_OPTS="-Dmaven.repo.local=/tmp/.m2/repository -DdependencyLocationsEnabled=false -Dmaven.wagon.httpconnectionManager.ttlSeconds=25 -Dmaven.wagon.http.retryHandler.count=3 -Dhttp.keepAlive=false -Dmaven.wagon.http.pool=false"
55
export MAVEN_CACHE="gs://feast-templocation-kf-feast/.m2.2020-11-17.tar"
66

77
infra/scripts/download-maven-cache.sh --archive-uri ${MAVEN_CACHE} --output-dir /tmp

‎infra/scripts/test-end-to-end.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22

3-
export MAVEN_OPTS="-Dmaven.repo.local=/tmp/.m2/repository -DdependencyLocationsEnabled=false"
3+
export MAVEN_OPTS="-Dmaven.repo.local=/tmp/.m2/repository -DdependencyLocationsEnabled=false -Dmaven.wagon.httpconnectionManager.ttlSeconds=25 -Dmaven.wagon.http.retryHandler.count=3 -Dhttp.keepAlive=false -Dmaven.wagon.http.pool=false"
44
export MAVEN_CACHE="gs://feast-templocation-kf-feast/.m2.2020-11-17.tar"
55

66
infra/scripts/download-maven-cache.sh --archive-uri ${MAVEN_CACHE} --output-dir /tmp

‎spark/ingestion/pom.xml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,14 @@
189189
<dependency>
190190
<groupId>com.dimafeng</groupId>
191191
<artifactId>testcontainers-scala-scalatest_${scala.version}</artifactId>
192-
<version>0.38.6</version>
192+
<version>0.38.8</version>
193193
<scope>test</scope>
194194
</dependency>
195195

196196
<dependency>
197197
<groupId>com.dimafeng</groupId>
198198
<artifactId>testcontainers-scala-kafka_${scala.version}</artifactId>
199-
<version>0.38.6</version>
199+
<version>0.38.8</version>
200200
<scope>test</scope>
201201
</dependency>
202202

‎spark/ingestion/src/main/scala/feast/ingestion/BasePipeline.scala‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ trait BasePipeline {
5555
.set("spark.metrics.conf.*.sink.statsd.port", c.port.toString)
5656
.set("spark.metrics.conf.*.sink.statsd.period", "30")
5757
.set("spark.metrics.conf.*.sink.statsd.unit", "seconds")
58-
.set("spark.metrics.namespace", jobConfig.mode.toString.toLowerCase)
58+
.set("spark.metrics.namespace", s"feast_${jobConfig.mode.toString.toLowerCase}")
5959
// until proto parser udf will be fixed, we have to use this
6060
.set("spark.sql.legacy.allowUntypedScalaUDF", "true")
6161
case None => ()

‎spark/ingestion/src/main/scala/feast/ingestion/BatchPipeline.scala‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
*/
1717
package feast.ingestion
1818

19+
import feast.ingestion.metrics.IngestionPipelineMetrics
1920
import feast.ingestion.sources.bq.BigQueryReader
2021
import feast.ingestion.sources.file.FileReader
2122
import feast.ingestion.validation.{RowValidator, TypeCheck}
2223
import org.apache.commons.lang.StringUtils
2324
import org.apache.spark.SparkEnv
24-
import org.apache.spark.sql.{SaveMode, SparkSession}
25+
import org.apache.spark.sql.catalyst.encoders.RowEncoder
26+
import org.apache.spark.sql.{Encoder, Row, SaveMode, SparkSession}
2527

2628
/**
2729
* Batch Ingestion Flow:
@@ -37,6 +39,7 @@ object BatchPipeline extends BasePipeline {
3739
val projection =
3840
inputProjection(config.source, featureTable.features, featureTable.entities)
3941
val rowValidator = new RowValidator(featureTable, config.source.eventTimestampColumn)
42+
val metrics = new IngestionPipelineMetrics
4043

4144
val input = config.source match {
4245
case source: BQSource =>
@@ -57,13 +60,16 @@ object BatchPipeline extends BasePipeline {
5760

5861
val projected = input.select(projection: _*).cache()
5962

63+
implicit def rowEncoder: Encoder[Row] = RowEncoder(projected.schema)
64+
6065
TypeCheck.allTypesMatch(projected.schema, featureTable) match {
6166
case Some(error) =>
6267
throw new RuntimeException(s"Dataframe columns don't match expected feature types: $error")
6368
case _ => ()
6469
}
6570

6671
val validRows = projected
72+
.mapPartitions(metrics.incrementRead)
6773
.filter(rowValidator.allChecks)
6874

6975
validRows.write
@@ -79,6 +85,7 @@ object BatchPipeline extends BasePipeline {
7985
case Some(path) =>
8086
projected
8187
.filter(!rowValidator.allChecks)
88+
.mapPartitions(metrics.incrementDeadLetters)
8289
.write
8390
.format("parquet")
8491
.mode(SaveMode.Append)

‎spark/ingestion/src/main/scala/feast/ingestion/StreamingPipeline.scala‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ package feast.ingestion
1919
import java.io.File
2020
import java.util.concurrent.TimeUnit
2121

22+
import feast.ingestion.metrics.IngestionPipelineMetrics
2223
import feast.ingestion.registry.proto.ProtoRegistryFactory
23-
import org.apache.spark.sql.{DataFrame, Row, SaveMode, SparkSession}
24+
import org.apache.spark.sql.{DataFrame, Encoder, Row, SaveMode, SparkSession}
2425
import org.apache.spark.sql.functions.{expr, struct, udf}
2526
import feast.ingestion.utils.ProtoReflection
2627
import feast.ingestion.utils.testing.MemoryStreamingSource
@@ -33,8 +34,8 @@ import org.apache.spark.sql.streaming.StreamingQuery
3334
import org.apache.spark.sql.avro._
3435
import org.apache.spark.sql.execution.python.UserDefinedPythonFunction
3536
import org.apache.spark.sql.execution.streaming.ProcessingTimeTrigger
36-
import org.apache.spark.sql.expressions.UserDefinedFunction
3737
import org.apache.spark.sql.types.BooleanType
38+
import org.apache.spark.sql.catalyst.encoders.RowEncoder
3839

3940
/**
4041
* Streaming pipeline (currently in micro-batches mode only, since we need to have multiple sinks: redis & deadletters).
@@ -55,8 +56,8 @@ object StreamingPipeline extends BasePipeline with Serializable {
5556
val featureTable = config.featureTable
5657
val projection =
5758
inputProjection(config.source, featureTable.features, featureTable.entities)
58-
val rowValidator = new RowValidator(featureTable, config.source.eventTimestampColumn)
59-
59+
val rowValidator = new RowValidator(featureTable, config.source.eventTimestampColumn)
60+
val metrics = new IngestionPipelineMetrics
6061
val validationUDF = createValidationUDF(sparkSession, config)
6162

6263
val input = config.source match {
@@ -103,8 +104,10 @@ object StreamingPipeline extends BasePipeline with Serializable {
103104
batchDF.withColumn("_isValid", rowValidator.allChecks)
104105
}
105106
rowsAfterValidation.persist()
107+
implicit def rowEncoder: Encoder[Row] = RowEncoder(rowsAfterValidation.schema)
106108

107109
rowsAfterValidation
110+
.mapPartitions(metrics.incrementRead)
108111
.filter(if (config.doNotIngestInvalidRows) expr("_isValid") else rowValidator.allChecks)
109112
.write
110113
.format("feast.ingestion.stores.redis")
@@ -119,6 +122,7 @@ object StreamingPipeline extends BasePipeline with Serializable {
119122
case Some(path) =>
120123
rowsAfterValidation
121124
.filter("!_isValid")
125+
.mapPartitions(metrics.incrementDeadLetters)
122126
.write
123127
.format("parquet")
124128
.mode(SaveMode.Append)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright 2018-2020 The Feast Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package feast.ingestion.metrics
18+
19+
import org.apache.spark.SparkEnv
20+
import org.apache.spark.metrics.source.IngestionPipelineMetricSource
21+
import org.apache.spark.sql.Row
22+
23+
class IngestionPipelineMetrics extends Serializable {
24+
25+
def incrementDeadLetters(rowIterator: Iterator[Row]): Iterator[Row] = {
26+
val materialized = rowIterator.toArray
27+
if (metricSource.nonEmpty)
28+
metricSource.get.METRIC_DEADLETTER_ROWS_INSERTED.inc(materialized.length)
29+
30+
materialized.toIterator
31+
}
32+
33+
def incrementRead(rowIterator: Iterator[Row]): Iterator[Row] = {
34+
val materialized = rowIterator.toArray
35+
if (metricSource.nonEmpty)
36+
metricSource.get.METRIC_ROWS_READ_FROM_SOURCE.inc(materialized.length)
37+
38+
materialized.toIterator
39+
}
40+
41+
private lazy val metricSource: Option[IngestionPipelineMetricSource] = {
42+
val metricsSystem = SparkEnv.get.metricsSystem
43+
IngestionPipelineMetricsLock.synchronized {
44+
if (metricsSystem.getSourcesByName(IngestionPipelineMetricSource.sourceName).isEmpty) {
45+
metricsSystem.registerSource(new IngestionPipelineMetricSource)
46+
}
47+
}
48+
49+
metricsSystem.getSourcesByName(IngestionPipelineMetricSource.sourceName) match {
50+
case Seq(head) => Some(head.asInstanceOf[IngestionPipelineMetricSource])
51+
case _ => None
52+
}
53+
}
54+
}
55+
56+
private object IngestionPipelineMetricsLock

‎spark/ingestion/src/main/scala/feast/ingestion/metrics/StatsdReporterWithTags.scala‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,13 @@ class StatsdReporterWithTags(
125125
private def reportGauge(name: String, gauge: Gauge[_])(implicit socket: DatagramSocket): Unit =
126126
formatAny(gauge.getValue).foreach(v => send(fullName(name), v, GAUGE))
127127

128-
private def reportCounter(name: String, counter: Counter)(implicit socket: DatagramSocket): Unit =
129-
send(fullName(name), format(counter.getCount), COUNTER)
128+
private def reportCounter(name: String, counter: Counter)(implicit
129+
socket: DatagramSocket
130+
): Unit = {
131+
val snapshot = counter.getCount
132+
send(fullName(name), format(snapshot), COUNTER)
133+
counter.dec(snapshot) // reset counter
134+
}
130135

131136
private def reportHistogram(name: String, histogram: Histogram)(implicit
132137
socket: DatagramSocket
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright 2018-2020 The Feast Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.spark.metrics.source
18+
19+
import com.codahale.metrics.MetricRegistry
20+
import org.apache.spark.SparkEnv
21+
22+
class BaseMetricSource extends Source {
23+
override val sourceName: String = ""
24+
25+
override val metricRegistry: MetricRegistry = new MetricRegistry
26+
27+
private val sparkConfig = SparkEnv.get.conf
28+
29+
private val metricLabels = sparkConfig.get("spark.metrics.labels", "")
30+
31+
private val appId = sparkConfig.get("spark.app.id", "")
32+
33+
private val executorId = sparkConfig.get("spark.executor.id", "")
34+
35+
protected def metricWithLabels(name: String) = {
36+
if (metricLabels.isEmpty) {
37+
name
38+
} else {
39+
s"$name#$metricLabels,job_id=$appId-$executorId"
40+
}
41+
}
42+
43+
protected def counterWithLabels(name: String) = {
44+
if (metricLabels.isEmpty) {
45+
name
46+
} else {
47+
s"$name#$metricLabels"
48+
}
49+
}
50+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright 2018-2020 The Feast Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.spark.metrics.source
18+
19+
class IngestionPipelineMetricSource extends BaseMetricSource {
20+
override val sourceName: String = IngestionPipelineMetricSource.sourceName
21+
22+
val METRIC_DEADLETTER_ROWS_INSERTED =
23+
metricRegistry.counter(counterWithLabels("deadletter_count"))
24+
25+
val METRIC_ROWS_READ_FROM_SOURCE =
26+
metricRegistry.counter(counterWithLabels("read_from_source_count"))
27+
}
28+
29+
object IngestionPipelineMetricSource {
30+
val sourceName = "ingestion_pipeline"
31+
}

0 commit comments

Comments
 (0)