From e137f0eac0ec29b255ccf03a9532ad4c05b3eced Mon Sep 17 00:00:00 2001 From: Christos KK Loverdos Date: Tue, 20 Dec 2011 11:51:01 +0200 Subject: [PATCH] Simplify UserActor death logic --- .../gr/grnet/aquarium/user/actor/UserActor.scala | 93 +++++++------------- .../aquarium/user/actor/UserActorMessage.scala | 2 - .../grnet/aquarium/user/actor/UserActorsLRU.scala | 6 +- 3 files changed, 33 insertions(+), 68 deletions(-) diff --git a/logic/src/main/scala/gr/grnet/aquarium/user/actor/UserActor.scala b/logic/src/main/scala/gr/grnet/aquarium/user/actor/UserActor.scala index 37a8841..9b101a8 100644 --- a/logic/src/main/scala/gr/grnet/aquarium/user/actor/UserActor.scala +++ b/logic/src/main/scala/gr/grnet/aquarium/user/actor/UserActor.scala @@ -53,88 +53,55 @@ class UserActor extends AquariumActor with Loggable { @volatile private[this] var _isInitialized: Boolean = false @volatile - private[this] var _isParked: Boolean = false - - @volatile private[this] var _userState: UserState = _ @volatile private[this] var _actorProvider: ActorProvider = _ def role = UserActorRole - private[this] def _checkNotParked(m: ActorMessage): Boolean = { - if(_isParked) { - logger.error("UserActor %s for userId %s is parked but %s was sent to it".format(this, this._userId, m)) - false - } else { - true - } - } - - private[this] def _selfCheckToStop(): Unit = { - self ! UserActorCheckToStop - } - protected def receive: Receive = { - case UserActorPark ⇒ - this._isParked = true - - case UserActorCheckToStop ⇒ - if(_isParked) { - self ! UserActorStop - } - case UserActorStop ⇒ self.stop() case m @ UserActorInitWithUserId(userId) ⇒ - if(_checkNotParked(m)) { - this._userId = userId - this._isInitialized = true - // TODO: query DB etc to get internal state - logger.info("Setup my userId = %s".format(userId)) - } - _selfCheckToStop() + this._userId = userId + this._isInitialized = true + // TODO: query DB etc to get internal state + logger.info("Setup my userId = %s".format(userId)) case m @ ActorProviderConfigured(actorProvider) ⇒ - if(_checkNotParked(m)) { - this._actorProvider = actorProvider - logger.info("Configured %s with %s".format(this, m)) - } - _selfCheckToStop() + this._actorProvider = actorProvider + logger.info("Configured %s with %s".format(this, m)) case m @ UserRequestGetBalance(userId, timestamp) ⇒ - if(_checkNotParked(m)) { - if(this._userId != userId) { - logger.error("Received %s but my userId = %s".format(m, this._userId)) - // TODO: throw an exception here - } else { - // This is the big party. - // Get the user state, if it exists and make sure it is not stale. - - // Do we have a user state? - if(_userState ne null) { - // Yep, we do. See what there is inside it. - val credits = _userState.credits - val creditsTimestamp = credits.snapshotTime - - // Check if data is stale - if(creditsTimestamp + 10000 > timestamp) { - // No, it's OK - self reply UserResponseGetBalance(userId, credits.data) - } else { - // Yep, data is stale and must recompute balance - // FIXME: implement - logger.error("FIXME: Should have computed a new value for %s".format(credits)) - self reply UserResponseGetBalance(userId, credits.data) - } + if(this._userId != userId) { + logger.error("Received %s but my userId = %s".format(m, this._userId)) + // TODO: throw an exception here + } else { + // This is the big party. + // Get the user state, if it exists and make sure it is not stale. + + // Do we have a user state? + if(_userState ne null) { + // Yep, we do. See what there is inside it. + val credits = _userState.credits + val creditsTimestamp = credits.snapshotTime + + // Check if data is stale + if(creditsTimestamp + 10000 > timestamp) { + // No, it's OK + self reply UserResponseGetBalance(userId, credits.data) } else { - // Nope. No user state exists. Must reproduce one + // Yep, data is stale and must recompute balance // FIXME: implement - logger.error("FIXME: Should have computed the user state for userId = %s".format(userId)) + logger.error("FIXME: Should have computed a new value for %s".format(credits)) + self reply UserResponseGetBalance(userId, credits.data) } + } else { + // Nope. No user state exists. Must reproduce one + // FIXME: implement + logger.error("FIXME: Should have computed the user state for userId = %s".format(userId)) } } - _selfCheckToStop() } } \ No newline at end of file diff --git a/logic/src/main/scala/gr/grnet/aquarium/user/actor/UserActorMessage.scala b/logic/src/main/scala/gr/grnet/aquarium/user/actor/UserActorMessage.scala index 12defad..b08bb66 100644 --- a/logic/src/main/scala/gr/grnet/aquarium/user/actor/UserActorMessage.scala +++ b/logic/src/main/scala/gr/grnet/aquarium/user/actor/UserActorMessage.scala @@ -47,6 +47,4 @@ trait UserActorMessage extends ActorMessage case class UserActorInitWithUserId(userId: String) extends UserActorMessage -case object UserActorPark extends UserActorMessage -case object UserActorCheckToStop extends UserActorMessage case object UserActorStop extends UserActorMessage \ No newline at end of file diff --git a/logic/src/main/scala/gr/grnet/aquarium/user/actor/UserActorsLRU.scala b/logic/src/main/scala/gr/grnet/aquarium/user/actor/UserActorsLRU.scala index b69ba00..6d2c153 100644 --- a/logic/src/main/scala/gr/grnet/aquarium/user/actor/UserActorsLRU.scala +++ b/logic/src/main/scala/gr/grnet/aquarium/user/actor/UserActorsLRU.scala @@ -82,9 +82,9 @@ class UserActorsLRU(val upperWaterMark: Int, val lowerWatermark: Int) extends Li private[this] object EvictionListener extends ConcurrentLRUCache.EvictionListener[String, ActorRef] with Loggable { def evictedEntry(userId: String, userActor: ActorRef): Unit = { logger.debug("Parking UserActor for userId = %s".format(userId)) - userActor ! UserActorPark - // hopefully no need to further track these actors as they now enter a state machine which ultimately leads - // to their shutting down + // Check this is received after any currently servicing business logic message. + userActor ! UserActorStop + // Hopefully no need to further track these actors as they will now cause their own death. } } } -- 1.7.10.4