Skip to content

Commit

Permalink
Merge pull request #1 from willianmrs/feat/adding-permissive-error-ha…
Browse files Browse the repository at this point in the history
…ndler

feat adding PermissiveRecordExceltionHandler to return a null row if …
  • Loading branch information
willianmrs committed Jun 30, 2023
2 parents 9ed1d27 + 69f083b commit 56af4ea
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 12 deletions.
26 changes: 25 additions & 1 deletion .github/workflows/build-scala2.12-spark3.2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ on:
pull_request:
branches: [ master ]

permissions:
contents: read
pull-requests: write

jobs:
build:

Expand All @@ -18,4 +22,24 @@ jobs:
with:
java-version: 1.8
- name: Run tests
run: mvn clean verify -Plicense-check,spark-3.2,scala-2.12
run: mvn clean verify -Plicense-check,spark-3.2,scala-2.12,code-coverage
- name: Add coverage to PR
id: jacoco
uses: madrapps/jacoco-report@v1.4
with:
paths: ${{ github.workspace }}/target/site/jacoco/jacoco.xml
token: ${{ secrets.GITHUB_TOKEN }}
min-coverage-overall: 0.0
min-coverage-changed-files: 80.0
title: JaCoCo code coverage report - Scala 2.12 & Spark 3.2
update-comment: true
- name: Get the Coverage info
run: |
echo "Total coverage ${{ steps.jacoco.outputs.coverage-overall }}"
echo "Changed Files coverage ${{ steps.jacoco.outputs.coverage-changed-files }}"
- name: Fail PR if changed files coverage is less than 80%
if: ${{ steps.jacoco.outputs.coverage-changed-files < 80.0 }}
uses: actions/github-script@v6
with:
script: |
core.setFailed('Changed files coverage is less than 80%!')
26 changes: 25 additions & 1 deletion .github/workflows/build-scala2.13-spark3.2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ on:
pull_request:
branches: [ master ]

permissions:
contents: read
pull-requests: write

jobs:
build:

Expand All @@ -18,4 +22,24 @@ jobs:
with:
java-version: 1.8
- name: Run tests
run: mvn clean verify -Plicense-check,spark-3.2,scala-2.13
run: mvn clean verify -Plicense-check,spark-3.2,scala-2.13,code-coverage
- name: Add coverage to PR
id: jacoco
uses: madrapps/jacoco-report@v1.4
with:
paths: ${{ github.workspace }}/target/site/jacoco/jacoco.xml
token: ${{ secrets.GITHUB_TOKEN }}
min-coverage-overall: 0.0
min-coverage-changed-files: 80.0
title: JaCoCo code coverage report - Scala 2.13 & Spark 3.2
update-comment: true
- name: Get the Coverage info
run: |
echo "Total coverage ${{ steps.jacoco.outputs.coverage-overall }}"
echo "Changed Files coverage ${{ steps.jacoco.outputs.coverage-changed-files }}"
- name: Fail PR if changed files coverage is less than 80%
if: ${{ steps.jacoco.outputs.coverage-changed-files < 80.0 }}
uses: actions/github-script@v6
with:
script: |
core.setFailed('Changed files coverage is less than 80%!')
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,25 @@ val abrisConfig = AbrisConfig

This is only for confluent-based configuration, not for standard avro.

#### PermissiveRecordExceptionHandler
The third option is to use the `PermissiveRecordExceptionHandler`. In case of a deserialization failure, this handler replaces the problematic record with a fully null record, instead of throwing an exception. This allows the data processing pipeline to continue without interruption.

The main use case for this option is when you want to prioritize continuity of processing over individual record integrity. It's especially useful when dealing with large datasets where occasional malformed records could be tolerated.

Here's how to use it:

```scala
val abrisConfig = AbrisConfig
.fromConfluentAvro
.downloadReaderSchemaByLatestVersion
.andTopicNameStrategy("topic123")
.usingSchemaRegistry(registryConfig)
.withSchemaConverter("custom")
.withExceptionHandler(new PermissiveRecordExceptionHandler())
```

With this configuration, in the event of a deserialization error, the `PermissiveRecordExceptionHandler` will log a warning, substitute the malformed record with a fully null one, and allow the data processing pipeline to continue.


### Data Conversions
This library also provides convenient methods to convert between Avro and Spark schemas.
Expand Down
15 changes: 5 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@

<!--Libs-->
<avro.version>1.10.2</avro.version>
<jacoco.version>0.8.10</jacoco.version>

<!--Tests-->
<scalatestplus.mockito.version>3.1.2.0</scalatestplus.mockito.version>
<scalatest.version>3.2.14</scalatest.version>
<scalatest.maven.plugin.version>2.2.0</scalatest.maven.plugin.version>
<jacoco.version>0.8.8</jacoco.version>

<scala.maven.plugin.version>3.3.2</scala.maven.plugin.version>

Expand Down Expand Up @@ -335,13 +335,6 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
</plugin>

</plugins>
</build>

Expand Down Expand Up @@ -566,7 +559,7 @@
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<groupId>za.co.absa.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
Expand All @@ -589,7 +582,9 @@
<goal>report</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/jacoco</outputDirectory>
<title>${project.name} - scala:${scala.version}</title>
<doMethodFiltration>true</doMethodFiltration>
<doScalaMethodFiltration>true</doScalaMethodFiltration>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2022 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.abris.avro.errors

import org.apache.avro.Schema
import org.apache.avro.generic.{GenericData, GenericRecord}
import org.apache.avro.specific.SpecificRecordBase
import org.apache.spark.internal.Logging
import org.apache.spark.sql.avro.AbrisAvroDeserializer

class PermissiveRecordExceptionHandler() extends DeserializationExceptionHandler with Logging {

def handle(exception: Throwable, deserializer: AbrisAvroDeserializer, readerSchema: Schema): Any = {
logWarning("Malformed record detected. Replacing with full null row.", exception)
val record = new GenericData.Record(readerSchema)
deserializer.deserialize(record)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2018 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.abris.avro.errors

import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.avro.{AbrisAvroDeserializer, SchemaConverters}
import org.apache.spark.sql.catalyst.expressions.SpecificInternalRow
import org.apache.spark.sql.types.{DataType, StructType}
import org.scalatest.flatspec.AnyFlatSpec
import za.co.absa.abris.avro.parsing.utils.AvroSchemaUtils
import za.co.absa.abris.examples.data.generation.TestSchemas

class PermissiveRecordExceptionHandlerSpec extends AnyFlatSpec {

private val spark = SparkSession
.builder()
.appName("unitTest")
.master("local[1]")
.config("spark.driver.bindAddress", "localhost")
.config("spark.ui.enabled", "false")
.getOrCreate()

it should "receive empty dataframe row back" in {

val expectedNestedFieldSchema = new StructType()
.add("int", "int")
.add("long", "long")
val expectedNestedStructSchema = new StructType()
.add("name", "string")
.add("nested", expectedNestedFieldSchema)

val expectedNestedFieldInternalRow = new SpecificInternalRow(expectedNestedFieldSchema)
expectedNestedFieldInternalRow.setNullAt(0)
expectedNestedFieldInternalRow.setNullAt(1)

val expectedNestedStructInternalRow = new SpecificInternalRow(expectedNestedStructSchema)
expectedNestedStructInternalRow.setNullAt(0)
expectedNestedStructInternalRow.setNullAt(1)

//actual
val deserializationExceptionHandler = new PermissiveRecordExceptionHandler()
val schema = AvroSchemaUtils.parse(TestSchemas.NATIVE_SIMPLE_OUTER_SCHEMA)
val dataType: DataType = SchemaConverters.toSqlType(schema).dataType

val actualResult = deserializationExceptionHandler
.handle(new Exception, new AbrisAvroDeserializer(schema, dataType), schema)

assert(actualResult == expectedNestedStructInternalRow)
}
}

0 comments on commit 56af4ea

Please sign in to comment.