diff --git a/indexer-app/src/cmr/indexer/data/collection_granule_aggregation_cache.clj b/indexer-app/src/cmr/indexer/data/collection_granule_aggregation_cache.clj index 6fde9a0ce7..1539952a01 100644 --- a/indexer-app/src/cmr/indexer/data/collection_granule_aggregation_cache.clj +++ b/indexer-app/src/cmr/indexer/data/collection_granule_aggregation_cache.clj @@ -227,11 +227,11 @@ (pr-str updated-collections)) ;; Reindex the collections that were modified - (->> updated-collections - (meta-db/get-latest-concepts context) - ;; wrap each concept in its own batch for bulk-index - (map vector) - (index-service/bulk-index context es-config/elastic-name))))) + (let [concept-batches (->> updated-collections + (meta-db/get-latest-concepts context) + ;; wrap each concept in its own batch for bulk-index + (map vector))] + (index-service/bulk-index context concept-batches es-config/elastic-name))))) ;; There's no existing value so a full refresh is required. (full-cache-refresh context)))) diff --git a/indexer-app/test/cmr/indexer/test/data/collection_granule_aggregation_cache.clj b/indexer-app/test/cmr/indexer/test/data/collection_granule_aggregation_cache.clj index 20bb1a2620..3b2a01449e 100644 --- a/indexer-app/test/cmr/indexer/test/data/collection_granule_aggregation_cache.clj +++ b/indexer-app/test/cmr/indexer/test/data/collection_granule_aggregation_cache.clj @@ -6,9 +6,14 @@ [clojure.test :refer :all] [clojure.test.check.generators :as gen] [clojure.test.check.properties :refer [for-all]] + [cmr.common.cache :as cache] + [cmr.common.cache.in-memory-cache :as in-memory-cache] [cmr.common.test.test-check-ext :as ext-gen :refer [defspec]] [cmr.common.util :as u] - [cmr.indexer.data.collection-granule-aggregation-cache :as cgac])) + [cmr.elastic-utils.config :as es-config] + [cmr.indexer.data.collection-granule-aggregation-cache :as cgac] + [cmr.indexer.services.index-service :as index-service] + [cmr.transmit.metadata-db :as meta-db])) (def sample-response {:took 7, @@ -48,6 +53,50 @@ edn/read-string (#'cgac/cached-value->coll-gran-aggregates))))) +;; Regression test for CMR-11401 (bulk-index was called with concept-batches and es-cluster-name swapped). +(deftest partial-cache-refresh-reindexes-updated-collections-test + (testing "collections with updated temporal ranges are reindexed with proper concept batches" + (let [cgac-cache (in-memory-cache/create-in-memory-cache) + context {:system {:caches {cgac/coll-gran-aggregate-cache-key cgac-cache}}} + ;; Widen C1-PROV1's end date so it is detected as updated. Leave C2-PROV1 unchanged. + updated-granule-map {"C1-PROV1" {:granule-start-date (t/date-time 2003) + :granule-end-date (t/date-time 2010)}} + bulk-index-calls (atom []) + c1-concept {:concept-id "C1-PROV1" :revision-id 1}] + (cache/set-value cgac-cache + cgac/coll-gran-aggregate-cache-key + (#'cgac/coll-gran-aggregates->cachable-value sample-coll-gran-aggregates)) + (with-redefs [cgac/fetch-coll-gran-aggregates-updated-in-last-n + (fn [_context _granules-updated-in-last-n] updated-granule-map) + + meta-db/get-latest-concepts + (fn [_context concept-ids] (map #(assoc c1-concept :concept-id %) concept-ids)) + + index-service/bulk-index + (fn [_context concept-batches es-cluster-name] + (swap! bulk-index-calls conj {:concept-batches concept-batches + :es-cluster-name es-cluster-name}))] + (cgac/refresh-cache context 3900)) + + (testing "bulk-index is called exactly once" + (is (= 1 (count @bulk-index-calls)))) + + (let [{:keys [concept-batches es-cluster-name]} (first @bulk-index-calls)] + (testing "the elastic cluster name is passed as es-cluster-name, not as concept-batches" + (is (= es-config/elastic-name es-cluster-name))) + + (testing "concept-batches is a seq of concept batches - not a seq of Characters from the + cluster name string" + (is (= [[(assoc c1-concept :concept-id "C1-PROV1")]] + concept-batches)))) + + (testing "the cache was updated with the widened temporal range" + (is (= {"C1-PROV1" {:granule-start-date (t/date-time 2003) + :granule-end-date (t/date-time 2010)} + "C2-PROV1" (get sample-coll-gran-aggregates "C2-PROV1")} + (#'cgac/cached-value->coll-gran-aggregates + (cache/get-value cgac-cache cgac/coll-gran-aggregate-cache-key)))))))) + (defn times->longs "Converts all the times in a collection granule aggregate to long values. This makes it easier to compare and construct for testing."