Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set current index on first call of config/index #997

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 26 additions & 11 deletions src/riemann/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -231,22 +231,37 @@
(reduce conj (:streams @next-core) things))))

(defn index
"Set the index used by this core. Returns the index."
"Create a new index and add it to the current core and next core as needed.

If the current core has no index, add the new index to it.

To the next core, add the current core's index (when equivalent) or add the
new index (when not equivalent).

In practice, because Riemann currently has only one index type (NBHM), only a
single index will ever be created. It will be preserved across all future
calls to `index`.

Returns the index."
[& opts]
(locking core
(let [index (:index @core)
(let [current-index (:index @core)
; Create a new index. Later we'll decide whether it needs to be added
; to the current core, next core, or both.
; Note that we need to wrap the *current* core's pubsub; the next
; core's pubsub module will be discarded in favor of the current one
; when core transition takes place.
index' (-> (apply riemann.index/index opts)
(core/wrap-index (:pubsub @core)))
; If the new index is equivalent to the old one, preserve the old
; one.
index' (if (service/equiv? index index')
index
index')]
(swap! next-core assoc :index index')
index')))
new-index (-> (apply riemann.index/index opts)
(core/wrap-index (:pubsub @core)))
; If the new index is equivalent to the old one, preserve the old one.
chosen-index (if (service/equiv? current-index new-index)
current-index
new-index)]
; If the current core has no index, add the newly created one.
(when (nil? current-index) (swap! core assoc :index chosen-index))
; Always add the chosen index to the next core, ready for core transition.
(swap! next-core assoc :index chosen-index)
chosen-index)))

(defn update-index
"Updates the given index with all events received. Also publishes to the
Expand Down
10 changes: 6 additions & 4 deletions test/riemann/config_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,12 @@
(deftest index-test
(let [i (index)]
(testing "index creation helper creates the index properly"
(is (satisfies? Index i))
(is (= i (:index @next-core)))
(is (nil? (:index @core))))
(testing "index is applied to the core properly during initial load"
(is (satisfies? Index i)))
(testing "index creation helper sets the index of the current core"
(is (= i (:index @core))))
(testing "index creation helper sets the index of the next core"
(is (= i (:index @next-core))))
(testing "current core index is preserved after apply"
(apply!)
(is (identical? i (:index @core))))
(testing "we have the proper reference to the index after a reload"
Expand Down