All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kvstore.Arbiter.scala Maven / Gradle / Ivy

package kvstore

import akka.actor.{ ActorRef, Actor }
import scala.collection.immutable

object Arbiter {
  case object Join

  case object JoinedPrimary
  case object JoinedSecondary

  /**
   * This message contains all replicas currently known to the arbiter, including the primary.
   */
  case class Replicas(replicas: Set[ActorRef])
}

/**
 * A subsystem which assigns the primary or secondary roles to nodes that Join.
 * The first node which joins is assigned as Primary Node and all the subsequent ones are Secondary.
 */
class Arbiter extends Actor {
  import Arbiter._
  var leader: Option[ActorRef] = None
  var replicas = Set.empty[ActorRef]

  def receive = {
    case Join =>
      if (leader.isEmpty) {
        leader = Some(sender)
        replicas += sender
        sender ! JoinedPrimary
      } else {
        replicas += sender
        sender ! JoinedSecondary
      }
      leader foreach (_ ! Replicas(replicas))
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy