~ne02ptzero/libfloat

ref: 0a170e94a6d19b9027a52113a2ef3324b45a4dae libfloat/log.c -rw-r--r-- 26.0 KiB
0a170e94 — Michael Bonfils Add callback to abort snapshot synchronization 2 months ago
                                                                                
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
#include "internal.h"

void libfloat_log_add_node_ack(libfloat_ctx_t *ctx, libfloat_log_entry_t *log, libfloat_node_id_t id)
{
    bool                found = false;
    libfloat_node_ack_t *ptr, *tmp;

    libfloat_list_for_each_entry_safe(ptr, tmp, &log->node_acks, next)
    {
        if (ptr->id == id)
        {
            found = true;
            break;
        }
    }

    if (found == false)
    {
        ptr = ctx->calloc(1, sizeof(*ptr));
        ptr->id = id;
        libfloat_list_add_tail(&ptr->next, &log->node_acks);
    }
}

void libfloat_log_free_acks(libfloat_ctx_t *ctx, libfloat_log_entry_t *log)
{
    libfloat_node_ack_t *ptr, *tmp;

    libfloat_list_for_each_entry_safe(ptr, tmp, &log->node_acks, next)
    {
        ctx->free(ptr);
    }

    /* Reset the list head */
    log->node_acks.prev = &log->node_acks;
    log->node_acks.next = &log->node_acks;
}

bool libfloat_add_log(libfloat_ctx_t *ctx, libfloat_commit_type_t commit_type, libfloat_log_data_t *log,
    void (*commit)(void *, libfloat_commit_status_t), void *udata)
{
    libfloat_node_t             *node;
    libfloat_log_entry_t        *entry = NULL;
    khint_t                     iterator;
    int                         absent;

    if (ctx->state != RAFT_STATE_LEADER)
    {
        ERROR(ctx ,"libfloat_add_log: Refusing write, not leader");
        libfloat_set_error_str("Refusing write, not leader");
        return false;
    }

    entry = ctx->calloc(1, sizeof(*entry));
    if (entry == NULL)
    {
        libfloat_set_error_str("Memory error");
        return false;
    }

    entry->data = ctx->calloc(1, sizeof(*entry->data));
    if (entry->data == NULL)
    {
        ctx->free(entry);
        libfloat_set_error_str("Memory error");
        return false;
    }

    if (ctx->is_snapshotting)
    {
        /* We are in the middle of a snapshot, let's get the ID from memory rather than persistent storage */
        entry->id = ctx->snapshot_to_commit + 1;
    }
    else
    {
        entry->id = ctx->persistent.commit_index + 1;
    }

    entry->term = ctx->persistent.term;
    entry->data->type = log->type;
    entry->data->len = log->len;
    entry->data->buf = log->buf;

    entry->commit = commit;
    entry->udata = udata;
    entry->commit_type = commit_type;
    entry->started = ctx->time(NULL);

    entry->node_acks.prev = &entry->node_acks;
    entry->node_acks.next = &entry->node_acks;

    iterator = kh_put(libfloat_entry_id_t, ctx->persistent.log, entry->id, &absent);
    kh_value(ctx->persistent.log, iterator) = entry;

    if (entry->commit_type == LIBFLOAT_HOPEFULLY_CONSISTENT)
    {
        if (entry->commit != NULL)
            entry->commit(entry->udata, LIBFLOAT_ENTRY_COMMITTED);
        entry->commit = NULL;
    }

    if (ctx->is_snapshotting || entry->data->type == LIBFLOAT_SNAPSHOT_LOG)
    {
        /* If we are in the middle of a snapshot, (or about to start one) let's keep the log in memory for now */
        ctx->snapshot_to_commit = entry->id;

        if (entry->data->type == LIBFLOAT_SNAPSHOT_LOG)
            libfloat_internal_snapshot_begin(ctx, entry->id, entry->term);
        return true;
    }

    /* Write the log in our database */
    if (!ctx->write_log(ctx, entry))
    {
        kh_del(libfloat_entry_id_t, ctx->persistent.log, iterator);
        ctx->free(entry->data);
        ctx->free(entry);
        ERROR(ctx, "libfloat_add_log: Refusing write, callback returned false");
        libfloat_set_error_str("User defined callback returned false");
        return false;
    }

    DEBUG(ctx, "libfloat_add_log: Successfully written log %d", entry->id);

    libfloat_log_add_node_ack(ctx, entry, ctx->me->id);
    ctx->timeout_elapsed = 0;
    ctx->last_log = entry;
    libfloat_set_current_commit_index(ctx, entry->id);

    for_every_node(ctx, node, {
        /* Skip myself */
        if (ctx->me == node)
            continue;

        if (ctx->conf.avoid_congestion)
        {
            /**
             * We've been asked to avoid congestion if possible, let's check if
             * that node is up to date with us, before sending the new entry
             */
            if (node->replicated_log < ctx->persistent.commit_index - 1)
                continue;
        }

        libfloat_send_append_entries(ctx, node, false);
    });

    if (entry->commit_type == LIBFLOAT_EVENTUALLY_CONSISTENT)
    {
        /* We have written the log, and send RPCs to all nodes of the cluster */
        if (entry->commit != NULL)
            entry->commit(entry->udata, LIBFLOAT_ENTRY_COMMITTED);
        entry->commit = NULL;
    }

    if (ctx->n_nodes == 1)
    {
        /* We're alone in the cluster, let's consider this entry committed */
        if (entry->commit_type > LIBFLOAT_EVENTUALLY_CONSISTENT)
        {
            /* We've already call the callback earlier */
            if (entry->commit != NULL)
                entry->commit(entry->udata, LIBFLOAT_ENTRY_COMMITTED);
            entry->commit = NULL;
        }

        ctx->free(entry->data->buf);
        ctx->free(entry->data);
        entry->data = NULL;
    }

    /* track timeout for submitted logs, only with commit set */
    if (entry->commit != NULL)
    {
        libfloat_list_add_tail(&entry->next, &ctx->logs);
    }

    if (ctx->conf.compact_every_n_log != 0)
    {
        /* User asked for snapshots, let's check it */
        if (ctx->persistent.commit_index - ctx->persistent.snapshot.index >= ctx->conf.compact_every_n_log)
        {
            ctx->append_snapshot_log(ctx, ctx->persistent.commit_index, ctx->persistent.term);
        }
    }

    return true;
}

static void libfloat_delete_log(libfloat_ctx_t *ctx, libfloat_log_entry_t *log)
{
    khint_t     iterator;

    ctx->delete_log(ctx, log);

    if (log->data != NULL)
    {
        ctx->free(log->data->buf);
        ctx->free(log->data);
    }
    if (log->commit != NULL)
    {
        log->commit(log->udata, LIBFLOAT_ENTRY_REFUSED);
        libfloat_list_del(&log->next);
    }

    iterator = kh_get(libfloat_entry_id_t, ctx->persistent.log, log->id);
    kh_del(libfloat_entry_id_t, ctx->persistent.log, iterator);
}

static bool libfloat_get_log_from_db(libfloat_ctx_t *ctx, size_t id, libfloat_log_entry_t **out)
{
    libfloat_log_entry_t        *entry = NULL;

    entry = ctx->calloc(1, sizeof(*entry));
    if (entry == NULL)
        return false;

    entry->id = id;
    if (ctx->get_log(ctx, entry->id, &entry->term, NULL))
    {
        khint_t                     iterator;
        int                         absent;

        /* Reset the list head */
        entry->node_acks.prev = &entry->node_acks;
        entry->node_acks.next = &entry->node_acks;

        iterator = kh_put(libfloat_entry_id_t, ctx->persistent.log, entry->id, &absent);
        kh_value(ctx->persistent.log, iterator) = entry;
        *out = entry;
        return true;
    }

    *out = NULL;
    ctx->free(entry);
    return false;
}

void libfloat_send_append_entries(libfloat_ctx_t *ctx, libfloat_node_t *node, bool heartbeat)
{
    libfloat_rpc_append_entries_t       req = { 0 };
    libfloat_log_entry_t                *log;
    libfloat_entry_id_t                 last_log = 1;
    khint_t                             iterator;
    libfloat_entry_id_t                 i;
    libfloat_entry_id_t                 j;

    req.term = ctx->persistent.term;
    req.leader_id = ctx->me->id;
    req.leader_commit = ctx->persistent.commit_index;
    req.n_entries = 0;

    if (heartbeat)
    {
        /* We've been asked to send an empty entry, skip everything */
        goto end;
    }

    if (node->next_log_to_send == ctx->persistent.snapshot.index)
    {
        /* Snapshot log was sent earlier, no-op till the node replies */
        return;
    }
    else if (node->next_log_to_send < ctx->persistent.snapshot.index)
    {
        if (node->snapshot_count > 5)
        {
            /* Node has missed too many logs since the snapshot, let's send the snapshot */
            DEBUG(ctx, "Send snapshot (next_log_to_send=%u, snapshot=%u)!",
                node->next_log_to_send,
                ctx->persistent.snapshot.index
            );
            node->snapshot_count = 0;
            node->snapshot_in_progress = true;
            ctx->send_snapshot(ctx, node);
            /* check that there is not already a snapshot in progress */
            return;
        }
        else
            node->snapshot_count++;

        goto end;
    }

    /* reset snapshot flag */
    node->snapshot_in_progress = false;

    if (node->next_log_to_send <= ctx->persistent.commit_index)
    {

        /* Need to send some data! */
        req.n_entries = min(ctx->persistent.commit_index - node->next_log_to_send + 1, ctx->conf.max_logs_per_ae);
        req.entries = ctx->malloc(sizeof(libfloat_log_entry_t *) * req.n_entries);

        /* Dump our entire log from the last known replicated point + 1 */
        for (i = 0, j = node->next_log_to_send; i < req.n_entries; i++, j++)
        {
            iterator = kh_get(libfloat_entry_id_t, ctx->persistent.log, j);
            if (iterator == kh_end(ctx->persistent.log))
            {
                /* Log is not in our hashtable, let's try to retrieve it from the consistent state */
                if (!libfloat_get_log_from_db(ctx, j, &log))
                {
                    /* Log is also not in our database, that's very bad, let's step down from leadership */
                    ERROR(ctx, "libfloat_send_append_entries: Can't find log %u in our hashtable, stepping down from leadership", j);
                    libfloat_become_follower(ctx);
                    ctx->stepping_down = true;
                    return;
                }
            }
            else
            {
                log = kh_value(ctx->persistent.log, iterator);
            }

            if (log->data == NULL)
            {
                /* Log is not in memory, let's get it from db */
                log->data = ctx->malloc(sizeof(*log->data));
                ctx->get_log(ctx, log->id, &log->term, log->data);
            }

            req.entries[i] = log;
            last_log = log->id;
        }

        if (i != req.n_entries)
        {
            ERROR(ctx, "libfloat_send_append_entries: Our log does not match what we should be sending, aborting");
            return;
        }
    }

    if (node->next_log_to_send > 1)
    {
        libfloat_entry_id_t     node_prev_id = node->next_log_to_send - 1;

        if (node_prev_id == ctx->persistent.snapshot.index)
        {
            req.prev_log_index = ctx->persistent.snapshot.index;
            req.prev_log_term = ctx->persistent.snapshot.term;
            goto end;
        }

        /* We need to find the previous log before the first we are about to send, for term informations */
        iterator = kh_get(libfloat_entry_id_t, ctx->persistent.log, node_prev_id);

        if (iterator == kh_end(ctx->persistent.log))
        {
            /* Log is not in our hashtable, let's try to retrieve it from the consistent state */
            if (!libfloat_get_log_from_db(ctx, node_prev_id, &log))
            {
                /* Can't find that log, that's probably very bad, let's step down from leadership */
                ERROR(ctx, "libfloat_send_append_entries: Can't find the previous log to send node->next_log_to_send=%u id=%u, stepping down from leadership", node->next_log_to_send, node_prev_id);
                libfloat_become_follower(ctx);
                ctx->stepping_down = true;
                return;
            }
        }
        else
        {
            log = kh_value(ctx->persistent.log, iterator);
        }

        req.prev_log_index = log->id;
        req.prev_log_term = log->term;
    }

end:
    if (req.n_entries > 0)
    {
        DEBUG(ctx, "Sending AE request to node %d: term=%d prev_log_index=%d prev_log_term=%d leader_commit=%u n_entries=%zu",
            node->id, req.term, req.prev_log_index, req.prev_log_term, req.leader_commit, req.n_entries
        );

        /* Let's be optimistic about replication, assume the node is gonna replicate and not spam it */
        if (ctx->conf.optimistic_replication && req.n_entries > 20)
            node->next_log_to_send = last_log + 1;
    }

    ctx->append_entries(ctx, node, &req);
    ctx->free(req.entries);
}

void libfloat_append_entries_receive(libfloat_ctx_t *ctx, libfloat_rpc_append_entries_t *req, libfloat_rpc_append_entries_response_t *resp)
{
    libfloat_node_t *node = libfloat_get_node(ctx, req->leader_id);

    if (ctx->state == RAFT_STATE_CANDIDATE && ctx->persistent.term == req->term)
    {
        /* Someone has been elected! */
        libfloat_become_follower(ctx);
    }
    else if (req->term > ctx->persistent.term)
    {
        libfloat_set_current_term(ctx, req->term);
        libfloat_become_follower(ctx);
    }
    else if (req->term < ctx->persistent.term)
    {
        if (req->leader_commit > ctx->persistent.commit_index)
        {
            /* Leader has a term lower than us but more log, let's rollback our term. */
            libfloat_set_current_term(ctx, req->term);
            libfloat_become_follower(ctx);
        }
        else
        {
            /* Who are you and how are you in a position of leadership? */
            DEBUG(ctx, "libfloat_append_entries_receive: Dropping AE term=%d remote_term=%d", ctx->persistent.term, req->term);
            libfloat_election_start(ctx);
            return;
        }
    }

    if (ctx->state == RAFT_STATE_LEADER)
    {
        DEBUG(ctx, "libfloat_append_entries_receive: Dropping AE receive because we are leader");
        return;
    }

    /* Reset timer */
    ctx->timeout_elapsed = 0;

    if (req->prev_log_index > 0)
    {
        libfloat_log_entry_t    *log;
        khint_t                 iterator;

        if (req->prev_log_index == ctx->persistent.snapshot.index)
        {
            if (ctx->persistent.snapshot.term == req->prev_log_term)
            {
                /* Prev log is a snapshot, and it's a match, let's skip all the checks */
                goto response;
            }

            /* Prev log is a snapshot, but term mismatch */
            // XXX: Drop DB // Force snapshot to be sent
        }

        iterator = kh_get(libfloat_entry_id_t, ctx->persistent.log, req->prev_log_index);
        if (iterator == kh_end(ctx->persistent.log))
        {
            if (!libfloat_get_log_from_db(ctx, req->prev_log_index, &log))
            {
                /* This log is unknown, abort */
                DEBUG(ctx, "libfloat_append_entries_receive: Unknown prev_log_index %u", req->prev_log_index);
                resp->success = false;

                if (kh_size(ctx->persistent.log) == 0)
                {
                    /* We don't have a log anymore apparently, let's inform the leader */
                    resp->current_index = 0;
                }
                goto end;
            }
        }
        else
        {
            log = kh_value(ctx->persistent.log, iterator);
        }

        if (log->term != req->prev_log_term)
        {
            /* XXXX On rare case, kh_get seems crash with heap-buffer-overflow */
            if (ctx->conf.do_revert)
            {
                /* That's a term mismatch, let's drop that log, and everything after it */
                resp->current_index = req->prev_log_index - 1;

                for (size_t i = log->id; i <= ctx->persistent.commit_index; i++)
                {
                    iterator = kh_get(libfloat_entry_id_t, ctx->persistent.log, i);
                    log = kh_value(ctx->persistent.log, iterator);
                    libfloat_delete_log(ctx, log);
                }

                libfloat_set_current_commit_index(ctx, resp->current_index);
                goto end;
            }
        }
    }

response:
    resp->success = true;

    /* Update leader if necessary  */
    if (node == NULL)
        node = libfloat_get_node(ctx, req->leader_id);

    if (ctx->leader != node)
        ctx->stat.leader_election++;

    ctx->leader = node;
    ctx->stepping_down = false;

    if (ctx->leader != NULL)
    {
        /* Let's update our internal state in case we get the leadership later */
        ctx->leader->replicated_log = req->leader_commit;
        ctx->leader->next_log_to_send = req->leader_commit + 1;
    }

    for (size_t i = 0; i < req->n_entries; i++)
    {
        libfloat_log_entry_t    *log = NULL;
        khint_t                 iterator;
        int                     absent;

        iterator = kh_get(libfloat_entry_id_t, ctx->persistent.log, req->entries[i]->id);
        if (iterator != kh_end(ctx->persistent.log))
        {
            /* This log was already committed, ignore it */
            resp->current_index = req->entries[i]->id;
            continue;
        }

        log = ctx->calloc(1, sizeof(*log));
        log->id = req->entries[i]->id;
        log->term = req->entries[i]->term;
        log->node_acks.prev = &log->node_acks;
        log->node_acks.next = &log->node_acks;
        /* Don't allocate memory for data, we don't need to keep it in RAM for now */

        iterator = kh_put(libfloat_entry_id_t, ctx->persistent.log, log->id, &absent);
        kh_value(ctx->persistent.log, iterator) = log;

        if (req->entries[i]->data->type == LIBFLOAT_SNAPSHOT_LOG)
        {
            ctx->snapshot_to_commit = log->id;

            /* We need to save the data for application later */
            log->data = ctx->calloc(1, sizeof(*log->data));
            log->data->type = req->entries[i]->data->type;
            log->data->len = req->entries[i]->data->len;

            log->data->buf = ctx->malloc(log->data->len);
            memcpy(log->data->buf, req->entries[i]->data->buf, log->data->len);

            libfloat_internal_snapshot_begin(ctx, log->id, log->term);
        }
        else if (!ctx->is_snapshotting)
        {
            if (!ctx->write_log(ctx, req->entries[i]))
            {
                kh_del(libfloat_entry_id_t, ctx->persistent.log, iterator);
                libfloat_log_free_acks(ctx, log);
                ctx->free(log);
                ERROR(ctx, "libfloat_append_entries_receive: Cannot write log id=%u", req->entries[i]->id);
                goto end;
            }

            ctx->last_log = log;
        }
        else if (ctx->is_snapshotting)
        {
            ctx->snapshot_to_commit = log->id;

            /* We need to save the data for application later */
            log->data = ctx->calloc(1, sizeof(*log->data));
            log->data->type = req->entries[i]->data->type;
            log->data->len = req->entries[i]->data->len;

            log->data->buf = ctx->malloc(log->data->len);
            memcpy(log->data->buf, req->entries[i]->data->buf, log->data->len);
        }

        resp->current_index = log->id;
     }

end:
    if (!resp->success)
        resp->current_index = ctx->persistent.commit_index;
    else
    {
        if (resp->current_index == 0)
            resp->current_index = ctx->persistent.commit_index;
        else
        {
            if (!ctx->is_snapshotting && ctx->last_log)
                libfloat_set_current_commit_index(ctx, ctx->last_log->id);
        }
    }

    if (node != NULL && req->n_entries > 0)
    {
        DEBUG(ctx, "Received AE request from node %d: term=%d my_term=%d prev_log_index=%d prev_log_term=%d leader_commit=%d my_commit=%d n_entries=%zu",
            node->id, req->term, ctx->persistent.term, req->prev_log_index,
            req->prev_log_term, req->leader_commit, resp->current_index, req->n_entries
        );
    }

    if (resp->current_index == req->leader_commit)
        ctx->me->is_up_to_date = true;
    else
        ctx->me->is_up_to_date = false;

    resp->node = ctx->me->id;
    resp->term = ctx->persistent.term;
    resp->first_index = req->prev_log_index;
}

void libfloat_append_entries_response(libfloat_ctx_t *ctx, libfloat_rpc_append_entries_response_t *resp)
{
    libfloat_node_t             *node = libfloat_get_node(ctx, resp->node);
    libfloat_log_entry_t        *log = NULL;

    if (node == NULL)
    {
        /* I don't know you, go away */
        return;
    }

    if (ctx->state != RAFT_STATE_LEADER)
    {
        /* Not a leader, can't take this request */
        return;
    }

    if (resp->current_index != resp->first_index)
    {
        DEBUG(ctx, "Received AE response from node %d: term=%d success=%d current_index=%d first_index=%d",
            node->id, resp->term, resp->success, resp->current_index, resp->first_index
        );
    }

    /* Set the next index to sent to the node */
    node->last_update = ctx->time(NULL);
    if (node->next_log_to_send <= resp->current_index)
    {
        node->next_log_to_send = resp->current_index + 1;
        node->replicated_log = resp->current_index;
    }

    if (ctx->persistent.term < resp->term)
    {
        libfloat_set_current_term(ctx, resp->term);
        libfloat_become_follower(ctx);
        ctx->leader = NULL;
    }
    else if (ctx->persistent.term != resp->term)
    {
        DEBUG(ctx, "libfloat_append_entries_response: Not same term");
        /* We're not on speaking terms (get it?) */
        return;
    }

    if (resp->success == false)
    {
        if (resp->current_index < node->replicated_log)
        {
            if (resp->current_index == 0)
            {
                /* Node has lost its log, let's reset our state */
                node->replicated_log = 0;
                node->next_log_to_send = 1;
            }

            return;
        }

        node->next_log_to_send = max(resp->current_index, 1);
        node->replicated_log = resp->current_index;

        libfloat_send_append_entries(ctx, node, false);
        return;
    }

    if (resp->current_index != resp->first_index)
    {
        /* Node has applied some logs */

        for (libfloat_entry_id_t i = resp->first_index + 1; i <= resp->current_index; i++)
        {
            khint_t     iterator;

            if (i <= ctx->persistent.snapshot.index)
            {
                /* This entry is in a snapshot, let's not bother searching for it anywhere */
                continue;
            }

            /**
             * For each log that the node committed, let's update our count and
             * calls some function if necessary
             */
            iterator = kh_get(libfloat_entry_id_t, ctx->persistent.log, i);
            if (iterator == kh_end(ctx->persistent.log))
            {
                /* That's a problem */
                ERROR(ctx, "libfloat_append_entries_response: Cannot find log for node->match_index=%u node=%d",
                    i, node->id
                );
                return;
            }

            log = kh_value(ctx->persistent.log, iterator);


            libfloat_log_add_node_ack(ctx, log, node->id);

            if (libfloat_list_count(&log->node_acks) >= ctx->n_nodes)
            {
                if (log->commit_type == LIBFLOAT_ABSOLUTELY_CONSISTENT)
                {
                    if (log->commit != NULL)
                    {
                        log->commit(log->udata, LIBFLOAT_ENTRY_COMMITTED);
                        libfloat_list_del(&log->next);
                    }
                    log->commit = NULL;
                }

                /* _ALL_ the nodes have replicated this log, we probably don't need this log in RAM for now */
                if (log->data != NULL)
                {
                    ctx->free(log->data->buf);
                    ctx->free(log->data);
                    libfloat_log_free_acks(ctx, log);
                    log->data = NULL;
                }
            }
            else if (libfloat_list_count(&log->node_acks) >= ctx->n_nodes / 2 + 1)
            {
                if (log->commit_type <= LIBFLOAT_STRONGLY_CONSISTENT)
                {
                    if (log->commit != NULL)
                    {
                        log->commit(log->udata, LIBFLOAT_ENTRY_COMMITTED);
                        libfloat_list_del(&log->next);
                    }
                    log->commit = NULL;
                }
            }
        }
    }

    if (node->replicated_log < ctx->persistent.commit_index)
    {
        /* We still have some log to send! */
        node->is_up_to_date = false;
        if (ctx->persistent.commit_index - node->replicated_log > ctx->conf.max_logs_per_ae)
        {
            /* We still have a lot of logs to send, let's do it */
            libfloat_send_append_entries(ctx, node, false);
        }
    }
    else
        node->is_up_to_date = true;
}

bool libfloat_get_last_term(libfloat_ctx_t *ctx, libfloat_entry_id_t *id, libfloat_term_t *term)
{
    libfloat_entry_id_t                 i = ctx->persistent.commit_index;
    khint_t                             iterator;
    libfloat_log_entry_t                *log = NULL;

    iterator = kh_get(libfloat_entry_id_t, ctx->persistent.log, i);
    if (iterator == kh_end(ctx->persistent.log))
    {
        /* NOT FOUND, NEED TO LOAD LOG */
        if (!libfloat_get_log_from_db(ctx, i, &log))
        {
            ERROR(ctx, "Log %u not found while retrieving last term", i);
            return false;
        }
    }
    else
    {
        log = kh_value(ctx->persistent.log, iterator);
    }

    if (log->data == NULL)
    {
        /* Log is not in memory, let's get it from db */
        log->data = ctx->malloc(sizeof(*log->data));
        ctx->get_log(ctx, log->id, &log->term, log->data);
    }

    if (term != NULL)
        *term = log->data->term;
    if (id != NULL)
        *id = log->id;

    return true;
}

void libfloat_log_memory_cleanup(libfloat_ctx_t *ctx)
{
    khiter_t                            iterator;
    libfloat_log_entry_t                *log = NULL;

    for (iterator = kh_begin(ctx->persistent.log); iterator != kh_end(ctx->persistent.log); iterator++)
    {
        if (kh_exist(ctx->persistent.log, iterator))
        {
            log = kh_value(ctx->persistent.log, iterator);

            if (log->data != NULL)
            {
                ctx->free(log->data->buf);
                ctx->free(log->data);
            }

            if (log->commit != NULL)
            {
                log->commit(log->udata, LIBFLOAT_ENTRY_REFUSED);
                libfloat_list_del(&log->next);
            }

            kh_del(libfloat_entry_id_t, ctx->persistent.log, iterator);
            ctx->free(log);
        }
    }
}