-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdafka_unacked_list.c
366 lines (301 loc) · 11.4 KB
/
dafka_unacked_list.c
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
/* =========================================================================
dafka_unacked_list - class description
Copyright (c) the Contributors as noted in the AUTHORS file.
This file is part of CZMQ, the high-level C binding for 0MQ:
http://czmq.zeromq.org.
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
=========================================================================
*/
/*
@header
dafka_unacked_list -
@discuss
@end
*/
#include "dafka_classes.h"
// Structure of our class
#define CHUNK_SIZE 256
typedef struct _chunk_t {
zmq_msg_t values[CHUNK_SIZE];
struct _chunk_t *next;
uint64_t begin_seq;
uint64_t count;
} chunk_t;
struct _dafka_unacked_list_t {
chunk_t *begin_chunk;
chunk_t *end_chunk;
chunk_t *spare_chunk;
zsock_t *publisher;
dafka_proto_t *direct_msg;
};
static chunk_t*
chunk_new (uint64_t begin_seq) {
chunk_t *chunk = (chunk_t *) malloc (sizeof (chunk_t));
for (int i = 0; i < CHUNK_SIZE; ++i) {
zmq_msg_init (&chunk->values[i]);
}
chunk->next = NULL;
chunk->begin_seq = begin_seq;
chunk->count = 0;
return chunk;
}
static void chunk_destroy (chunk_t **self_p) {
assert (self_p);
if (*self_p) {
chunk_t *self = *self_p;
// Free class properties here
for (int i = 0; i < CHUNK_SIZE; ++i)
zmq_msg_close (&self->values[i]);
// Free object itself
free (self);
*self_p = NULL;
}
}
// --------------------------------------------------------------------------
// Create a new dafka_unacked_list
dafka_unacked_list_t *
dafka_unacked_list_new (zsock_t *publisher, const char* address, const char *topic)
{
dafka_unacked_list_t *self = (dafka_unacked_list_t *) zmalloc (sizeof (dafka_unacked_list_t));
assert (self);
// Initialize class properties here
self->begin_chunk = chunk_new (0);
self->end_chunk = self->begin_chunk;
self->publisher = publisher;
self->direct_msg = dafka_proto_new ();
dafka_proto_set_id (self->direct_msg, DAFKA_PROTO_DIRECT_RECORD);
dafka_proto_set_subject (self->direct_msg, topic);
dafka_proto_set_address (self->direct_msg, address);
return self;
}
// --------------------------------------------------------------------------
// Destroy the dafka_unacked_list
void
dafka_unacked_list_destroy (dafka_unacked_list_t **self_p)
{
assert (self_p);
if (*self_p) {
dafka_unacked_list_t *self = *self_p;
// Free class properties here
dafka_proto_destroy (&self->direct_msg);
chunk_destroy (&self->begin_chunk);
chunk_destroy (&self->spare_chunk);
// Free object itself
free (self);
*self_p = NULL;
}
}
uint64_t
dafka_unacked_list_push (dafka_unacked_list_t *self, zmq_msg_t *msg)
{
zmq_msg_copy (&self->end_chunk->values[self->end_chunk->count], msg);
uint64_t sequence = self->end_chunk->begin_seq + self->end_chunk->count;
self->end_chunk->count++;
if (self->end_chunk->count == CHUNK_SIZE) {
chunk_t *spare_chunk = self->spare_chunk;
self->spare_chunk = NULL;
if (spare_chunk) {
self->end_chunk->next = spare_chunk;
self->end_chunk->next->count = 0;
self->end_chunk->next->begin_seq = sequence + 1;
}
else
self->end_chunk->next = chunk_new (sequence + 1);
self->end_chunk = self->end_chunk->next;
}
return sequence;
}
void
dafka_unacked_list_ack (dafka_unacked_list_t *self, uint64_t sequence)
{
while (sequence >= self->begin_chunk->begin_seq) {
uint64_t last_sequence = self->begin_chunk->begin_seq + self->begin_chunk->count;
if (sequence < last_sequence) {
uint64_t begin_sequence = sequence + 1;
uint64_t count = last_sequence - begin_sequence;
self->begin_chunk->begin_seq = begin_sequence;
self->begin_chunk->count = count;
return;
}
else {
// Is it the last chunk? lets re-use it
if (self->begin_chunk->next == NULL) {
self->begin_chunk->begin_seq = self->begin_chunk->begin_seq + self->begin_chunk->count;
self->begin_chunk->count = 0;
return;
} else {
chunk_t *temp = self->begin_chunk;
self->begin_chunk = self->begin_chunk->next;
if (self->spare_chunk == NULL) {
self->spare_chunk = temp;
}
else
chunk_destroy (&temp);
}
}
}
}
void
dafka_unacked_list_send (dafka_unacked_list_t *self, const char* address, uint64_t sequence, uint32_t count)
{
if (sequence < self->begin_chunk->begin_seq)
return;
dafka_proto_set_topic (self->direct_msg, address);
chunk_t *current = self->begin_chunk;
zmq_msg_t msg;
zmq_msg_init (&msg);
while (current && count > 0) {
if (sequence >= current->begin_seq && sequence < current->begin_seq + current->count) {
int rc = zmq_msg_copy (&msg, &self->begin_chunk->values[sequence - current->begin_seq]);
assert (rc == 0);
dafka_proto_set_sequence(self->direct_msg, sequence);
dafka_proto_set_content(self->direct_msg, &msg);
dafka_proto_send (self->direct_msg, self->publisher);
count--;
sequence++;
} else
current = current->next;
}
zmq_msg_close (&msg);
}
bool
dafka_unacked_list_is_empty (dafka_unacked_list_t *self)
{
return self->begin_chunk == self->end_chunk && self->begin_chunk->count == 0;
}
uint64_t
dafka_unacked_list_last_acked (dafka_unacked_list_t *self) {
return self->begin_chunk->begin_seq - 1;
}
// --------------------------------------------------------------------------
// Self test of this class
// If your selftest reads SCMed fixture data, please keep it in
// src/selftest-ro; if your test creates filesystem objects, please
// do so under src/selftest-rw.
// The following pattern is suggested for C selftest code:
// char *filename = NULL;
// filename = zsys_sprintf ("%s/%s", SELFTEST_DIR_RO, "mytemplate.file");
// assert (filename);
// ... use the "filename" for I/O ...
// zstr_free (&filename);
// This way the same "filename" variable can be reused for many subtests.
#define SELFTEST_DIR_RO "src/selftest-ro"
#define SELFTEST_DIR_RW "src/selftest-rw"
void
dafka_unacked_list_test (bool verbose)
{
printf (" * dafka_unacked_list: ");
// @selftest
// Simple create/destroy test
const char* subAddress = "SUBADDRESS";
zsock_t *publisher = zsock_new_pub("inproc://unacked-list-selftest");
zsock_set_sndhwm (publisher, 0);
zsock_t *subscriber = zsock_new_sub("inproc://unacked-list-selftest", "");
zsock_set_rcvhwm (subscriber, 0);
zsock_set_rcvtimeo (subscriber, 100);
dafka_unacked_list_t *self = dafka_unacked_list_new (publisher, "ADDRESS", subAddress);
assert (self);
// Storing messages
for (int i = 0; i < 1000000; ++i) {
zmq_msg_t msg;
zmq_msg_init_size(&msg, 1000);
dafka_unacked_list_push (self, &msg);
zmq_msg_close (&msg);
}
// Checks that the list is not empty
assert (dafka_unacked_list_is_empty (self) == false);
// Receive the two requested messages
dafka_unacked_list_send (self, subAddress, 0, 2);
dafka_proto_t *proto = dafka_proto_new ();
int rc = dafka_proto_recv (proto, subscriber);
assert (rc == 0);
assert (dafka_proto_sequence (proto) == 0);
assert (zmq_msg_size (dafka_proto_content (proto)) == 1000);
rc = dafka_proto_recv (proto, subscriber);
assert (rc == 0);
assert (dafka_proto_sequence (proto) == 1);
assert (zmq_msg_size (dafka_proto_content (proto)) == 1000);
// Ack some messages
dafka_unacked_list_ack (self, 100000);
assert (dafka_unacked_list_is_empty (self) == false);
// Try to send messages that was already acked, nothing should be sent
dafka_unacked_list_send (self, subAddress, 0, 2);
rc = dafka_proto_recv (proto, subscriber);
assert (rc == -1 && errno == EAGAIN);
// Try to send messages that was not acked yet, should work
dafka_unacked_list_send (self, subAddress, 100001, 2);
rc = dafka_proto_recv (proto, subscriber);
assert (rc == 0);
assert (dafka_proto_sequence (proto) == 100001);
assert (zmq_msg_size (dafka_proto_content (proto)) == 1000);
rc = dafka_proto_recv (proto, subscriber);
assert (rc == 0);
assert (dafka_proto_sequence (proto) == 100002);
assert (zmq_msg_size (dafka_proto_content (proto)) == 1000);
// Asking to send sequence which doesn't exist
dafka_unacked_list_send (self, subAddress, 999999, 2);
rc = dafka_proto_recv (proto, subscriber);
assert (rc == 0);
assert (dafka_proto_sequence (proto) == 999999);
assert (zmq_msg_size (dafka_proto_content (proto)) == 1000);
rc = dafka_proto_recv (proto, subscriber);
assert (rc == -1 && errno == EAGAIN);
// Acking more messages
dafka_unacked_list_ack (self, 200000);
assert (dafka_unacked_list_is_empty (self) == false);
assert (dafka_unacked_list_last_acked (self) == 200000);
dafka_unacked_list_ack (self, 304000);
assert (dafka_unacked_list_is_empty (self) == false);
assert (dafka_unacked_list_last_acked (self) == 304000);
dafka_unacked_list_ack (self, 400073);
assert (dafka_unacked_list_is_empty (self) == false);
assert (dafka_unacked_list_last_acked (self) == 400073);
dafka_unacked_list_ack (self, 586321);
assert (dafka_unacked_list_is_empty (self) == false);
assert (dafka_unacked_list_last_acked (self) == 586321);
// Storing some more
for (int i = 0; i < 100000; ++i) {
zmq_msg_t msg;
zmq_msg_init_size(&msg, 1000);
dafka_unacked_list_push (self, &msg);
zmq_msg_close (&msg);
}
// Acking the rest
dafka_unacked_list_ack (self, 874321);
assert (dafka_unacked_list_is_empty (self) == false);
assert (dafka_unacked_list_last_acked (self) == 874321);
dafka_unacked_list_ack (self, 1099999);
assert (dafka_unacked_list_is_empty (self));
assert (dafka_unacked_list_last_acked (self) == 1099999);
// Storing some more
for (int i = 0; i < 100000; ++i) {
zmq_msg_t msg;
zmq_msg_init_size(&msg, 1000);
dafka_unacked_list_push (self, &msg);
zmq_msg_close (&msg);
}
assert (dafka_unacked_list_is_empty (self) == false);
// Asking for all messages
dafka_unacked_list_send (self, subAddress, 1100000, 100000);
for (int i = 0; i < 100000; ++i) {
rc = dafka_proto_recv (proto, subscriber);
assert (rc == 0);
assert (dafka_proto_sequence (proto) == 1100000 + i);
assert (zmq_msg_size (dafka_proto_content (proto)) == 1000);
}
// Acking the rest
dafka_unacked_list_ack (self, 1199999);
assert (dafka_unacked_list_is_empty (self));
assert (dafka_unacked_list_last_acked (self) == 1199999);
// Ack a sequence which doesn't exist
dafka_unacked_list_ack (self, 2000000);
assert (dafka_unacked_list_is_empty (self));
zsock_destroy (&publisher);
zsock_destroy(&subscriber);
dafka_proto_destroy (&proto);
dafka_unacked_list_destroy (&self);
// @end
printf ("OK\n");
}