This repository was archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathProducerConsumer.fsx
More file actions
354 lines (261 loc) · 10.4 KB
/
ProducerConsumer.fsx
File metadata and controls
354 lines (261 loc) · 10.4 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
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
#load "Refs.fsx"
#time "on"
open FSharp.Control
open Kafunk
open System
open System.Threading
open System.Threading.Tasks
open System.Collections.Generic
open System.Collections.Concurrent
let Log = Log.create __SOURCE_FILE__
let argiDefault i def = fsi.CommandLineArgs |> Seq.tryItem i |> Option.getOr def
let host = argiDefault 1 "localhost"
let topicName = argiDefault 2 "absurd-topic"
let totalMessageCount = argiDefault 3 "10000" |> Int32.Parse
let batchSize = argiDefault 4 "1000" |> Int32.Parse
let consumerCount = argiDefault 5 "1" |> Int32.Parse
let producerThreads = argiDefault 6 "100" |> Int32.Parse
let contigDeltaThreshold = 200000
let testId = Guid.NewGuid().ToString("n")
let consumerGroup = "kafunk-producer-consumer-test-" + testId
let messageKey = "at-least-once-test-" + testId
let messageKeyBytes = messageKey |> System.Text.Encoding.UTF8.GetBytes |> Binary.ofArray
let chanConfig =
ChanConfig.create (
requestTimeout = TimeSpan.FromSeconds 10.0)
let consuming = new CountdownEvent(consumerCount)
let completed = IVar.create ()
let sw = System.Diagnostics.Stopwatch.StartNew()
//type Value = int
//type State =
// | Produced of DateTime
// | Received of DateTime * DateTime
type Ack () =
/// maps values to their state (prepared | ack'd)
let pending = SortedList<int, int>(Comparer.Default)
let mutable contig = -1
let mutable duplicates = 0
let mutable received = 0
let mutable sent = 0
/// Find the longest contiguous sequence starting at the specified index.
/// Returns the largest contiguous value.
let rec findContig i prev =
if i = pending.Count then prev else
let v = pending.Keys.[i]
let s = pending.Values.[i]
if s = 1 then
findContig (i + 1) v
else
prev
/// Clears the contiguous sequence up to the specified value.
let pruneUpTo v =
let i = pending.IndexOfKey v
if i > 0 then
for _ in [0..i] do
pending.RemoveAt 0
member __.Contig = contig
member __.Duplicates = duplicates
member __.Sent = sent
member __.Received = received
member __.Pending = pending.Count
/// Marks a sequence of items as pending and awaiting acknowledgement.
member __.Prepare (vs:int seq) =
for v in vs do
pending.Add (v, 0)
sent <- sent + 1
/// Marks a set of items as acknowledged, keeping track of duplicates.
/// Also maintains a contiguous sequence of items to verify ordering.
member __.Ack (vs:int seq) =
for v in vs do
let mutable s = Unchecked.defaultof<_>
if (pending.TryGetValue (v, &s)) then
if s = 1 then
// already ack'd
duplicates <- duplicates + 1
else
// first time ack
pending.[v] <- 1
received <- received + 1
else
let first,last =
if pending.Count > 0 then pending.Keys.[0], pending.Keys.[pending.Keys.Count - 1]
else -1,-1
if first < v then
failwithf "ack v=%i is not in pending list and greater than prune watermark first=%i last=%i" v first last
let wm = contig
let wm' = findContig 0 wm
if wm' > wm then
contig <- wm'
pruneUpTo wm'
type Report =
struct
val received : int
val duplicates : int
val produced : int
val contigCount : int
new (r,d,p,cc) =
{ received = r ; duplicates = d ; produced = p ; contigCount = cc }
end
let printReport (report:Report) =
let pending = totalMessageCount - report.received
let lag = report.produced - report.received
//let offsetStr = report.offsets |> Seq.map (fun kvp -> sprintf "p=%i o=%i" kvp.Key kvp.Value) |> String.concat " ; "
let contigDelta = report.received - report.contigCount - 1
Log.info "monitor|produced=%i received=%i lag=%i duplicates=%i pending=%i contig=%i contig_delta=%i running_time_min=%f"
report.produced report.received lag report.duplicates pending report.contigCount contigDelta sw.Elapsed.TotalMinutes
// ----------------------------------------------------------------------------------------------------------------------------------
[<Compile(Module)>]
module Reporter =
type Reporter =
private
| R of Mb<ReportReq>
and private ReportReq =
| Received of values:(int * (Partition * Offset))[] * messageCount:int
| Produced of values:int[] * p:Partition * o:Offset
| Report of AsyncReplyChannel<Report>
let create () =
let mb = Mb.Start (fun mb ->
let ack = Ack ()
mb.Error.Add (fun e -> Log.error "mailbox_error|%O" e ; completed.TrySetException e |> ignore)
let report () =
let r = new Report(ack.Received, ack.Duplicates, ack.Sent, ack.Contig)
//printReport r
r
let rec loop () = async {
let! req = mb.Receive ()
match req with
| Received (values,_messageBatchCount) ->
ack.Ack (values |> Seq.map fst)
if ack.Received >= totalMessageCount then
Log.info "received_complete_set|receive_count=%i" ack.Contig
IVar.tryPut () completed |> ignore
| Produced (values,p,o) ->
ack.Prepare values
| Report rep ->
rep.Reply (report ())
return! loop () }
loop ())
R mb
let report (R(mb)) =
mb.PostAndAsyncReply (ReportReq.Report)
let produced (R(mb)) (batch,p,o) =
mb.Post (ReportReq.Produced(batch,p,o))
let consumed (R(mb)) (values,ms:ConsumerMessageSet) =
mb.Post (ReportReq.Received (values,ms.messageSet.messages.Length))
let reporter = Reporter.create ()
let monitor = async {
while not completed.Task.IsCompleted do
do! Async.Sleep 5000
let! report = Reporter.report reporter
printReport report
if (report.received - report.contigCount) > contigDeltaThreshold then
Log.error "contig_delta_surpassed_threshold"
IVar.tryPut () completed |> ignore }
// ----------------------------------------------------------------------------------------------------------------------------------
let producer = async {
let message (messageNumber:int) =
let value = Binary.ofArray (Array.zeroCreate 4)
let _ = Binary.writeInt32 messageNumber value
ProducerMessage.ofBytes (value, messageKeyBytes)
let batchCount = totalMessageCount / batchSize
do! consuming.WaitHandle |> Async.AwaitWaitHandle |> Async.Ignore
do! Async.Sleep 5000 // TODO: consumer coordination
Log.info "starting_producer_process|batch_count=%i" batchCount
let connCfg =
KafkaConfig.create (
[KafkaUri.parse host],
tcpConfig = chanConfig,
version = Versions.V_0_10_1)
use! conn = Kafka.connAsync connCfg
let producerCfg =
ProducerConfig.create (
topic = topicName,
partition = Partitioner.roundRobin,
requiredAcks = RequiredAcks.AllInSync,
batchSizeBytes = ProducerConfig.DefaultBatchSizeBytes,
bufferSizeBytes = ProducerConfig.DefaultBufferSizeBytes)
let! producer = Producer.createAsync conn producerCfg
let produceProcess =
Seq.init batchCount id
|> Seq.map (fun batchNumber -> async {
let batch = Array.init batchSize (fun j -> (batchNumber * batchSize + j))
let pms = batch |> Array.map message
Reporter.produced reporter (batch,0,0L)
let! res = Producer.produceBatch producer (fun pc -> batchNumber % pc,pms)
return () })
|> Async.parallelThrottledIgnore producerThreads
return! Async.choose (IVar.get completed) produceProcess
Log.info "producer_done" }
// ----------------------------------------------------------------------------------------------------------------------------------
let consumer = async {
let partitionOffsets = new ConcurrentDictionary<Partition, Offset> ()
let handle (_:ConsumerState) (ms:ConsumerMessageSet) = async {
//failwithf "testing ERRORs!"
//Log.error "testing error!"
//let firstOffset' = ConsumerMessageSet.firstOffset ms
//let mutable lastOffset = 0L
//if partitionOffsets.TryGetValue (ms.partition, &lastOffset) then
// if firstOffset' > lastOffset + 1L then
// failwithf "offset_gap_detected|partition=%i last_offset=%i first_offset_next_batch=%i" ms.partition lastOffset firstOffset'
//partitionOffsets.[ms.partition] <- ConsumerMessageSet.lastOffset ms
//ms.messageSet.messages
//|> Seq.pairwise
//|> Seq.iter (fun (msi1,msi2) ->
// if msi1.offset + 1L <> msi2.offset then
// failwithf "non_contiguous_offsets_detected|offset1=%i offset2=%i" msi1.offset msi2.offset
// ())
for msi in ms.messageSet.messages do
match partitionOffsets.TryGetValue (ms.partition) with
| true, lastOffset ->
if (lastOffset + 1L <> msi.offset) then
failwithf "non_contig_offsets|partition=%i last_offset=%i current_offset=%i" ms.partition lastOffset msi.offset
| _ -> ()
partitionOffsets.[ms.partition] <- msi.offset
let values =
ms.messageSet.messages
|> Seq.choose (fun m ->
let key = Binary.toString m.message.key
if key = messageKey then
let i,_ = Binary.readInt32 m.message.value
Some (i,(ms.partition,m.offset))
else
None)
|> Seq.toArray
Reporter.consumed reporter (values,ms) }
let connCfg =
KafkaConfig.create (
[KafkaUri.parse host],
tcpConfig = chanConfig,
version = Versions.V_0_10_1)
use! conn = Kafka.connAsync connCfg
let consumerCfg =
ConsumerConfig.create (
consumerGroup,
topic = topicName,
autoOffsetReset = AutoOffsetReset.StartFromTime Time.EarliestOffset,
endOfTopicPollPolicy = RetryPolicy.constantMs 1000)
let! consumer = Consumer.createAsync conn consumerCfg
consuming.Signal () |> ignore
let consumeProcess =
Consumer.consumePeriodicCommit consumer (TimeSpan.FromSeconds 5.0) handle
return! Async.choose (consumeProcess) (IVar.get completed) }
// ----------------------------------------------------------------------------------------------------------------------------------
Log.info "starting_producer_consumer_test|host=%s topic=%s message_count=%i batch_size=%i consumer_count=%i producer_parallelism=%i"
host topicName totalMessageCount batchSize consumerCount producerThreads
let go =
Async.Parallel
[
yield monitor
for _ in [1..consumerCount] do
yield consumer
Thread.Sleep 100
yield producer
]
|> Async.Ignore
try
Async.RunSynchronously (go)
with ex ->
Log.error "%O" ex
sw.Stop()
let report = Reporter.report reporter |> Async.RunSynchronously
printReport report