-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathrebalance_util.clj
More file actions
62 lines (55 loc) · 3 KB
/
rebalance_util.clj
File metadata and controls
62 lines (55 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
(ns cmr.bootstrap.data.rebalance-util
"Utilities for helping with rebalancing a collection."
(:require
[cmr.bootstrap.embedded-system-helper :as helper]
[cmr.elastic-utils.es-helper :as es-helper]
[cmr.elastic-utils.config :as es-config]
[cmr.indexer.indexer-util :as indexer-util]
[cmr.indexer.data.index-set :as index-set]))
(def granule-mapping-type-name
"The mapping type for granules in Elasticsearch"
"_doc")
(defn es-query-for-collection-concept-id
"Returns an elasticsearch query to find granules in the collection."
[concept-id]
{:bool {:must {:match_all {}}
:filter {:term {:collection-concept-id concept-id}}}})
(defn- granule-count-for-collection
"Gets the granule count for the collection in the elastic index."
[indexer-context index-name concept-id]
(let [conn (indexer-util/context->conn indexer-context es-config/gran-elastic-name)
query (es-query-for-collection-concept-id concept-id)]
(:count (es-helper/count-query conn index-name granule-mapping-type-name query))))
(defn rebalancing-collection-counts
"Returns the counts of a rebalancing collection from metadata db, the small collections index,
and the separate index."
[context concept-id]
(let [indexer-context {:system (helper/get-indexer (:system context))}
index-names (index-set/fetch-concept-type-index-names
indexer-context index-set/index-set-id)
granule-index-names (get-in index-names [:index-names :granule])
;; Small collections
small-coll-index (:small_collections granule-index-names)
small-count (granule-count-for-collection indexer-context small-coll-index concept-id)]
;; Metadata db counts will be added as part of CMR-2569
; mdb-context {:system (helper/get-metadata-db (:system context))}]
(if-let [sep-index (get granule-index-names (keyword concept-id))]
{:small-collections small-count
:separate-index (granule-count-for-collection indexer-context sep-index concept-id)}
{:small-collections small-count})))
(defn rebalancing-collection-status
"Returns the status of the rebalancing collections."
[context concept-id]
(let [indexer-context {:system (helper/get-indexer (:system context))}
rebalancing-info (index-set/fetch-rebalancing-collection-info indexer-context)]
(get (:rebalancing-status rebalancing-info) (keyword concept-id) "NOT_REBALANCING")))
(defn delete-collection-granules-from-small-collections
"Deletes by query any granules in small collections for the given collection"
[context concept-id]
(let [indexer-context {:system (helper/get-indexer (:system context))}
index-names (index-set/fetch-concept-type-index-names
indexer-context index-set/index-set-id)
small-coll-index (get-in index-names [:index-names :granule :small_collections])]
(es-helper/delete-by-query
(indexer-util/context->conn indexer-context es-config/gran-elastic-name) small-coll-index "_doc"
(es-query-for-collection-concept-id concept-id))))