Skip to content

Commit 385badd

Browse files
committed
adding helper funcs
1 parent 0bcadbd commit 385badd

9 files changed

Lines changed: 187 additions & 6 deletions

File tree

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,56 @@
181181
(when-let [error (index-cfg-validation index-set es-cluster-name)]
182182
(errors/throw-service-error :invalid-data error)))
183183

184+
(defn get-index-set-revision
185+
"Fetch a specific revision of an index-set from metadata-db.
186+
If revision-id is nil, the latest revision is returned.
187+
Returns a map with :index-set containing :revision-id and :deleted."
188+
[context index-set-id revision-id]
189+
(if-let [concept-id (meta-db/get-concept-id context :index-set "CMR" (str index-set-id))]
190+
(let [concept (if revision-id
191+
(meta-db/get-concept context concept-id revision-id)
192+
(meta-db/get-latest-concept context concept-id))
193+
metadata-map (when-let [metadata (:metadata concept)]
194+
(json/parse-string metadata true))]
195+
(let [index-set (if (contains? metadata-map :index-set)
196+
(:index-set metadata-map)
197+
metadata-map)]
198+
{:index-set (assoc index-set
199+
:revision-id (:revision-id concept)
200+
:deleted (true? (:deleted concept)))}))
201+
(errors/throw-service-error :not-found
202+
(m/index-set-not-found-msg index-set-id))))
203+
204+
(defn save-index-set-to-mdb
205+
"Saves the provided combined-index-set to Metadata DB. Index set saved in DB is both the elastic and gran elastic index sets combined.
206+
Returns the revision-id from Metadata DB."
207+
[context combined-index-set]
208+
(let [index-set-id (get-in combined-index-set [:index-set :id])
209+
user-id (try
210+
(cxt/context->user-id context)
211+
(catch Exception _ "CMR"))]
212+
(let [concept {:concept-type :index-set
213+
:native-id (str index-set-id)
214+
:provider-id "CMR"
215+
:metadata (json/generate-string combined-index-set)
216+
:user-id user-id
217+
:format "application/json"}]
218+
(:revision-id (meta-db/save-concept context concept)))))
219+
220+
(defn- save-combined-index-set-to-mdb
221+
"Given a cluster's index-set (half of a combined index set), this func reconstructs the combined index-set of both elastic and gran elastic index-sets
222+
and saves it to Metadata DB. Uses the provided index-set for the current cluster and fetches the other from ES.
223+
Returns the revision-id from Metadata DB."
224+
[context es-index-set es-cluster-name]
225+
(let [index-set-id (get-in es-index-set [:index-set :id])
226+
other-cluster (if (= es-cluster-name es-config/elastic-name)
227+
es-config/gran-elastic-name
228+
es-config/elastic-name)
229+
;; Use es/get-index-set directly to avoid :not-found exception during initial creation
230+
other-index-set (es/get-index-set context other-cluster index-set-id)
231+
combined-index-set (util/deep-merge other-index-set es-index-set)]
232+
(save-index-set-to-mdb context combined-index-set)))
233+
184234
(defn index-requested-index-set
185235
"Index requested index-set along with generated elastic index names"
186236
[context index-set es-cluster-name]
@@ -874,6 +924,21 @@
874924
(errors/throw-service-error :internal-error
875925
(format "Failed to rollback resharding for [%s]; see server logs." index))))))
876926

927+
(defn sync-index-sets-from-db
928+
"Fetches all latest non-deleted index-sets from Metadata DB and creates them in Elasticsearch."
929+
[context]
930+
(info "Syncing index-sets from Metadata DB to Elasticsearch...")
931+
(let [index-sets (meta-db/find-concepts context {:latest true} :index-set)]
932+
(doseq [concept index-sets]
933+
(if (:deleted concept)
934+
(info (format "Skipping deleted index-set [%s]" (:concept-id concept)))
935+
(let [combined-index-set (json/parse-string (:metadata concept) true)
936+
index-set-id (get-in combined-index-set [:index-set :id])]
937+
(info (format "Restoring index-set [%s] (ID: %s) to Elasticsearch..."
938+
(:concept-id concept) index-set-id))
939+
;; Use put-index-set to ensure we update existing or create new
940+
(put-index-set context combined-index-set))))))
941+
877942
(defn reset
878943
"Put elastic in a clean state after deleting indices associated with index-sets and index-set docs."
879944
[context]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
(ns cmr.metadata-db.data.oracle.concepts.index-set
2+
"Implements multi-method variations for index-sets"
3+
(:require [cmr.metadata-db.data.oracle.concepts :as c]))
4+
5+
(defmethod c/db-result->concept-map :index-set
6+
[concept-type db provider-id result]
7+
(some-> (c/db-result->concept-map :default db provider-id result)
8+
(assoc :concept-type :index-set)
9+
(assoc :user-id (:user_id result))))
10+
11+
;; Only "CMR" provider is supported now which is not considered a 'small' provider.
12+
;; If we ever associate real providers with index-sets then we will need to add support
13+
;; for small providers as well.
14+
(defmethod c/concept->insert-args [:index-set false]
15+
[concept _]
16+
(let [{user-id :user-id} concept
17+
[cols values] (c/concept->common-insert-args concept)]
18+
[(concat cols ["user_id"])
19+
(concat values [user-id])]))

metadata-db-app/src/cmr/metadata_db/migrations/092_update_cmr_subscriptions_table.clj

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
"Migrates the database up to version 92."
88
[]
99
(println "cmr.metadata-db.migrations.092-update-cmr-sub-notifications-table up...")
10-
(h/sql "ALTER TABLE METADATA_DB.CMR_SUB_NOTIFICATIONS DROP COLUMN AWS_ARN")
11-
(h/sql "ALTER TABLE METADATA_DB.CMR_SUBSCRIPTIONS ADD AWS_ARN VARCHAR(2048) NULL"))
10+
(when (h/column-exists? "CMR_SUB_NOTIFICATIONS" "AWS_ARN")
11+
(h/sql "ALTER TABLE METADATA_DB.CMR_SUB_NOTIFICATIONS DROP COLUMN AWS_ARN"))
12+
(when-not (h/column-exists? "CMR_SUBSCRIPTIONS" "AWS_ARN")
13+
(h/sql "ALTER TABLE METADATA_DB.CMR_SUBSCRIPTIONS ADD AWS_ARN VARCHAR(2048) NULL")))
1214

1315
(defn down
1416
"Migrates the database down from version 92."
1517
[]
1618
(println "cmr.metadata-db.migrations.092-update-cmr-sub-notifications-table down.")
17-
(h/sql "ALTER TABLE METADATA_DB.CMR_SUB_NOTIFICATIONS ADD AWS_ARN VARCHAR(2048) NULL")
18-
(h/sql "ALTER TABLE METADATA_DB.CMR_SUBSCRIPTIONS DROP COLUMN AWS_ARN"))
19+
(when-not (h/column-exists? "CMR_SUB_NOTIFICATIONS" "AWS_ARN")
20+
(h/sql "ALTER TABLE METADATA_DB.CMR_SUB_NOTIFICATIONS ADD AWS_ARN VARCHAR(2048) NULL"))
21+
(when (h/column-exists? "CMR_SUBSCRIPTIONS" "AWS_ARN")
22+
(h/sql "ALTER TABLE METADATA_DB.CMR_SUBSCRIPTIONS DROP COLUMN AWS_ARN")))

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,15 @@
182182
(def humanizer-concept-validation
183183
"Builds a function that validates a concept map that has no provider and returns a list of errors"
184184
(v-util/compose-validations (conj base-concept-validations
185-
concept-id-matches-concept-fields-validation-no-provider
186-
humanizer-native-id-validation)))
185+
concept-id-matches-concept-fields-validation-no-provider
186+
humanizer-native-id-validation)))
187+
188+
(def index-set-concept-validation
189+
"Builds a function that validates a concept map that has no provider and returns a list of errors.
190+
Index-sets are global system-level entities and are not associated with any specific data provider.
191+
They are owned by the internal 'CMR' system provider."
192+
(v-util/compose-validations (conj base-concept-validations
193+
concept-id-matches-concept-fields-validation-no-provider)))
187194

188195
(def validate-concept-default
189196
"Validates a concept. Throws an error if invalid."
@@ -205,6 +212,10 @@
205212
"validates a humanizer concept. Throws an error if invalid."
206213
(v-util/build-validator :invalid-data humanizer-concept-validation))
207214

215+
(def validate-index-set-concept
216+
"validates an index-set concept. Throws an error if invalid."
217+
(v-util/build-validator :invalid-data index-set-concept-validation))
218+
208219
(defmulti validate-concept
209220
"Validates a concept. Throws an error if invalid."
210221
(fn [concept]
@@ -226,6 +237,10 @@
226237
[concept]
227238
(validate-humanizer-concept concept))
228239

240+
(defmethod validate-concept :index-set
241+
[concept]
242+
(validate-index-set-concept concept))
243+
229244
(defmethod validate-concept :service-association
230245
[concept]
231246
(validate-association-concept concept))

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@
212212
(format "Humanizer could not be associated with provider [%s]. Humanizer is system level entity."
213213
provider-id))
214214

215+
(defn index-sets-only-system-level
216+
[provider-id]
217+
(format "Index-set could not be associated with provider [%s]. Index-set is system level entity."
218+
provider-id))
219+
215220
(defn variable-associations-only-system-level
216221
[provider-id]
217222
(format (str "Variable association could not be associated with provider [%s]. "

metadata-db-app/src/config/mdb_migrate_helper.clj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,13 @@
122122
"select count(*) as c from all_sequences where sequence_name='CONCEPT_ID_SEQ'")
123123
seq-count (:c (first result))]
124124
(= 0 (int seq-count))))
125+
126+
(defn column-exists?
127+
"Returns true if the column exists in the table"
128+
[table-name column-name]
129+
(let [result (j/query
130+
(config/db)
131+
["select count(*) as c from all_tab_columns where owner='METADATA_DB' and table_name=upper(?) and column_name=upper(?)"
132+
table-name column-name])
133+
col-count (:c (first result))]
134+
(not= 0 (int col-count))))

system-int-test/src/cmr/system_int_test/utils/index_util.clj

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,29 @@
224224
:body (json/generate-string index-set)
225225
:connection-manager (s/conn-mgr)
226226
:throw-exceptions false}))
227+
228+
(defn create-index-set
229+
"Creates the given index-set in the indexer"
230+
[index-set]
231+
(client/post (url/indexer-index-sets-url)
232+
{:headers {transmit-config/token-header (transmit-config/echo-system-token)
233+
"content-type" "application/json"}
234+
:body (json/generate-string index-set)
235+
:connection-manager (s/conn-mgr)
236+
:throw-exceptions false}))
237+
238+
(defn delete-index-set
239+
"Deletes the index-set with the given id"
240+
[id]
241+
(client/delete (url/indexer-index-sets-by-id-url id)
242+
{:headers {transmit-config/token-header (transmit-config/echo-system-token)}
243+
:connection-manager (s/conn-mgr)
244+
:throw-exceptions false}))
245+
246+
(defn sync-index-sets-from-db
247+
"Triggers a sync of index-sets from database to elasticsearch"
248+
[]
249+
(client/post (url/index-set-sync-url)
250+
{:headers {transmit-config/token-header (transmit-config/echo-system-token)}
251+
:connection-manager (s/conn-mgr)
252+
:throw-exceptions false}))

system-int-test/src/cmr/system_int_test/utils/metadata_db_util.clj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,13 @@
9999
:url (url/mdb-expired-concept-cleanup-url)
100100
:headers {transmit-config/token-header (transmit-config/echo-system-token)}
101101
:connection-manager (s/conn-mgr)}))
102+
103+
(defn get-concept-id
104+
"Returns a concept-id for the given concept-type, provider-id, and native-id"
105+
[concept-type provider-id native-id]
106+
(let [response (client/get (url/mdb-concept-id-url concept-type provider-id native-id)
107+
{:accept :json
108+
:throw-exceptions false
109+
:connection-manager (s/conn-mgr)})
110+
body (json/decode (:body response) true)]
111+
(get body :concept-id)))

system-int-test/src/cmr/system_int_test/utils/url_helper.clj

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@
9999
[]
100100
(format "http://localhost:%s/concepts" (transmit-config/metadata-db-port)))
101101

102+
(defn mdb-concept-id-url
103+
"URL to concept id in mdb."
104+
[concept-type provider-id native-id]
105+
(format "http://localhost:%s/concept-id/%s/%s/%s"
106+
(transmit-config/metadata-db-port)
107+
(name concept-type)
108+
provider-id
109+
native-id))
110+
102111
(defn mdb-force-delete-concept-url
103112
[concept-id revision-id]
104113
(format "http://localhost:%s/concepts/force-delete/%s/%s"
@@ -673,6 +682,16 @@
673682
[]
674683
(format "http://localhost:%s/index-sets/reset" (transmit-config/indexer-port)))
675684

685+
(defn index-set-sync-url
686+
"Sync index-sets from database to elasticsearch."
687+
[]
688+
(format "http://localhost:%s/index-sets/sync-with-database" (transmit-config/indexer-port)))
689+
690+
(defn indexer-index-sets-url
691+
"Gets the index sets URL"
692+
[]
693+
(format "http://localhost:%s/index-sets" (transmit-config/indexer-port)))
694+
676695
(defn indexer-update-indexes
677696
"Updates the indexes in the indexer to update mappings and settings"
678697
[]
@@ -683,6 +702,14 @@
683702
[id]
684703
(format "http://localhost:%s/index-sets/%s" (transmit-config/indexer-port) (str id)))
685704

705+
(defn indexer-rebalance-url
706+
[id concept-id]
707+
(format "%s/rebalancing-collections/%s" (indexer-index-sets-by-id-url id) concept-id))
708+
709+
(defn indexer-reshard-url
710+
[id index-name]
711+
(format "%s/reshard/%s" (indexer-index-sets-by-id-url id) index-name))
712+
686713
(defn full-refresh-collection-granule-aggregate-cache-url
687714
[]
688715
(format "http://localhost:%s/jobs/trigger-full-collection-granule-aggregate-cache-refresh"

0 commit comments

Comments
 (0)