Skip to content

Commit

Permalink
feat: expose ulid to uri mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
stakach committed Apr 11, 2024
1 parent 4aeea9b commit d94f140
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: redis_service_manager
version: 3.0.0
version: 3.1.0

dependencies:
ulid:
Expand Down
5 changes: 5 additions & 0 deletions spec/lookup_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ describe RedisServiceManager do
URI.parse("http://node2/node2"),
]

lookup_local.node_hash.should eq({
node1.ulid => URI.parse("http://node1/node1"),
node2.ulid => URI.parse("http://node2/node2"),
})

node2.unregister
node1.unregister
end
Expand Down
4 changes: 4 additions & 0 deletions spec/manager_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ describe RedisServiceManager do
node2.leader?.should be_true
node2.cluster_ready?.should be_true
leader.should eq("node2")
node2.node_hash.should eq({
node2.ulid => URI.parse(node2.uri),
node3.ulid => URI.parse(node3.uri),
})

node2.unregister
node3.unregister
Expand Down
6 changes: 3 additions & 3 deletions src/redis_service_manager.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class RedisServiceManager < Clustering
@node_info = NodeInfo.new(@uri, "")
end

getter version : String = ""
getter? registered : Bool = false
getter? watching : Bool = false

Expand All @@ -47,8 +46,9 @@ class RedisServiceManager < Clustering
node_keys.size
end

def cluster_nodes
@hash.to_h
def node_hash : Hash(String, URI)
hash = @lock.synchronize { @hash.to_h }.transform_keys(&.split("}.", 2)[1])
hash.transform_values { |uri| URI.parse(uri) }
end

def register : Bool
Expand Down
7 changes: 7 additions & 0 deletions src/redis_service_manager/clustering.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ abstract class Clustering

# the name of the service you are clustering
getter service : String
getter version : String = ""

getter rebalance_callbacks : Array((RendezvousHash, RebalanceComplete) ->)
getter cluster_stable_callbacks : Array(->)

# the service uri for this host
abstract def uri : String

# the id of the node
abstract def ulid : String

# Called when the cluster has changed
def on_rebalance(&callback : (RendezvousHash, RebalanceComplete) ->)
rebalance_callbacks << callback
Expand Down Expand Up @@ -44,6 +48,9 @@ abstract class Clustering

# returns the list of known nodes
abstract def rendezvous : RendezvousHash

# returns a node_id => URI mapping
abstract def node_hash : Hash(String, URI)
end

require "./clustering/*"
5 changes: 5 additions & 0 deletions src/redis_service_manager/clustering/discovery.cr
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class Clustering::Discovery
rendezvous.nodes.map { |node| URI.parse(node) }
end

# Returns the list of node IDs => URI mapping
def node_hash : Hash(String, URI)
@cluster.node_hash
end

protected def update_node_list(rendezvous : RendezvousHash)
@rendezvous = rendezvous
@last_updated = Time.utc
Expand Down

0 comments on commit d94f140

Please sign in to comment.