Repository: etcd-io/etcd
File: server/storage/wal/wal.go (Go)

Code before:
	// create a WAL ready for reading
	w := &WAL{
		lg:        lg,
		dir:       dirpath,
		start:     snap,
		decoder:   NewDecoder(rs...),
		readClose: closer,
		locks:     ls,
	}

	if write {
		// write reuses the file descriptors from read; don't close so
		// WAL can append without dropping the file lock
		w.readClose = nil
		if _, _, err := parseWALName(filepath.Base(w.tail().Name())); err != nil {
			closer()
			return nil, fmt.Errorf("[openAtIndex] parseWALName failed: %w", err)
		}
		w.fp = newFilePipeline(lg, w.dir, SegmentSizeBytes)
	}

	return w, nil
}

func ReadAllEntries(lg *zap.Logger, dirpath string) (state raftpb.HardState, ents []raftpb.Entry, err error) {
	names, nameIndex, err := selectWALFiles(lg, dirpath, walpb.Snapshot{Term: 0, Index: 0})
	if err != nil {
		return state, nil, fmt.Errorf("[ReadAllEntries] selectWALFiles failed: %w", err)
	}

	rs, _, closer, err := openWALFiles(lg, dirpath, names, nameIndex, false)
	if err != nil {
		return state, nil, fmt.Errorf("[ReadAllEntries] openWALFiles failed: %w", err)
	}
	defer closer()
	rec := &walpb.Record{}
	decoder := NewDecoder(rs...)
	var startIndex uint64
	for err = decoder.Decode(rec); err == nil; err = decoder.Decode(rec) {
		switch rec.Type {
		case EntryType:
			e := MustUnmarshalEntry(rec.Data)
			if e.Index == 0 {
				return state, nil, fmt.Errorf("entry with index 0 found")
			}
			if startIndex == 0 {
				startIndex = e.Index
				ents = append(ents, e)
				continue
			}
			if e.Index > startIndex {

Review comment (suggestion):
The implementation can be replaced with code something like below. 

```
w := OpenForRead(...., walpb.Snapshot{Term: 0, Index: 0})
... := w.ReadAll(...)
```

But note that both implementations have flaw, usually it works in test because the WAL files normally won't be rotated in a test case. Once it gets rotated, `selectWALFiles` may return error, as the minimal WAL index > 0.

Code after:
	// create a WAL ready for reading
	w := &WAL{
		lg:        lg,
		dir:       dirpath,
		start:     snap,
		decoder:   NewDecoder(rs...),
		readClose: closer,
		locks:     ls,
	}

	if write {
		// write reuses the file descriptors from read; don't close so
		// WAL can append without dropping the file lock
		w.readClose = nil
		if _, _, err := parseWALName(filepath.Base(w.tail().Name())); err != nil {
			closer()
			return nil, fmt.Errorf("[openAtIndex] parseWALName failed: %w", err)
		}
		w.fp = newFilePipeline(lg, w.dir, SegmentSizeBytes)
	}

	return w, nil
}

func selectWALFiles(lg *zap.Logger, dirpath string, snap walpb.Snapshot) ([]string, int, error) {
	names, err := readWALNames(lg, dirpath)
	if err != nil {
		return nil, -1, fmt.Errorf("readWALNames failed: %w", err)
	}

	nameIndex, ok := searchIndex(lg, names, snap.Index)
	if !ok {
		return nil, -1, fmt.Errorf("wal: file not found which matches the snapshot index '%d'", snap.Index)
	}

	if !isValidSeq(lg, names[nameIndex:]) {
		return nil, -1, fmt.Errorf("wal: file sequence numbers (starting from %d) do not increase continuously", nameIndex)
	}

	return names, nameIndex, nil
}

func openWALFiles(lg *zap.Logger, dirpath string, names []string, nameIndex int, write bool) ([]fileutil.FileReader, []*fileutil.LockedFile, func() error, error) {
	rcs := make([]io.ReadCloser, 0)
	rs := make([]fileutil.FileReader, 0)
	ls := make([]*fileutil.LockedFile, 0)
	for _, name := range names[nameIndex:] {
		p := filepath.Join(dirpath, name)
		var f *os.File
		if write {
			l, err := fileutil.TryLockFile(p, os.O_RDWR, fileutil.PrivateFileMode)