Skip to content

Commit 2ad115c

Browse files
authored
CMR-10139: Updated code to remove subscription information from redis cache upon subscription delete. Updated schema to allow multiple modes. (#2179)
* CMR-10139: Updating Ingest Subscriptions to add/delete needed information to redis. * CMR-10139: Adding in code to correctly delete the subscription from the redis subscription cache. * CMR-10139: Adding data to the ingest subscription cache. * CMR-10139: Fixing subscription api. * CMR-10139: Adding redis library. * CMR-10139: Removing debug statement. * CMR-10139: adding helper function to create tombstone.
1 parent 206bf56 commit 2ad115c

File tree

16 files changed

+369
-69
lines changed

16 files changed

+369
-69
lines changed

indexer-app/src/cmr/indexer/services/index_service.clj

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@
326326
(Long/parseLong (str milliseconds))
327327
all-value)))
328328

329+
#_{:clj-kondo/ignore [:unresolved-var]}
329330
(defn- send-time-to-visibility-log
330331
"Send either a JSON message as a report or the original log entry to info."
331332
[concept-id revision-id ms-durration all-revisions-index?]
@@ -337,15 +338,16 @@
337338
"Add a log message indicating the time it took to go from ingest to completed indexing."
338339
[{:keys [concept-id revision-id revision-date]} options]
339340
(let [now (tk/now)
340-
{:keys [all-revisions-index?]} options
341-
rev-datetime (f/parse (f/formatters :date-time) revision-date)
342-
ms-duration (t/in-millis (t/interval rev-datetime now))]
343-
341+
rev-datetime (f/parse (f/formatters :date-time) revision-date)]
344342
;; Guard against revision-date that is set to the future by a provider or a test.
343+
;; if the interval is set before with rev-datetime later an exception will be thrown.
345344
(if (t/before? rev-datetime now)
346-
;; WARNING: Splunk is dependent on this log message. DO NOT change this without updating
347-
;; Splunk searches used by ops.
348-
(send-time-to-visibility-log concept-id revision-id ms-duration all-revisions-index?)
345+
(let [{:keys [all-revisions-index?]} options
346+
interval (t/interval rev-datetime now)
347+
ms-duration (t/in-millis interval)]
348+
;; WARNING: Splunk is dependent on this log message. DO NOT change this without updating
349+
;; Splunk searches used by ops.
350+
(send-time-to-visibility-log concept-id revision-id ms-duration all-revisions-index?))
349351
(warn (format
350352
"Cannot compute time from ingest to search visibility for [%s] with revision date [%s]."
351353
concept-id
@@ -833,6 +835,7 @@
833835
(concept-mapping-types concept-type)
834836
{:term {(query-field->elastic-field :provider-id concept-type) provider-id}})))))
835837

838+
#_{:clj-kondo/ignore [:unresolved-var]}
836839
(defn publish-provider-event
837840
"Put a provider event on the message queue."
838841
[context msg]
@@ -881,6 +884,7 @@
881884
(es/update-indexes context params)
882885
(reset-index-set-mappings-cache context))
883886

887+
#_{:clj-kondo/ignore [:unresolved-var]}
884888
(def health-check-fns
885889
"A map of keywords to functions to be called for health checks"
886890
{:elastic_search #(es-util/health % :db)

ingest-app/src/cmr/ingest/services/ingest_service/subscription.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
:subscription-type (or (:Type subscription) "granule")
1616
:normalized-query (:normalized-query concept)
1717
:endpoint (:EndPoint subscription)
18-
:mode (:Mode subscription)}))
18+
:mode (:Mode subscription)
19+
:method (:Method subscription)}))
1920

2021
(declare save-subscription)
2122
#_{:clj-kondo/ignore [:unresolved-symbol]}

metadata-db-app/project.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
[nasa-cmr/cmr-common-lib "0.1.1-SNAPSHOT"]
1414
[nasa-cmr/cmr-message-queue-lib "0.1.0-SNAPSHOT"]
1515
[nasa-cmr/cmr-oracle-lib "0.1.0-SNAPSHOT"]
16+
[nasa-cmr/cmr-redis-utils-lib "0.1.0-SNAPSHOT"]
1617
[nasa-cmr/cmr-schemas "0.0.1-SNAPSHOT"]
1718
[nasa-cmr/cmr-umm-spec-lib "0.1.0-SNAPSHOT"]
1819
[org.clojure/clojure "1.11.2"]

metadata-db-app/src/cmr/metadata_db/api/routes.clj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
[cmr.common.api.context :as context]
1111
[cmr.common.api.errors :as errors]
1212
[cmr.common.cache :as cache]
13-
[cmr.common.log :refer (debug info warn error)]
13+
[cmr.common.log :refer (info)]
1414
[cmr.metadata-db.api.concepts :as concepts-api]
1515
[cmr.metadata-db.api.provider :as provider-api]
1616
[cmr.metadata-db.api.subscriptions :as subscription-api]
1717
[cmr.metadata-db.services.concept-service :as concept-service]
1818
[cmr.metadata-db.services.health-service :as hs]
1919
[cmr.metadata-db.services.jobs :as mdb-jobs]
20-
[compojure.core :refer :all]
20+
[compojure.core :refer [GET POST context routes]]
2121
[compojure.route :as route]
2222
[drift.core]
2323
[drift.execute]
@@ -54,12 +54,12 @@
5454
;; Dev dockerfile manually creates /app/cmr-files to store the unzipped cmr jar so that drift
5555
;; can find the migration files correctly
5656
;; we had to force method change in drift to set the correct path
57-
(if (not (instance? cmr.common.memory_db.connection.MemoryStore db))
57+
(when (not (instance? cmr.common.memory_db.connection.MemoryStore db))
5858
(try
5959
;; trying non-local path to find drift migration files for external oracle db"
6060
(with-redefs [drift.core/user-directory (fn [] (new File (str (.getProperty (System/getProperties) "user.dir") "/drift-migration-files")))]
6161
(drift.execute/run migrate-args))
62-
(catch Exception e
62+
(catch Exception _e
6363
(println "caught exception trying to find migration files. We are probably in local env w/ external db. Trying local route to migration files...")
6464
(with-redefs [drift.core/user-directory (fn [] (new File (str (.getProperty (System/getProperties) "user.dir") "/checkouts/metadata-db-app/src")))]
6565
(drift.execute/run migrate-args))))))
@@ -69,12 +69,12 @@
6969
(common-routes/job-api-routes
7070
(routes
7171
;; Trigger the old revision concept cleanup
72-
(POST "/old-revision-concept-cleanup" {:keys [request-context params headers]}
72+
(POST "/old-revision-concept-cleanup" {:keys [request-context]}
7373
(acl/verify-ingest-management-permission request-context :update)
7474
(mdb-jobs/old-revision-concept-cleanup request-context)
7575
{:status 204})
7676

77-
(POST "/expired-concept-cleanup" {:keys [request-context params headers]}
77+
(POST "/expired-concept-cleanup" {:keys [request-context]}
7878
(acl/verify-ingest-management-permission request-context :update)
7979
(mdb-jobs/expired-concept-cleanup request-context)
8080
{:status 204}))))

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22
"Defines the HTTP URL routes for the application as related to subscriptions."
33
(:require
44
[cmr.metadata-db.services.sub-notifications :as sub-note]
5-
[compojure.core :refer :all]))
5+
[cmr.metadata-db.services.subscriptions :as subscriptions]
6+
[compojure.core :refer [PUT POST context]]))
67

78
(defn- update-subscription-notification-time
89
"Update a subscription notification time"
910
[context params]
1011
(let [sub-id (:subscription-concept-id params)
11-
result (sub-note/update-subscription-notification context sub-id)]
12+
_ (sub-note/update-subscription-notification context sub-id)]
1213
{:status 204}))
1314

14-
; TODO add a get interface to help with testing. CMR-6621
15-
1615
(def subscription-api-routes
1716
(context "/subscription" []
1817
;; receive notification to update subscription time
1918
(PUT "/:subscription-concept-id/notification-time"
20-
{{:keys [subscription-concept-id] :as params} :params
19+
{params :params
2120
request-context :request-context}
22-
(update-subscription-notification-time request-context params))))
21+
(update-subscription-notification-time request-context params))
22+
(POST "/refresh-subscription-cache"
23+
{request-context :request-context}
24+
(subscriptions/refresh-subscription-cache request-context))))

metadata-db-app/src/cmr/metadata_db/data/oracle/concepts/subscription.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
subscription))
1414

1515
(defmethod concepts/db-result->concept-map :subscription
16-
[concept-type db provider-id result]
16+
[_concept-type db provider-id result]
1717
(some-> (concepts/db-result->concept-map :default db provider-id result)
1818
(assoc :concept-type :subscription)
1919
(assoc :provider-id (:provider_id result))
@@ -52,7 +52,7 @@
5252
(subscription-concept->insert-args concept))
5353

5454
(defmethod concepts/after-save :subscription
55-
[db provider sub]
55+
[db _provider sub]
5656
(when (:deleted sub)
5757
;; Cascade deletion to real deletes of subscription notifications. The intended functionality
5858
;; since the subscription_notification row is purged is that, if this subscription were

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

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns cmr.metadata-db.services.concept-service
22
"Services to support the business logic of the metadata db."
33
(:require
4+
[cheshire.core :as json]
45
[clj-time.core :as t]
56
[clojure.set :as set]
67
[cmr.common.api.context :as cmn-context]
@@ -757,6 +758,32 @@
757758

758759
:else nil))
759760

761+
(defn create-tombstone-concept
762+
"Helper function to create a tombstone concept by merging in parts of the
763+
previous concept to the tombstone."
764+
[metadata concept previous-revision]
765+
(let [{:keys [concept-id revision-id revision-date user-id]} concept
766+
{:keys [concept-type _]} (cu/parse-concept-id concept-id)]
767+
(if (= :subscription concept-type)
768+
(let [metadata-edn (json/decode metadata true)
769+
extra-fields (:extra-fields previous-revision)]
770+
(merge previous-revision {:concept-id concept-id
771+
:revision-id revision-id
772+
:revision-date revision-date
773+
:user-id user-id
774+
:metadata ""
775+
:deleted true
776+
:extra-fields (merge extra-fields
777+
{:endpoint (:EndPoint metadata-edn)
778+
:mode (:Mode metadata-edn)
779+
:method (:Method metadata-edn)})}))
780+
(merge previous-revision {:concept-id concept-id
781+
:revision-id revision-id
782+
:revision-date revision-date
783+
:user-id user-id
784+
:metadata metadata
785+
:deleted true}))))
786+
760787
;; true implies creation of tombstone for the revision
761788
(defmethod save-concept-revision true
762789
[context concept]
@@ -776,15 +803,11 @@
776803
;; to send concept updates and deletions out of order.
777804
(if (and (util/is-tombstone? previous-revision) (nil? revision-id))
778805
previous-revision
779-
(let [metadata (if (cu/generic-concept? concept-type)
806+
(let [metadata (if (or (cu/generic-concept? concept-type)
807+
(= :subscription concept-type))
780808
(:metadata previous-revision)
781809
"")
782-
tombstone (merge previous-revision {:concept-id concept-id
783-
:revision-id revision-id
784-
:revision-date revision-date
785-
:user-id user-id
786-
:metadata metadata
787-
:deleted true})]
810+
tombstone (create-tombstone-concept metadata concept previous-revision)]
788811
(cv/validate-concept tombstone)
789812
(validate-concept-revision-id db provider tombstone previous-revision)
790813
(let [revisioned-tombstone (->> (set-or-generate-revision-id db provider tombstone previous-revision)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
can sometimes be nil."
9797
[concept]
9898
(nil-fields-validation (apply dissoc (:extra-fields concept)
99-
[:delete-time :version-id :source-revision-id :associated-revision-id :target-provider-id :collection-concept-id :endpoint :mode])))
99+
[:delete-time :version-id :source-revision-id :associated-revision-id :target-provider-id :collection-concept-id :endpoint :mode :method])))
100100

101101
(defn concept-id-validation
102102
[concept]

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
<collection-concept-id> --> <ingest subscription map>
55
66
Example:
7-
'C12344554-PROV1' --> { 'enabled' true|false
8-
'mode' New|Update|Delete|All} "
7+
{Collection concept id 1: {\"New\" 1
8+
\"Update\" 1}
9+
Collection concept id 2: {\"New\" 2
10+
\"Update\" 1
11+
\"Delete\" 3}"
912
(:require
1013
[cmr.common.hash-cache :as hash-cache]
1114
[cmr.common.redis-log-util :as rl-util]
@@ -47,3 +50,12 @@
4750
(hash-cache/remove-value cache-client subscription-cache-key collection-concept-id))]
4851
(rl-util/log-redis-write-complete "ingest-subscription-cache remove-value" subscription-cache-key tm)
4952
value))
53+
54+
(defn get-keys
55+
"Gets the collection-concept-ids from the cache."
56+
[context]
57+
(let [cache-client (hash-cache/context->cache context subscription-cache-key)
58+
[tm value] (util/time-execution
59+
(hash-cache/get-keys cache-client subscription-cache-key))]
60+
(rl-util/log-redis-read-complete "ingest-subscription-cache get-keys" subscription-cache-key tm)
61+
value))

0 commit comments

Comments
 (0)