2009-03-23 19:17:10 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009 Scalable Solutions.
|
|
|
|
|
*/
|
|
|
|
|
|
2009-06-21 14:08:43 +02:00
|
|
|
package se.scalablesolutions.akka.kernel.stm
|
2009-03-23 19:17:10 +01:00
|
|
|
|
2009-06-05 22:08:53 +02:00
|
|
|
import java.util.concurrent.atomic.{AtomicInteger, AtomicLong}
|
2009-06-29 23:38:10 +02:00
|
|
|
import kernel.state.Transactional
|
2009-06-21 14:08:43 +02:00
|
|
|
import kernel.util.Logging
|
2009-06-25 23:47:30 +02:00
|
|
|
|
2009-07-04 12:06:07 +02:00
|
|
|
class TransactionRollbackException(msg: String) extends RuntimeException(msg)
|
2009-06-05 22:08:53 +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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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>
|
|
|
|
|
*/
|
2009-06-21 14:08:43 +02:00
|
|
|
@serializable class Transaction extends Logging {
|
2009-03-23 19:17:10 +01:00
|
|
|
val id = TransactionIdFactory.newId
|
2009-03-26 20:22:49 +01:00
|
|
|
|
2009-05-13 08:58:50 +02:00
|
|
|
log.debug("Creating a new transaction with id [%s]", id)
|
|
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
private[this] val transactionals = new ChangeSet
|
|
|
|
|
|
|
|
|
|
private[this] var participants: List[String] = Nil
|
|
|
|
|
private[this] var precommitted: List[String] = Nil
|
|
|
|
|
|
2009-06-05 22:08:53 +02:00
|
|
|
private[this] val depth = new AtomicInteger(0)
|
2009-06-29 23:38:10 +02:00
|
|
|
|
2009-03-23 19:17:10 +01:00
|
|
|
@volatile private[this] var status: TransactionStatus = TransactionStatus.New
|
|
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
def increment = synchronized { depth.incrementAndGet }
|
|
|
|
|
def decrement = synchronized { depth.decrementAndGet }
|
|
|
|
|
def topLevel_? = synchronized { depth.get == 0 }
|
2009-06-05 22:08:53 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
def register(transactional: Transactional) = synchronized {
|
2009-05-13 08:58:50 +02:00
|
|
|
ensureIsActiveOrNew
|
2009-06-29 23:38:10 +02:00
|
|
|
transactionals + transactional
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def begin(participant: String) = synchronized {
|
|
|
|
|
ensureIsActiveOrNew
|
2009-07-02 18:07:29 +02:00
|
|
|
if (status == TransactionStatus.New) log.debug("TX BEGIN - Server with UUID [%s] is starting NEW transaction [%s]", participant, toString)
|
2009-06-29 23:38:10 +02:00
|
|
|
else log.debug("Server [%s] is participating in transaction", participant)
|
|
|
|
|
participants ::= participant
|
2009-03-23 19:17:10 +01:00
|
|
|
status = TransactionStatus.Active
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
def precommit(participant: String) = synchronized {
|
2009-03-26 20:22:49 +01:00
|
|
|
if (status == TransactionStatus.Active) {
|
2009-07-02 18:07:29 +02:00
|
|
|
log.debug("TX PRECOMMIT - Pre-committing transaction [%s] for server with UUID [%s]", toString, participant)
|
2009-06-29 23:38:10 +02:00
|
|
|
precommitted ::= participant
|
2009-03-26 20:22:49 +01:00
|
|
|
}
|
2009-03-23 19:17:10 +01:00
|
|
|
}
|
|
|
|
|
|
2009-07-04 12:06:07 +02:00
|
|
|
def commit(participant: String): Boolean = synchronized {
|
2009-03-26 20:22:49 +01:00
|
|
|
if (status == TransactionStatus.Active) {
|
2009-07-02 18:07:29 +02:00
|
|
|
log.debug("TX COMMIT - Committing transaction [%s] for server with UUID [%s]", toString, participant)
|
2009-03-26 20:22:49 +01:00
|
|
|
val haveAllPreCommitted =
|
2009-04-04 21:34:10 +02:00
|
|
|
if (participants.size == precommitted.size) {{
|
2009-06-29 23:38:10 +02:00
|
|
|
for (part <- participants) yield {
|
|
|
|
|
if (precommitted.exists(_ == part)) true
|
2009-03-26 20:22:49 +01:00
|
|
|
else false
|
2009-05-13 08:58:50 +02:00
|
|
|
}}.exists(_ == true)
|
2009-03-26 20:22:49 +01:00
|
|
|
} else false
|
2009-05-13 19:28:55 +02:00
|
|
|
if (haveAllPreCommitted) {
|
2009-06-29 23:38:10 +02:00
|
|
|
transactionals.items.foreach(_.commit)
|
2009-05-13 19:28:55 +02:00
|
|
|
status = TransactionStatus.Completed
|
2009-07-04 12:06:07 +02:00
|
|
|
reset
|
|
|
|
|
true
|
|
|
|
|
} else false
|
|
|
|
|
} else {
|
|
|
|
|
reset
|
|
|
|
|
true
|
2009-03-26 20:22:49 +01:00
|
|
|
}
|
2009-03-23 19:17:10 +01:00
|
|
|
}
|
|
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
def rollback(participant: String) = synchronized {
|
2009-03-26 20:22:49 +01:00
|
|
|
ensureIsActiveOrAborted
|
2009-07-02 18:07:29 +02:00
|
|
|
log.debug("TX ROLLBACK - Server with UUID [%s] has initiated transaction rollback for [%s]", participant, toString)
|
2009-06-29 23:38:10 +02:00
|
|
|
transactionals.items.foreach(_.rollback)
|
2009-03-23 19:17:10 +01:00
|
|
|
status = TransactionStatus.Aborted
|
2009-06-29 23:38:10 +02:00
|
|
|
reset
|
2009-03-23 19:17:10 +01:00
|
|
|
}
|
|
|
|
|
|
2009-07-04 12:06:07 +02:00
|
|
|
def rollbackForRescheduling(participant: String) = synchronized {
|
|
|
|
|
ensureIsActiveOrAborted
|
|
|
|
|
log.debug("TX ROLLBACK for recheduling - Server with UUID [%s] has initiated transaction rollback for [%s]", participant, toString)
|
|
|
|
|
transactionals.items.foreach(_.rollback)
|
|
|
|
|
reset
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
def join(participant: String) = synchronized {
|
2009-05-13 08:58:50 +02:00
|
|
|
ensureIsActive
|
2009-07-02 18:07:29 +02:00
|
|
|
log.debug("TX JOIN - Server with UUID [%s] is joining transaction [%s]" , participant, toString)
|
2009-06-29 23:38:10 +02:00
|
|
|
participants ::= participant
|
2009-04-09 15:49:42 +02:00
|
|
|
}
|
|
|
|
|
|
2009-06-10 20:04:33 +02:00
|
|
|
def isNew = status == TransactionStatus.New
|
|
|
|
|
def isActive = status == TransactionStatus.Active
|
|
|
|
|
def isCompleted = status == TransactionStatus.Completed
|
|
|
|
|
def isAborted = status == TransactionStatus.Aborted
|
|
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
private def reset = {
|
|
|
|
|
transactionals.clear
|
|
|
|
|
participants = Nil
|
|
|
|
|
precommitted = Nil
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-26 20:22:49 +01:00
|
|
|
private def ensureIsActive = if (status != TransactionStatus.Active)
|
2009-06-30 16:01:50 +02:00
|
|
|
throw new IllegalStateException("Expected ACTIVE transaction - current status [" + status + "]: " + toString)
|
2009-03-23 19:17:10 +01:00
|
|
|
|
2009-05-01 13:25:43 +02:00
|
|
|
private def ensureIsActiveOrAborted = if (!(status == TransactionStatus.Active || status == TransactionStatus.Aborted))
|
2009-06-30 16:01:50 +02:00
|
|
|
throw new IllegalStateException("Expected ACTIVE or ABORTED transaction - current status [" + status + "]: " + toString)
|
2009-03-26 20:22:49 +01:00
|
|
|
|
2009-05-13 08:58:50 +02:00
|
|
|
private def ensureIsActiveOrNew = if (!(status == TransactionStatus.Active || status == TransactionStatus.New))
|
2009-06-30 16:01:50 +02:00
|
|
|
throw new IllegalStateException("Expected ACTIVE or NEW transaction - current status [" + status + "]: " + toString)
|
2009-05-13 08:58:50 +02:00
|
|
|
|
2009-06-29 23:38:10 +02:00
|
|
|
// For reinitialize transaction after sending it over the wire
|
2009-06-25 23:47:30 +02:00
|
|
|
private[kernel] def reinit = {
|
|
|
|
|
import net.lag.logging.{Logger, Level}
|
|
|
|
|
if (log == null) {
|
|
|
|
|
log = Logger.get(this.getClass.getName)
|
|
|
|
|
log.setLevel(Level.ALL)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-01 13:25:43 +02:00
|
|
|
override def equals(that: Any): Boolean = synchronized {
|
2009-03-23 19:17:10 +01:00
|
|
|
that != null &&
|
|
|
|
|
that.isInstanceOf[Transaction] &&
|
|
|
|
|
that.asInstanceOf[Transaction].id == this.id
|
2009-05-01 13:25:43 +02:00
|
|
|
}
|
2009-03-23 19:17:10 +01:00
|
|
|
|
|
|
|
|
override def hashCode(): Int = id.toInt
|
|
|
|
|
|
2009-05-01 13:25:43 +02:00
|
|
|
override def toString(): String = synchronized {
|
|
|
|
|
"Transaction[" + id + ", " + status + "]"
|
|
|
|
|
}
|
2009-06-29 23:38:10 +02:00
|
|
|
}
|