2009-03-23 19:17:10 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009 Scalable Solutions.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package se.scalablesolutions.akka.kernel
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.atomic.AtomicLong
|
|
|
|
|
import scala.collection.mutable.HashMap
|
|
|
|
|
|
|
|
|
|
sealed abstract case class TransactionStatus
|
|
|
|
|
object TransactionStatus {
|
|
|
|
|
case object New extends TransactionStatus
|
|
|
|
|
case object Active extends TransactionStatus
|
|
|
|
|
case object Aborted extends TransactionStatus
|
|
|
|
|
case object Completed extends TransactionStatus
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Represents a snapshot of the current invocation.
|
|
|
|
|
*
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
|
|
|
|
object TransactionIdFactory {
|
2009-03-26 20:22:49 +01:00
|
|
|
// FIXME: will not work in distributed env
|
2009-03-23 19:17:10 +01:00
|
|
|
private val currentId = new AtomicLong(0L)
|
|
|
|
|
def newId = currentId.getAndIncrement
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Represents a snapshot of the current invocation.
|
|
|
|
|
*
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
|
|
|
|
class Transaction extends Logging {
|
|
|
|
|
val id = TransactionIdFactory.newId
|
2009-03-26 20:22:49 +01:00
|
|
|
|
|
|
|
|
log.debug("Creating a new transaction [%s]", id)
|
2009-03-23 19:17:10 +01:00
|
|
|
private[this] var parent: Option[Transaction] = None
|
2009-04-09 15:49:42 +02:00
|
|
|
private[this] var participants: List[GenericServerContainer] = Nil
|
2009-03-23 19:17:10 +01:00
|
|
|
private[this] var precommitted: List[GenericServerContainer] = Nil
|
|
|
|
|
@volatile private[this] var status: TransactionStatus = TransactionStatus.New
|
|
|
|
|
|
|
|
|
|
def begin(server: GenericServerContainer) = synchronized {
|
2009-04-09 15:49:42 +02:00
|
|
|
println("===== begin 1 " + server)
|
2009-03-23 19:17:10 +01:00
|
|
|
if (status == TransactionStatus.Aborted) throw new IllegalStateException("Can't begin ABORTED transaction")
|
|
|
|
|
if (status == TransactionStatus.Completed) throw new IllegalStateException("Can't begin COMPLETED transaction")
|
|
|
|
|
if (status == TransactionStatus.New) log.debug("Actor [%s] is starting NEW transaction", server)
|
|
|
|
|
else log.debug("Actor [%s] is participating in transaction", server)
|
2009-04-09 15:49:42 +02:00
|
|
|
println("===== begin 2 " + server)
|
|
|
|
|
server.states.foreach(_.begin)
|
|
|
|
|
participants ::= server
|
2009-03-23 19:17:10 +01:00
|
|
|
status = TransactionStatus.Active
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def precommit(server: GenericServerContainer) = synchronized {
|
2009-03-26 20:22:49 +01:00
|
|
|
if (status == TransactionStatus.Active) {
|
2009-04-09 15:49:42 +02:00
|
|
|
println("===== precommit " + server)
|
2009-03-26 20:22:49 +01:00
|
|
|
log.debug("Pre-committing transaction for actor [%s]", server)
|
|
|
|
|
precommitted ::= server
|
|
|
|
|
}
|
2009-03-23 19:17:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def commit(server: GenericServerContainer) = synchronized {
|
2009-03-26 20:22:49 +01:00
|
|
|
if (status == TransactionStatus.Active) {
|
2009-04-09 15:49:42 +02:00
|
|
|
println("===== commit " + server)
|
2009-03-26 20:22:49 +01:00
|
|
|
log.debug("Committing transaction for actor [%s]", server)
|
|
|
|
|
val haveAllPreCommitted =
|
2009-04-04 21:34:10 +02:00
|
|
|
if (participants.size == precommitted.size) {{
|
2009-04-09 15:49:42 +02:00
|
|
|
for (server <- participants) yield {
|
2009-03-26 20:22:49 +01:00
|
|
|
if (precommitted.exists(_.id == server.id)) true
|
|
|
|
|
else false
|
|
|
|
|
}}.exists(_ == false)
|
|
|
|
|
} else false
|
|
|
|
|
if (haveAllPreCommitted) status = TransactionStatus.Completed
|
|
|
|
|
else rollback(server)
|
|
|
|
|
}
|
2009-03-23 19:17:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def rollback(server: GenericServerContainer) = synchronized {
|
2009-03-26 20:22:49 +01:00
|
|
|
ensureIsActiveOrAborted
|
2009-04-09 15:49:42 +02:00
|
|
|
println("===== rollback " + server)
|
|
|
|
|
log.debug("Actor [%s] has initiated transaction rollback, rolling back [%s]" , server, participants)
|
|
|
|
|
participants.foreach(_.states.foreach(_.rollback))
|
2009-03-23 19:17:10 +01:00
|
|
|
status = TransactionStatus.Aborted
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-09 15:49:42 +02:00
|
|
|
def join(server: GenericServerContainer) = synchronized {
|
|
|
|
|
println("===== joining " + server)
|
|
|
|
|
server.states.foreach(_.begin)
|
|
|
|
|
participants ::= server
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-26 20:22:49 +01:00
|
|
|
private def ensureIsActive = if (status != TransactionStatus.Active)
|
2009-03-23 19:17:10 +01:00
|
|
|
throw new IllegalStateException("Expected ACTIVE transaction - current status [" + status + "]")
|
|
|
|
|
|
2009-03-26 20:22:49 +01:00
|
|
|
private def ensureIsActiveOrAborted =
|
|
|
|
|
if (!(status == TransactionStatus.Active || status == TransactionStatus.Aborted))
|
|
|
|
|
throw new IllegalStateException("Expected ACTIVE or ABORTED transaction - current status [" + status + "]")
|
|
|
|
|
|
2009-03-23 19:17:10 +01:00
|
|
|
override def equals(that: Any): Boolean =
|
|
|
|
|
that != null &&
|
|
|
|
|
that.isInstanceOf[Transaction] &&
|
|
|
|
|
that.asInstanceOf[Transaction].id == this.id
|
|
|
|
|
|
|
|
|
|
override def hashCode(): Int = id.toInt
|
|
|
|
|
|
|
|
|
|
override def toString(): String = "Transaction[" + id + ", " + status + "]"
|
|
|
|
|
}
|