/
TamerRestJob.scala
324 lines (286 loc) · 14.6 KB
/
TamerRestJob.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package tamer.rest
import com.sksamuel.avro4s.Codec
import log.effect.LogWriter
import log.effect.zio.ZioLogWriter.log4sFromName
import sttp.client3.httpclient.zio.{SttpClient, send}
import sttp.client3.{Request, Response, UriContext, basicRequest}
import sttp.model.StatusCode.{Forbidden, NotFound, Unauthorized}
import tamer.config.KafkaConfig
import tamer.job.AbstractTamerJob
import tamer.rest.LocalSecretCache.LocalSecretCache
import tamer.{AvroCodec, HashableState, TamerError}
import zio.clock.Clock
import zio.console.Console
import zio.duration.{durationInt, Duration => ZDuration}
import zio.{Chunk, Has, Queue, RIO, Ref, Schedule, Task, UIO, ULayer, URIO, ZEnv, ZIO, ZLayer, clock}
import java.time.Instant
import scala.annotation.{nowarn, unused}
import scala.concurrent.duration.Duration
import scala.util.hashing.MurmurHash3.stringHash
trait Authentication[-R] {
def addAuthentication(request: SttpRequest, supplementalSecret: Option[String]): SttpRequest
def setSecret(@unused secretRef: Ref[Option[String]]): ZIO[R, TamerError, Unit] = UIO(())
// TODO: could return the value to set instead of unit
def refreshSecret(secretRef: Ref[Option[String]]): ZIO[R, TamerError, Unit] = setSecret(secretRef)
// TODO: widen error since this is user space
}
object Authentication {
def basic[R](username: String, password: String): Some[Authentication[R]] =
Some((request: SttpRequest, _: Option[String]) => request.auth.basic(username, password))
}
object LocalSecretCache {
type LocalSecretCache = Has[Ref[Option[String]]]
val localSecretCacheM: UIO[Ref[Option[String]]] = Ref.make[Option[String]](None)
val live: ULayer[LocalSecretCache] = ZLayer.fromEffect(localSecretCacheM)
}
object TamerRestJob {
case class Offset(offset: Int, nextIndex: Int) {
def incrementedBy(increment: Int): Offset = this.copy(offset = offset + increment, nextIndex = 0)
def nextIndex(index: Int): Offset = this.copy(nextIndex = index)
}
object Offset {
implicit val codec = AvroCodec.codec[Offset]
implicit val hashableState: HashableState[Offset] = new HashableState[Offset] {
/** It is required for this hash to be consistent even across executions
* for the same semantic state. This is in contrast with the built-in
* `hashCode` method.
*/
override def stateHash(s: Offset): Int = s.offset * s.nextIndex
}
}
case class PeriodicOffset(offset: Int, periodStart: Instant) {
def incrementedBy(increment: Int): PeriodicOffset = this.copy(offset = offset + increment)
}
object PeriodicOffset {
implicit val codec = AvroCodec.codec[PeriodicOffset]
implicit val hashableState: HashableState[PeriodicOffset] = new HashableState[PeriodicOffset] {
/** It is required for this hash to be consistent even across executions
* for the same semantic state. This is in contrast with the built-in
* `hashCode` method.
*/
override def stateHash(s: PeriodicOffset): Int = s.offset * (s.periodStart.getEpochSecond + s.periodStart.getNano.longValue).toInt
}
}
def apply[
R <: ZEnv with SttpClient with KafkaConfig with LocalSecretCache,
K: Codec,
V: Codec,
S: Codec
](
setup: RestConfiguration[R, K, V, S]
) = new TamerRestJob[R, K, V, S](setup)
def withPagination[
R <: ZEnv with SttpClient with KafkaConfig with LocalSecretCache,
K: Codec,
V: Codec
](
baseUrl: String,
pageDecoder: String => RIO[R, DecodedPage[V, Offset]],
offsetParameterName: String = "page",
increment: Int = 1,
fixedPageElementCount: Option[Int] = None,
authenticationMethod: Option[Authentication[R]] = None,
readRequestTimeout: zio.duration.Duration = 30.seconds
)(deriveKafkaRecordKey: (Offset, V) => K): TamerRestJob[R, K, V, Offset] = {
val queryBuilder: RestQueryBuilder[R, Offset] = new RestQueryBuilder[R, Offset] {
/** Used for hashing purposes
*/
override val queryId: Int = stringHash(baseUrl + offsetParameterName) + increment
override def query(state: Offset): Request[Either[String, String], Any] =
basicRequest
.get(uri"$baseUrl".addParam(offsetParameterName, state.offset.toString))
.readTimeout(Duration.fromNanos(readRequestTimeout.toNanos))
override val authentication: Option[Authentication[R]] = authenticationMethod
}
def nextPageOrNextIndexIfPageNotComplete(decodedPage: DecodedPage[V, Offset], offset: Offset): UIO[Offset] = {
val fetchedElementCount = decodedPage.data.length
val nextElementIndex = decodedPage.data.length
lazy val defaultNextState = fixedPageElementCount match {
case Some(expectedElemCount) if fetchedElementCount <= expectedElemCount - 1 => UIO(offset.nextIndex(nextElementIndex))
case _ => UIO(offset.incrementedBy(increment))
}
decodedPage.nextState.map(UIO(_)).getOrElse(defaultNextState)
}
def filterPage(decodedPage: DecodedPage[V, Offset], offset: Offset): List[V] = {
val previousElementCount = offset.nextIndex
decodedPage.data.drop(previousElementCount)
}
@nowarn
val transitions: RestConfiguration.State[R, K, V, Offset] =
new RestConfiguration.State(Offset(0, 0))(deriveKafkaRecordKey)(nextPageOrNextIndexIfPageNotComplete, filterPage)
val setup = new RestConfiguration(queryBuilder = queryBuilder, pageDecoder = pageDecoder)(transitions = transitions)
new TamerRestJob[R, K, V, Offset](setup) {
override protected def next(currentState: Offset, q: Queue[Chunk[(K, V)]]): ZIO[R, TamerError, Offset] = {
val logic: RIO[R with SttpClient with LocalSecretCache, Offset] = for {
log <- this.logTask
tokenCache <- ZIO.service[Ref[Option[String]]]
decodedPage <- fetchWaitingNewEntries(currentState, log, tokenCache)
chunk = Chunk.fromIterable(
setup.transitions.filterPage(decodedPage, currentState).map(value => (setup.transitions.deriveKafkaRecordKey(currentState, value), value))
)
_ <- q.offer(chunk)
nextState <- setup.transitions.getNextState(decodedPage, currentState)
} yield nextState
logic.mapError(e => TamerError(e.getLocalizedMessage, e))
}
private final def fetchWaitingNewEntries(
currentState: Offset,
log: LogWriter[Task],
tokenCache: Ref[Option[String]]
): RIO[R with Clock, DecodedPage[V, Offset]] =
fetchAndDecode(currentState, tokenCache, log).repeat(
Schedule.exponential(500.millis) *> Schedule.recurWhile(_.data.isEmpty)
)
private final def fetchAndDecode(currentState: Offset, tokenCache: Ref[Option[String]], log: LogWriter[Task]): RIO[R, DecodedPage[V, Offset]] =
for {
decodedPage <- fetchAndDecodePage(setup.queryBuilder.query(currentState), tokenCache, log)
dataFromIndex = decodedPage.data.drop(currentState.nextIndex)
pageLength = decodedPage.data.length
_ <- log.debug(s"Fetch and Decode retrieved ${dataFromIndex.length} new datapoints. ($pageLength total for page ${currentState.offset})")
} yield decodedPage
}
}
def withPaginationPeriodic[
R <: ZEnv with SttpClient with KafkaConfig with LocalSecretCache,
K: Codec,
V: Codec
](
baseUrl: String,
pageDecoder: String => RIO[R, DecodedPage[V, PeriodicOffset]],
periodStart: Instant,
offsetParameterName: String = "page",
increment: Int = 1,
authenticationMethod: Option[Authentication[R]] = None,
minPeriod: zio.duration.Duration = 5.minutes,
maxPeriod: zio.duration.Duration = 1.hour,
readRequestTimeout: zio.duration.Duration = 30.seconds
)(deriveKafkaRecordKey: (PeriodicOffset, V) => K): TamerRestJob[R, K, V, PeriodicOffset] = {
val queryBuilder: RestQueryBuilder[R with Clock, PeriodicOffset] = new RestQueryBuilder[R, PeriodicOffset] {
/** Used for hashing purposes
*/
override val queryId: Int = stringHash(baseUrl + offsetParameterName) + increment
override def query(state: PeriodicOffset): Request[Either[String, String], Any] =
basicRequest
.get(uri"$baseUrl".addParam(offsetParameterName, state.offset.toString))
.readTimeout(Duration.fromNanos(readRequestTimeout.toNanos))
override val authentication: Option[Authentication[R]] = authenticationMethod
}
def getNextState(decodedPage: DecodedPage[V, PeriodicOffset], periodicOffset: PeriodicOffset): URIO[R with Clock, PeriodicOffset] = {
lazy val defaultNextState: URIO[Clock with Console, PeriodicOffset] = for {
now <- zio.clock.instant
newOffset <-
if (
now.isAfter(periodicOffset.periodStart.plus(maxPeriod)) ||
(decodedPage.data.isEmpty && now.isAfter(periodicOffset.periodStart.plus(minPeriod)))
) {
UIO(PeriodicOffset(offset = 0, periodStart = now))
} else if (decodedPage.data.isEmpty) {
val nextPeriodStart: Instant = periodicOffset.periodStart.plus(minPeriod)
UIO(PeriodicOffset(offset = 0, periodStart = nextPeriodStart))
} else {
UIO(periodicOffset.incrementedBy(increment))
}
} yield newOffset
decodedPage.nextState.map(UIO(_)).getOrElse(defaultNextState)
}
val transitions =
new RestConfiguration.State[R with Clock, K, V, PeriodicOffset](PeriodicOffset(0, periodStart))(deriveKafkaRecordKey)(getNextState)
val setup = new RestConfiguration(queryBuilder = queryBuilder, pageDecoder = pageDecoder)(transitions = transitions)
new TamerRestJob[R with Clock with Console, K, V, PeriodicOffset](setup) {
override protected def next(currentState: PeriodicOffset, q: Queue[Chunk[(K, V)]]): ZIO[R with Clock, TamerError, PeriodicOffset] = {
val logic: RIO[R with SttpClient with LocalSecretCache with Clock with Console, PeriodicOffset] = for {
log <- logTask
tokenCache <- ZIO.service[Ref[Option[String]]]
now <- clock.instant
delayUntilNextPeriod <-
if (currentState.periodStart.isBefore(now))
UIO(ZDuration.Zero)
else
UIO(ZDuration.fromInterval(now, currentState.periodStart)).tap(delayUntilNextPeriod =>
log.info(s"Time until the next period: ${delayUntilNextPeriod.toString}")
)
decodedPage <- fetchAndDecodePage(setup.queryBuilder.query(currentState), tokenCache, log).delay(delayUntilNextPeriod)
chunk = Chunk.fromIterable(
setup.transitions.filterPage(decodedPage, currentState).map(value => (setup.transitions.deriveKafkaRecordKey(currentState, value), value))
)
_ <- q.offer(chunk)
nextState <- setup.transitions.getNextState(decodedPage, currentState)
} yield nextState
logic.mapError(e => TamerError(e.getLocalizedMessage, e))
}
}
}
}
class TamerRestJob[
-R <: ZEnv with SttpClient with KafkaConfig with LocalSecretCache,
K: Codec,
V: Codec,
S: Codec
](
setup: RestConfiguration[R, K, V, S]
) extends AbstractTamerJob[R, K, V, S](setup.generic) {
protected[this] final val logTask: Task[LogWriter[Task]] = log4sFromName.provide("tamer.rest")
protected def fetchAndDecodePage(request: SttpRequest, tokenCacheRef: Ref[Option[String]], log: LogWriter[Task]): RIO[R, DecodedPage[V, S]] = {
def authenticateAndSendRequest(
requestToTransform: SttpRequest,
tokenCacheRef: Ref[Option[String]],
log: LogWriter[Task]
): RIO[R, Response[Either[String, String]]] = {
val auth = setup.queryBuilder.authentication.get // FIXME: unsafe
def authenticateAndSendHelper(maybeToken: Option[String]): ZIO[SttpClient, Throwable, Response[Either[String, String]]] = {
val authenticatedRequest = auth.addAuthentication(requestToTransform, maybeToken)
log.debug(s"Going to execute request $authenticatedRequest") *> send(authenticatedRequest)
}
for {
maybeToken <- tokenCacheRef.get
_ <- maybeToken match {
case Some(_) => UIO.unit // secret is already present do nothing
case None => auth.setSecret(tokenCacheRef)
}
firstResponse <- authenticateAndSendHelper(maybeToken)
response <- firstResponse.code match {
case Forbidden | Unauthorized | NotFound =>
log.warn("Authentication failed, assuming credentials are expired, will retry, possibly refreshing them...") *>
log.debug(s"Response was: ${firstResponse.body}") *>
auth.refreshSecret(tokenCacheRef) *>
tokenCacheRef.get.flatMap(authenticateAndSendHelper)
case _ => UIO(firstResponse) <* log.debug("Authentication succeeded: proceeding as normal")
}
} yield response
}
for {
response <- setup.queryBuilder.authentication match {
case Some(_) => authenticateAndSendRequest(request, tokenCacheRef, log)
case None => send(request)
}
decodedPage <- response.body match {
case Left(error) =>
// assume the cookie/auth expired
log.error(s"""Request failed with error "$error"""") *>
tokenCacheRef.set(None) *> // clear cache
RIO.fail(TamerError("Request failed, giving up."))
case Right(body) =>
setup.pageDecoder(body)
}
} yield decodedPage
}
// TODO: it seems the common pattern for the specializations of this function is to schedule
// `fetchAndDecode` page depending on the current state. Either the execution is delayed
// (for example when we have to repeat all the queries after resetting the offset) or repeated
// (for example when we are waiting new pages to appear). This has to be combined with an
// intuitive filtering of the page result and automatic type inference of the state for
// the page decoder helper functions.
override protected def next(currentState: S, q: Queue[Chunk[(K, V)]]): ZIO[R, TamerError, S] = {
val logic: RIO[R with SttpClient with LocalSecretCache, S] = for {
log <- logTask
tokenCache <- ZIO.service[Ref[Option[String]]]
decodedPage <- fetchAndDecodePage(setup.queryBuilder.query(currentState), tokenCache, log)
chunk = Chunk.fromIterable(
setup.transitions.filterPage(decodedPage, currentState).map(value => (setup.transitions.deriveKafkaRecordKey(currentState, value), value))
)
_ <- q.offer(chunk)
nextState <- setup.transitions.getNextState(decodedPage, currentState)
} yield nextState
logic.mapError(e => TamerError(e.getLocalizedMessage, e))
}
}