forked from sandreev/scala-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
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 sandreev#1 from RomezzBorisov/master
Implemented nodes migration support
- Loading branch information
Showing
16 changed files
with
670 additions
and
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.redis.cluster | ||
|
||
case class ClusterConfigDiff(newNodes: Map[String, NodeConfig], | ||
updatedNodes: Map[String, NodeConfig], | ||
deletedNodes: Map[String, NodeConfig]) | ||
|
||
object ClusterConfigDiff { | ||
def apply(oldCluster: Map[String, NodeConfig], newCluster: Map[String, NodeConfig]): ClusterConfigDiff = { | ||
val newNodes = newCluster -- oldCluster.keys | ||
val removedNodes = oldCluster -- newCluster.keys | ||
val changedNodes = newCluster.filter { | ||
case (name, cfg) if oldCluster.contains(name) => cfg != oldCluster(name) | ||
case _ => false | ||
} | ||
ClusterConfigDiff(newNodes, changedNodes, removedNodes) | ||
} | ||
} |
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,7 @@ | ||
package com.redis.cluster | ||
|
||
|
||
trait ClusterConfigListener { | ||
def configUpdated(newConfig: Map[String, NodeConfig]) | ||
|
||
} |
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,9 @@ | ||
package com.redis.cluster | ||
|
||
|
||
trait ConfigManager { | ||
def readConfig: Map[String, NodeConfig] | ||
|
||
def addListener(listener: ClusterConfigListener) | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/main/scala/com/redis/cluster/CopyOnWriteHashRing.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,39 @@ | ||
package com.redis.cluster | ||
|
||
import java.util.concurrent.atomic.AtomicReference | ||
|
||
|
||
class CopyOnWriteHashRing[T](nodes: Map[String, T], replicas: Int) { | ||
val ringRef = new AtomicReference[HashRing[T]](HashRing(nodes, replicas)) | ||
|
||
def addNode(nodeName: String, node: T) { | ||
val ring = ringRef.get() | ||
if (!ringRef.compareAndSet(ring, ring.addNode(nodeName, node))) | ||
addNode(nodeName, node) | ||
} | ||
|
||
def udpateNode(nodeName: String, node: T): T = { | ||
val ring = ringRef.get() | ||
val oldNode = ring.cluster(nodeName) | ||
val newRing = ring.removeNode(nodeName).addNode(nodeName, node) | ||
if (!ringRef.compareAndSet(ring, newRing)) | ||
udpateNode(nodeName, node) | ||
else | ||
oldNode | ||
} | ||
|
||
// remove node from the ring | ||
def removeNode(nodeName: String): T = { | ||
val ring = ringRef.get() | ||
val newRing = ring.removeNode(nodeName) | ||
if (!ringRef.compareAndSet(ring, newRing)) | ||
removeNode(nodeName) | ||
else | ||
ring.cluster(nodeName) | ||
} | ||
|
||
// get node for the key | ||
def getNode(key: Seq[Byte]): T = ringRef.get().getNode(key) | ||
|
||
def cluster = ringRef.get().cluster | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.redis.cluster | ||
|
||
case class NodeConfig(host: String, port: Int) { | ||
override def toString = host + ":" + port | ||
} |
Oops, something went wrong.