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
|
|
|
|
|
|
|
|
|
|
import se.scalablesolutions.akka.state.Committable
|
2009-10-17 00:37:56 +02:00
|
|
|
import se.scalablesolutions.akka.util.Logging
|
2009-08-04 16:46:51 +02:00
|
|
|
|
2009-10-17 00:37:56 +02:00
|
|
|
import org.multiverse.api.{Stm, Transaction => MultiverseTransaction}
|
2009-10-24 21:21:43 +02:00
|
|
|
import org.multiverse.api.GlobalStmInstance.getGlobalStmInstance
|
2009-11-25 20:37:00 +01:00
|
|
|
import org.multiverse.api.ThreadLocalTransaction._
|
2009-10-17 00:37:56 +02:00
|
|
|
import org.multiverse.templates.OrElseTemplate
|
2009-08-04 16:46:51 +02:00
|
|
|
|
2009-10-06 00:07:27 +02:00
|
|
|
import scala.collection.mutable.HashMap
|
|
|
|
|
|
2009-10-17 00:37:56 +02:00
|
|
|
class NoTransactionInScopeException extends RuntimeException
|
2009-10-06 00:07:27 +02:00
|
|
|
class TransactionRetryException(message: String) extends RuntimeException(message)
|
2009-08-04 16:46:51 +02:00
|
|
|
|
2009-09-10 01:33:01 +02:00
|
|
|
/**
|
2009-11-26 16:52:33 +01:00
|
|
|
* Example of atomic transaction management using the atomic block:
|
2009-09-13 12:20:44 +02:00
|
|
|
* <pre>
|
|
|
|
|
* import se.scalablesolutions.akka.stm.Transaction._
|
2009-11-26 16:52:33 +01:00
|
|
|
*
|
2009-10-08 21:35:08 +02:00
|
|
|
* atomic {
|
2009-09-13 12:20:44 +02:00
|
|
|
* .. // do something within a transaction
|
|
|
|
|
* }
|
|
|
|
|
* </pre>
|
|
|
|
|
*
|
2009-11-26 16:52:33 +01:00
|
|
|
* Example of atomic transaction management using atomic block with retry count:
|
2009-09-10 01:33:01 +02:00
|
|
|
* <pre>
|
2009-09-13 12:20:44 +02:00
|
|
|
* import se.scalablesolutions.akka.stm.Transaction._
|
2009-11-26 16:52:33 +01:00
|
|
|
*
|
|
|
|
|
* atomic(maxNrOfRetries) {
|
|
|
|
|
* .. // do something within a transaction
|
|
|
|
|
* }
|
|
|
|
|
* </pre>
|
|
|
|
|
*
|
|
|
|
|
* Example of atomically-orElse transaction management.
|
|
|
|
|
* Which is a good way to reduce contention and transaction collisions.
|
|
|
|
|
* <pre>
|
|
|
|
|
* import se.scalablesolutions.akka.stm.Transaction._
|
|
|
|
|
*
|
|
|
|
|
* atomically {
|
2009-09-10 01:33:01 +02:00
|
|
|
* .. // try to do something
|
2009-10-08 21:35:08 +02:00
|
|
|
* } orElse {
|
2009-09-10 01:33:01 +02:00
|
|
|
* .. // if transaction clashes try do do something else to minimize contention
|
|
|
|
|
* }
|
|
|
|
|
* </pre>
|
|
|
|
|
*
|
2009-11-26 16:52:33 +01:00
|
|
|
* Example of atomic transaction management using for comprehensions (monadic):
|
2009-11-02 22:14:24 +01:00
|
|
|
*
|
|
|
|
|
* <pre>
|
|
|
|
|
* import se.scalablesolutions.akka.stm.Transaction._
|
|
|
|
|
* for (tx <- Transaction) {
|
|
|
|
|
* ... // do transactional stuff
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* val result = for (tx <- Transaction) yield {
|
|
|
|
|
* ... // do transactional stuff yielding a result
|
|
|
|
|
* }
|
|
|
|
|
* </pre>
|
|
|
|
|
*
|
2009-11-26 16:52:33 +01:00
|
|
|
* Example of using Transaction and TransactionalRef in for comprehensions (monadic):
|
2009-11-02 22:14:24 +01:00
|
|
|
*
|
|
|
|
|
* <pre>
|
|
|
|
|
* // For example, if you have a List with TransactionalRef
|
|
|
|
|
* val refs: List[TransactionalRef] = ...
|
|
|
|
|
*
|
|
|
|
|
* // You can use them together with Transaction in a for comprehension since TransactionalRef is also monadic
|
|
|
|
|
* for {
|
|
|
|
|
* tx <- Transaction
|
|
|
|
|
* ref <- refs
|
|
|
|
|
* } {
|
|
|
|
|
* ... // use the ref inside a transaction
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* val result = for {
|
|
|
|
|
* tx <- Transaction
|
|
|
|
|
* ref <- refs
|
|
|
|
|
* } yield {
|
|
|
|
|
* ... // use the ref inside a transaction, yield a result
|
|
|
|
|
* }
|
|
|
|
|
* </pre>
|
|
|
|
|
*
|
2009-09-10 01:33:01 +02:00
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
2009-10-17 00:37:56 +02:00
|
|
|
object Transaction extends TransactionManagement {
|
2009-09-17 09:47:22 +02:00
|
|
|
val idFactory = new AtomicLong(-1L)
|
2009-09-10 01:33:01 +02:00
|
|
|
|
2009-11-02 22:14:24 +01:00
|
|
|
// -- monadic api --------------------------
|
|
|
|
|
def map[T](f: Transaction => T): T = atomic { f(getTransactionInScope) }
|
|
|
|
|
|
|
|
|
|
def flatMap[T](f: Transaction => T): T = atomic { f(getTransactionInScope) }
|
|
|
|
|
|
|
|
|
|
def foreach(f: Transaction => Unit): Unit = atomic { f(getTransactionInScope) }
|
2009-09-10 01:33:01 +02:00
|
|
|
|
2009-09-13 12:20:44 +02:00
|
|
|
// -- atomic block --------------------------
|
2009-11-26 16:52:33 +01:00
|
|
|
/**
|
|
|
|
|
* Creates a "pure" STM atomic transaction and by-passes all transactions hooks such as persistence etc.
|
|
|
|
|
* Only for internal usage.
|
|
|
|
|
*/
|
|
|
|
|
private[akka] def pureAtomic[T](body: => T): T = new AtomicTemplate[T](
|
2009-11-04 00:36:18 +01:00
|
|
|
getGlobalStmInstance, "akka", false, false, TransactionManagement.MAX_NR_OF_RETRIES) {
|
|
|
|
|
def execute(mtx: MultiverseTransaction): T = body
|
|
|
|
|
}.execute()
|
|
|
|
|
|
2009-10-24 21:21:43 +02:00
|
|
|
def atomic[T](body: => T): T = new AtomicTemplate[T](
|
|
|
|
|
getGlobalStmInstance, "akka", false, false, TransactionManagement.MAX_NR_OF_RETRIES) {
|
2009-10-17 00:37:56 +02:00
|
|
|
def execute(mtx: MultiverseTransaction): T = body
|
|
|
|
|
override def postStart(mtx: MultiverseTransaction) = {
|
|
|
|
|
val tx = new Transaction
|
|
|
|
|
tx.transaction = Some(mtx)
|
|
|
|
|
setTransaction(Some(tx))
|
|
|
|
|
}
|
|
|
|
|
override def postCommit = {
|
2009-11-04 00:36:18 +01:00
|
|
|
if (isTransactionInScope) getTransactionInScope.commit
|
2009-10-17 00:37:56 +02:00
|
|
|
else throw new IllegalStateException("No transaction in scope")
|
|
|
|
|
}
|
2009-09-13 12:20:44 +02:00
|
|
|
}.execute()
|
|
|
|
|
|
2009-11-02 22:14:24 +01:00
|
|
|
def atomic[T](retryCount: Int)(body: => T): T = {
|
|
|
|
|
new AtomicTemplate[T](getGlobalStmInstance, "akka", false, false, retryCount) {
|
|
|
|
|
def execute(mtx: MultiverseTransaction): T = body
|
|
|
|
|
override def postStart(mtx: MultiverseTransaction) = {
|
|
|
|
|
val tx = new Transaction
|
|
|
|
|
tx.transaction = Some(mtx)
|
|
|
|
|
setTransaction(Some(tx))
|
|
|
|
|
}
|
|
|
|
|
override def postCommit = {
|
2009-11-04 00:36:18 +01:00
|
|
|
if (isTransactionInScope) getTransactionInScope.commit
|
2009-11-02 22:14:24 +01:00
|
|
|
else throw new IllegalStateException("No transaction in scope")
|
|
|
|
|
}
|
|
|
|
|
}.execute
|
|
|
|
|
}
|
2009-10-17 00:37:56 +02:00
|
|
|
|
2009-11-02 22:14:24 +01:00
|
|
|
def atomicReadOnly[T](retryCount: Int)(body: => T): T = {
|
|
|
|
|
new AtomicTemplate[T](getGlobalStmInstance, "akka", false, true, retryCount) {
|
|
|
|
|
def execute(mtx: MultiverseTransaction): T = body
|
|
|
|
|
override def postStart(mtx: MultiverseTransaction) = {
|
|
|
|
|
val tx = new Transaction
|
|
|
|
|
tx.transaction = Some(mtx)
|
|
|
|
|
setTransaction(Some(tx))
|
|
|
|
|
}
|
|
|
|
|
override def postCommit = {
|
2009-11-04 00:36:18 +01:00
|
|
|
if (isTransactionInScope) getTransactionInScope.commit
|
2009-11-02 22:14:24 +01:00
|
|
|
else throw new IllegalStateException("No transaction in scope")
|
|
|
|
|
}
|
|
|
|
|
}.execute
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def atomicReadOnly[T](body: => T): T = {
|
|
|
|
|
new AtomicTemplate[T](true) {
|
|
|
|
|
def execute(mtx: MultiverseTransaction): T = body
|
|
|
|
|
override def postStart(mtx: MultiverseTransaction) = {
|
|
|
|
|
val tx = new Transaction
|
|
|
|
|
tx.transaction = Some(mtx)
|
|
|
|
|
setTransaction(Some(tx))
|
|
|
|
|
}
|
|
|
|
|
override def postCommit = {
|
2009-11-04 00:36:18 +01:00
|
|
|
if (isTransactionInScope) getTransactionInScope.commit
|
2009-11-02 22:14:24 +01:00
|
|
|
else throw new IllegalStateException("No transaction in scope")
|
|
|
|
|
}
|
|
|
|
|
}.execute
|
|
|
|
|
}
|
2009-10-17 00:37:56 +02:00
|
|
|
|
2009-11-26 16:52:33 +01:00
|
|
|
// -- atomically-orElse --------------------------
|
|
|
|
|
def atomically[A](orBody: => A) = elseBody(orBody)
|
2009-09-10 01:33:01 +02:00
|
|
|
def elseBody[A](orBody: => A) = new {
|
2009-10-08 21:35:08 +02:00
|
|
|
def orElse(elseBody: => A) = new OrElseTemplate[A] {
|
2009-09-10 01:33:01 +02:00
|
|
|
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 21:35:08 +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-10-17 00:37:56 +02:00
|
|
|
import Transaction._
|
|
|
|
|
|
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-10-17 00:37:56 +02:00
|
|
|
private[akka] var transaction: Option[MultiverseTransaction] = None
|
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-11-02 22:14:24 +01:00
|
|
|
// --- public methods ---------
|
2009-08-04 16:46:51 +02:00
|
|
|
|
2009-10-17 00:37:56 +02:00
|
|
|
def commit = synchronized {
|
2009-11-26 16:52:33 +01:00
|
|
|
pureAtomic {
|
2009-10-17 00:37:56 +02:00
|
|
|
persistentStateMap.values.foreach(_.commit)
|
|
|
|
|
TransactionManagement.clearTransaction
|
2009-08-04 16:46:51 +02:00
|
|
|
}
|
2009-10-17 00:37:56 +02:00
|
|
|
status = TransactionStatus.Completed
|
2009-10-06 00:07:27 +02:00
|
|
|
}
|
|
|
|
|
|
2009-09-24 10:56:51 +02:00
|
|
|
def isNew = synchronized { status == TransactionStatus.New }
|
2009-11-02 22:14:24 +01:00
|
|
|
|
2009-09-24 10:56:51 +02:00
|
|
|
def isActive = synchronized { status == TransactionStatus.Active }
|
2009-11-02 22:14:24 +01:00
|
|
|
|
2009-09-24 10:56:51 +02:00
|
|
|
def isCompleted = synchronized { status == TransactionStatus.Completed }
|
2009-11-02 22:14:24 +01:00
|
|
|
|
2009-09-24 10:56:51 +02:00
|
|
|
def isAborted = synchronized { status == TransactionStatus.Aborted }
|
2009-08-04 16:46:51 +02:00
|
|
|
|
2009-11-02 22:14:24 +01:00
|
|
|
// --- internal methods ---------
|
|
|
|
|
|
|
|
|
|
private[akka] def status_? = status
|
|
|
|
|
|
|
|
|
|
private[akka] def increment = depth.incrementAndGet
|
|
|
|
|
|
|
|
|
|
private[akka] def decrement = depth.decrementAndGet
|
|
|
|
|
|
|
|
|
|
private[akka] def isTopLevel = depth.get == 0
|
|
|
|
|
|
|
|
|
|
private[akka] def register(uuid: String, storage: Committable) = persistentStateMap.put(uuid, storage)
|
|
|
|
|
|
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-11-02 22:14:24 +01:00
|
|
|
/**
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
2009-06-21 14:08:43 +02:00
|
|
|
@serializable sealed abstract class TransactionStatus
|
2009-11-02 22:14:24 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
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
|
|
|
|
|
}
|
2009-11-02 22:14:24 +01:00
|
|
|
|