From d94f140574a5035159cf62a2d6898fa044d10891 Mon Sep 17 00:00:00 2001 From: Stephen von Takach Date: Thu, 11 Apr 2024 19:14:02 +1000 Subject: [PATCH] feat: expose ulid to uri mappings --- shard.yml | 2 +- spec/lookup_spec.cr | 5 +++++ spec/manager_spec.cr | 4 ++++ src/redis_service_manager.cr | 6 +++--- src/redis_service_manager/clustering.cr | 7 +++++++ src/redis_service_manager/clustering/discovery.cr | 5 +++++ 6 files changed, 25 insertions(+), 4 deletions(-) diff --git a/shard.yml b/shard.yml index 44a8858..a2e2964 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: redis_service_manager -version: 3.0.0 +version: 3.1.0 dependencies: ulid: diff --git a/spec/lookup_spec.cr b/spec/lookup_spec.cr index 248ff34..5d400e2 100644 --- a/spec/lookup_spec.cr +++ b/spec/lookup_spec.cr @@ -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 diff --git a/spec/manager_spec.cr b/spec/manager_spec.cr index 2f25919..b0b0b88 100644 --- a/spec/manager_spec.cr +++ b/spec/manager_spec.cr @@ -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 diff --git a/src/redis_service_manager.cr b/src/redis_service_manager.cr index 02aa0ec..5f1a4df 100644 --- a/src/redis_service_manager.cr +++ b/src/redis_service_manager.cr @@ -27,7 +27,6 @@ class RedisServiceManager < Clustering @node_info = NodeInfo.new(@uri, "") end - getter version : String = "" getter? registered : Bool = false getter? watching : Bool = false @@ -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 diff --git a/src/redis_service_manager/clustering.cr b/src/redis_service_manager/clustering.cr index 2348e65..4b140e2 100644 --- a/src/redis_service_manager/clustering.cr +++ b/src/redis_service_manager/clustering.cr @@ -10,6 +10,7 @@ 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(->) @@ -17,6 +18,9 @@ abstract class Clustering # 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 @@ -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/*" diff --git a/src/redis_service_manager/clustering/discovery.cr b/src/redis_service_manager/clustering/discovery.cr index 98a0d5b..e600105 100644 --- a/src/redis_service_manager/clustering/discovery.cr +++ b/src/redis_service_manager/clustering/discovery.cr @@ -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