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

Code before:
			if e.Index > startIndex {
				offset := e.Index - startIndex
				if offset > uint64(len(ents)) {
					ents = append(ents, make([]raftpb.Entry, offset-uint64(len(ents)))...)
				}
				// The line below is potentially overriding some 'uncommitted' entries.
				ents = append(ents[:offset], e)
			}
		case StateType:
			state = MustUnmarshalState(rec.Data)
		case MetadataType:
		case CrcType:
		case SnapshotType:
		default:
			return state, nil, fmt.Errorf("unexpected block type %d", rec.Type)
		}
	}
	if err != nil && !errors.Is(err, io.EOF) {
		return state, ents, err
	}
	// Filter empty entries
	i := 0
	for _, e := range ents {
		if e.Index == 0 {
			continue
		}
		ents[i] = e
		i++
	}
	ents = ents[:i]
	return state, ents, 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
}


Review comment (suggestion):
Maybe it's just me, but the first I saw this line, I thought the code could not reach here because of the logic on line 417.  Then looking again, the empty entries are from offset handling on line 428.  

Perhaps we should make it clear on that line that line 428 pads the empty slots.  Not sure if something like below would make it clearer...:
```go
ents = append(ents, 
  slices.Repeat(raftpb.Entry{}, offset-uint64(len(ents))...)
```

Code after:
			l, err := fileutil.TryLockFile(p, os.O_RDWR, fileutil.PrivateFileMode)
			if err != nil {
				closeAll(lg, rcs...)
				return nil, nil, nil, fmt.Errorf("[openWALFiles] fileutil.TryLockFile failed: %w", err)
			}
			ls = append(ls, l)
			rcs = append(rcs, l)
			f = l.File
		} else {
			rf, err := os.OpenFile(p, os.O_RDONLY, fileutil.PrivateFileMode)
			if err != nil {
				closeAll(lg, rcs...)
				return nil, nil, nil, fmt.Errorf("[openWALFiles] os.OpenFile failed (%q): %w", p, err)
			}
			ls = append(ls, nil)
			rcs = append(rcs, rf)
			f = rf
		}
		fileReader := fileutil.NewFileReader(f)
		rs = append(rs, fileReader)
	}

	closer := func() error { return closeAll(lg, rcs...) }

	return rs, ls, closer, nil
}

// ReadAll reads out records of the current WAL.
// If opened in write mode, it must read out all records until EOF. Or an error
// will be returned.
// If opened in read mode, it will try to read all records if possible.
// If it cannot read out the expected snap, it will return ErrSnapshotNotFound.
// If loaded snap doesn't match with the expected one, it will return
// all the records and error ErrSnapshotMismatch.
// TODO: detect not-last-snap error.
// TODO: maybe loose the checking of match.
// After ReadAll, the WAL will be ready for appending new records.
//
// ReadAll suppresses WAL entries that got overridden (i.e. a newer entry with the same index
// exists in the log). Such a situation can happen in cases described in figure 7. of the
// RAFT paper (http://web.stanford.edu/~ouster/cgi-bin/papers/raft-atc14.pdf).
//
// ReadAll may return uncommitted yet entries, that are subject to be overridden.
// Do not apply entries that have index > state.commit, as they are subject to change.
func (w *WAL) ReadAll() (metadata []byte, state raftpb.HardState, ents []raftpb.Entry, err error) {
	w.mu.Lock()
	defer w.mu.Unlock()

	rec := &walpb.Record{}

	if w.decoder == nil {