-
Notifications
You must be signed in to change notification settings - Fork 16
/
Config.scala
283 lines (243 loc) · 11.7 KB
/
Config.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Copyright (c) 2012-2021 Snowplow Analytics Ltd. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package com.snowplowanalytics.snowplow.rdbloader.common.config
import java.net.URI
import java.time.Instant
import java.util.UUID
import scala.concurrent.duration.Duration
import cats.syntax.either._
import cats.syntax.show._
import io.circe._
import io.circe.syntax._
import io.circe.generic.semiauto._
import com.typesafe.config.ConfigFactory
import pureconfig._
import pureconfig.module.circe._
import pureconfig.error.{CannotParse, ConfigReaderFailures}
import monocle.Lens
import monocle.macros.GenLens
import com.snowplowanalytics.iglu.core.SchemaCriterion
import com.snowplowanalytics.snowplow.rdbloader.common._
import com.snowplowanalytics.snowplow.rdbloader.common.config.Config._
import com.snowplowanalytics.snowplow.rdbloader.common.{S3, LoaderMessage}
/**
* Main config file parsed from HOCON
* @tparam D kind of supported warehouse
*/
final case class Config[+D <: StorageTarget](name: String,
id: UUID,
region: String,
jsonpaths: Option[S3.Folder],
monitoring: Monitoring,
messageQueue: String,
shredder: Shredder,
storage: D,
formats: Formats,
steps: Set[Step])
object Config {
def fromString(s: String): Either[String, Config[StorageTarget]] =
Either
.catchNonFatal(ConfigSource.fromConfig(ConfigFactory.parseString(s)))
.leftMap(error => ConfigReaderFailures(CannotParse(s"Not valid HOCON. ${error.getMessage}", None)))
.flatMap { config =>
config
.load[Json]
.flatMap { json =>
json.as[Config[StorageTarget]].leftMap(failure => ConfigReaderFailures(CannotParse(failure.show, None)))
}
}
.leftMap(_.prettyPrint())
.flatMap { config =>
val overlaps = config.formats.findOverlaps
val message =
s"Following schema criterions overlap in different groups (TSV, JSON, skip): " +
s"${overlaps.map(_.asString).mkString(", ")}. " +
s"Make sure every schema can have only one format"
Either.cond(overlaps.isEmpty, config, message)
}
sealed trait Shredder {
def output: Shredder.Output
}
object Shredder {
final case class Batch(input: URI, output: Output) extends Shredder
final case class Stream(input: StreamInput, output: Output, windowing: Duration) extends Shredder
sealed trait InitPosition
object InitPosition {
case object Latest extends InitPosition
case object TrimHorizon extends InitPosition
final case class AtTimestamp(timestamp: Instant) extends InitPosition
implicit val ioCirceInitPositionDecoder: Decoder[InitPosition] =
Decoder.decodeJson.emap { json =>
json.asString match {
case Some("TRIM_HORIZON") => TrimHorizon.asRight
case Some("LATEST") => Latest.asRight
case Some(other) =>
s"Initial position $other is unknown. Choose from LATEST and TRIM_HORIZON. AT_TIMESTAMP must provide the timestamp".asLeft
case None =>
val result = for {
root <- json.asObject.map(_.toMap)
atTimestamp <- root.get("AT_TIMESTAMP")
atTimestampObj <- atTimestamp.asObject.map(_.toMap)
timestampStr <- atTimestampObj.get("timestamp")
timestamp <- timestampStr.as[Instant].toOption
} yield AtTimestamp(timestamp)
result match {
case Some(atTimestamp) => atTimestamp.asRight
case None =>
"Initial position can be either LATEST or TRIM_HORIZON string or AT_TIMESTAMP object (e.g. 2020-06-03T00:00:00Z)".asLeft
}
}
}
}
sealed trait StreamInput
object StreamInput {
case class Kinesis(appName: String, streamName: String, region: String, position: InitPosition) extends StreamInput
case class File(dir: String) extends StreamInput
}
case class Output(path: URI, compression: Compression)
sealed trait Compression extends StringEnum
object Compression {
final case object None extends Compression { val asString = "NONE" }
final case object Gzip extends Compression { val asString = "GZIP" }
}
}
final case class Formats(default: LoaderMessage.Format,
tsv: List[SchemaCriterion],
json: List[SchemaCriterion],
skip: List[SchemaCriterion]) {
/** Find if there are overlapping criterions in any two of known three groups */
def findOverlaps: Set[SchemaCriterion] =
Formats.findOverlaps(tsv, json) ++
Formats.findOverlaps(json, skip) ++
Formats.findOverlaps(skip, tsv)
}
object Formats {
val Default: Formats = Formats(LoaderMessage.Format.TSV, Nil, Nil, Nil)
/** Find all criterion overlaps in two lists */
def findOverlaps(as: List[SchemaCriterion], bs: List[SchemaCriterion]): Set[SchemaCriterion] =
as.flatMap(a => bs.map(b => (a, b))).foldLeft(Set.empty[SchemaCriterion])(aggregateMatching(overlap))
/** Check if two criterions can have a potential overlap, i.e. a schema belongs to two groups */
def overlap(a: SchemaCriterion, b: SchemaCriterion): Boolean = (a, b) match {
case (SchemaCriterion(av, an, _, am, ar, aa), SchemaCriterion(bv, bn, _, bm, br, ba)) =>
av == bv && an == bn && versionOverlap(am, bm) && versionOverlap(ar, br) && versionOverlap(aa, ba)
}
/** Check if two version numbers (MODEL, REVISION or ADDITION) can overlap */
private def versionOverlap(av: Option[Int], bv: Option[Int]): Boolean = (av, bv) match {
case (Some(aam), Some(bbm)) if aam == bbm => true // Identical and explicit - overlap
case (Some(_), Some(_)) => false // Different and explicit
case _ => true // At least one is a wildcard - overlap
}
/** Accumulate all pairs matching predicate */
def aggregateMatching[A](predicate: (A, A) => Boolean)(acc: Set[A], pair: (A, A)): Set[A] = (acc, pair) match {
case (acc, (a, b)) if predicate(a, b) => acc + a + b
case (acc, _) => acc
}
}
final case class Monitoring(snowplow: Option[SnowplowMonitoring], sentry: Option[Sentry], metrics: Option[Metrics])
final case class SnowplowMonitoring(appId: String, collector: String)
final case class Sentry(dsn: URI)
sealed trait Metrics {
def prefix: String
}
object Metrics {
final case class Stdout(prefix: String) extends Metrics
final case class StatsD(
prefix: String,
hostname: String,
port: Int,
tags: Map[String, String]
) extends Metrics
implicit val stdoutDecoder: Decoder[Metrics.Stdout] =
deriveDecoder[Metrics.Stdout]
implicit val statsdDecoder: Decoder[Metrics.StatsD] =
deriveDecoder[Metrics.StatsD]
implicit val metricsDecoder: Decoder[Metrics] =
Decoder.instance { cur =>
val typeCur = cur.downField("type")
typeCur.as[String].map(_.toLowerCase) match {
case Right("stdout") =>
cur.as[Metrics.Stdout]
case Right("statsd") =>
cur.as[Metrics.StatsD]
case Right(other) =>
Left(DecodingFailure(s"Metrics type '$other' is not supported. Supported types: 'stdout' and 'statsD'", typeCur.history))
case Left(DecodingFailure(_, List(CursorOp.DownField("type")))) =>
Left(DecodingFailure("Cannot find 'type' string in metrics configuration", typeCur.history))
case Left(other) =>
Left(other)
}
}
}
implicit val batchShredderDecoder: Decoder[Shredder.Batch] =
deriveDecoder[Shredder.Batch]
implicit val streamShredderDecoder: Decoder[Shredder.Stream] =
deriveDecoder[Shredder.Stream]
implicit val shredderOutputDecoder: Decoder[Shredder.Output] =
deriveDecoder[Shredder.Output]
implicit val shredderFileInputDecoder: Decoder[Shredder.StreamInput.File] =
deriveDecoder[Shredder.StreamInput.File]
implicit val shredderKinesisInputDecoder: Decoder[Shredder.StreamInput.Kinesis] =
deriveDecoder[Shredder.StreamInput.Kinesis]
implicit val shredderStreamInputDecoder: Decoder[Shredder.StreamInput] =
Decoder.instance { cur =>
val typeCur = cur.downField("type")
typeCur.as[String].map(_.toLowerCase) match {
case Right("file") =>
cur.as[Shredder.StreamInput.File]
case Right("kinesis") =>
cur.as[Shredder.StreamInput.Kinesis]
case Right(other) =>
Left(DecodingFailure(s"Shredder input type $other is not supported yet. Supported types: 'kinesis', 's3' and 'file'", typeCur.history))
case Left(DecodingFailure(_, List(CursorOp.DownField("type")))) =>
Left(DecodingFailure("Cannot find 'type' string in shredder configuration", typeCur.history))
case Left(other) =>
Left(other)
}
}
implicit val shredderDecoder: Decoder[Shredder] =
Decoder.instance { cur =>
val typeCur = cur.downField("type")
typeCur.as[String].map(_.toLowerCase) match {
case Right("batch") =>
cur.as[Shredder.Batch]
case Right("stream") =>
cur.as[Shredder.Stream]
case Right(other) =>
Left(DecodingFailure(s"Shredder input type $other is not supported yet. Supported types: 'batch' and 'stream'", typeCur.history))
case Left(DecodingFailure(_, List(CursorOp.DownField("type")))) =>
Left(DecodingFailure("Cannot find 'type' string in shredder configuration", typeCur.history))
case Left(other) =>
Left(other)
}
}
implicit val durationDecoder: Decoder[Duration] =
Decoder[String].emap(s => Either.catchOnly[NumberFormatException](Duration(s)).leftMap(_.toString))
implicit val uriDecoder: Decoder[URI] =
Decoder[String].emap(s => Either.catchOnly[IllegalArgumentException](URI.create(s)).leftMap(_.toString))
implicit val formatsDecoder: Decoder[Formats] =
deriveDecoder[Formats]
implicit val sentryDecoder: Decoder[Sentry] =
deriveDecoder[Sentry]
implicit val snowplowMonitoringDecoder: Decoder[SnowplowMonitoring] =
deriveDecoder[SnowplowMonitoring]
implicit val monitoringDecoder: Decoder[Monitoring] =
deriveDecoder[Monitoring]
implicit val decodeOutputCompression: Decoder[Shredder.Compression] =
StringEnum.decodeStringEnum[Shredder.Compression]
implicit val encodeOutputCompression: Encoder[Shredder.Compression] =
Encoder.instance(_.toString.toUpperCase.asJson)
implicit val configDecoder: Decoder[Config[StorageTarget]] =
deriveDecoder[Config[StorageTarget]]
val formats: Lens[Config[StorageTarget], Formats] = GenLens[Config[StorageTarget]](_.formats)
val monitoring: Lens[Config[StorageTarget], Monitoring] = GenLens[Config[StorageTarget]](_.monitoring)
}