-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a forced by-node shards deallocation to all the allocation strate…
…gies
- Loading branch information
Showing
8 changed files
with
162 additions
and
67 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
76 changes: 76 additions & 0 deletions
76
src/main/scala/com/evolutiongaming/cluster/ExtendedShardAllocationStrategy.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,76 @@ | ||
package com.evolutiongaming.cluster | ||
|
||
import java.time.Instant | ||
|
||
import akka.actor.{ActorRef, ActorSystem, Address} | ||
import akka.cluster.sharding.ShardCoordinator.ShardAllocationStrategy | ||
import akka.cluster.sharding.ShardRegion | ||
|
||
import scala.collection.immutable | ||
import scala.concurrent.{ExecutionContext, Future} | ||
import scala.concurrent.duration._ | ||
|
||
abstract class ExtendedShardAllocationStrategy( | ||
system: ActorSystem, | ||
implicit val ec: ExecutionContext, | ||
maxSimultaneousRebalance: Int, | ||
deallocationTimeout: FiniteDuration) extends ShardAllocationStrategy { | ||
|
||
val addressHelper = AddressHelperExtension(system) | ||
import addressHelper._ | ||
|
||
class Node(val address: Address, added: Instant = Instant.now()) { | ||
def expired: Boolean = Instant.now() isAfter (added plusMillis deallocationTimeout.toMillis) | ||
override def equals(obj: Any): Boolean = obj match { | ||
case node: Node => address equals node.address | ||
case _ => false | ||
} | ||
override def hashCode(): Int = address.hashCode | ||
} | ||
|
||
@volatile | ||
private var nodesToForcedDeallocation: Set[Node] = Set.empty | ||
|
||
def deallocateShardsFromNode(regionGlobalAddress: Address): Unit = | ||
nodesToForcedDeallocation += new Node(regionGlobalAddress) | ||
|
||
protected def doRebalance( | ||
currentShardAllocations: Map[ActorRef, immutable.IndexedSeq[ShardRegion.ShardId]], | ||
rebalanceInProgress: Set[ShardRegion.ShardId]): Future[Set[ShardRegion.ShardId]] | ||
|
||
final def rebalance( | ||
currentShardAllocations: Map[ActorRef, immutable.IndexedSeq[ShardRegion.ShardId]], | ||
rebalanceInProgress: Set[ShardRegion.ShardId]): Future[Set[ShardRegion.ShardId]] = { | ||
|
||
def limitRebalance(f: => Set[ShardRegion.ShardId]): Set[ShardRegion.ShardId] = | ||
if (rebalanceInProgress.size >= maxSimultaneousRebalance) Set.empty | ||
else f take maxSimultaneousRebalance | ||
|
||
val shardsToRebalance: Future[Set[ShardRegion.ShardId]] = | ||
if (nodesToForcedDeallocation.isEmpty) { | ||
doRebalance(currentShardAllocations, rebalanceInProgress) | ||
} else { | ||
val emptyNodes = for { | ||
(k, v) <- currentShardAllocations if v.isEmpty | ||
} yield new Node(k.path.address.global) | ||
|
||
val nodesToRemove = (nodesToForcedDeallocation filter (_.expired)) ++ emptyNodes.toSet | ||
|
||
nodesToForcedDeallocation = nodesToForcedDeallocation -- nodesToRemove | ||
|
||
val shardsToForcedDeallocation = (for { | ||
(k, v) <- currentShardAllocations if nodesToForcedDeallocation contains new Node(k.path.address.global) | ||
} yield v).flatten.toSet -- rebalanceInProgress | ||
|
||
for { | ||
doRebalanceResult <- doRebalance(currentShardAllocations, rebalanceInProgress -- shardsToForcedDeallocation) | ||
} yield shardsToForcedDeallocation ++ doRebalanceResult | ||
} | ||
|
||
val result = for { | ||
shardsToRebalance <- shardsToRebalance | ||
} yield limitRebalance(shardsToRebalance) | ||
|
||
result | ||
} | ||
} |
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
14 changes: 8 additions & 6 deletions
14
src/main/scala/com/evolutiongaming/cluster/RequesterAllocationStrategy.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
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.