Permalink
Browse files

add zk-root option to specify zookeeper root path

  • Loading branch information...
1 parent 5f10fd7 commit f2f791d495fc700c5499312183468500f346cfad @sunng87 committed Aug 6, 2012
Showing with 43 additions and 14 deletions.
  1. +13 −8 src/slacker/client/cluster.clj
  2. +14 −6 src/slacker/server/cluster.clj
  3. +16 −0 src/slacker/utils.clj
@@ -58,8 +58,8 @@
:NodeChildrenChanged (refresh-all-servers sc) ;;TODO
nil))
-(defn- meta-data-from-zk [zk-conn cluster-name fname]
- (let [fnode (utils/zk-path cluster-name "functions" fname)]
+(defn- meta-data-from-zk [zk-conn zk-root cluster-name fname]
+ (let [fnode (utils/zk-path zk-root cluster-name "functions" fname)]
(if-let [node-data (zk/data zk-conn fnode)]
(deserialize :clj (:data node-data) :bytes))))
@@ -69,7 +69,8 @@
options]
CoordinatorAwareClient
(refresh-associated-servers [this nsname]
- (let [node-path (utils/zk-path cluster-name "namespaces" nsname)
+ (let [node-path (utils/zk-path (:zk-root options)
+ cluster-name "namespaces" nsname)
servers (zk/children zk-conn node-path
:watch? true)]
;; update servers for this namespace
@@ -82,7 +83,7 @@
(swap! slacker-clients assoc s sc))))
servers))
(refresh-all-servers [this]
- (let [node-path (utils/zk-path cluster-name "servers")
+ (let [node-path (utils/zk-path (:zk-root options) cluster-name "servers")
servers (into #{} (zk/children zk-conn node-path :watch? true))]
;; close connection to offline servers, remove from slacker-clients
(doseq [s (keys @slacker-clients)]
@@ -121,10 +122,12 @@
(case cmd
:functions
(let [nsname (or args "")
- ns-root (utils/zk-path cluster-name "functions" nsname)
+ ns-root (utils/zk-path (:zk-root options) cluster-name
+ "functions" nsname)
fnames (or (zk/children zk-conn ns-root) [])]
(map #(str nsname "/" %) fnames))
- :meta (meta-data-from-zk zk-conn cluster-name args))))
+ :meta (meta-data-from-zk zk-conn (:zk-root options)
+ cluster-name args))))
(defn- on-zk-events [e sc]
(if (.endsWith ^String (:path e) "servers")
@@ -147,8 +150,10 @@
options)]
(zk/register-watcher zk-conn (fn [e] (on-zk-events e sc)))
;; watch 'servers' node
- (zk/children zk-conn
- (utils/zk-path cluster-name "servers") :watch? true)
+ (zk/children zk-conn (utils/zk-path (:zk-root options)
+ cluster-name
+ "servers")
+ :watch? true)
sc))
@@ -38,22 +38,30 @@
"publish server information to zookeeper as cluster for client"
[cluster port ns-names funcs-map]
(let [cluster-name (cluster :name)
+ zk-root (cluster :zk-root)
server-node (str (or (cluster :node)
(auto-detect-ip (:zk cluster)))
":" port)
funcs (keys funcs-map)]
- (create-node *zk-conn* (utils/zk-path cluster-name "servers")
+ (create-node *zk-conn* (utils/zk-path zk-root cluster-name "servers")
:persistent? true)
- (create-node *zk-conn*
- (utils/zk-path cluster-name "servers" server-node ))
+ (create-node *zk-conn* (utils/zk-path zk-root
+ cluster-name
+ "servers"
+ server-node ))
(doseq [nn ns-names]
- (create-node *zk-conn* (utils/zk-path cluster-name "namespaces" nn)
+ (create-node *zk-conn* (utils/zk-path zk-root
+ cluster-name
+ "namespaces"
+ nn)
:persistent? true)
- (create-node *zk-conn* (utils/zk-path cluster-name "namespaces"
+ (create-node *zk-conn* (utils/zk-path zk-root
+ cluster-name
+ "namespaces"
nn server-node)))
(doseq [fname funcs]
(create-node *zk-conn*
- (utils/zk-path cluster-name "functions" fname )
+ (utils/zk-path zk-root cluster-name "functions" fname)
:persistent? true
:data (serialize
:clj
View
@@ -0,0 +1,16 @@
+(ns slacker.utils
+ (:refer-clojure :exclude [replace])
+ (:use [clojure.string :only [join replace]]))
+
+
+(defn zk-path
+ "concat a list of string to zookeeper path"
+ [zk-root & nodes]
+ (str (or zk-root "/slacker/cluster/") (join "/" nodes)))
+
+(defn escape-zkpath [fname]
+ (replace fname "/" "_slash_"))
+
+(defn unescape-zkpath [fname]
+ (replace fname "_slash_" "/"))
+

0 comments on commit f2f791d

Please sign in to comment.