Skip to content

Commit

Permalink
Add option to exit on missing Iglu schemas (#382)
Browse files Browse the repository at this point in the history
Before this PR, the loader would generate a failed event if it failed to
fetch a required schema from Iglu.  However, all events have already
passed validation in Enrich, so it is completely unexpected to have an
Iglu failure.  An Iglu error _probably_ means some type of configuration
error or service outage.

After this PR, the loader will crash and exit on an Iglu error, instead
of creating a failed event.  This is probably the preferred behaviour,
while the pipeline operator addresses the underlying infrastructure
problem.

If an Iglu schema is genuinely now unavailable, then the pipeline
operator can override the default behaviour by setting
`exitOnMissingIgluSchema: false` in the configuration file or by listing
the missing schema in `skipschemas`.
  • Loading branch information
istreeter authored Sep 5, 2024
1 parent 531d02c commit 32a1a0e
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 32 deletions.
6 changes: 6 additions & 0 deletions config/config.azure.reference.hocon
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@
"iglu:com.acme/legacy/jsonschema/2-*-*"
]

# -- Whether the loader should crash and exit if it fails to resolve an Iglu Schema.
# -- We recommend `true` because Snowplow enriched events have already passed validation, so a missing schema normally
# -- indicates an error that needs addressing.
# -- Change to `false` so events go the failed events stream instead of crashing the loader.
"exitOnMissingIgluSchema": true

"monitoring": {
"metrics": {

Expand Down
6 changes: 6 additions & 0 deletions config/config.kinesis.reference.hocon
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@
"iglu:com.acme/legacy/jsonschema/2-*-*"
]

# -- Whether the loader should crash and exit if it fails to resolve an Iglu Schema.
# -- We recommend `true` because Snowplow enriched events have already passed validation, so a missing schema normally
# -- indicates an error that needs addressing.
# -- Change to `false` so events go the failed events stream instead of crashing the loader.
"exitOnMissingIgluSchema": true

"monitoring": {
"metrics": {

Expand Down
6 changes: 6 additions & 0 deletions config/config.pubsub.reference.hocon
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@
"iglu:com.acme/legacy/jsonschema/2-*-*"
]

# -- Whether the loader should crash and exit if it fails to resolve an Iglu Schema.
# -- We recommend `true` because Snowplow enriched events have already passed validation, so a missing schema normally
# -- indicates an error that needs addressing.
# -- Change to `false` so events go the failed events stream instead of crashing the loader.
"exitOnMissingIgluSchema": true

"monitoring": {
"metrics": {

Expand Down
1 change: 1 addition & 0 deletions modules/core/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

"skipSchemas": []
"legacyColumns": []
"exitOnMissingIgluSchema": true

"monitoring": {
"metrics": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ case class Config[+Source, +Sink](
monitoring: Config.Monitoring,
license: AcceptedLicense,
skipSchemas: List[SchemaCriterion],
legacyColumns: List[SchemaCriterion]
legacyColumns: List[SchemaCriterion],
exitOnMissingIgluSchema: Boolean
)

object Config {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ case class Environment[F[_]](
batching: Config.Batching,
badRowMaxSize: Int,
schemasToSkip: List[SchemaCriterion],
legacyColumns: List[SchemaCriterion]
legacyColumns: List[SchemaCriterion],
exitOnMissingIgluSchema: Boolean
)

object Environment {
Expand Down Expand Up @@ -66,20 +67,21 @@ object Environment {
writerBuilder <- Writer.builder(config.main.output.good, creds)
writerProvider <- Writer.provider(writerBuilder, config.main.retries, appHealth)
} yield Environment(
appInfo = appInfo,
source = sourceAndAck,
badSink = badSink,
resolver = resolver,
httpClient = httpClient,
tableManager = tableManagerWrapped,
writer = writerProvider,
metrics = metrics,
appHealth = appHealth,
alterTableWaitPolicy = BigQueryRetrying.policyForAlterTableWait[F](config.main.retries),
batching = config.main.batching,
badRowMaxSize = config.main.output.bad.maxRecordSize,
schemasToSkip = config.main.skipSchemas,
legacyColumns = config.main.legacyColumns
appInfo = appInfo,
source = sourceAndAck,
badSink = badSink,
resolver = resolver,
httpClient = httpClient,
tableManager = tableManagerWrapped,
writer = writerProvider,
metrics = metrics,
appHealth = appHealth,
alterTableWaitPolicy = BigQueryRetrying.policyForAlterTableWait[F](config.main.retries),
batching = config.main.batching,
badRowMaxSize = config.main.output.bad.maxRecordSize,
schemasToSkip = config.main.skipSchemas,
legacyColumns = config.main.legacyColumns,
exitOnMissingIgluSchema = config.main.exitOnMissingIgluSchema
)

private def enableSentry[F[_]: Sync](appInfo: AppInfo, config: Option[Config.Sentry]): Resource[F, Unit] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ object RuntimeService {

case object BigQueryClient extends RuntimeService
case object BadSink extends RuntimeService
case object Iglu extends RuntimeService

implicit val show: Show[RuntimeService] = Show.show {
case BigQueryClient => "BigQuery client"
case BadSink => "Bad sink"
case BadSink => "Failed event sink"
case Iglu => "Iglu repositories"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import cats.{Applicative, Foldable}
import cats.effect.{Async, Sync}
import cats.effect.kernel.Unique
import fs2.{Chunk, Pipe, Stream}
import io.circe.syntax._
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.slf4j.Slf4jLogger
import retry.{PolicyDecision, RetryDetails, RetryPolicy}
Expand Down Expand Up @@ -151,6 +152,7 @@ object Processing {
_ <- Logger[F].debug(s"Processing batch of size ${events.size}")
v2NonAtomicFields <- NonAtomicFields.resolveTypes[F](env.resolver, entities, env.schemasToSkip ::: env.legacyColumns)
legacyFields <- LegacyColumns.resolveTypes[F](env.resolver, entities, env.legacyColumns)
_ <- possiblyExitOnMissingIgluSchema(env, v2NonAtomicFields, legacyFields)
(moreBad, rows) <- transformBatch[F](badProcessor, loadTstamp, events, v2NonAtomicFields, legacyFields)
fields = v2NonAtomicFields.fields.flatMap { tte =>
tte.mergedField :: tte.recoveries.map(_._2)
Expand Down Expand Up @@ -380,4 +382,19 @@ object Processing {
a.countBytes
}

private def possiblyExitOnMissingIgluSchema[F[_]: Sync](
env: Environment[F],
v2NonAtomicFields: NonAtomicFields.Result,
legacyFields: LegacyColumns.Result
): F[Unit] =
if (env.exitOnMissingIgluSchema && (v2NonAtomicFields.igluFailures.nonEmpty || legacyFields.igluFailures.nonEmpty)) {
val base =
"Exiting because failed to resolve Iglu schemas. Either check the configuration of the Iglu repos, or set the `skipSchemas` config option, or set `exitOnMissingIgluSchema` to false.\n"
val failures = v2NonAtomicFields.igluFailures.map(_.failure) ::: legacyFields.igluFailures.map(_.failure)
val msg = failures.map(_.asJson.noSpaces).mkString(base, "\n", "")
env.appHealth.beUnhealthyForRuntimeService(RuntimeService.Iglu) *> Logger[F].error(base) *> Sync[F].raiseError(
new RuntimeException(msg)
)
} else Applicative[F].unit

}
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ object MockEnvironment {
maxDelay = 10.seconds,
writeBatchConcurrency = 1
),
badRowMaxSize = 1000000,
schemasToSkip = List.empty,
legacyColumns = legacyColumns
badRowMaxSize = 1000000,
schemasToSkip = List.empty,
legacyColumns = legacyColumns,
exitOnMissingIgluSchema = false
)
MockEnvironment(state, env)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class ProcessingSpec extends Specification with CatsEffect {
Set the latency metric based off the message timestamp $e9
Mark app as unhealthy when sinking badrows fails $e10
Mark app as unhealthy when writing to the Writer fails with runtime exception $e11
Emit BadRows for an unknown schema, if exitOnMissingIgluSchema is false $e12 $e12Legacy
Crash and exit for an unknown schema, if exitOnMissingIgluSchema is true $e13 $e13Legacy
"""

def e1 =
Expand Down Expand Up @@ -506,6 +508,79 @@ class ProcessingSpec extends Specification with CatsEffect {

}

def e12Base(legacyColumns: Boolean) = {
val unstructEvent: UnstructEvent = json"""
{
"schema": "iglu:com.snowplowanalytics.snowplow/unstruct_event/jsonschema/1-0-0",
"data": {
"schema": "iglu:com.unkown/unknown_event/jsonschema/1-0-0",
"data": {
"abc": 50
}
}
}
""".as[UnstructEvent].fold(throw _, identity)

val legacyCriteria =
if (legacyColumns) List(SchemaCriterion("com.unknown", "unknown_event", "jsonschema", 1))
else Nil

runTest(inputEvents(count = 1, good(unstructEvent)), legacyCriteria = legacyCriteria) { case (inputs, control) =>
for {
_ <- Processing.stream(control.environment).compile.drain
state <- control.state.get
} yield state should beEqualTo(
Vector(
Action.CreatedTable,
Action.OpenedWriter,
Action.WroteRowsToBigQuery(1),
Action.SentToBad(1),
Action.AddedGoodCountMetric(1),
Action.AddedBadCountMetric(1),
Action.Checkpointed(List(inputs(0).ack))
)
)
}
}

def e12 = e12Base(legacyColumns = false)
def e12Legacy = e12Base(legacyColumns = true)

def e13Base(legacyColumns: Boolean) = {
val unstructEvent: UnstructEvent = json"""
{
"schema": "iglu:com.snowplowanalytics.snowplow/unstruct_event/jsonschema/1-0-0",
"data": {
"schema": "iglu:com.unkown/unknown_event/jsonschema/1-0-0",
"data": {
"abc": 50
}
}
}
""".as[UnstructEvent].fold(throw _, identity)

val legacyCriteria =
if (legacyColumns) List(SchemaCriterion("com.unknown", "unknown_event", "jsonschema", 1))
else Nil

runTest(inputEvents(count = 1, good(unstructEvent)), legacyCriteria = legacyCriteria) { case (_, control) =>
val environment = control.environment.copy(exitOnMissingIgluSchema = true)
for {
_ <- Processing.stream(environment).compile.drain.voidError
state <- control.state.get
} yield state should beEqualTo(
Vector(
Action.CreatedTable,
Action.OpenedWriter,
Action.BecameUnhealthy(RuntimeService.Iglu)
)
)
}
}

def e13 = e13Base(legacyColumns = false)
def e13Legacy = e13Base(legacyColumns = true)

}

object ProcessingSpec {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ object KafkaConfigSpec {
healthProbe = Config.HealthProbe(port = Port.fromInt(8000).get, unhealthyLatency = 5.minutes),
webhook = Webhook.Config(endpoint = None, tags = Map.empty, heartbeat = 60.minutes)
),
license = AcceptedLicense(),
skipSchemas = List.empty,
legacyColumns = List.empty
license = AcceptedLicense(),
skipSchemas = List.empty,
legacyColumns = List.empty,
exitOnMissingIgluSchema = true
)

private val extendedConfig = Config[KafkaSourceConfig, KafkaSinkConfig](
Expand Down Expand Up @@ -217,6 +218,7 @@ object KafkaConfigSpec {
legacyColumns = List(
SchemaCriterion.parse("iglu:com.acme/legacy/jsonschema/1-*-*").get,
SchemaCriterion.parse("iglu:com.acme/legacy/jsonschema/2-*-*").get
)
),
exitOnMissingIgluSchema = true
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,10 @@ object KinesisConfigSpec {
healthProbe = Config.HealthProbe(port = Port.fromInt(8000).get, unhealthyLatency = 5.minutes),
webhook = Webhook.Config(endpoint = None, tags = Map.empty, heartbeat = 60.minutes)
),
license = AcceptedLicense(),
skipSchemas = List.empty,
legacyColumns = List.empty
license = AcceptedLicense(),
skipSchemas = List.empty,
legacyColumns = List.empty,
exitOnMissingIgluSchema = true
)

// workerIdentifer coming from "HOSTNAME" env variable set in BuildSettings
Expand Down Expand Up @@ -212,6 +213,7 @@ object KinesisConfigSpec {
legacyColumns = List(
SchemaCriterion.parse("iglu:com.acme/legacy/jsonschema/1-*-*").get,
SchemaCriterion.parse("iglu:com.acme/legacy/jsonschema/2-*-*").get
)
),
exitOnMissingIgluSchema = true
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,10 @@ object PubsubConfigSpec {
healthProbe = Config.HealthProbe(port = Port.fromInt(8000).get, unhealthyLatency = 5.minutes),
webhook = Webhook.Config(endpoint = None, tags = Map.empty, heartbeat = 60.minutes)
),
license = AcceptedLicense(),
skipSchemas = List.empty,
legacyColumns = List.empty
license = AcceptedLicense(),
skipSchemas = List.empty,
legacyColumns = List.empty,
exitOnMissingIgluSchema = true
)

private val extendedConfig = Config[PubsubSourceConfig, PubsubSinkConfig](
Expand Down Expand Up @@ -205,6 +206,7 @@ object PubsubConfigSpec {
legacyColumns = List(
SchemaCriterion.parse("iglu:com.acme/legacy/jsonschema/1-*-*").get,
SchemaCriterion.parse("iglu:com.acme/legacy/jsonschema/2-*-*").get
)
),
exitOnMissingIgluSchema = true
)
}

0 comments on commit 32a1a0e

Please sign in to comment.