File: submit.c

package info (click to toggle)
raft 0.22.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,504 kB
  • sloc: ansic: 37,539; makefile: 264; sh: 77; python: 22
file content (340 lines) | stat: -rw-r--r-- 7,596 bytes parent folder | download | duplicates (2)
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
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <uv.h>

#include "../../include/raft.h"
#include "../../include/raft/uv.h"

#include "fs.h"
#include "submit.h"
#include "submit_parse.h"
#include "timer.h"

static int fsmApply(struct raft_fsm *fsm,
                    const struct raft_buffer *buf,
                    void **result)
{
    (void)fsm;
    (void)buf;
    (void)result;
    return 0;
}

struct server
{
    struct uv_loop_s *loop;
    struct uv_timer_s timer;
    struct raft_tracer tracer;
    struct raft_configuration configuration;
    struct raft_uv_transport transport;
    struct raft_io io;
    struct raft_fsm fsm;
    struct raft raft;
    struct raft_buffer buf;
    struct raft_apply req;
    size_t entry;
    char *path;
    unsigned i;
    unsigned n;
    unsigned long start;
    struct histogram histogram;
    struct timer write_timer;
    struct histogram writes;
};

static void traceWriteSubmit(struct server *s)
{
    TimerStart(&s->write_timer);
}

static void traceWriteComplete(struct server *s)
{
    HistogramCount(&s->writes, TimerStop(&s->write_timer));
}

static void emit(struct raft_tracer *t, int type, const void *info)
{
    struct server *s = t->impl;
    (void)info;
    switch (type) {
        case RAFT_UV_TRACER_WRITE_SUBMIT:
            traceWriteSubmit(s);
            break;
        case RAFT_UV_TRACER_WRITE_COMPLETE:
            traceWriteComplete(s);
            break;
    };
}

int serverInit(struct server *s,
               struct submitOptions *opts,
               struct uv_loop_s *loop)
{
    struct FsFileInfo info;
    const char *address = "127.0.0.1:8080";
    int rv;

    s->loop = loop;
    s->transport.version = 1;
    s->transport.data = NULL;

    s->i = 0;
    s->n = (unsigned)(opts->size / opts->buf);
    s->entry = opts->buf;
    s->timer.data = s;

    rv = FsFileInfo(opts->dir, &info);
    if (rv != 0) {
        printf("failed to get dir info\n");
        return -1;
    }

    HistogramInit(&s->histogram, info.buckets, info.resolution);
    HistogramInit(&s->writes, info.buckets, info.resolution);

    rv = FsCreateTempDir(opts->dir, &s->path);
    if (rv != 0) {
        printf("failed to create temp dir\n");
        return -1;
    }

    s->tracer.version = 2;
    s->tracer.emit = emit;
    s->tracer.impl = s;

    rv = raft_uv_tcp_init(&s->transport, loop);
    if (rv != 0) {
        printf("failed to init transport\n");
        return -1;
    }

    rv = raft_uv_init(&s->io, loop, s->path, &s->transport);
    if (rv != 0) {
        printf("failed to init io\n");
        return -1;
    }

    raft_uv_set_tracer(&s->io, &s->tracer);

    s->fsm.version = 1;
    s->fsm.apply = fsmApply;
    s->fsm.snapshot = NULL;
    s->fsm.restore = NULL;

    rv = raft_init(&s->raft, &s->io, &s->fsm, 1, address);
    if (rv != 0) {
        printf("failed to init raft\n");
        return -1;
    }

    raft_configuration_init(&s->configuration);
    rv = raft_configuration_add(&s->configuration, 1, address, RAFT_VOTER);
    if (rv != 0) {
        printf("failed to populate configuration\n");
        return -1;
    }
    rv = raft_bootstrap(&s->raft, &s->configuration);
    if (rv != 0) {
        printf("failed to bootstrap\n");
        return -1;
    }
    raft_configuration_close(&s->configuration);

    /* Effectively disable snapshotting. */
    raft_set_snapshot_threshold(&s->raft, 1024 * 1024);

    rv = raft_start(&s->raft);
    if (rv != 0) {
        printf("failed to start raft '%s'\n", raft_strerror(rv));
        return -1;
    }

    s->req.data = s;

    uv_timer_init(s->loop, &s->timer);

    return 0;
}

static int serverClose(struct server *s)
{
    int rv;

    raft_uv_close(&s->io);
    raft_uv_tcp_close(&s->transport);

    rv = FsRemoveTempDir(s->path);
    if (rv != 0) {
        printf("failed to remove temp dir\n");
        return -1;
    }

    HistogramClose(&s->histogram);
    HistogramClose(&s->writes);

    return 0;
}

static int submitEntry(struct server *s);

static void raftCloseCb(struct raft *r)
{
    struct server *s = r->data;
    uv_close((struct uv_handle_s *)&s->timer, NULL);
}

static void serverTimerCb(uv_timer_t *timer)
{
    struct server *s = timer->data;
    s->raft.data = s;
    raft_close(&s->raft, raftCloseCb);
}

static void serverWaitOpenSegments(uv_timer_t *timer)
{
    struct server *s = timer->data;
    bool exists;
    int rv;

    rv = FsFileExists(s->path, "open-1", &exists);
    assert(rv == 0);
    assert(exists);

    rv = FsFileExists(s->path, "open-2", &exists);
    assert(rv == 0);
    if (!exists) {
        return;
    }

    rv = FsFileExists(s->path, "open-3", &exists);
    assert(rv == 0);
    if (!exists) {
        return;
    }

    uv_timer_stop(&s->timer);

    rv = submitEntry(s);
    assert(rv == 0);
}

static void submitEntryCb(struct raft_apply *req, int status, void *result)
{
    struct server *s = req->data;
    int rv;

    (void)result;

    if (status != 0) {
        printf("submission cb failed\n");
        exit(1);
    }

    HistogramCount(&s->histogram, (unsigned long)uv_hrtime() - s->start);

    s->i++;

    /* After the first write, wait for all open segments to be created. */
    if (s->i == 1) {
        uv_timer_start(&s->timer, serverWaitOpenSegments, 10, 10);
        return;
    }

    if (s->i == s->n) {
        /* Run raft_close in the next loop iteration, to avoid calling it from a
         * this commit callback, which triggers a bug in raft. */
        uv_timer_start(&s->timer, serverTimerCb, 125, 0);
        return;
    }

    rv = submitEntry(s);
    if (rv != 0) {
        printf("submission failed\n");
        exit(1);
    }
}

static int submitEntry(struct server *s)
{
    int rv;
    s->start = (unsigned long)uv_hrtime();
    const struct raft_buffer *bufs = &s->buf;

    s->buf.len = s->entry - 8 /* CRC */ - 8 /* N entries */ - 16 /* header */;
    if (s->i == 0) {
        s->buf.len -= 8; /* segment format */
    }
    s->buf.base = raft_malloc(s->buf.len);
    assert(s->buf.base != NULL);

    rv = raft_apply(&s->raft, &s->req, bufs, 1, submitEntryCb);
    if (rv != 0) {
        return -1;
    }

    return 0;
}

int SubmitRun(int argc, char *argv[], struct report *report)
{
    struct submitOptions opts;
    struct uv_loop_s loop;
    struct server server;
    struct metric *m;
    struct benchmark *benchmark;
    char *name;
    int rv;

    SubmitParse(argc, argv, &opts);

    rv = uv_loop_init(&loop);
    if (rv != 0) {
        printf("failed to init loop\n");
        return -1;
    }

    rv = serverInit(&server, &opts, &loop);
    if (rv != 0) {
        printf("failed to init server\n");
        return -1;
    }

    rv = submitEntry(&server);
    if (rv != 0) {
        printf("failed to submit entry\n");
        return -1;
    }

    rv = uv_run(&loop, UV_RUN_DEFAULT);
    if (rv != 0) {
        printf("failed to run loop\n");
        return -1;
    }

    uv_loop_close(&loop);

    rv = asprintf(&name, "submit:%zu", opts.buf);
    assert(rv > 0);
    assert(name != NULL);

    benchmark = ReportGrow(report, name);
    m = BenchmarkGrow(benchmark, METRIC_KIND_LATENCY);
    MetricFillHistogram(m, &server.histogram);

    rv = asprintf(&name, "submit:write:%zu", opts.buf);
    assert(rv > 0);
    assert(name != NULL);

    benchmark = ReportGrow(report, name);
    m = BenchmarkGrow(benchmark, METRIC_KIND_LATENCY);
    MetricFillHistogram(m, &server.writes);

    rv = serverClose(&server);
    if (rv != 0) {
        printf("failed to cleanup\n");
        return -1;
    }

    return 0;
}