This repository was archived by the owner on Jul 5, 2025. It is now read-only.
forked from etaty/rediscala
-
Notifications
You must be signed in to change notification settings - Fork 8
RedisCluster -> Mutable/Immutable #13
Open
jhiggins-bam
wants to merge
1
commit into
Ma27:master
Choose a base branch
from
jhiggins-bam:feature-mutable-cluster
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,35 @@ | ||
| package redis | ||
|
|
||
| import java.net.InetAddress | ||
| import java.time.Duration | ||
|
|
||
| import akka.actor.ActorSystem | ||
|
|
||
| import scala.concurrent.Future | ||
|
|
||
| /** | ||
| * A mutable Redis cluster that uses the CLUSTER NODES command, which broadcasts out to | ||
| * all nodes in the cluster and returns a list of nodes, to connect to new nodes and | ||
| * disconnect from old nodes automatically | ||
| */ | ||
| class ClusterCommandRedisCluster(configEndpoint: String, | ||
| port: Option[Int] = None, | ||
| timeout: Option[Duration] = Some(Duration.ofSeconds(30))) | ||
| (implicit _system: ActorSystem) extends MutableRedisCluster(timeout, Seq()) { | ||
|
|
||
| val ipPortPattern = """(([0-9]{1,3}\.){3}[0-9]{1,3})\:([0-9]+).*"""r("ip", "unused", "port") | ||
|
|
||
| def getNodesFromCluster(): Future[Seq[RedisServer]] = { | ||
| clusterNodes().map(_.map(info => { | ||
| val ipPortPattern(ip, _, port) = info.ip_port | ||
| RedisServer(ip, port.toInt) | ||
| })) | ||
| } | ||
|
|
||
| private def cnameToARecords(cname: String): Seq[String] = InetAddress.getAllByName(cname).map(_.getHostAddress) | ||
|
|
||
| private def seedCluster(): Unit = cnameToARecords(configEndpoint).map(RedisServer(_, port.getOrElse(6379))).foreach(addServer) | ||
|
|
||
| seedCluster() | ||
| asyncRefreshClusterSlots(true) | ||
| } |
This file contains hidden or 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,28 @@ | ||
| package redis | ||
|
|
||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| import akka.actor.ActorSystem | ||
|
|
||
| import scala.concurrent.Await | ||
| import scala.concurrent.duration.Duration | ||
|
|
||
|
|
||
| /* | ||
| * An immutable Redis cluster implementation. Only the servers that are passed to the constructor will be used. | ||
| */ | ||
| class ImmutableRedisCluster(val redisServers: Seq[RedisServer], | ||
| override val name: String = "RedisClientPool", | ||
| password: Option[String] = None) | ||
| (implicit _system: ActorSystem, | ||
| redisDispatcher: RedisDispatcher = RedisDispatcher("rediscala.rediscala-client-worker-dispatcher") | ||
| ) extends RedisCluster { | ||
| override val redisServerConnections = { | ||
| redisServers.map { server => | ||
| makeRedisConnection(server, defaultActive = true) | ||
| } toMap | ||
| } | ||
|
|
||
| refreshConnections() | ||
| Await.result(asyncRefreshClusterSlots(force=true), Duration(10,TimeUnit.SECONDS)) | ||
| } |
This file contains hidden or 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,105 @@ | ||
| package redis | ||
|
|
||
| import akka.actor.ActorSystem | ||
| import java.time.Duration | ||
|
|
||
| import scala.concurrent.Future | ||
| import scala.concurrent.duration._ | ||
| import scala.concurrent.stm.Ref | ||
|
|
||
|
|
||
| /* | ||
| * A Redis cluster that can dynamically add and remove nodes. | ||
| * Refreshes the node listing automatically based on a timeout parameter. | ||
| */ | ||
| abstract class MutableRedisCluster(timeout: Option[Duration], | ||
| val initialServers: Seq[RedisServer], | ||
| override val name: String = "RedisClientPool", | ||
| password: Option[String] = None) | ||
| (implicit _system: ActorSystem, | ||
| redisDispatcher: RedisDispatcher = RedisDispatcher("rediscala.rediscala-client-worker-dispatcher") | ||
| ) extends RedisCluster { | ||
|
|
||
| def refreshNodeReferences(): Future[Unit] = refreshNodeReferencesHelper(false) | ||
|
|
||
| def getNodesFromCluster(): Future[Seq[RedisServer]] | ||
|
|
||
| override val redisServerConnections: collection.mutable.Map[RedisServer, RedisConnection] = { | ||
| collection.mutable.Map() ++ initialServers.map(makeConnection).toMap[RedisServer, RedisConnection] | ||
| } | ||
|
|
||
| private def makeConnection(server: RedisServer): (RedisServer, RedisConnection) = | ||
| makeRedisConnection( | ||
| server = server.copy(password = password, db = None), | ||
| defaultActive = true | ||
| ) | ||
|
|
||
| def redisServers: Seq[RedisServer] = redisServerConnections.synchronized { | ||
| redisServerConnections.keys.toSeq | ||
| } | ||
|
|
||
| def addServer(server: RedisServer) { | ||
| if (!redisServerConnections.contains(server)) { | ||
| redisServerConnections.synchronized { | ||
| if (!redisServerConnections.contains(server)) { | ||
| log.info(s"Adding connection: $server") | ||
| redisServerConnections += makeRedisConnection(server) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| def removeServer(askServer: RedisServer) { | ||
| if (redisServerConnections.contains(askServer)) { | ||
| log.info(s"Removing connection: $askServer") | ||
| redisServerConnections.synchronized { | ||
| redisServerConnections.get(askServer).foreach { redisServerConnection => | ||
| _system stop redisServerConnection.actor | ||
| } | ||
| redisServerConnections.remove(askServer) | ||
| refreshConnections() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| protected val lockRefreshNodes = Ref(false) | ||
|
|
||
| protected def refreshNodeReferencesHelper(force: Boolean = false): Future[Unit] = { | ||
| if (lockRefreshNodes.single.compareAndSet(false, true)) { | ||
| for { | ||
| refreshedNodes <- getNodesFromCluster() | ||
| } yield { | ||
| val currentNodes = redisServerConnections.toSeq.map(_._1) | ||
| log.debug(s"refreshNodeReferences: $refreshedNodes") | ||
| refreshedNodes match { | ||
| case Nil => log.warning("Refreshed nodes is an empty list - not updating connections") | ||
| case _ => | ||
| refreshedNodes.foreach(node => { | ||
| if (!currentNodes.contains(node)) { | ||
| addServer(node) | ||
| } | ||
| }) | ||
| currentNodes.foreach(node => { | ||
| if (!refreshedNodes.contains(node)) { | ||
| removeServer(node) | ||
| } | ||
| }) | ||
| } | ||
| refreshConnections() | ||
| asyncRefreshClusterSlots(force) | ||
| } | ||
| lockRefreshNodes.single.compareAndSet(true, false) | ||
| } | ||
| Future.successful(()) | ||
| } | ||
|
|
||
| protected val timeoutMillis: Option[Long] = timeout.map(_.toMillis) | ||
|
|
||
| if(timeout.isDefined) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like |
||
| val loopingTimer = timeout.get.getSeconds.seconds | ||
| log.info(s"Registering elasticache node refresh on a loop of $loopingTimer seconds.") | ||
| _system.scheduler.schedule(initialDelay = loopingTimer, interval = loopingTimer)({ | ||
| refreshNodeReferences() | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Procedure syntax is deprecated