/
store.clj
536 lines (299 loc) · 13.9 KB
/
store.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
(ns dvlopt.kstreams.store
"Kafka Streams state stores.
Streaming applications often need to store some kind of state. This is the purposes of state stores which are typically backed-up
to compacted Kafka topics called changelogs in order to be fault-tolerant. Those changelogs topics are named '$APPLICATION_ID-$GENERATED_NAME-changelog`.
Stores
======
Three type of stores exist, typically persistent by using RocksDB under the hood :
Key-value stores
----------------
Behave like regular tables or, when decided, like LRU ones. A regular key-value store can also be in-memory rather than persistent,
which is great for cloud environments where applications are started from scratch everytime. However, an in-memory key-value
store will have to catch up with its corresponding changelog everytime the application is restarted.
Persistent window stores
-----------------------------
In window stores, each value is associated with a key but also a timestamp. They are used for computing over fixed time intervals.
Persistent session stores
-------------------------
Sessions are non-fixed intervals of activity organized around fixed intervals of inactivity.
For the same key, with an interval of 5 seconds, let us suppose we receive values at time 10, 12 and 20. There is a gap of more than
5 seconds between event 2 and 3. Hence, event 3 would be part of another session. Now let us suppose a 4th event arrives out of order
with a timestamp of 16. Now, all 4 events would merge into the same session as no event is more than 5 seconds away from another one.
In the high-level API, stores are often created automatically but can be tweaked to some extent. When a store needs to be created manually,
these options might be provided :
:dvlopt.kafka/deserializer.key
:dvlopt.kafka/deserializer.value
:dvlopt.kafka/serializer.key
:dvlopt.kafka/serializer.value
Cf. `dvlopt.kafka` for description of serializers and deserializers.
::cache?
Caching tries to minimize the number of updates. For instance, if 2 subsequent values share the same key, only the second one
will be persisted. It improves performance and IO but it means records will not be persisted as soon as possible.
True by default, should be false only for testing and debugging.
::changelog?
In order for state stores to be fault tolerant, they are continuously backed up to a changelog topic behind the scenes.
Default is true and this option should not be disabled unless specifically needed.
::configuration.changelog
Map of Kafka topic properties for configuring the changelog topic.
Cf. https://kafka.apache.org/documentation/#topicconfigs
::name
Generated in the form of 'dvlopt.kafka-store-8_DIGIT_NUMBER' when not supplied.
::type
Determines the type of the store, one of :
:kv.in-memory
:kv.lru
:kv.regular
:session
:window
LRU key-value stores have this additional option :
::lru-size
Maximum number of items in the LRU store.
Session and window stores have these additional options :
::retention
Time period for which the state store will retain historical data, cannot be smaller than the chosen interval.
During any kind of stream processing, it is common that data arrives late or out of order and instead of dropping this data,
it is better to update past time windows. However, because disks are not unlimited, one cannot keep the data for all time windows
just in case. Hence the need for this option. The higher the retention period and the later can data arrive, but the more is stored.
Default is [1 :days].
Window stores have these additional options and mandatory arguments :
::duplicate-keys?
Whether or not to retain duplicate keys, akin to caching.
Default is false.
::interval (mandatory)
Fixed interval of each window.
Cf. `dvlopt.kafka` for description of time intervals
::segments
Number of database segments (must be >= 2).
Default is 2.
Cursors
=======
Retrieving several values from a store always returns a stateful iterator implementing Closeable and acting as a database cursor. It must be
closed after usage otherwise resources will leak. For ease of use, the iterator can be transformed into a sequence by using clojure's `iterator-seq`.
However, the resulting sequence should be consumed eagerly right away.
Each item is a map containing :
:dvlopt.kafka/key
Deserialized key.
:dvlopt.kafka/value
Deserialized value.
Window stores and sessions stores items also have :
:dvlopt.kafka/timestamp.from
Window beginning.
:dvlopt.kafka/timestamp.to
Window end.
Ex. ;; Eargerly sum all values.
(with-open [kvs (dvlopt.kstreams.store/kv-range my-store)]
(reduce (fn [sum kv]
(+ sum
(:dvlopt.kafka/value kv)))
0
(iterator-seq kvs)))"
{:author "Adam Helinski"}
(:refer-clojure :exclude [flush])
(:require [dvlopt.kafka :as K]
[dvlopt.kafka.-interop.clj :as K.-interop.clj]
[dvlopt.kafka.-interop.java :as K.-interop.java])
(:import java.lang.AutoCloseable
org.apache.kafka.streams.processor.StateStore
(org.apache.kafka.streams.state KeyValueStore
ReadOnlyKeyValueStore
ReadOnlySessionStore
ReadOnlyWindowStore
SessionStore
WindowStore)))
;;;;;;;;;; Misc
(defn close-cursor
"Closes a cursor."
[^AutoCloseable cursor]
(.close cursor))
(defn flush
"Flushes any cached data."
[^StateStore store]
(.flush store))
(defn open?
"Is this store open for IO ?"
[^StateStore store]
(.isOpen store))
(defn persistent?
"Is this store persistent ? Rather than in-memory ?"
[^StateStore store]
(.persistent store))
(defn store-name
"Returns the name of the given store which might be automatically generated."
[^StateStore store]
(.name store))
;;;;;;;;;; Key value stores
(defn kv-count
"Returns the approximate number of entries in the key-value store."
[^ReadOnlyKeyValueStore kv-store]
(.approximateNumEntries kv-store))
(defn kv-get
"Returns the value mapped to the given key in the key-value store."
[^ReadOnlyKeyValueStore kv-store k]
(.get kv-store
k))
(defn kv-range
"Returns a cursor for a range of keys or all of them if no range is specified.
Cf. Namespace description for a description of cursors."
(^AutoCloseable
[^ReadOnlyKeyValueStore kv-store]
(K.-interop.clj/key-value-iterator (.all kv-store)))
(^AutoCloseable
[^ReadOnlyKeyValueStore kv-store from-key to-key]
(K.-interop.clj/key-value-iterator (.range kv-store
from-key
to-key))))
(defn kv-put
"Adds the key-value to the key-value store.
A list of [key value]'s can be provided."
(^KeyValueStore
[^KeyValueStore kv-store kvs]
(.putAll kv-store
(map K.-interop.java/key-value
kvs))
kv-store)
(^KeyValueStore
[^KeyValueStore kv-store k v]
(.put kv-store
k
v)
kv-store))
(defn kv-offer
"Adds the key-value to the key-value store only if the key does not exist yet.
Returns the already existing value or nil."
[^KeyValueStore kv-store k v]
(.putIfAbsent kv-store
k
v))
(defn kv-remove
"Removes the key from the key-value store.
Returns the existing value for that key or nil."
[^KeyValueStore store k]
(.delete store
k))
;;;;;;;;;; Window stores
(defn ws-get
"Returns the value mapped to the given key at the given timestamp in the window store."
[^ReadOnlyWindowStore window-store k timestamp]
(.fetch window-store
k
timestamp))
(defn ws-multi-get
"Returns a cursor for several windows for the given key in the window store.
A map of options may be given :
:dvlopt.kafka/timestamp.from
Earliest timestamp, defaults to 0.
:dvlopt.kafka/timestamp.to
Latest timestamp, defaults to current system time.
Cf. Namespace description for a description of cursors."
(^AutoCloseable
[window-store k]
(ws-multi-get window-store
k
nil))
(^AutoCloseable
[^ReadOnlyWindowStore window-store k options]
(K.-interop.clj/window-store-iterator (.fetch window-store
k
(or (::K/timestamp.from options)
0)
(or (::K/timestamp.to options)
(System/currentTimeMillis))))))
(defn ws-multi-range
"Like `ws-multi-get` but for a range of keys (or all of them if no range is provided."
([window-store]
(ws-multi-range window-store
nil))
([^ReadOnlyWindowStore window-store options]
(K.-interop.clj/key-value-iterator--windowed (.fetchAll window-store
(or (::K/timestamp.from options)
0)
(or (::K/timestamp.to options)
(System/currentTimeMillis)))))
([window-store from-key to-key]
(ws-multi-range window-store
from-key
to-key))
([^ReadOnlyWindowStore window-store from-key to-key options]
(K.-interop.clj/key-value-iterator--windowed (.fetch window-store
from-key
to-key
(or (::K/timestamp.from options)
0)
(or (::K/timestamp.to options)
(System/currentTimeMillis))))))
(defn ws-put
"Adds a key to the window store.
If none is provided, the timestamp will be the system time."
(^WindowStore
[^WindowStore window-store k v]
(.put window-store
k
v)
window-store)
(^WindowStore
[^WindowStore window-store k v timestamp]
(.put window-store
k
v
timestamp)
window-store))
;;;;;;;;;; Session stores
(defn ss-multi-get
"Like `ws-multi-get` but for session stores.
For internal reasons, only writable session stores can work with options."
([session-store k]
(ss-multi-get session-store
k
nil))
([session-store k options]
(if options
(K.-interop.clj/key-value-iterator--windowed (.findSessions ^SessionStore session-store
k
(or (::K/timestamp.from options)
0)
(or (::K/timestamp.to options)
(System/currentTimeMillis))))
(K.-interop.clj/key-value-iterator--windowed (.fetch ^ReadOnlySessionStore
session-store
k)))))
(defn ss-multi-range
"Like `ws-multi-range` but for session stores and the range must always be specified.
For internal reasons, only writable session stores can work with options."
([session-store from-key to-key]
(ss-multi-range session-store
from-key
to-key
nil))
([session-store from-key to-key options]
(if options
(K.-interop.clj/key-value-iterator--windowed (.findSessions ^SessionStore session-store
from-key
to-key
(or (::K/timestamp.from options)
0)
(or (::K/timestamp.to options)
(System/currentTimeMillis))))
(K.-interop.clj/key-value-iterator--windowed (.fetch ^ReadOnlySessionStore session-store
from-key
to-key)))))
(comment
;; TODO. Ultimately, these functions needs to instantiate a Window which is an abstract class (weird).
(defn ss-put
"Adds a key-value to a session in a session store."
^SessionStore
[^SessionStore store from-timestamp to-timestamp k v]
(.put store
(K.-interop.java/windowed k
from-timestamp
to-timestamp)
v)
store)
(defn ss-remove
"Removes a key-value from a session in a session store."
^SessionStore
[^SessionStore store k timestamp-start timestamp-end]
(.remove store
(K.-interop.java/windowed k
timestamp-start
timestamp-end))
store))