Skip to content

Commit

Permalink
replaced agents with request channel
Browse files Browse the repository at this point in the history
added more verbose logging for session periodic and final save
  • Loading branch information
mhaemmerle committed Oct 27, 2012
1 parent 8d6f701 commit dbc3303
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 185 deletions.
22 changes: 17 additions & 5 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[org.slf4j/slf4j-api "1.6.6"]
[org.slf4j/slf4j-log4j12 "1.6.6"]
[org.clojure/tools.logging "0.2.3"]
[aleph "0.3.0-beta4"]
[aleph "0.3.0-beta5"]
[zookeeper-clj "0.9.2"]
[ring "1.1.1"]
[compojure "1.1.1"]
Expand All @@ -12,10 +12,11 @@
[ring-json-params "0.1.3"]
[compojure "1.1.0"]
[slingshot "0.10.3"]
[cheshire "4.0.1"]
[clj-aws-s3 "0.3.2"]
[cheshire "4.0.3"]
[timewarrior/clj-aws-s3 "0.3.3"]
[org.clojure/tools.cli "0.2.1"]
[protobuf "0.6.1"]
[clj-oauth2 "0.2.0"]
[org.clojure/tools.nrepl "0.2.0-beta9"]]
:dev-dependencies [[lein-marginalia "0.7.1"]
[criterium "0.2.1"]]
Expand All @@ -27,6 +28,17 @@
:compiler {:output-to "resources/public/js/main.js"
:optimizations :whitespace
:pretty-print true}}]}
:jvm-opts ["-Djava.awt.headless=true" "-Dio.netty.epollBugWorkaround=true"
"-server" "-XX:+UseConcMarkSweepGC" "-Xmx2g" "-XX:NewSize=1g"]
:jvm-opts ["-server"
"-Djava.awt.headless=true"
"-Dio.netty.epollBugWorkaround=true"
"-XX:+UseConcMarkSweepGC"
"-XX:+UseParNewGC"
"-d64"
"-Xms4g"
"-Xmx4g"
"-XX:MaxPermSize=256m"
"-XX:NewRatio=2"
"-XX:ParallelGCThreads=8"
"-XX:ParallelCMSThreads=8"
"-XX:ConcGCThreads=8"]
:main grimoire.core)
40 changes: 8 additions & 32 deletions src/grimoire/map.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
2 {:name "contract 2" :duration 4 :reward 22}
3 {:name "contract 3" :duration 6 :reward 30}})

(def default-width 100)
(def default-height 100)
(def default-width 10)
(def default-height 10)

(defn ^:private m-keyword
([x y]
Expand All @@ -26,37 +26,13 @@
(defn generate-empty-map
;; Creates an empty map of the structure {:x1y1 nil :x1y2 nil}
[width height]
(reduce
#(assoc %1 (m-keyword %2) nil) {} (map vector (range width) (range height))))
(let [m (reduce
#(assoc! %1 (m-keyword %2) nil)
(transient {})
(mapcat (fn [x] (for [y (range width)] [x y])) (range height)))]
(persistent! m)))

(def start-map (generate-empty-map default-width default-height))

;; (defmacro defaction
;; [name body]
;; `(defn ~name
;; [request-body#]
;; (fn [channel# state#]
;; (try
;; (let [result-map# (~@body state# (:body request-body#))]
;; ;; (enqueue-and-close channel# (:response result-map#))
;; (enqueue channel# (:response result-map#))
;; (:result result-map#))
;; (catch Exception e#
;; (log/error "update failed with" (.getMessage e#))
;; (when-not (closed? channel#)
;; ;; (enqueue-and-close channel# {"error" "fatal"})
;; (enqueue channel# {"error" "fatal"})
;; state#))))))

;; (defaction update
;; (fn [state {:keys [id x y]}]
;; (let [entity-config (example-entities id)
;; coord-key (m-keyword x y)
;; entity (get-in state [:map coord-key])]
;; (-> state
;; (assoc-in [:map coord-key] {:id id})
;; (update-in [:balance] - (:cost entity-config))
;; build-response))))
(def empty-map (memoize generate-empty-map))

(defmulti update :verb)

Expand Down
29 changes: 16 additions & 13 deletions src/grimoire/registry.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,27 @@
(def ^:dynamic *client* nil)

(defn ^:private to-zk-location
[user-id & nodes]
(str registry-group-name "/" user-id (clojure.string/join "/" nodes)))
[user-id & z-nodes]
(str registry-group-name "/" user-id (clojure.string/join "/" z-nodes)))

(defn register
[user-id valid-until node]
(let [user-node (to-zk-location user-id)
lock-node (str user-node "/_lock-")]
(let [create-response (zk/create-all *client*
lock-node :persistent? false :sequential? true)
[user-id valid-until cluster-node]
(log/info "register" user-id valid-until cluster-node)
(let [user-znode (to-zk-location user-id)
lock-znode (str user-znode "/_lock-")]
(let [create-response (zk/create-all *client* lock-znode
:persistent? true :sequential? true)
create-id (zk-util/extract-id create-response)
user-node-response (zk/exists *client* user-node)]
user-node-response (zk/exists *client* user-znode)]
(if (= 0 create-id)
(zk/set-data *client* user-node
(cluster/serialize {:valid-until valid-until :node node})
(zk/set-data *client* user-znode
(cluster/serialize {:valid-until valid-until :node cluster-node})
(:version user-node-response))
(throw (Exception. (format "session_already_registered, args=[%s]" user-id)))))))

(defn deregister
[user-id]
(log/info "deregister" user-id (to-zk-location user-id))
(zk/delete-all *client* (to-zk-location user-id)))

(defn extend-timeout
Expand All @@ -49,12 +51,12 @@

(defn get-node-data
[user-id]
(:data (zk/exists *client* (to-zk-location user-id))))
(let [response (zk/data *client* (to-zk-location user-id))]
(cluster/deserialize (:data response))))

(defn get-location
[user-id]
(log/info "get-location" user-id)
(:node (get-node-data (to-zk-location user-id))))
(:node (get-node-data user-id)))

(defn local?
[user-id]
Expand All @@ -72,6 +74,7 @@
(when (nil? (zk/exists *client* registry-group-name))
(zk/create *client* registry-group-name :persistent? true)))

;; TODO check for dev environment
(defn ^:private dev-clean
[]
(let [remote-nodes (cluster/get-remote-nodes)]
Expand Down
Loading

0 comments on commit dbc3303

Please sign in to comment.