如何在Akka Multi Node测试中重启节点?

时间:2016-12-20 02:07:49

标签: akka akka-testkit

我想做一些Akka Multi Node测试,并在达到某个障碍时重新启动一个节点。类似的东西:

runOn(nodeA) {
  // do something while both nodes are up and running

  enterBarrier("nodeBCrashes")

  // do something while I'm the only node up and running

  enterBarrier("bothNodesUp")

  // do something with both nodes up and running again
}

runOn(nodeB) {
  // do something while both nodes are up and running

  crash()
  enterBarrier("nodeBCrashes")

  // do nothing because I'm out

  enterBarrier("bothNodesUp")
  start()

  // do something with both nodes up and running again

}

这是不可能完成的,至少需要一种方法能够关闭nodeB并使用相同的akka​​.remote.netty.tcp.port 启动另一个nodeC (这是严格的必要)。像这样的东西

runOn(nodeA) {
  // do something while both nodes are up and running

  enterBarrier("nodeBCrashes")

  // do something while I'm the only node up and running

  enterBarrier("bothNodesUp")

  // do something with both nodes up and running again
}

runOn(nodeB) {
  // do something while both nodes are up and running

  enterBarrier("nodeBCrashes")
  shutdown()

}

// How I can delay nodeC start until nodeA reaches bothNodesUp barrier?
runOn(nodeC) {      
  // do something when both nodes are up and running
} 

问题可以恢复为:

我们可以重新创建一个节点崩溃然后重新启动的情况吗?

  1. 我们可以重启一个节点吗?
  2. 如果没有,我们可以在剩余的节点到达berrier时启动节点吗?
  3. 我们可以将相同的akka​​.remote.netty.tcp.port分配给不同的节点(不应该并行运行)。我尝试使用*.opts文件,但没有成功,是这样的吗?

1 个答案:

答案 0 :(得分:2)

您应该能够重新启动ActorSystem重用崩溃的同一个端口。在Akka自己的多节点测试中,他们采取以下措施:

  lazy val restartedSecondSystem = ActorSystem(
    system,
    ConfigFactory.parseString("akka.remote.netty.tcp.port=" + secondUniqueAddress.address.port.get).
      withFallback(system.settings.config))

  ...      

  runOn(nodeB) {        
    shutdown(secondSystem)
  }

  enterBarrier("second-shutdown")

  runOn(nodeB) {
    Cluster(restartedSecondSystem).joinSeedNodes(seedNodes)
  }

在Akka的源代码中查看以下测试以获取更多提示。

https://github.com/akka/akka/blob/master/akka-cluster/src/multi-jvm/scala/akka/cluster/RestartNodeSpec.scala

https://github.com/akka/akka/blob/master/akka-cluster/src/multi-jvm/scala/akka/cluster/RestartNode2Spec.scala

https://github.com/akka/akka/blob/master/akka-cluster/src/multi-jvm/scala/akka/cluster/RestartNode3Spec.scala