Skip to content

Commit

Permalink
CMR-9519 Fixing KMS replacement of cache when KMS API is down. (#1983)
Browse files Browse the repository at this point in the history
  • Loading branch information
eereiter authored Oct 20, 2023
1 parent a53cf62 commit a5345d8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
39 changes: 21 additions & 18 deletions common-app-lib/src/cmr/common_app/services/kms_fetcher.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,17 @@
{...}]}
:providers [...]}"
(:require
[clojure.string :as str]
[cmr.common-app.services.kms-lookup :as kms-lookup]
[cmr.common.cache :as cache]
[cmr.common.cache.deflating-cache :as deflating-cache]
[cmr.common.cache.fallback-cache :as fallback-cache]
[cmr.common.cache.single-thread-lookup-cache :as stl-cache]
[cmr.common.config :refer [defconfig]]
[cmr.common.jobs :refer [def-stateful-job]]
[cmr.common.log :as log :refer [debug info warn error]]
[cmr.common.services.errors :as errors]
[cmr.common.util :as util]
[cmr.redis-utils.redis-cache :as redis-cache]
[cmr.transmit.cache.consistent-cache :as consistent-cache]
[cmr.transmit.kms :as kms]))
[cmr.common-app.services.kms-lookup :as kms-lookup]
[cmr.common.cache :as cache]
[cmr.common.cache.deflating-cache :as deflating-cache]
[cmr.common.cache.fallback-cache :as fallback-cache]
[cmr.common.cache.single-thread-lookup-cache :as stl-cache]
[cmr.common.config :refer [defconfig]]
[cmr.common.jobs :refer [def-stateful-job]]
[cmr.common.log :as log :refer [debug info warn error]]
[cmr.redis-utils.redis-cache :as redis-cache]
[cmr.transmit.cache.consistent-cache :as consistent-cache]
[cmr.transmit.kms :as kms]))

(def nested-fields-mappings
"Mapping from field name to the list of subfield names in order from the top of the hierarchy to
Expand Down Expand Up @@ -78,10 +75,16 @@
"Calls GCMD KMS endpoints to retrieve the keywords. Response is a map structured in the same way
as used in the KMS cache."
[context]
(kms-lookup/create-kms-index
(into {}
(for [keyword-scheme (keys kms/keyword-scheme->field-names)]
[keyword-scheme (kms/get-keywords-for-keyword-scheme context keyword-scheme)]))))
(let [kms-cache (cache/context->cache context kms-cache-key)
kms-cache-value (cache/get-value kms-cache kms-cache-key)]
(kms-lookup/create-kms-index
(into {}
(for [keyword-scheme (keys kms/keyword-scheme->field-names)]
;; if the keyword-scheme-value is nil that means we could not get the KMS keywords
;; in this case use the cached value value instead so that we dont wipe out the cache.
(if-let [keyword-scheme-value (kms/get-keywords-for-keyword-scheme context keyword-scheme)]
[keyword-scheme keyword-scheme-value]
[keyword-scheme (get kms-cache-value keyword-scheme)]))))))

(defn get-kms-index
"Retrieves the GCMD keywords map from the cache."
Expand Down
28 changes: 28 additions & 0 deletions common-app-lib/test/cmr/common_app/services/kms_fetcher_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(ns cmr.common-app.services.kms-fetcher-test
(:require
[clojure.test :refer :all]
[cmr.common.cache :as cache]
[cmr.common-app.services.kms-fetcher :as fetcher]
[cmr.indexer.system :as idx-sys]
[cmr.transmit.kms :as trans-kms]))

(deftest validate-getting-kms-keywords-test
(let [context {:system (idx-sys/create-system)}
kms-cache (cache/context->cache context fetcher/kms-cache-key)
_ (#'fetcher/refresh-kms-cache context)
kms-map (cache/get-value kms-cache fetcher/kms-cache-key)]

(testing "Testing that KMS keywords such as projects exist after normal loading."
(is (some? (:projects kms-map))))

(testing "Test KMS keywords API returning nil for testing purposes."
(let [context (assoc context :testing-for-nil-keyword-scheme-value true)]
(is (nil? (trans-kms/get-keywords-for-keyword-scheme context :projects)))))

;; Test to make sure that if KMS keywords API returns nil for a keyword after
;; parsing, (KMS API is down) that previous cache value is used, so that we don't
;; wipe out the KMS keyword values in the cache.
(testing "Makes sure that KMS keywords are not wiped out."
(let [context (assoc context :testing-for-nil-keyword-scheme-value true)]
(#'fetcher/refresh-kms-cache context)
(is (some? (:projects (cache/get-value kms-cache fetcher/kms-cache-key))))))))
6 changes: 3 additions & 3 deletions transmit-lib/src/cmr/transmit/kms.clj
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@
subregion-4, but KMS has already implemented it. This allows the CMR to use either the subregion-3
or subregion-4 KMS output."
[keyword-scheme keyword-entries]
(def keyword-entries keyword-entries)
(if (= keyword-scheme :spatial-keywords)
(map #(dissoc % :subregion-4) keyword-entries)
keyword-entries))
Expand Down Expand Up @@ -301,9 +300,10 @@
:category \"Earth Observation Satellites\" :basis \"Space-based Platforms\"} ..."
[context keyword-scheme]
{:pre (some? (keyword-scheme keyword-scheme->field-names))}
(when-not (= keyword-scheme :spatial-keywords-old)
(when-not (or (= keyword-scheme :spatial-keywords-old)
(:testing-for-nil-keyword-scheme-value context))
(let [keywords
(parse-entries-from-csv keyword-scheme (get-by-keyword-scheme context keyword-scheme))]
(parse-entries-from-csv keyword-scheme (get-by-keyword-scheme context keyword-scheme))]
(info (format "Found %s keywords for %s" (count (keys keywords)) (name keyword-scheme)))
keywords)))

Expand Down

0 comments on commit a5345d8

Please sign in to comment.