Commit 9de8af47 authored by Yossi Gottlieb's avatar Yossi Gottlieb Committed by Willem
Browse files

Off by one fixes (#65)

* Fix off-by-one in apply_log idx.

* Fix log_get_from_idx() off by one error.
parent ef956c12
...@@ -172,7 +172,7 @@ raft_entry_t* log_get_from_idx(log_t* me_, int idx, int *n_etys) ...@@ -172,7 +172,7 @@ raft_entry_t* log_get_from_idx(log_t* me_, int idx, int *n_etys)
assert(0 <= idx - 1); assert(0 <= idx - 1);
if (me->base + me->count < idx || idx < me->base) if (me->base + me->count < idx || idx <= me->base)
{ {
*n_etys = 0; *n_etys = 0;
return NULL; return NULL;
......
...@@ -805,7 +805,7 @@ int raft_apply_entry(raft_server_t* me_) ...@@ -805,7 +805,7 @@ int raft_apply_entry(raft_server_t* me_)
me->last_applied_idx++; me->last_applied_idx++;
if (me->cb.applylog) if (me->cb.applylog)
{ {
int e = me->cb.applylog(me_, me->udata, ety, me->last_applied_idx - 1); int e = me->cb.applylog(me_, me->udata, ety, me->last_applied_idx);
if (RAFT_ERR_SHUTDOWN == e) if (RAFT_ERR_SHUTDOWN == e)
return RAFT_ERR_SHUTDOWN; return RAFT_ERR_SHUTDOWN;
} }
...@@ -1259,6 +1259,12 @@ int raft_end_snapshot(raft_server_t *me_) ...@@ -1259,6 +1259,12 @@ int raft_end_snapshot(raft_server_t *me_)
me->snapshot_in_progress = 0; me->snapshot_in_progress = 0;
__log(me_, NULL,
"end snapshot base:%d commit-index:%d current-index:%d\n",
log_get_base(me->log),
raft_get_commit_idx(me_),
raft_get_current_idx(me_));
if (!raft_is_leader(me_)) if (!raft_is_leader(me_))
return 0; return 0;
......
...@@ -579,3 +579,49 @@ void TestLog_delete_after_polling_from_double_append(CuTest * tc) ...@@ -579,3 +579,49 @@ void TestLog_delete_after_polling_from_double_append(CuTest * tc)
CuAssertIntEquals(tc, log_delete(l, 1), 0); CuAssertIntEquals(tc, log_delete(l, 1), 0);
CuAssertIntEquals(tc, 0, log_count(l)); CuAssertIntEquals(tc, 0, log_count(l));
} }
void TestLog_get_from_idx_with_base_off_by_one(CuTest * tc)
{
void* queue = llqueue_new();
void *r = raft_new();
raft_cbs_t funcs = {
.log_pop = __log_pop,
.log_get_node_id = __logentry_get_node_id
};
raft_set_callbacks(r, &funcs, queue);
void *l;
raft_entry_t e1, e2;
memset(&e1, 0, sizeof(raft_entry_t));
memset(&e2, 0, sizeof(raft_entry_t));
e1.id = 1;
e2.id = 2;
l = log_alloc(1);
log_set_callbacks(l, &funcs, r);
raft_entry_t* ety;
/* append append */
CuAssertIntEquals(tc, 0, log_append_entry(l, &e1));
CuAssertIntEquals(tc, 0, log_append_entry(l, &e2));
CuAssertIntEquals(tc, 2, log_count(l));
/* poll */
CuAssertIntEquals(tc, log_poll(l, (void*)&ety), 0);
CuAssertIntEquals(tc, ety->id, 1);
CuAssertIntEquals(tc, 1, log_count(l));
/* get off-by-one index */
int n_etys;
CuAssertPtrEquals(tc, log_get_from_idx(l, 1, &n_etys), NULL);
CuAssertIntEquals(tc, n_etys, 0);
/* now get the correct index */
ety = log_get_from_idx(l, 2, &n_etys);
CuAssertPtrNotNull(tc, ety);
CuAssertIntEquals(tc, n_etys, 1);
CuAssertIntEquals(tc, ety->id, 2);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment