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
|
|
|
|
|
import akka.pattern.{ pipe, PromiseActorRef }
|
|
|
|
|
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 = {
|
|
|
|
|
case Write(persistent, processor) ⇒ {
|
|
|
|
|
val sdr = if (sender.isInstanceOf[PromiseActorRef]) context.system.deadLetters else sender
|
2013-10-27 08:01:14 +01:00
|
|
|
Try(write(persistent.prepareWrite(sdr))) match {
|
2013-10-08 11:46:02 +02:00
|
|
|
case Success(_) ⇒ processor forward WriteSuccess(persistent)
|
|
|
|
|
case Failure(e) ⇒ processor forward WriteFailure(persistent, e); throw e
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-27 08:01:14 +01:00
|
|
|
case WriteBatch(persistentBatch, processor) ⇒ {
|
|
|
|
|
val sdr = if (sender.isInstanceOf[PromiseActorRef]) context.system.deadLetters else sender
|
|
|
|
|
Try(writeBatch(persistentBatch.map(_.prepareWrite(sdr)))) match {
|
|
|
|
|
case Success(_) ⇒ persistentBatch.foreach(processor forward WriteSuccess(_))
|
|
|
|
|
case Failure(e) ⇒ persistentBatch.foreach(processor forward WriteFailure(_, e)); throw e
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-08 11:46:02 +02:00
|
|
|
case Replay(fromSequenceNr, toSequenceNr, processorId, processor) ⇒ {
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
case c @ Confirm(processorId, sequenceNr, channelId) ⇒ {
|
|
|
|
|
confirm(processorId, sequenceNr, channelId)
|
2013-11-12 09:02:02 +01:00
|
|
|
if (extension.publishPluginCommands) context.system.eventStream.publish(c)
|
2013-10-08 11:46:02 +02:00
|
|
|
}
|
2013-11-12 09:02:02 +01:00
|
|
|
case d @ Delete(processorId, fromSequenceNr, toSequenceNr, permanent) ⇒ {
|
|
|
|
|
delete(processorId, fromSequenceNr, toSequenceNr, permanent)
|
|
|
|
|
if (extension.publishPluginCommands) context.system.eventStream.publish(d)
|
2013-10-08 11:46:02 +02:00
|
|
|
}
|
|
|
|
|
case Loop(message, processor) ⇒ {
|
|
|
|
|
processor forward LoopSuccess(message)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//#journal-plugin-api
|
|
|
|
|
/**
|
2013-11-07 10:45:02 +01:00
|
|
|
* Plugin API: synchronously writes a `persistent` message to the journal.
|
2013-10-08 11:46:02 +02:00
|
|
|
*/
|
2013-11-07 10:45:02 +01:00
|
|
|
def write(persistent: PersistentRepr): Unit
|
2013-10-08 11:46:02 +02:00
|
|
|
|
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-07 10:45:02 +01:00
|
|
|
def writeBatch(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
|
|
|
|
|
}
|