需求實現邏輯
- 啓動master和worker
- 在worker端對應的preStart方法中拿到master的引用對象,通過這個master引用向master發送註冊信息,註冊信息包含workerId, workCores, workMemory等信息
- master接受worker註冊信息,保存註冊信息在一個map集合當中,key爲workerId,value爲註冊信息樣例類。master將worker註冊成功的信息反饋給worker端
- worker接受master反饋的註冊成功信息,定時向master發送心跳信息。發送心跳信息,證明worker還活着
- master接受worker心跳信息,定時檢查超時worker,並從map當中移除掉超時的worker節點信息
具體代碼
- Master類
package spark
import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import com.typesafe.config.ConfigFactory
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.concurrent.duration._
//todo:利用akka實現簡易版的spark通信框架-----Master端
class Master extends Actor{
//構造代碼塊先被執行
println("master constructor invoked")
//定義一個map集合,用於存放worker信息
private val workerMap = new mutable.HashMap[String,WorkerInfo]()
//定義一個list集合,用於存放WorkerInfo信息,方便後期按照worker上的資源進行排序
private val workerList = new ListBuffer[WorkerInfo]
//master定時檢查的時間間隔
val CHECK_OUT_TIME_INTERVAL=15000 //15秒
//prestart方法會在構造代碼塊執行後被調用,並且只被調用一次
override def preStart(): Unit = {
println("preStart method invoked")
//master定時檢查超時的worker
//需要手動導入隱式轉換
import context.dispatcher
context.system.scheduler.schedule(0 millis,CHECK_OUT_TIME_INTERVAL millis,self,CheckOutTime)
}
//receive方法會在prestart方法執行後被調用,表示不斷的接受消息
override def receive: Receive = {
//master接受worker的註冊信息
case RegisterMessage(workerId,memory,cores) =>{
//判斷當前worker是否已經註冊
if(!workerMap.contains(workerId)){
//保存信息到map集合中
val workerInfo = new WorkerInfo(workerId,memory,cores)
workerMap.put(workerId,workerInfo)
//保存workerinfo到list集合中
workerList +=workerInfo
//master反饋註冊成功給worker
sender ! RegisteredMessage(s"workerId:$workerId 註冊成功")
}
}
//master接受worker的心跳信息
case SendHeartBeat(workerId)=>{
//判斷worker是否已經註冊,master只接受已經註冊過的worker的心跳信息
if(workerMap.contains(workerId)){
//獲取workerinfo信息
val workerInfo: WorkerInfo = workerMap(workerId)
//獲取當前系統時間
val lastTime: Long = System.currentTimeMillis()
workerInfo.lastHeartBeatTime=lastTime
}
}
case CheckOutTime=>{
//過濾出超時的worker 判斷邏輯: 獲取當前系統時間 - worker上一次心跳時間 >master定時檢查的時間間隔
val outTimeWorkers: ListBuffer[WorkerInfo] = workerList.filter(x => System.currentTimeMillis() -x.lastHeartBeatTime > CHECK_OUT_TIME_INTERVAL)
//遍歷超時的worker信息,然後移除掉超時的worker
for(workerInfo <- outTimeWorkers){
//獲取workerid
val workerId: String = workerInfo.workerId
//從map集合中移除掉超時的worker信息
workerMap.remove(workerId)
//從list集合中移除掉超時的workerInfo信息
workerList -= workerInfo
println("超時的workerId:" +workerId)
}
println("活着的worker總數:" + workerList.size)
//master按照worker內存大小進行降序排列
println(workerList.sortBy(x => x.memory).reverse.toList)
}
}
}
object Master{
def main(args: Array[String]): Unit = {
//master的ip地址
val host=args(0)
//master的port端口
val port=args(1)
//準備配置文件信息
val configStr=
s"""
|akka.actor.provider = "akka.remote.RemoteActorRefProvider"
|akka.remote.netty.tcp.hostname = "$host"
|akka.remote.netty.tcp.port = "$port"
""".stripMargin
//配置config對象 利用ConfigFactory解析配置文件,獲取配置信息
val config=ConfigFactory.parseString(configStr)
// 1、創建ActorSystem,它是整個進程中老大,它負責創建和監督actor,它是單例對象
val masterActorSystem = ActorSystem("masterActorSystem",config)
// 2、通過ActorSystem來創建master actor
val masterActor: ActorRef = masterActorSystem.actorOf(Props(new Master),"masterActor")
// 3、向master actor發送消息
//masterActor ! "connect"
}
}
- Worker類
package spark
import java.util.UUID
import akka.actor.{Actor, ActorRef, ActorSelection, ActorSystem, Props}
import com.typesafe.config.ConfigFactory
import scala.concurrent.duration._
//todo:利用akka實現簡易版的spark通信框架-----Worker端
class Worker(val memory:Int,val cores:Int,val masterHost:String,val masterPort:String) extends Actor{
println("Worker constructor invoked")
//定義workerId
private val workerId: String = UUID.randomUUID().toString
//定義發送心跳的時間間隔
val SEND_HEART_HEAT_INTERVAL=10000 //10秒
//定義全局變量
var master: ActorSelection=_
//prestart方法會在構造代碼塊之後被調用,並且只會被調用一次
override def preStart(): Unit = {
println("preStart method invoked")
//獲取master actor的引用
//ActorContext全局變量,可以通過在已經存在的actor中,尋找目標actor
//調用對應actorSelection方法,
// 方法需要一個path路徑:1、通信協議、2、master的IP地址、3、master的端口 4、創建master actor老大 5、actor層級
master= context.actorSelection(s"akka.tcp://masterActorSystem@$masterHost:$masterPort/user/masterActor")
//向master發送註冊信息,將信息封裝在樣例類中,主要包含:workerId,memory,cores
master ! RegisterMessage(workerId,memory,cores)
}
//receive方法會在prestart方法執行後被調用,不斷的接受消息
override def receive: Receive = {
//worker接受master的反饋信息
case RegisteredMessage(message) =>{
println(message)
//向master定期的發送心跳
//worker先自己給自己發送心跳
//需要手動導入隱式轉換
import context.dispatcher
context.system.scheduler.schedule(0 millis,SEND_HEART_HEAT_INTERVAL millis,self,HeartBeat)
}
//worker接受心跳
case HeartBeat =>{
//這個時候纔是真正向master發送心跳
master ! SendHeartBeat(workerId)
}
}
}
object Worker{
def main(args: Array[String]): Unit = {
//定義worker的IP地址
val host=args(0)
//定義worker的端口
val port=args(1)
//定義worker的內存
val memory=args(2).toInt
//定義worker的核數
val cores=args(3).toInt
//定義master的ip地址
val masterHost=args(4)
//定義master的端口
val masterPort=args(5)
//準備配置文件
val configStr=
s"""
|akka.actor.provider = "akka.remote.RemoteActorRefProvider"
|akka.remote.netty.tcp.hostname = "$host"
|akka.remote.netty.tcp.port = "$port"
""".stripMargin
//通過configFactory來解析配置信息
val config=ConfigFactory.parseString(configStr)
// 1、創建ActorSystem,它是整個進程中的老大,它負責創建和監督actor
val workerActorSystem = ActorSystem("workerActorSystem",config)
// 2、通過actorSystem來創建 worker actor
val workerActor: ActorRef = workerActorSystem.actorOf(Props(new Worker(memory,cores,masterHost,masterPort)),"workerActor")
//向worker actor發送消息
workerActor ! "connect"
}
}
- WorkerInfo類
-
package spark //封裝worker信息 class WorkerInfo(val workerId:String,val memory:Int,val cores:Int) { //定義一個變量用於存放worker上一次心跳時間 var lastHeartBeatTime:Long=_ override def toString: String = { s"workerId:$workerId , memory:$memory , cores:$cores" }
- 樣例類
package spark
trait RemoteMessage extends Serializable{
}
//worker向master發送註冊信息,由於不在同一進程中,需要實現序列化
case class RegisterMessage(val workerId:String,val memory:Int,val cores:Int) extends RemoteMessage
//master反饋註冊成功信息給worker,由於不在同一進程中,也需要實現序列化
case class RegisteredMessage(message:String) extends RemoteMessage
//worker向worker發送心跳 由於在同一進程中,不需要實現序列化
case object HeartBeat
//worker向master發送心跳,由於不在同一進程中,需要實現序列化
case class SendHeartBeat(val workerId:String) extends RemoteMessage
//master自己向自己發送消息,由於在同一進程中,不需要實現序列化
case object CheckOutTime