2009-03-23 19:17:10 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009 Scalable Solutions.
|
|
|
|
|
*/
|
|
|
|
|
|
2009-09-02 09:10:21 +02:00
|
|
|
package se.scalablesolutions.akka.stm
|
2009-03-23 19:17:10 +01:00
|
|
|
|
2009-10-06 00:07:27 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicLong
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger
|
|
|
|
|
|
2009-10-08 19:01:04 +02:00
|
|
|
import se.scalablesolutions.akka.dispatch.MessageInvocation
|
2009-10-06 00:07:27 +02:00
|
|
|
import se.scalablesolutions.akka.util.Logging
|
|
|
|
|
import se.scalablesolutions.akka.state.Committable
|
2009-08-04 16:46:51 +02:00
|
|
|
|
2009-08-15 22:44:29 +02:00
|
|
|
import org.multiverse.api.{Transaction => MultiverseTransaction}
|
|
|
|
|
import org.multiverse.stms.alpha.AlphaStm
|
2009-08-04 16:46:51 +02:00
|
|
|
import org.multiverse.utils.GlobalStmInstance
|
|
|
|
|
import org.multiverse.utils.TransactionThreadLocal._
|
2009-09-10 01:33:01 +02:00
|
|
|
import org.multiverse.templates.{OrElseTemplate, AtomicTemplate}
|
2009-08-04 16:46:51 +02:00
|
|
|
|
2009-10-06 00:07:27 +02:00
|
|
|
import scala.collection.mutable.HashMap
|
|
|
|
|
|
|
|
|
|
class TransactionRetryException(message: String) extends RuntimeException(message)
|
2009-08-04 16:46:51 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
2009-08-15 22:44:29 +02:00
|
|
|
object Multiverse {
|
|
|
|
|
val STM = new AlphaStm
|
|
|
|
|
GlobalStmInstance.set(STM)
|
2009-08-04 16:46:51 +02:00
|
|
|
setThreadLocalTransaction(null)
|
|
|
|
|
}
|
2009-09-10 01:33:01 +02:00
|
|
|
|
|
|
|
|
/**
|
2009-09-13 12:20:44 +02:00
|
|
|
* Example of atomic transaction management.
|
|
|
|
|
* <pre>
|
|
|
|
|
* import se.scalablesolutions.akka.stm.Transaction._
|
|
|
|
|
* Atomic {
|
|
|
|
|
* .. // do something within a transaction
|
|
|
|
|
* }
|
|
|
|
|
* </pre>
|
|
|
|
|
*
|
2009-09-10 01:33:01 +02:00
|
|
|
* Example of Or-Else transaction management.
|
|
|
|
|
* <pre>
|
2009-09-13 12:20:44 +02:00
|
|
|
* import se.scalablesolutions.akka.stm.Transaction._
|
|
|
|
|
* Or {
|
2009-09-10 01:33:01 +02:00
|
|
|
* .. // try to do something
|
2009-09-13 12:20:44 +02:00
|
|
|
* } Else {
|
2009-09-10 01:33:01 +02:00
|
|
|
* .. // if transaction clashes try do do something else to minimize contention
|
|
|
|
|
* }
|
|
|
|
|
* </pre>
|
|
|
|
|
*
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
|
|
|
|
object Transaction {
|
2009-09-17 09:47:22 +02:00
|
|
|
val idFactory = new AtomicLong(-1L)
|
2009-09-10 01:33:01 +02:00
|
|
|
|
|
|
|
|
// -- Monad --------------------------
|
|
|
|
|
|
2009-09-13 12:20:44 +02:00
|
|
|
// -- atomic block --------------------------
|
|
|
|
|
def Atomic[T](body: => T) = new AtomicTemplate[T]() {
|
|
|
|
|
def execute(t: MultiverseTransaction): T = body
|
|
|
|
|
}.execute()
|
|
|
|
|
|
2009-09-10 01:33:01 +02:00
|
|
|
|
|
|
|
|
// -- OrElse --------------------------
|
|
|
|
|
def Or[A](orBody: => A) = elseBody(orBody)
|
|
|
|
|
def elseBody[A](orBody: => A) = new {
|
|
|
|
|
def Else(elseBody: => A) = new OrElseTemplate[A] {
|
|
|
|
|
def run(t: MultiverseTransaction) = orBody
|
|
|
|
|
def orelserun(t: MultiverseTransaction) = elseBody
|
2009-09-13 12:20:44 +02:00
|
|
|
}.execute()
|
2009-09-10 01:33:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
2009-10-08 19:01:04 +02:00
|
|
|
|
2009-08-04 16:46:51 +02:00
|
|
|
/**
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
|
|
|
|
@serializable class Transaction extends Logging {
|
2009-09-13 12:20:44 +02:00
|
|
|
val id = Transaction.idFactory.incrementAndGet
|
2009-08-04 16:46:51 +02:00
|
|
|
@volatile private[this] var status: TransactionStatus = TransactionStatus.New
|
2009-09-10 01:33:01 +02:00
|
|
|
private[akka] var transaction: MultiverseTransaction = _
|
2009-08-04 16:46:51 +02:00
|
|
|
|
2009-10-06 00:07:27 +02:00
|
|
|
private[this] var message: Option[MessageInvocation] = None
|
|
|
|
|
|
2009-08-04 16:46:51 +02:00
|
|
|
private[this] var participants: List[String] = Nil
|
|
|
|
|
private[this] var precommitted: List[String] = Nil
|
|
|
|
|
|
2009-10-06 00:07:27 +02:00
|
|
|
private[this] val persistentStateMap = new HashMap[String, Committable]
|
|
|
|
|
|
|
|
|
|
private[akka] val depth = new AtomicInteger(0)
|
2009-08-04 16:46:51 +02:00
|
|
|
|
2009-09-24 10:56:51 +02:00
|
|
|
def increment = depth.incrementAndGet
|
|
|
|
|
def decrement = depth.decrementAndGet
|
2009-10-06 00:07:27 +02:00
|
|
|
def isTopLevel = depth.get == 0
|
|
|
|
|
|
|
|
|
|
def register(uuid: String, storage: Committable) = persistentStateMap.put(uuid, storage)
|
2009-08-04 16:46:51 +02:00
|
|
|
|
2009-10-06 00:07:27 +02:00
|
|
|
def begin(participant: String, msg: MessageInvocation) = synchronized {
|
2009-08-04 16:46:51 +02:00
|
|
|
ensureIsActiveOrNew
|
2009-10-06 00:07:27 +02:00
|
|
|
message = Some(msg)
|
2009-09-10 01:33:01 +02:00
|
|
|
transaction = Multiverse.STM.startUpdateTransaction("akka")
|
2009-10-06 00:07:27 +02:00
|
|
|
log.debug("TX BEGIN - Creating a new transaction with id [%s]", id)
|
2009-08-15 22:44:29 +02:00
|
|
|
|
2009-10-06 00:07:27 +02:00
|
|
|
if (status == TransactionStatus.New) log.debug("TX BEGIN - Actor with UUID [%s] is starting NEW transaction [%s]", participant, toString)
|
|
|
|
|
else log.debug("Actor [%s] is participating in transaction", participant)
|
2009-08-04 16:46:51 +02:00
|
|
|
participants ::= participant
|
|
|
|
|
status = TransactionStatus.Active
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def precommit(participant: String) = synchronized {
|
|
|
|
|
if (status == TransactionStatus.Active) {
|
|
|
|
|
log.debug("TX PRECOMMIT - Pre-committing transaction [%s] for server with UUID [%s]", toString, participant)
|
|
|
|
|
precommitted ::= participant
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def commit(participant: String): Boolean = synchronized {
|
|
|
|
|
if (status == TransactionStatus.Active) {
|
2009-10-08 19:01:04 +02:00
|
|
|
log.debug("TX COMMIT - Trying to commit transaction [%s] for server with UUID [%s]", toString, participant)
|
2009-08-04 16:46:51 +02:00
|
|
|
val haveAllPreCommitted =
|
|
|
|
|
if (participants.size == precommitted.size) {{
|
|
|
|
|
for (part <- participants) yield {
|
|
|
|
|
if (precommitted.exists(_ == part)) true
|
|
|
|
|
else false
|
|
|
|
|
}}.exists(_ == true)
|
|
|
|
|
} else false
|
2009-08-15 22:44:29 +02:00
|
|
|
if (haveAllPreCommitted && transaction != null) {
|
2009-10-08 19:01:04 +02:00
|
|
|
setThreadLocalTransaction(transaction)
|
2009-10-06 00:07:27 +02:00
|
|
|
log.debug("TX COMMIT - Committing transaction [%s] for server with UUID [%s]", toString, participant)
|
2009-08-15 22:44:29 +02:00
|
|
|
transaction.commit
|
2009-08-04 16:46:51 +02:00
|
|
|
reset
|
2009-10-06 00:07:27 +02:00
|
|
|
status = TransactionStatus.Completed
|
|
|
|
|
Transaction.Atomic {
|
|
|
|
|
persistentStateMap.values.foreach(_.commit)
|
|
|
|
|
}
|
2009-10-08 19:01:04 +02:00
|
|
|
setThreadLocalTransaction(null)
|
2009-08-04 16:46:51 +02:00
|
|
|
true
|
|
|
|
|
} else false
|
|
|
|
|
} else {
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def rollback(participant: String) = synchronized {
|
|
|
|
|
ensureIsActiveOrAborted
|
2009-10-06 00:07:27 +02:00
|
|
|
log.debug("TX ROLLBACK - Actor with UUID [%s] has initiated transaction rollback for [%s]", participant, toString)
|
2009-08-04 16:46:51 +02:00
|
|
|
status = TransactionStatus.Aborted
|
2009-09-13 12:20:44 +02:00
|
|
|
transaction.abort
|
2009-08-04 16:46:51 +02:00
|
|
|
reset
|
|
|
|
|
}
|
2009-10-08 19:01:04 +02:00
|
|
|
|
2009-08-04 16:46:51 +02:00
|
|
|
def join(participant: String) = synchronized {
|
|
|
|
|
ensureIsActive
|
2009-10-06 00:07:27 +02:00
|
|
|
log.debug("TX JOIN - Actor with UUID [%s] is joining transaction [%s]" , participant, toString)
|
2009-08-04 16:46:51 +02:00
|
|
|
participants ::= participant
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-06 00:07:27 +02:00
|
|
|
def retry: Boolean = synchronized {
|
|
|
|
|
println("----- 2 " + message.isDefined)
|
|
|
|
|
println("----- 3 " + message.get.nrOfDeliveryAttempts)
|
|
|
|
|
if (message.isDefined && message.get.nrOfDeliveryAttempts.get < TransactionManagement.MAX_NR_OF_RETRIES) {
|
|
|
|
|
log.debug("TX RETRY - Restarting transaction [%s] resending message [%s]", transaction, message.get)
|
|
|
|
|
message.get.send
|
|
|
|
|
true
|
|
|
|
|
} else false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private def reset = synchronized {
|
|
|
|
|
transaction.reset
|
|
|
|
|
participants = Nil
|
|
|
|
|
precommitted = Nil
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-08 19:01:04 +02:00
|
|
|
def status_? = status
|
2009-09-24 10:56:51 +02:00
|
|
|
def isNew = synchronized { status == TransactionStatus.New }
|
|
|
|
|
def isActive = synchronized { status == TransactionStatus.Active }
|
|
|
|
|
def isCompleted = synchronized { status == TransactionStatus.Completed }
|
|
|
|
|
def isAborted = synchronized { status == TransactionStatus.Aborted }
|
2009-08-04 16:46:51 +02:00
|
|
|
|
|
|
|
|
private def ensureIsActive = if (status != TransactionStatus.Active)
|
|
|
|
|
throw new IllegalStateException("Expected ACTIVE transaction - current status [" + status + "]: " + toString)
|
|
|
|
|
|
|
|
|
|
private def ensureIsActiveOrAborted = if (!(status == TransactionStatus.Active || status == TransactionStatus.Aborted))
|
|
|
|
|
throw new IllegalStateException("Expected ACTIVE or ABORTED transaction - current status [" + status + "]: " + toString)
|
|
|
|
|
|
|
|
|
|
private def ensureIsActiveOrNew = if (!(status == TransactionStatus.Active || status == TransactionStatus.New))
|
|
|
|
|
throw new IllegalStateException("Expected ACTIVE or NEW transaction - current status [" + status + "]: " + toString)
|
|
|
|
|
|
|
|
|
|
// For reinitialize transaction after sending it over the wire
|
2009-09-10 01:33:01 +02:00
|
|
|
private[akka] def reinit = synchronized {
|
2009-08-04 16:46:51 +02:00
|
|
|
import net.lag.logging.{Logger, Level}
|
|
|
|
|
if (log == null) {
|
|
|
|
|
log = Logger.get(this.getClass.getName)
|
2009-09-24 10:56:51 +02:00
|
|
|
log.setLevel(Level.ALL) // TODO: preserve logging level
|
2009-08-04 16:46:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def equals(that: Any): Boolean = synchronized {
|
|
|
|
|
that != null &&
|
|
|
|
|
that.isInstanceOf[Transaction] &&
|
|
|
|
|
that.asInstanceOf[Transaction].id == this.id
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-24 10:56:51 +02:00
|
|
|
override def hashCode(): Int = synchronized { id.toInt }
|
2009-08-04 16:46:51 +02:00
|
|
|
|
2009-09-24 10:56:51 +02:00
|
|
|
override def toString(): String = synchronized { "Transaction[" + id + ", " + status + "]" }
|
2009-08-04 16:46:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-06-21 14:08:43 +02:00
|
|
|
@serializable sealed abstract class TransactionStatus
|
2009-03-23 19:17:10 +01:00
|
|
|
object TransactionStatus {
|
|
|
|
|
case object New extends TransactionStatus
|
|
|
|
|
case object Active extends TransactionStatus
|
|
|
|
|
case object Aborted extends TransactionStatus
|
|
|
|
|
case object Completed extends TransactionStatus
|
|
|
|
|
}
|