|
181 | 181 | (when-let [error (index-cfg-validation index-set es-cluster-name)] |
182 | 182 | (errors/throw-service-error :invalid-data error))) |
183 | 183 |
|
| 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 | + |
184 | 234 | (defn index-requested-index-set |
185 | 235 | "Index requested index-set along with generated elastic index names" |
186 | 236 | [context index-set es-cluster-name] |
|
874 | 924 | (errors/throw-service-error :internal-error |
875 | 925 | (format "Failed to rollback resharding for [%s]; see server logs." index)))))) |
876 | 926 |
|
| 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 | + |
877 | 942 | (defn reset |
878 | 943 | "Put elastic in a clean state after deleting indices associated with index-sets and index-set docs." |
879 | 944 | [context] |
|
0 commit comments