-
Notifications
You must be signed in to change notification settings - Fork 160
/
node.clj
245 lines (214 loc) · 10.7 KB
/
node.clj
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
(ns crux.node
(:require [clojure.java.io :as io]
[clojure.spec.alpha :as s]
[clojure.tools.logging :as log]
[com.stuartsierra.dependency :as dep]
[crux.api :as api]
[crux.backup :as backup]
[crux.codec :as c]
[crux.config :as cc]
[crux.db :as db]
[crux.index :as idx]
[crux.io :as cio]
[crux.kv :as kv]
[crux.lru :as lru]
crux.object-store
[crux.query :as q]
[crux.status :as status]
[crux.topology :as topo]
[crux.tx :as tx]
[crux.tx.event :as txe]
[crux.bus :as bus]
[crux.tx.conform :as txc])
(:import [crux.api ICruxAPI ICruxAsyncIngestAPI NodeOutOfSyncException ICursor]
java.io.Closeable
java.util.function.Consumer
[java.util.concurrent Executors]
java.util.concurrent.locks.StampedLock))
(def crux-version
(when-let [pom-file (io/resource "META-INF/maven/juxt/crux-core/pom.properties")]
(with-open [in (io/reader pom-file)]
(let [{:strs [version revision]} (cio/load-properties in)]
{:crux.version/version version
:crux.version/revision revision}))))
(defn- ensure-node-open [{:keys [closed?]}]
(when @closed?
(throw (IllegalStateException. "Crux node is closed"))))
(defrecord CruxNode [kv-store tx-log document-store indexer tx-consumer object-store bus query-engine
options close-fn status-fn closed? ^StampedLock lock]
ICruxAPI
(db [this] (.db this nil nil))
(db [this valid-time] (.db this valid-time nil))
(db [this valid-time tx-time]
(cio/with-read-lock lock
(ensure-node-open this)
(let [latest-tx-time (:crux.tx/tx-time (.latestCompletedTx this))
_ (when (and tx-time (or (nil? latest-tx-time) (pos? (compare tx-time latest-tx-time))))
(throw (NodeOutOfSyncException. (format "node hasn't indexed the requested transaction: requested: %s, available: %s"
tx-time latest-tx-time)
tx-time latest-tx-time)))
valid-time (or valid-time (cio/next-monotonic-date))
tx-time (or tx-time latest-tx-time valid-time)]
(q/db query-engine valid-time tx-time))))
(openDB [this] (.openDB this nil nil))
(openDB [this valid-time] (.openDB this valid-time nil))
(openDB [this valid-time tx-time]
(let [db (.db this valid-time tx-time)]
(assoc db :index-store (q/open-index-store db))))
(document [this content-hash]
(cio/with-read-lock lock
(ensure-node-open this)
(with-open [index-store (db/open-index-store indexer)]
(-> (db/get-single-object object-store index-store (c/new-id content-hash))
(idx/keep-non-evicted-doc)))))
(documents [this content-hash-set]
(cio/with-read-lock lock
(ensure-node-open this)
(with-open [index-store (db/open-index-store indexer)]
(->> (db/get-objects object-store index-store (map c/new-id content-hash-set))
(into {} (remove (comp idx/evicted-doc? val)))))))
(history [this eid]
(reverse (.historyRange this eid nil nil nil nil)))
(historyRange [this eid valid-time-start transaction-time-start valid-time-end transaction-time-end]
(cio/with-read-lock lock
(ensure-node-open this)
(let [latest-tx-time (:crux.tx/tx-time (.latestCompletedTx this))]
(when (and transaction-time-end
(or (nil? latest-tx-time)
(pos? (compare transaction-time-end latest-tx-time))))
(throw (NodeOutOfSyncException. (format "node hasn't indexed the requested transaction: requested: %s, available: %s"
transaction-time-end latest-tx-time)
transaction-time-end latest-tx-time))))
(with-open [index-store (db/open-index-store indexer)]
(->> (db/entity-history-range index-store eid valid-time-start transaction-time-start valid-time-end transaction-time-end)
(mapv c/entity-tx->edn)
(sort-by (juxt :crux.db/valid-time :crux.tx/tx-time))))))
(status [this]
(cio/with-read-lock lock
(ensure-node-open this)
;; we don't have status-fn set when other components use node as a dependency within the topology
(if status-fn
(status-fn)
(into {} (mapcat status/status-map) [indexer kv-store object-store tx-log]))))
(attributeStats [this]
(cio/with-read-lock lock
(ensure-node-open this)
(db/read-index-meta indexer :crux.kv/stats)))
(submitTx [this tx-ops]
(cio/with-read-lock lock
(ensure-node-open this)
(let [conformed-tx-ops (mapv txc/conform-tx-op tx-ops)]
(db/submit-docs document-store (into {} (mapcat :docs) conformed-tx-ops))
@(db/submit-tx tx-log (mapv txc/->tx-event conformed-tx-ops)))))
(hasTxCommitted [this {:keys [::tx/tx-id ::tx/tx-time] :as submitted-tx}]
(cio/with-read-lock lock
(ensure-node-open this)
(let [{latest-tx-id ::tx/tx-id, latest-tx-time ::tx/tx-time} (.latestCompletedTx this)]
(if (and tx-id (or (nil? latest-tx-id) (pos? (compare tx-id latest-tx-id))))
(throw
(NodeOutOfSyncException.
(format "Node hasn't indexed the transaction: requested: %s, available: %s" tx-time latest-tx-time)
tx-time latest-tx-time))
(db/tx-failed? indexer tx-id)))))
(openTxLog ^ICursor [this after-tx-id with-ops?]
(cio/with-read-lock lock
(ensure-node-open this)
(if (let [latest-submitted-tx-id (::tx/tx-id (api/latest-submitted-tx this))]
(or (nil? latest-submitted-tx-id)
(and after-tx-id (>= after-tx-id latest-submitted-tx-id))))
(cio/->cursor #() [])
(let [tx-log-iterator (db/open-tx-log tx-log after-tx-id)
index-store (db/open-index-store indexer)
tx-log (-> (iterator-seq tx-log-iterator)
(->> (filter #(db/tx-failed? indexer (:crux.tx/tx-id %))))
(cond->> with-ops? (map (fn [{:keys [crux.tx/tx-id crux.tx.event/tx-events]
:as tx-log-entry}]
(-> tx-log-entry
(dissoc :crux.tx.event/tx-events)
(assoc :crux.api/tx-ops (txc/tx-events->tx-ops document-store tx-events)))))))]
(cio/->cursor (fn []
(.close index-store)
(.close tx-log-iterator))
tx-log)))))
(sync [this timeout]
(when-let [tx (db/latest-submitted-tx (:tx-log this))]
(-> (api/await-tx this tx timeout)
:crux.tx/tx-time)))
(awaitTxTime [this tx-time timeout]
(cio/with-read-lock lock
(ensure-node-open this)
(-> (tx/await-tx-time indexer tx-consumer tx-time (or timeout (:crux.tx-log/await-tx-timeout options)))
:crux.tx/tx-time)))
(awaitTx [this submitted-tx timeout]
(cio/with-read-lock lock
(ensure-node-open this)
(tx/await-tx indexer tx-consumer submitted-tx (or timeout (:crux.tx-log/await-tx-timeout options)))))
(listen [this {:crux/keys [event-type] :as event-opts} consumer]
(case event-type
:crux/indexed-tx
(bus/listen bus
(assoc event-opts :crux/event-type ::tx/indexed-tx)
(fn [{:keys [::tx/submitted-tx ::txe/tx-events] :as ev}]
(.accept ^Consumer consumer
(merge {:crux/event-type :crux/indexed-tx}
(select-keys ev [:committed?])
(select-keys submitted-tx [::tx/tx-time ::tx/tx-id])
(when (:with-tx-ops? event-opts)
(with-open [index-store (db/open-index-store indexer)]
{:crux/tx-ops (txc/tx-events->tx-ops document-store tx-events)}))))))))
(latestCompletedTx [this]
(db/latest-completed-tx indexer))
(latestSubmittedTx [this]
(db/latest-submitted-tx tx-log))
ICruxAsyncIngestAPI
(submitTxAsync [this tx-ops]
(cio/with-read-lock lock
(ensure-node-open this)
(let [conformed-tx-ops (mapv txc/conform-tx-op tx-ops)]
(db/submit-docs document-store (into {} (mapcat :docs) conformed-tx-ops))
(db/submit-tx tx-log (mapv txc/->tx-event conformed-tx-ops)))))
backup/INodeBackup
(write-checkpoint [this {:keys [crux.backup/checkpoint-directory] :as opts}]
(cio/with-read-lock lock
(ensure-node-open this)
(kv/backup kv-store (io/file checkpoint-directory "kv-store"))
(when (satisfies? backup/INodeBackup tx-log)
(backup/write-checkpoint tx-log opts))))
Closeable
(close [_]
(cio/with-write-lock lock
(when (and (not @closed?) close-fn) (close-fn))
(reset! closed? true))))
(def ^:private node-component
{:start-fn (fn [{::keys [indexer tx-consumer document-store object-store tx-log kv-store bus query-engine]} node-opts]
(map->CruxNode {:options node-opts
:kv-store kv-store
:tx-log tx-log
:indexer indexer
:tx-consumer tx-consumer
:document-store document-store
:object-store object-store
:bus bus
:query-engine query-engine
:closed? (atom false)
:lock (StampedLock.)}))
:deps #{::indexer ::tx-consumer ::kv-store ::bus ::document-store ::object-store ::tx-log ::query-engine}
:args {:crux.tx-log/await-tx-timeout {:doc "Default timeout for awaiting transactions being indexed."
:default nil
:crux.config/type :crux.config/duration}}})
(def base-topology
{::kv-store 'crux.kv.memdb/kv
::object-store 'crux.object-store/kv-object-store
::indexer 'crux.kv-indexer/kv-indexer
::tx-consumer 'crux.tx/tx-consumer
::bus 'crux.bus/bus
::query-engine 'crux.query/query-engine
::node 'crux.node/node-component})
(defn start ^crux.api.ICruxAPI [options]
(let [[{::keys [node] :as components} close-fn] (topo/start-topology options)]
(-> node
(assoc :status-fn (fn []
(merge crux-version
(into {} (mapcat status/status-map) (vals (dissoc components ::node)))))
:close-fn close-fn)
(vary-meta assoc ::topology components))))