This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 841
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2712 from mesosphere/mv/fix_2509
Fixes #2509 by listening for connection drops and abdicating directly
- Loading branch information
Showing
10 changed files
with
215 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/scala/mesosphere/marathon/core/leadership/impl/AbdicateOnConnectionLossActor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package mesosphere.marathon.core.leadership.impl | ||
|
||
import akka.actor.{ Actor, ActorLogging, Props } | ||
import com.twitter.common.zookeeper.ZooKeeperClient | ||
import mesosphere.marathon.LeadershipAbdication | ||
import org.apache.zookeeper.{ ZooKeeper, WatchedEvent, Watcher } | ||
import AbdicateOnConnectionLossActor._ | ||
|
||
private[leadership] object AbdicateOnConnectionLossActor { | ||
def props(zk: ZooKeeperClient, leader: LeadershipAbdication): Props = { | ||
Props(new AbdicateOnConnectionLossActor(zk, leader)) | ||
} | ||
|
||
private val connectionDropped = Set( | ||
Watcher.Event.KeeperState.Disconnected, | ||
Watcher.Event.KeeperState.Expired | ||
) | ||
} | ||
|
||
/** | ||
* Register as ZK Listener and abdicates leadership on connection loss. | ||
*/ | ||
private[impl] class AbdicateOnConnectionLossActor(zk: ZooKeeperClient, | ||
leader: LeadershipAbdication) extends Actor with ActorLogging { | ||
|
||
private[impl] val watcher = new Watcher { | ||
val reference = self | ||
override def process(event: WatchedEvent): Unit = reference ! event | ||
} | ||
|
||
override def preStart(): Unit = { | ||
log.info("Register as ZK Listener") | ||
zk.register(watcher) | ||
//make sure, we are connected so we can act on subsequent events | ||
if (zk.get().getState != ZooKeeper.States.CONNECTED) leader.abdicateLeadership() | ||
} | ||
override def postStop(): Unit = { | ||
log.info("Unregister as ZK Listener") | ||
zk.unregister(watcher) | ||
} | ||
|
||
def disconnected(): Unit = { | ||
log.warning("ZooKeeper connection has been dropped. Abdicate Leadership.") | ||
leader.abdicateLeadership() | ||
} | ||
|
||
override def receive: Receive = { | ||
case event: WatchedEvent if connectionDropped.contains(event.getState) => disconnected() | ||
case event: WatchedEvent => log.info(s"Received ZooKeeper Status event: $event") | ||
} | ||
} |
12 changes: 9 additions & 3 deletions
12
src/test/scala/mesosphere/marathon/core/leadership/AlwaysElectedLeadershipModule.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...st/scala/mesosphere/marathon/core/leadership/impl/AbdicateOnConnectionLossActorTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package mesosphere.marathon.core.leadership.impl | ||
|
||
import akka.actor.ActorSystem | ||
import akka.testkit.{ TestActorRef, TestKit } | ||
import com.twitter.common.zookeeper.ZooKeeperClient | ||
import mesosphere.marathon.test.Mockito | ||
import mesosphere.marathon.{ LeadershipAbdication, MarathonSpec } | ||
import org.apache.zookeeper.{ ZooKeeper, WatchedEvent, Watcher } | ||
import org.scalatest.GivenWhenThen | ||
|
||
class AbdicateOnConnectionLossActorTest extends TestKit(ActorSystem("test")) with MarathonSpec with Mockito with GivenWhenThen { | ||
|
||
test("register as zk listener on start") { | ||
Given("ZK and leader refs") | ||
val leader = mock[LeadershipAbdication] | ||
|
||
When("The actor is created") | ||
val actor = TestActorRef(AbdicateOnConnectionLossActor.props(zk, leader)) | ||
|
||
Then("register is called") | ||
verify(zk).register(any) | ||
} | ||
|
||
test("zk disconnect events lead to abdication") { | ||
Given("A started AbdicateOnConnectionLossActor") | ||
val leader = mock[LeadershipAbdication] | ||
val actor = TestActorRef[AbdicateOnConnectionLossActor](AbdicateOnConnectionLossActor.props(zk, leader)) | ||
|
||
When("The actor is killed") | ||
val disconnected = new WatchedEvent(Watcher.Event.EventType.None, Watcher.Event.KeeperState.Disconnected, "") | ||
actor.underlyingActor.watcher.process(disconnected) | ||
|
||
Then("Abdication is called") | ||
verify(leader).abdicateLeadership() | ||
} | ||
|
||
test("other zk events do not lead to abdication") { | ||
Given("A started AbdicateOnConnectionLossActor") | ||
val leader = mock[LeadershipAbdication] | ||
val actor = TestActorRef[AbdicateOnConnectionLossActor](AbdicateOnConnectionLossActor.props(zk, leader)) | ||
|
||
When("An event is fired, that is not a disconnected event") | ||
val authFailed = new WatchedEvent(Watcher.Event.EventType.None, Watcher.Event.KeeperState.AuthFailed, "") | ||
actor.underlyingActor.watcher.process(authFailed) | ||
|
||
Then("Abdication is _NOT_ called") | ||
verify(leader, never).abdicateLeadership() | ||
} | ||
|
||
var zk: ZooKeeperClient = _ | ||
|
||
before { | ||
val zookeeper = mock[ZooKeeper] | ||
zookeeper.getState returns ZooKeeper.States.CONNECTED | ||
zk = mock[ZooKeeperClient] | ||
zk.get() returns zookeeper | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.