2013-10-08 11:46:02 +02:00
|
|
|
/**
|
2013-11-07 10:45:02 +01:00
|
|
|
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
2013-10-08 11:46:02 +02:00
|
|
|
* Copyright (C) 2012-2013 Eligotech BV.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.persistence.journal
|
|
|
|
|
|
2013-10-27 08:01:14 +01:00
|
|
|
import scala.collection.immutable
|
2013-10-08 11:46:02 +02:00
|
|
|
import scala.util._
|
|
|
|
|
|
|
|
|
|
import akka.actor.Actor
|
2013-12-06 12:48:44 +01:00
|
|
|
import akka.pattern.pipe
|
2013-10-08 11:46:02 +02:00
|
|
|
import akka.persistence._
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Abstract journal, optimized for synchronous writes.
|
|
|
|
|
*/
|
|
|
|
|
trait SyncWriteJournal extends Actor with AsyncReplay {
|
|
|
|
|
import JournalProtocol._
|
|
|
|
|
import context.dispatcher
|
|
|
|
|
|
|
|
|
|
private val extension = Persistence(context.system)
|
|
|
|
|
|
|
|
|
|
final def receive = {
|
2013-11-20 13:47:42 +01:00
|
|
|
case WriteBatch(persistentBatch, processor) ⇒
|
|
|
|
|
Try(write(persistentBatch.map(_.prepareWrite()))) match {
|
|
|
|
|
case Success(_) ⇒
|
|
|
|
|
processor ! WriteBatchSuccess
|
|
|
|
|
persistentBatch.foreach(p ⇒ processor.tell(WriteSuccess(p), p.sender))
|
|
|
|
|
case Failure(e) ⇒
|
|
|
|
|
processor ! WriteBatchFailure(e)
|
|
|
|
|
persistentBatch.foreach(p ⇒ processor tell (WriteFailure(p, e), p.sender))
|
|
|
|
|
throw e
|
2013-10-08 11:46:02 +02:00
|
|
|
}
|
2013-11-20 13:47:42 +01:00
|
|
|
case Replay(fromSequenceNr, toSequenceNr, processorId, processor) ⇒
|
2013-10-08 11:46:02 +02:00
|
|
|
replayAsync(processorId, fromSequenceNr, toSequenceNr) { p ⇒
|
2013-10-09 13:11:53 +02:00
|
|
|
if (!p.deleted) processor.tell(Replayed(p), p.sender)
|
2013-10-08 11:46:02 +02:00
|
|
|
} map {
|
|
|
|
|
maxSnr ⇒ ReplaySuccess(maxSnr)
|
|
|
|
|
} recover {
|
|
|
|
|
case e ⇒ ReplayFailure(e)
|
|
|
|
|
} pipeTo (processor)
|
2013-12-06 12:48:44 +01:00
|
|
|
case c @ Confirm(processorId, messageSequenceNr, channelId, wrapperSequenceNr, channelEndpoint) ⇒
|
|
|
|
|
if (wrapperSequenceNr == 0L) {
|
|
|
|
|
// A wrapperSequenceNr == 0L means that the corresponding message was delivered by a
|
|
|
|
|
// transient channel. We can now write a delivery confirmation for this message.
|
|
|
|
|
confirm(processorId, messageSequenceNr, channelId)
|
|
|
|
|
} else {
|
|
|
|
|
// A wrapperSequenceNr != 0L means that the corresponding message was delivered by a
|
|
|
|
|
// persistent channel. We can now safely delete the wrapper message (that contains the
|
|
|
|
|
// delivered message).
|
|
|
|
|
delete(channelId, wrapperSequenceNr, wrapperSequenceNr, true)
|
|
|
|
|
}
|
|
|
|
|
if (channelEndpoint != null) channelEndpoint ! c
|
2013-11-12 09:02:02 +01:00
|
|
|
if (extension.publishPluginCommands) context.system.eventStream.publish(c)
|
2013-11-20 13:47:42 +01:00
|
|
|
case d @ Delete(processorId, fromSequenceNr, toSequenceNr, permanent) ⇒
|
2013-11-12 09:02:02 +01:00
|
|
|
delete(processorId, fromSequenceNr, toSequenceNr, permanent)
|
|
|
|
|
if (extension.publishPluginCommands) context.system.eventStream.publish(d)
|
2013-11-20 13:47:42 +01:00
|
|
|
case Loop(message, processor) ⇒
|
2013-10-08 11:46:02 +02:00
|
|
|
processor forward LoopSuccess(message)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//#journal-plugin-api
|
2013-10-27 08:01:14 +01:00
|
|
|
/**
|
2013-11-07 10:45:02 +01:00
|
|
|
* Plugin API: synchronously writes a batch of persistent messages to the journal.
|
|
|
|
|
* The batch write must be atomic i.e. either all persistent messages in the batch
|
|
|
|
|
* are written or none.
|
2013-10-27 08:01:14 +01:00
|
|
|
*/
|
2013-11-20 13:47:42 +01:00
|
|
|
def write(persistentBatch: immutable.Seq[PersistentRepr]): Unit
|
2013-10-27 08:01:14 +01:00
|
|
|
|
2013-10-08 11:46:02 +02:00
|
|
|
/**
|
2013-11-12 09:02:02 +01:00
|
|
|
* Plugin API: synchronously deletes all persistent messages within the range from
|
|
|
|
|
* `fromSequenceNr` to `toSequenceNr` (both inclusive). If `permanent` is set to
|
|
|
|
|
* `false`, the persistent messages are marked as deleted, otherwise they are
|
|
|
|
|
* permanently deleted.
|
|
|
|
|
*
|
|
|
|
|
* @see [[AsyncReplay]]
|
2013-10-08 11:46:02 +02:00
|
|
|
*/
|
2013-11-12 09:02:02 +01:00
|
|
|
def delete(processorId: String, fromSequenceNr: Long, toSequenceNr: Long, permanent: Boolean): Unit
|
2013-10-08 11:46:02 +02:00
|
|
|
|
|
|
|
|
/**
|
2013-11-07 10:45:02 +01:00
|
|
|
* Plugin API: synchronously writes a delivery confirmation to the journal.
|
2013-10-08 11:46:02 +02:00
|
|
|
*/
|
|
|
|
|
def confirm(processorId: String, sequenceNr: Long, channelId: String): Unit
|
|
|
|
|
//#journal-plugin-api
|
|
|
|
|
}
|