Pro2a中storage.FirstIndex()为什么需要加1

func (ms *MemoryStorage) FirstIndex() (uint64, error) {
	ms.Lock()
	defer ms.Unlock()
	return ms.firstIndex(), nil
}

func (ms *MemoryStorage) firstIndex() uint64 {
        // ???
	return ms.ents[0].Index + 1
}

因为ents[0]里面的是dummy index,指的是之前被 compact 的最大 index,所以你的first index要在dummy index 上面 + 1.

2 个赞

它这里log从1开始,原本的memorystorage在开始时就会放入一个dummy entry占位

1 个赞

懂了 谢谢

这里的 firstIndex指的是实际会用到的 entries 的第一个 Index。即这里的第0项目是用来存储之前已经压缩的 Index 或 dummyIndex 即 0。

2 个赞

如果有使用ApplySnapshot的话那么ms.ents的第一位就是snapshot的index,那么第一个index就应该在此基础上加一
如果没有使用ApplySnapshot,那么在初始化的时候就放入了一个dummy entry,默认的index为0,+1后为1刚好是index的起始数字

1 个赞

谢谢,明白了,很详细

此话题已在最后回复的 1 分钟后被自动关闭。不再允许新回复。