예제 #1
0
def test_recover_raftlog(tmp_path):
    for i in range(3):
        with open(tmp_path / str(i), "w") as f:
            f.write(LogEntry(i, i).toyaml())
    l1 = RaftLog(tmp_path)
    assert l1.measure_log() == (2, 3)
    assert l1 == [LogEntry(0, 0), LogEntry(1, 1), LogEntry(2, 2)]
예제 #2
0
def test_append_entries():
    l1 = RaftLog()

    assert l1.measure_log() == (-1, 0)

    assert l1.append_entries(0, None, [LogEntry(1, 1)])
    assert l1 == [LogEntry(1, 1)]

    # do the same thing again
    assert l1.append_entries(0, None, [LogEntry(1, 1)])
    assert l1 == [LogEntry(1, 1)]

    # skip index not allowed
    assert not l1.append_entries(2, 1, [LogEntry(1, 1)])

    assert l1.measure_log() == (1, 1)
    assert not l1.append_entries(1, 0, [LogEntry(0, 0)])
    assert not l1.append_entries(2, 1, [LogEntry(0, 0)])
    assert l1 == [LogEntry(1, 1)]

    assert l1.append_entries(1, 1, [LogEntry(1, 1)])
    assert l1 == [LogEntry(1, 1), LogEntry(1, 1)]

    assert l1.measure_log() == (1, 2)
    assert l1.append_entries(1, 1, [LogEntry(1, 2)])
    assert l1 == [LogEntry(1, 1), LogEntry(1, 2)]

    assert l1.append_entries(0, None, [LogEntry(1, 3)])
    assert l1 == [LogEntry(1, 3)]

    assert l1.append_entries(0, None, [LogEntry(1, 4), LogEntry(1, 5)])
    assert l1 == [LogEntry(1, 4), LogEntry(1, 5)]

    # adding entries that don't satisfy the non-decreasing terms
    with pytest.raises(RaftLogInconsistent):
        l1.append_entries(1, 1, [LogEntry(2, 6), LogEntry(1, 5)])

    # Note: we detect this inconsistency, but don't attempt a fix
    with pytest.raises(RaftLogInconsistent):
        l1._invariant()

    # reset l1
    assert l1.append_entries(0, None, [LogEntry(1, 4), LogEntry(1, 5)])
    assert l1 == [LogEntry(1, 4), LogEntry(1, 5)]

    # added entries satisfy the non-decreasing terms but not in combination
    # with what is already there
    with pytest.raises(RaftLogInconsistent):
        l1.append_entries(1, 1, [LogEntry(0, 6), LogEntry(2, 5)])

    # reset l1
    assert l1.append_entries(0, None, [LogEntry(1, 4), LogEntry(1, 5)])
    assert l1 == [LogEntry(1, 4), LogEntry(1, 5)]

    assert l1.append_entries(1, 1, [LogEntry(2, 6), LogEntry(2, 5)])
    assert l1.measure_log() == (2, 3)

    assert l1.append_entries(3, 2, [])
    assert l1.measure_log() == (2, 3)

    assert not l1.append_entries(4, 2, [])
    assert not l1.append_entries(3, 1, [])

    # the paper doesn't quite seem to specify if the truncate should
    # happen in this case, but I believe it does no harm
    assert l1.append_entries(2, 2, [])
    assert l1.measure_log() == (2, 2)