This repository has been archived by the owner on May 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
EventsByPersistenceIdSource.scala
221 lines (189 loc) · 7.6 KB
/
EventsByPersistenceIdSource.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
/*
* Copyright © 2017 Safety Data - CFH SAS.
*
* 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 akka
package persistence
package query
package journal
package redis
import akka.persistence.redis._
import RedisKeys._
import _root_.redis._
import api._
import pubsub._
import akka.actor._
import akka.util._
import SupervisorStrategy._
import akka.stream._
import akka.stream.stage._
import akka.serialization.SerializationExtension
import scala.concurrent.duration._
import scala.collection.mutable.Queue
import scala.util.{
Try,
Success,
Failure
}
import scala.reflect._
import com.typesafe.config.Config
private class EventsByPersistenceIdSource(conf: Config, redis: RedisClient, persistenceId: String, from: Long, to: Long, system: ActorSystem, live: Boolean) extends GraphStage[SourceShape[EventEnvelope]] {
val out: Outlet[EventEnvelope] =
if (live)
Outlet("EventsByPersistenceIdSource")
else
Outlet("CurrentEventsByPersistenceIdSource")
override val shape: SourceShape[EventEnvelope] = SourceShape(out)
case class EventRef(sequenceNr: Long, persistenceId: String)
// The logic class is in one of the following states:
// - Waiting for client request
val Idle = 0
// - Buffer was empty so database query was sent
val Querying = 1
// - Database query is running and notification about new event arrived
val NotifiedWhenQuerying = 2
// - Client requested element but no new one in database, waiting for notification
val WaitingForNotification = 3
implicit object eventRefDeserializer extends ByteStringDeserializer[EventRef] {
private val EventRe = "(\\d+):(.*)".r
def deserialize(bs: ByteString): EventRef = bs.utf8String match {
case EventRe(sequenceNr, persistenceId) => EventRef(sequenceNr.toLong, persistenceId)
case s => throw new RuntimeException(f"Unable to deserializer $s")
}
}
object Long {
def unapply(bs: ByteString): Option[Long] =
Try(bs.utf8String.toLong).toOption
}
val Channel = journalChannel(persistenceId)
implicit val serialization = SerializationExtension(system)
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
new GraphStageLogicWithLogging(shape) {
private var state = Idle
private var buffer = Queue.empty[EventEnvelope]
private var subscription: RedisPubSub = null
private val max = conf.getInt("max")
private var currentSequenceNr = from
private var callback: AsyncCallback[Seq[PersistentRepr]] = null
implicit def ec = materializer.executionContext
override def preStart(): Unit = {
callback = getAsyncCallback[Seq[PersistentRepr]] { events =>
if (events.isEmpty) {
state match {
case NotifiedWhenQuerying =>
// maybe we missed some new event when querying, retry
query()
case Querying =>
if (live) {
// nothing new, wait for notification
state = WaitingForNotification
} else {
// not a live stream, nothing else currently in the database, close the stream
completeStage()
}
case _ =>
log.error(f"Unexpected source state: $state")
failStage(new IllegalStateException(f"Unexpected source state: $state"))
}
} else {
val (evts, maxSequenceNr) = events.foldLeft(Seq.empty[EventEnvelope] -> currentSequenceNr) {
case ((evts, _), repr @ PersistentRepr(event, sequenceNr)) if !repr.deleted && sequenceNr >= currentSequenceNr && sequenceNr <= to =>
(evts :+ EventEnvelope(Sequence(sequenceNr), persistenceId, sequenceNr, event), sequenceNr + 1)
case ((evts, _), PersistentRepr(_, sequenceNr)) =>
(evts, sequenceNr + 1)
}
currentSequenceNr = maxSequenceNr
log.debug(f"Max sequence number is now $maxSequenceNr")
if (evts.nonEmpty) {
buffer.enqueue(evts: _*)
deliver()
} else {
// requery immediately
state = Idle
query()
}
}
}
if (live) {
// subscribe to notification stream only if live stream was required
val messageCallback = getAsyncCallback[Message] {
case Message(Channel, Long(sequenceNr)) =>
log.debug("Message received")
state match {
case Idle =>
// do nothing, no query is running and no client request was performed
case Querying =>
state = NotifiedWhenQuerying
case NotifiedWhenQuerying =>
// do nothing we already know that some new events may exist
case WaitingForNotification =>
state = Idle
query()
}
case Message(ch, _) =>
if (log.isDebugEnabled)
log.debug(f"Message from unexpected channel: $ch")
}
// subscribe to the identifier change channel to be notifier about new ones
// and invoke the enqueuing and delivering callback on each message
subscription = RedisPubSub(host = RedisUtils.host(conf),
port = RedisUtils.port(conf),
channels = Seq(Channel),
patterns = Nil,
authPassword = RedisUtils.password(conf),
onMessage = messageCallback.invoke)(system)
}
}
override def postStop(): Unit = if (subscription != null) {
subscription.stop()
}
private val StringSeq = classTag[Seq[String]]
setHandler(out, new OutHandler {
override def onPull(): Unit = {
query()
}
})
private def query(): Unit =
state match {
case Idle =>
if (buffer.isEmpty) {
// so, we need to fill this buffer
state = Querying
redis.zrangebyscore[Array[Byte]](journalKey(persistenceId), Limit(currentSequenceNr), Limit(math.min(currentSequenceNr + max - 1, to))).onComplete {
case Success(events) =>
callback.invoke(events.map(persistentFromBytes(_)))
case Failure(t) =>
log.error(t, "Error while querying events by persistence identifier")
failStage(t)
}
} else {
// buffer is non empty, let’s deliver buffered data
deliver()
}
case _ =>
log.error(f"Unexpected source state when querying: $state")
failStage(new IllegalStateException(f"Unexpected source state when querying: $state"))
}
private def deliver(): Unit = {
// go back to idle state, waiting for more client request
state = Idle
val elem = buffer.dequeue
push(out, elem)
if (buffer.isEmpty && currentSequenceNr > to) {
// we delivered last buffered event and the upper bound was reached, complete
completeStage()
}
}
}
}