Skip to content

Commit f863c9a

Browse files
committed
CMR-10139: Re-writing the cache contents to cover when the same subscription is updated.
1 parent 2ad115c commit f863c9a

File tree

4 files changed

+315
-160
lines changed

4 files changed

+315
-160
lines changed

metadata-db-app/src/cmr/metadata_db/services/concept_service.clj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@
852852
(= concept-type :tool-association))
853853
(ingest-events/publish-event
854854
context (ingest-events/concept-delete-event revisioned-tombstone)))
855-
(subscriptions/delete-subscription context concept-type revisioned-tombstone)
855+
(subscriptions/change-subscription context concept-type revisioned-tombstone)
856856
revisioned-tombstone)))
857857
(if revision-id
858858
(cmsg/data-error :not-found
@@ -940,8 +940,7 @@
940940
(ingest-events/publish-event
941941
context
942942
(ingest-events/concept-update-event concept))
943-
944-
(subscriptions/add-subscription context concept-type concept)
943+
(subscriptions/change-subscription context concept-type concept)
945944
concept)))
946945

947946
(defn- delete-associated-tag-associations

metadata-db-app/src/cmr/metadata_db/services/subscription_cache.clj

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
(ns cmr.metadata-db.services.subscription-cache
22
"Defines common functions and defs for the subscription cache.
33
Structure of the hash-cache is as follows:
4-
<collection-concept-id> --> <ingest subscription map>
4+
<collection-concept-id> --> <ingest subscription vector>
55
66
Example:
7-
{Collection concept id 1: {\"New\" 1
8-
\"Update\" 1}
9-
Collection concept id 2: {\"New\" 2
10-
\"Update\" 1
11-
\"Delete\" 3}"
7+
{Collection concept id 1: [\"New\" \"Update\"]
8+
Collection concept id 2: [\"New\" \"Update\" \"Delete\"]"
129
(:require
1310
[cmr.common.hash-cache :as hash-cache]
1411
[cmr.common.redis-log-util :as rl-util]

metadata-db-app/src/cmr/metadata_db/services/subscriptions.clj

Lines changed: 47 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -24,82 +24,64 @@
2424
(and subscriptions-enabled?
2525
(= :granule concept-type)))
2626

27-
(defn remove-from-existing-mode
28-
"Depending on the passed in new-mode ['New' 'Update'] remove from the structure
29-
{Collection concept id: {\"New\" 1
30-
\"Update\" 1}}
31-
either the count or the mode if count is 0."
32-
[existing-mode new-mode]
33-
(loop [ms new-mode
34-
result existing-mode]
35-
(let [mode (first ms)
36-
mode-count (if (and result
37-
(result mode))
38-
(result mode) 0)
39-
compare-to-one (compare mode-count 1)]
40-
(if mode
41-
(when (seq result)
42-
(if (or (= 0 compare-to-one)
43-
(= -1 compare-to-one))
44-
(recur (rest ms) (dissoc result mode))
45-
(recur (rest ms) (assoc result mode (dec mode-count)))))
46-
result))))
47-
48-
(defn delete-subscription
49-
"When a subscription is deleted, the collection-concept-id must be removed
50-
from the subscription cache. Decrement the count if more than 1 subscription
51-
uses the collection-concept-id."
52-
[context concept-type revisioned-tombstone]
53-
(when (subscription-concept? concept-type revisioned-tombstone)
54-
(let [coll-concept-id (:collection-concept-id (:extra-fields revisioned-tombstone))
55-
existing-value (subscription-cache/get-value context coll-concept-id)
56-
mode (:mode (:extra-fields revisioned-tombstone))
57-
new-mode (remove-from-existing-mode existing-value mode)]
58-
(if (seq new-mode)
59-
(subscription-cache/set-value context coll-concept-id new-mode)
60-
(subscription-cache/remove-value context coll-concept-id)))))
27+
(defn ^:dynamic get-subscriptions-from-db
28+
"Get the subscriptions from the database. This function primarily exists so that
29+
it can be stubbed out for unit tests."
30+
([context]
31+
(mdb-search/find-concepts context {:latest true
32+
:concept-type :subscription
33+
:subscription-type "granule"}))
34+
([context coll-concept-id]
35+
(mdb-search/find-concepts context {:latest true
36+
:concept-type :subscription
37+
:collection-concept-id coll-concept-id})))
6138

6239
(defn add-to-existing-mode
63-
"Depending on the passed in new-mode ['New' 'Update'] create a structure that looks like
64-
{Collection concept id: {\"New\" 1
65-
\"Update\" 1}}"
66-
[existing-mode new-mode]
67-
(loop [ms new-mode
68-
result existing-mode]
69-
(let [mode (first ms)
70-
mode-count (if (and result
71-
(result mode))
72-
(result mode)
73-
0)]
74-
(if mode
40+
"Depending on the passed in new-mode [\"New\" \"Update\"] create a structure that merges
41+
the new mode to the existing mode. The result looks like [\"New\" \"Update\"]"
42+
[existing-modes new-modes]
43+
(loop [ms new-modes
44+
result existing-modes]
45+
(let [mode (first ms)]
46+
(if (nil? mode)
47+
result
7548
(if result
76-
(recur (rest ms) (assoc result mode (inc mode-count)))
77-
(recur (rest ms) (assoc {} mode (inc mode-count))))
78-
result))))
49+
(if (some #(= mode %) result)
50+
(recur (rest ms) result)
51+
(recur (rest ms) (merge result mode)))
52+
(recur (rest ms) [mode]))))))
53+
54+
(defn merge-modes
55+
"Go through the list of subscriptions to see if any exist that match the
56+
passed in collection-concept-id. Return true if any subscription exists
57+
otherwise return false."
58+
[subscriptions]
59+
(loop [subs subscriptions
60+
result []]
61+
(let [sub (first subs)]
62+
(if (nil? sub)
63+
result
64+
(recur (rest subs) (add-to-existing-mode result (get-in sub [:extra-fields :mode])))))))
7965

80-
(defn add-subscription
81-
"When a subscription is added, the collection-concept-id must be put into
82-
the subscription cache. If the collection-concept-id already exists then another
83-
subscription is also using it so increment the count. The count is used to determine
84-
whether or not the entry can be deleted. The modes are concatenated so that the user
85-
of the cache knows which modes to key off of."
66+
(defn change-subscription
67+
"When a subscription is added or deleted, the collection-concept-id must be put into
68+
or deleted from the subscription cache. Get the subscriptions that match the collection-concept-id
69+
from the database and rebuild the modes list."
8670
[context concept-type concept]
8771
(when (subscription-concept? concept-type concept)
8872
(let [coll-concept-id (:collection-concept-id (:extra-fields concept))
89-
mode (:mode (:extra-fields concept))
90-
existing-mode (subscription-cache/get-value context coll-concept-id)
91-
new-value (add-to-existing-mode existing-mode mode)]
92-
(subscription-cache/set-value context coll-concept-id new-value))))
73+
subs (filter #(subscription-concept? (get % :concept-type) %)
74+
(get-subscriptions-from-db context coll-concept-id))]
75+
(if (seq subs)
76+
(subscription-cache/set-value context coll-concept-id (merge-modes subs))
77+
(subscription-cache/remove-value context coll-concept-id)))))
9378

9479
(defn create-subscription-cache-contents-for-refresh
9580
"Go through all of the subscriptions and find the ones that are
9681
ingest subscriptions. Create the mode values for each collection-concept-id
9782
and put those into a map. The end result looks like:
98-
{Collection concept id 1: {\"New\" 1
99-
\"Update\" 1}
100-
Collection concept id 2: {\"New\" 2
101-
\"Update\" 1
102-
\"Delete\" 3}
83+
{Collection concept id 1: [\"New\" \"Update\"]
84+
Collection concept id 2: [\"New\" \"Update\" \"Delete\"]
10385
...}"
10486
[result sub]
10587
(let [metadata (:metadata sub)
@@ -111,16 +93,9 @@
11193
mode (:Mode metadata-edn)]
11294
(if concept-map
11395
(update result coll-concept-id #(add-to-existing-mode % mode))
114-
(assoc concept-map coll-concept-id (add-to-existing-mode nil mode))))
96+
(assoc result coll-concept-id (add-to-existing-mode nil mode))))
11597
result)))
11698

117-
(defn ^:dynamic get-subscriptions-from-db
118-
"Get the subscriptions from the database. This function primarily exists so that
119-
it can be stubbed out for unit tests."
120-
[context]
121-
(mdb-search/find-concepts context {:latest true
122-
:concept-type :subscription}))
123-
12499
(defn refresh-subscription-cache
125100
"Go through all of the subscriptions and create a map of collection concept ids and
126101
their mode values. Get the old keys from the cache and if the keys exist in the new structure

0 commit comments

Comments
 (0)