Skip to content

Commit 7f34040

Browse files
Omar-SalehOmar Elhadidy
andauthored
[SPARK] Emit event for CDF calls. (delta-io#4883)
<!-- Thanks for sending a pull request! Here are some tips for you: 1. If this is your first time, please read our contributor guidelines: https://github.com/delta-io/delta/blob/master/CONTRIBUTING.md 2. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP] Your PR title ...'. 3. Be sure to keep the PR description updated to reflect all changes. 4. Please write your PR title to summarize what this PR proposes. 5. If possible, provide a concise example to reproduce the issue for a faster review. 6. If applicable, include the corresponding issue number in the PR title and link it in the body. --> #### Which Delta project/connector is this regarding? <!-- Please add the component selected below to the beginning of the pull request title For example: [Spark] Title of my pull request --> - [x] Spark - [ ] Standalone - [ ] Flink - [ ] Kernel - [ ] Other (fill in here) ## Description Add a new log event for cdf execution. <!-- - Describe what this PR changes. - Describe why we need the change. If this PR resolves an issue be sure to include "Resolves #XXX" to correctly link and close the issue upon merge. --> ## How was this patch tested? UT <!-- If tests were added, say they were added here. Please make sure to test the changes thoroughly including negative and positive cases if possible. If the changes were tested in any way other than unit tests, please clarify how you tested step by step (ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future). If the changes were not tested, please explain why. --> ## Does this PR introduce _any_ user-facing changes? <!-- If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible. If possible, please also clarify if this is a user-facing change compared to the released Delta Lake versions or within the unreleased branches such as master. If no, write 'No'. --> --------- Co-authored-by: Omar Elhadidy <[email protected]>
1 parent a720fe5 commit 7f34040

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

spark/src/main/scala/org/apache/spark/sql/delta/commands/cdc/CDCReader.scala

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ trait CDCReaderImpl extends DeltaLogging {
616616
}
617617

618618
var totalBytes = 0L
619-
var totalFiles = 0L
619+
var numAddFiles, numRemoveFiles, numAddCRCFiles = 0L
620620

621621
changes.foreach {
622622
case (v, actions) =>
@@ -659,13 +659,13 @@ trait CDCReaderImpl extends DeltaLogging {
659659
actions.foreach {
660660
case c: AddCDCFile =>
661661
cdcActions.append(c)
662-
totalFiles += 1L
662+
numAddCRCFiles += 1L
663663
totalBytes += c.size
664664
case a: AddFile =>
665-
totalFiles += 1L
665+
numAddFiles += 1L
666666
totalBytes += a.size
667667
case r: RemoveFile =>
668-
totalFiles += 1L
668+
numRemoveFiles += 1L
669669
totalBytes += r.size.getOrElse(0L)
670670
case i: CommitInfo => commitInfo = Some(i)
671671
case _ => // do nothing
@@ -753,6 +753,21 @@ trait CDCReaderImpl extends DeltaLogging {
753753
val emptyDf =
754754
DataFrameUtils.ofRows(spark.sqlContext.sparkSession, emptyRdd)
755755

756+
recordDeltaEvent(
757+
deltaLog,
758+
"delta.changeDataFeed.changesToDF",
759+
data = Map(
760+
"startVersion" -> start,
761+
"endVersion" -> end,
762+
"useCoarseGrainedCDC" -> useCoarseGrainedCDC,
763+
"numAddFiles" -> numAddFiles,
764+
"numRemoveFiles" -> numRemoveFiles,
765+
"numAddCRCFiles" -> numAddCRCFiles,
766+
"totalBytes" -> totalBytes,
767+
"isStreaming" -> isStreaming
768+
)
769+
)
770+
val totalFiles = numAddFiles + numRemoveFiles + numAddCRCFiles
756771
CDCVersionDiffInfo(
757772
(emptyDf +: dfs).reduce((df1, df2) => df1.union(
758773
df2

spark/src/test/scala/org/apache/spark/sql/delta/cdc/CDCReaderSuite.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package org.apache.spark.sql.delta.cdc
1919
// scalastyle:off import.ordering.noEmptyLine
2020
import java.io.File
2121

22+
import com.databricks.spark.util.Log4jUsageLogger
2223
import org.apache.spark.sql.delta._
2324
import org.apache.spark.sql.delta.DeltaOperations.Delete
2425
import org.apache.spark.sql.delta.DeltaTestUtils.BOOLEAN_DOMAIN
@@ -449,5 +450,21 @@ class CDCReaderSuite
449450
}
450451
}
451452
}
453+
454+
test("Logs are generated for changesToDF") {
455+
withTempDir { dir =>
456+
val events = Log4jUsageLogger.track {
457+
val log = DeltaLog.forTable(spark, dir.getAbsolutePath)
458+
val data = spark.range(10)
459+
460+
data.write.format("delta").save(dir.getAbsolutePath)
461+
sql(s"DELETE FROM delta.`${dir.getAbsolutePath}`")
462+
CDCReader.changesToBatchDF(log, 0, 1, spark)
463+
}
464+
465+
assert(events.exists(event => event.metric == "tahoeEvent" &&
466+
event.tags.get("opType") == Option("delta.changeDataFeed.changesToDF")))
467+
}
468+
}
452469
}
453470

0 commit comments

Comments
 (0)