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

Code before:
	}

	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 firstIndex uint64
	var firstEntry = true
	for err = decoder.Decode(rec); err == nil; err = decoder.Decode(rec) {
		switch rec.Type {
		case EntryType:
			e := MustUnmarshalEntry(rec.Data)
			if firstEntry {
				firstEntry = false
				firstIndex = e.Index
				ents = append(ents, e)
				continue
			}
			if e.Index > firstIndex {
				offset := e.Index - firstIndex
				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:
			crc := decoder.LastCRC()
			// current crc of decoder must match the crc of the record.
			// do no need to match 0 crc, since the decoder is a new one at this case.
			if crc != 0 && rec.Validate(crc) != nil {
				state.Reset()
				return state, nil, ErrCRCMismatch
			}
			decoder.UpdateCRC(rec.Crc)
		case SnapshotType:
		default:
			return state, nil, fmt.Errorf("unexpected block type %d", rec.Type)
		}
	}
	if err != nil && !errors.Is(err, io.EOF) {
		return state, nil, err
	}
	// Filter empty entries
	i := 0

Review comment (refactor):
You add empty entries (with 0 index) here, and you filter them out below (at line 452-460). It seems you can simplify the implementation by removing both?

Code after:
	}

	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)
			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