Skip to content

Commit

Permalink
Set current index on first call of config/index
Browse files Browse the repository at this point in the history
Fix the "orphaned indicies" problem described in riemann#957.

Allows a Riemann config to make multiple calls to riemann.config/index
with predictable results, both on initial load and after a reload.

Without this change, multiple calls are OK after reload or explicit
apply, but create orphan indices on initial load.

Closes: riemann#957
  • Loading branch information
jarpy committed Jul 2, 2021
1 parent 2d590cf commit c13f6d6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
39 changes: 28 additions & 11 deletions src/riemann/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -231,22 +231,39 @@
(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 core and next core as needed.
If the current core has no index, add the new index to it.
If the next core has no index, 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 new 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

0 comments on commit c13f6d6

Please sign in to comment.