-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't crash app for Snowflake failures (close #11)
- Loading branch information
Showing
11 changed files
with
159 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,10 @@ | |
"uploadConcurrency": 3 | ||
} | ||
|
||
"retries": { | ||
"backoff": "30 seconds" | ||
} | ||
|
||
"skipSchemas": [] | ||
|
||
"monitoring": { | ||
|
38 changes: 38 additions & 0 deletions
38
modules/core/src/main/scala/com.snowplowanalytics.snowplow.snowflake/AppHealth.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.snowplowanalytics.snowplow.snowflake | ||
|
||
import cats.implicits._ | ||
import cats.{Functor, Monad, Monoid} | ||
import com.snowplowanalytics.snowplow.runtime.HealthProbe | ||
import com.snowplowanalytics.snowplow.runtime.HealthProbe.{Healthy, Unhealthy} | ||
import com.snowplowanalytics.snowplow.snowflake.processing.SnowflakeHealth | ||
import com.snowplowanalytics.snowplow.sources.SourceAndAck | ||
|
||
object AppHealth { | ||
|
||
def isHealthy[F[_]: Monad]( | ||
config: Config.HealthProbe, | ||
source: SourceAndAck[F], | ||
snowflakeHealth: SnowflakeHealth[F] | ||
): F[HealthProbe.Status] = | ||
List( | ||
latencyHealth(config, source), | ||
snowflakeHealth.state.get | ||
).foldA | ||
|
||
private def latencyHealth[F[_]: Functor](config: Config.HealthProbe, source: SourceAndAck[F]): F[HealthProbe.Status] = | ||
source.processingLatency.map { latency => | ||
if (latency > config.unhealthyLatency) | ||
Unhealthy(show"Processing latency is $latency") | ||
else | ||
Healthy | ||
} | ||
|
||
private val combineHealth: (HealthProbe.Status, HealthProbe.Status) => HealthProbe.Status = { | ||
case (Healthy, Healthy) => Healthy | ||
case (Healthy, unhealthy) => unhealthy | ||
case (unhealthy, Healthy) => unhealthy | ||
case (Unhealthy(first), Unhealthy(second)) => Unhealthy(reason = s"$first, $second") | ||
} | ||
|
||
private implicit val healthMonoid: Monoid[HealthProbe.Status] = Monoid.instance(Healthy, combineHealth) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
.../src/main/scala/com.snowplowanalytics.snowplow.snowflake/processing/SnowflakeHealth.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.snowplowanalytics.snowplow.snowflake.processing | ||
|
||
import cats.effect.{Concurrent, Ref} | ||
import cats.implicits._ | ||
import com.snowplowanalytics.snowplow.runtime.HealthProbe | ||
import com.snowplowanalytics.snowplow.snowflake.processing.SnowflakeHealth.unhealthy | ||
|
||
final case class SnowflakeHealth[F[_]](state: Ref[F, HealthProbe.Status]) { | ||
def setUnhealthy(): F[Unit] = state.set(unhealthy) | ||
def setHealthy(): F[Unit] = state.set(HealthProbe.Healthy) | ||
} | ||
|
||
object SnowflakeHealth { | ||
private val unhealthy = HealthProbe.Unhealthy("Snowflake connection is not healthy") | ||
|
||
def initUnhealthy[F[_]: Concurrent]: F[SnowflakeHealth[F]] = | ||
Ref | ||
.of[F, HealthProbe.Status](unhealthy) | ||
.map(SnowflakeHealth.apply) | ||
} |
37 changes: 37 additions & 0 deletions
37
...rc/main/scala/com.snowplowanalytics.snowplow.snowflake/processing/SnowflakeRetrying.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.snowplowanalytics.snowplow.snowflake.processing | ||
|
||
import cats.effect.Sync | ||
import cats.implicits._ | ||
import com.snowplowanalytics.snowplow.snowflake.Config | ||
import org.typelevel.log4cats.Logger | ||
import org.typelevel.log4cats.slf4j.Slf4jLogger | ||
import retry._ | ||
import retry.implicits.retrySyntaxError | ||
|
||
object SnowflakeRetrying { | ||
|
||
private implicit def logger[F[_]: Sync] = Slf4jLogger.getLogger[F] | ||
|
||
def retryIndefinitely[F[_]: Sync: Sleep, A](snowflakeHealth: SnowflakeHealth[F], config: Config.Retries)(action: F[A]): F[A] = | ||
retryUntilSuccessful(snowflakeHealth, config, action) <* | ||
snowflakeHealth.setHealthy() | ||
|
||
private def retryUntilSuccessful[F[_]: Sync: Sleep, A]( | ||
snowflakeHealth: SnowflakeHealth[F], | ||
config: Config.Retries, | ||
action: F[A] | ||
): F[A] = | ||
action | ||
.onError(_ => snowflakeHealth.setUnhealthy()) | ||
.retryingOnAllErrors( | ||
policy = RetryPolicies.exponentialBackoff[F](config.backoff), | ||
onError = (error, details) => Logger[F].error(error)(s"Executing Snowflake command failed. ${extractRetryDetails(details)}") | ||
) | ||
|
||
private def extractRetryDetails(details: RetryDetails): String = details match { | ||
case RetryDetails.GivingUp(totalRetries, totalDelay) => | ||
s"Giving up on retrying, total retries: $totalRetries, total delay: ${totalDelay.toSeconds} seconds" | ||
case RetryDetails.WillDelayAndRetry(nextDelay, retriesSoFar, cumulativeDelay) => | ||
s"Will retry in ${nextDelay.toSeconds} seconds, retries so far: $retriesSoFar, total delay so far: ${cumulativeDelay.toSeconds} seconds" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters