wal.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package wal
  15. import (
  16. "bytes"
  17. "errors"
  18. "fmt"
  19. "hash/crc32"
  20. "io"
  21. "os"
  22. "path/filepath"
  23. "sync"
  24. "time"
  25. "github.com/coreos/etcd/pkg/fileutil"
  26. "github.com/coreos/etcd/pkg/pbutil"
  27. "github.com/coreos/etcd/raft"
  28. "github.com/coreos/etcd/raft/raftpb"
  29. "github.com/coreos/etcd/wal/walpb"
  30. "github.com/coreos/pkg/capnslog"
  31. "go.uber.org/zap"
  32. )
  33. const (
  34. metadataType int64 = iota + 1
  35. entryType
  36. stateType
  37. crcType
  38. snapshotType
  39. // warnSyncDuration is the amount of time allotted to an fsync before
  40. // logging a warning
  41. warnSyncDuration = time.Second
  42. )
  43. var (
  44. // SegmentSizeBytes is the preallocated size of each wal segment file.
  45. // The actual size might be larger than this. In general, the default
  46. // value should be used, but this is defined as an exported variable
  47. // so that tests can set a different segment size.
  48. SegmentSizeBytes int64 = 64 * 1000 * 1000 // 64MB
  49. plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "wal")
  50. ErrMetadataConflict = errors.New("wal: conflicting metadata found")
  51. ErrFileNotFound = errors.New("wal: file not found")
  52. ErrCRCMismatch = errors.New("wal: crc mismatch")
  53. ErrSnapshotMismatch = errors.New("wal: snapshot mismatch")
  54. ErrSnapshotNotFound = errors.New("wal: snapshot not found")
  55. crcTable = crc32.MakeTable(crc32.Castagnoli)
  56. )
  57. // WAL is a logical representation of the stable storage.
  58. // WAL is either in read mode or append mode but not both.
  59. // A newly created WAL is in append mode, and ready for appending records.
  60. // A just opened WAL is in read mode, and ready for reading records.
  61. // The WAL will be ready for appending after reading out all the previous records.
  62. type WAL struct {
  63. lg *zap.Logger
  64. dir string // the living directory of the underlay files
  65. // dirFile is a fd for the wal directory for syncing on Rename
  66. dirFile *os.File
  67. metadata []byte // metadata recorded at the head of each WAL
  68. state raftpb.HardState // hardstate recorded at the head of WAL
  69. start walpb.Snapshot // snapshot to start reading
  70. decoder *decoder // decoder to decode records
  71. readClose func() error // closer for decode reader
  72. mu sync.Mutex
  73. enti uint64 // index of the last entry saved to the wal
  74. encoder *encoder // encoder to encode records
  75. locks []*fileutil.LockedFile // the locked files the WAL holds (the name is increasing)
  76. fp *filePipeline
  77. }
  78. // Create creates a WAL ready for appending records. The given metadata is
  79. // recorded at the head of each WAL file, and can be retrieved with ReadAll.
  80. func Create(lg *zap.Logger, dirpath string, metadata []byte) (*WAL, error) {
  81. if Exist(dirpath) {
  82. return nil, os.ErrExist
  83. }
  84. // keep temporary wal directory so WAL initialization appears atomic
  85. tmpdirpath := filepath.Clean(dirpath) + ".tmp"
  86. if fileutil.Exist(tmpdirpath) {
  87. if err := os.RemoveAll(tmpdirpath); err != nil {
  88. return nil, err
  89. }
  90. }
  91. if err := fileutil.CreateDirAll(tmpdirpath); err != nil {
  92. return nil, err
  93. }
  94. p := filepath.Join(tmpdirpath, walName(0, 0))
  95. f, err := fileutil.LockFile(p, os.O_WRONLY|os.O_CREATE, fileutil.PrivateFileMode)
  96. if err != nil {
  97. return nil, err
  98. }
  99. if _, err = f.Seek(0, io.SeekEnd); err != nil {
  100. return nil, err
  101. }
  102. if err = fileutil.Preallocate(f.File, SegmentSizeBytes, true); err != nil {
  103. return nil, err
  104. }
  105. w := &WAL{
  106. lg: lg,
  107. dir: dirpath,
  108. metadata: metadata,
  109. }
  110. w.encoder, err = newFileEncoder(f.File, 0)
  111. if err != nil {
  112. return nil, err
  113. }
  114. w.locks = append(w.locks, f)
  115. if err = w.saveCrc(0); err != nil {
  116. return nil, err
  117. }
  118. if err = w.encoder.encode(&walpb.Record{Type: metadataType, Data: metadata}); err != nil {
  119. return nil, err
  120. }
  121. if err = w.SaveSnapshot(walpb.Snapshot{}); err != nil {
  122. return nil, err
  123. }
  124. if w, err = w.renameWal(tmpdirpath); err != nil {
  125. return nil, err
  126. }
  127. // directory was renamed; sync parent dir to persist rename
  128. pdir, perr := fileutil.OpenDir(filepath.Dir(w.dir))
  129. if perr != nil {
  130. return nil, perr
  131. }
  132. if perr = fileutil.Fsync(pdir); perr != nil {
  133. return nil, perr
  134. }
  135. if perr = pdir.Close(); err != nil {
  136. return nil, perr
  137. }
  138. return w, nil
  139. }
  140. func (w *WAL) renameWal(tmpdirpath string) (*WAL, error) {
  141. if err := os.RemoveAll(w.dir); err != nil {
  142. return nil, err
  143. }
  144. // On non-Windows platforms, hold the lock while renaming. Releasing
  145. // the lock and trying to reacquire it quickly can be flaky because
  146. // it's possible the process will fork to spawn a process while this is
  147. // happening. The fds are set up as close-on-exec by the Go runtime,
  148. // but there is a window between the fork and the exec where another
  149. // process holds the lock.
  150. if err := os.Rename(tmpdirpath, w.dir); err != nil {
  151. if _, ok := err.(*os.LinkError); ok {
  152. return w.renameWalUnlock(tmpdirpath)
  153. }
  154. return nil, err
  155. }
  156. w.fp = newFilePipeline(w.lg, w.dir, SegmentSizeBytes)
  157. df, err := fileutil.OpenDir(w.dir)
  158. w.dirFile = df
  159. return w, err
  160. }
  161. func (w *WAL) renameWalUnlock(tmpdirpath string) (*WAL, error) {
  162. // rename of directory with locked files doesn't work on windows/cifs;
  163. // close the WAL to release the locks so the directory can be renamed.
  164. if w.lg != nil {
  165. w.lg.Info(
  166. "releasing flock to rename",
  167. zap.String("from", tmpdirpath),
  168. zap.String("to", w.dir),
  169. )
  170. } else {
  171. plog.Infof("releasing file lock to rename %q to %q", tmpdirpath, w.dir)
  172. }
  173. w.Close()
  174. if err := os.Rename(tmpdirpath, w.dir); err != nil {
  175. return nil, err
  176. }
  177. // reopen and relock
  178. newWAL, oerr := Open(w.lg, w.dir, walpb.Snapshot{})
  179. if oerr != nil {
  180. return nil, oerr
  181. }
  182. if _, _, _, err := newWAL.ReadAll(); err != nil {
  183. newWAL.Close()
  184. return nil, err
  185. }
  186. return newWAL, nil
  187. }
  188. // Open opens the WAL at the given snap.
  189. // The snap SHOULD have been previously saved to the WAL, or the following
  190. // ReadAll will fail.
  191. // The returned WAL is ready to read and the first record will be the one after
  192. // the given snap. The WAL cannot be appended to before reading out all of its
  193. // previous records.
  194. func Open(lg *zap.Logger, dirpath string, snap walpb.Snapshot) (*WAL, error) {
  195. w, err := openAtIndex(lg, dirpath, snap, true)
  196. if err != nil {
  197. return nil, err
  198. }
  199. if w.dirFile, err = fileutil.OpenDir(w.dir); err != nil {
  200. return nil, err
  201. }
  202. return w, nil
  203. }
  204. // OpenForRead only opens the wal files for read.
  205. // Write on a read only wal panics.
  206. func OpenForRead(lg *zap.Logger, dirpath string, snap walpb.Snapshot) (*WAL, error) {
  207. return openAtIndex(lg, dirpath, snap, false)
  208. }
  209. func openAtIndex(lg *zap.Logger, dirpath string, snap walpb.Snapshot, write bool) (*WAL, error) {
  210. names, err := readWalNames(lg, dirpath)
  211. if err != nil {
  212. return nil, err
  213. }
  214. nameIndex, ok := searchIndex(names, snap.Index)
  215. if !ok || !isValidSeq(names[nameIndex:]) {
  216. return nil, ErrFileNotFound
  217. }
  218. // open the wal files
  219. rcs := make([]io.ReadCloser, 0)
  220. rs := make([]io.Reader, 0)
  221. ls := make([]*fileutil.LockedFile, 0)
  222. for _, name := range names[nameIndex:] {
  223. p := filepath.Join(dirpath, name)
  224. if write {
  225. l, err := fileutil.TryLockFile(p, os.O_RDWR, fileutil.PrivateFileMode)
  226. if err != nil {
  227. closeAll(rcs...)
  228. return nil, err
  229. }
  230. ls = append(ls, l)
  231. rcs = append(rcs, l)
  232. } else {
  233. rf, err := os.OpenFile(p, os.O_RDONLY, fileutil.PrivateFileMode)
  234. if err != nil {
  235. closeAll(rcs...)
  236. return nil, err
  237. }
  238. ls = append(ls, nil)
  239. rcs = append(rcs, rf)
  240. }
  241. rs = append(rs, rcs[len(rcs)-1])
  242. }
  243. closer := func() error { return closeAll(rcs...) }
  244. // create a WAL ready for reading
  245. w := &WAL{
  246. lg: lg,
  247. dir: dirpath,
  248. start: snap,
  249. decoder: newDecoder(rs...),
  250. readClose: closer,
  251. locks: ls,
  252. }
  253. if write {
  254. // write reuses the file descriptors from read; don't close so
  255. // WAL can append without dropping the file lock
  256. w.readClose = nil
  257. if _, _, err := parseWalName(filepath.Base(w.tail().Name())); err != nil {
  258. closer()
  259. return nil, err
  260. }
  261. w.fp = newFilePipeline(w.lg, w.dir, SegmentSizeBytes)
  262. }
  263. return w, nil
  264. }
  265. // ReadAll reads out records of the current WAL.
  266. // If opened in write mode, it must read out all records until EOF. Or an error
  267. // will be returned.
  268. // If opened in read mode, it will try to read all records if possible.
  269. // If it cannot read out the expected snap, it will return ErrSnapshotNotFound.
  270. // If loaded snap doesn't match with the expected one, it will return
  271. // all the records and error ErrSnapshotMismatch.
  272. // TODO: detect not-last-snap error.
  273. // TODO: maybe loose the checking of match.
  274. // After ReadAll, the WAL will be ready for appending new records.
  275. func (w *WAL) ReadAll() (metadata []byte, state raftpb.HardState, ents []raftpb.Entry, err error) {
  276. w.mu.Lock()
  277. defer w.mu.Unlock()
  278. rec := &walpb.Record{}
  279. decoder := w.decoder
  280. var match bool
  281. for err = decoder.decode(rec); err == nil; err = decoder.decode(rec) {
  282. switch rec.Type {
  283. case entryType:
  284. e := mustUnmarshalEntry(rec.Data)
  285. if e.Index > w.start.Index {
  286. ents = append(ents[:e.Index-w.start.Index-1], e)
  287. }
  288. w.enti = e.Index
  289. case stateType:
  290. state = mustUnmarshalState(rec.Data)
  291. case metadataType:
  292. if metadata != nil && !bytes.Equal(metadata, rec.Data) {
  293. state.Reset()
  294. return nil, state, nil, ErrMetadataConflict
  295. }
  296. metadata = rec.Data
  297. case crcType:
  298. crc := decoder.crc.Sum32()
  299. // current crc of decoder must match the crc of the record.
  300. // do no need to match 0 crc, since the decoder is a new one at this case.
  301. if crc != 0 && rec.Validate(crc) != nil {
  302. state.Reset()
  303. return nil, state, nil, ErrCRCMismatch
  304. }
  305. decoder.updateCRC(rec.Crc)
  306. case snapshotType:
  307. var snap walpb.Snapshot
  308. pbutil.MustUnmarshal(&snap, rec.Data)
  309. if snap.Index == w.start.Index {
  310. if snap.Term != w.start.Term {
  311. state.Reset()
  312. return nil, state, nil, ErrSnapshotMismatch
  313. }
  314. match = true
  315. }
  316. default:
  317. state.Reset()
  318. return nil, state, nil, fmt.Errorf("unexpected block type %d", rec.Type)
  319. }
  320. }
  321. switch w.tail() {
  322. case nil:
  323. // We do not have to read out all entries in read mode.
  324. // The last record maybe a partial written one, so
  325. // ErrunexpectedEOF might be returned.
  326. if err != io.EOF && err != io.ErrUnexpectedEOF {
  327. state.Reset()
  328. return nil, state, nil, err
  329. }
  330. default:
  331. // We must read all of the entries if WAL is opened in write mode.
  332. if err != io.EOF {
  333. state.Reset()
  334. return nil, state, nil, err
  335. }
  336. // decodeRecord() will return io.EOF if it detects a zero record,
  337. // but this zero record may be followed by non-zero records from
  338. // a torn write. Overwriting some of these non-zero records, but
  339. // not all, will cause CRC errors on WAL open. Since the records
  340. // were never fully synced to disk in the first place, it's safe
  341. // to zero them out to avoid any CRC errors from new writes.
  342. if _, err = w.tail().Seek(w.decoder.lastOffset(), io.SeekStart); err != nil {
  343. return nil, state, nil, err
  344. }
  345. if err = fileutil.ZeroToEnd(w.tail().File); err != nil {
  346. return nil, state, nil, err
  347. }
  348. }
  349. err = nil
  350. if !match {
  351. err = ErrSnapshotNotFound
  352. }
  353. // close decoder, disable reading
  354. if w.readClose != nil {
  355. w.readClose()
  356. w.readClose = nil
  357. }
  358. w.start = walpb.Snapshot{}
  359. w.metadata = metadata
  360. if w.tail() != nil {
  361. // create encoder (chain crc with the decoder), enable appending
  362. w.encoder, err = newFileEncoder(w.tail().File, w.decoder.lastCRC())
  363. if err != nil {
  364. return
  365. }
  366. }
  367. w.decoder = nil
  368. return metadata, state, ents, err
  369. }
  370. // cut closes current file written and creates a new one ready to append.
  371. // cut first creates a temp wal file and writes necessary headers into it.
  372. // Then cut atomically rename temp wal file to a wal file.
  373. func (w *WAL) cut() error {
  374. // close old wal file; truncate to avoid wasting space if an early cut
  375. off, serr := w.tail().Seek(0, io.SeekCurrent)
  376. if serr != nil {
  377. return serr
  378. }
  379. if err := w.tail().Truncate(off); err != nil {
  380. return err
  381. }
  382. if err := w.sync(); err != nil {
  383. return err
  384. }
  385. fpath := filepath.Join(w.dir, walName(w.seq()+1, w.enti+1))
  386. // create a temp wal file with name sequence + 1, or truncate the existing one
  387. newTail, err := w.fp.Open()
  388. if err != nil {
  389. return err
  390. }
  391. // update writer and save the previous crc
  392. w.locks = append(w.locks, newTail)
  393. prevCrc := w.encoder.crc.Sum32()
  394. w.encoder, err = newFileEncoder(w.tail().File, prevCrc)
  395. if err != nil {
  396. return err
  397. }
  398. if err = w.saveCrc(prevCrc); err != nil {
  399. return err
  400. }
  401. if err = w.encoder.encode(&walpb.Record{Type: metadataType, Data: w.metadata}); err != nil {
  402. return err
  403. }
  404. if err = w.saveState(&w.state); err != nil {
  405. return err
  406. }
  407. // atomically move temp wal file to wal file
  408. if err = w.sync(); err != nil {
  409. return err
  410. }
  411. off, err = w.tail().Seek(0, io.SeekCurrent)
  412. if err != nil {
  413. return err
  414. }
  415. if err = os.Rename(newTail.Name(), fpath); err != nil {
  416. return err
  417. }
  418. if err = fileutil.Fsync(w.dirFile); err != nil {
  419. return err
  420. }
  421. // reopen newTail with its new path so calls to Name() match the wal filename format
  422. newTail.Close()
  423. if newTail, err = fileutil.LockFile(fpath, os.O_WRONLY, fileutil.PrivateFileMode); err != nil {
  424. return err
  425. }
  426. if _, err = newTail.Seek(off, io.SeekStart); err != nil {
  427. return err
  428. }
  429. w.locks[len(w.locks)-1] = newTail
  430. prevCrc = w.encoder.crc.Sum32()
  431. w.encoder, err = newFileEncoder(w.tail().File, prevCrc)
  432. if err != nil {
  433. return err
  434. }
  435. if w.lg != nil {
  436. } else {
  437. plog.Infof("segmented wal file %v is created", fpath)
  438. }
  439. return nil
  440. }
  441. func (w *WAL) sync() error {
  442. if w.encoder != nil {
  443. if err := w.encoder.flush(); err != nil {
  444. return err
  445. }
  446. }
  447. start := time.Now()
  448. err := fileutil.Fdatasync(w.tail().File)
  449. took := time.Since(start)
  450. if took > warnSyncDuration {
  451. if w.lg != nil {
  452. w.lg.Warn(
  453. "slow fdatasync",
  454. zap.Duration("took", took),
  455. zap.Duration("expected-duration", warnSyncDuration),
  456. )
  457. } else {
  458. plog.Warningf("sync duration of %v, expected less than %v", took, warnSyncDuration)
  459. }
  460. }
  461. syncDurations.Observe(took.Seconds())
  462. return err
  463. }
  464. // ReleaseLockTo releases the locks, which has smaller index than the given index
  465. // except the largest one among them.
  466. // For example, if WAL is holding lock 1,2,3,4,5,6, ReleaseLockTo(4) will release
  467. // lock 1,2 but keep 3. ReleaseLockTo(5) will release 1,2,3 but keep 4.
  468. func (w *WAL) ReleaseLockTo(index uint64) error {
  469. w.mu.Lock()
  470. defer w.mu.Unlock()
  471. if len(w.locks) == 0 {
  472. return nil
  473. }
  474. var smaller int
  475. found := false
  476. for i, l := range w.locks {
  477. _, lockIndex, err := parseWalName(filepath.Base(l.Name()))
  478. if err != nil {
  479. return err
  480. }
  481. if lockIndex >= index {
  482. smaller = i - 1
  483. found = true
  484. break
  485. }
  486. }
  487. // if no lock index is greater than the release index, we can
  488. // release lock up to the last one(excluding).
  489. if !found {
  490. smaller = len(w.locks) - 1
  491. }
  492. if smaller <= 0 {
  493. return nil
  494. }
  495. for i := 0; i < smaller; i++ {
  496. if w.locks[i] == nil {
  497. continue
  498. }
  499. w.locks[i].Close()
  500. }
  501. w.locks = w.locks[smaller:]
  502. return nil
  503. }
  504. func (w *WAL) Close() error {
  505. w.mu.Lock()
  506. defer w.mu.Unlock()
  507. if w.fp != nil {
  508. w.fp.Close()
  509. w.fp = nil
  510. }
  511. if w.tail() != nil {
  512. if err := w.sync(); err != nil {
  513. return err
  514. }
  515. }
  516. for _, l := range w.locks {
  517. if l == nil {
  518. continue
  519. }
  520. if err := l.Close(); err != nil {
  521. if w.lg != nil {
  522. w.lg.Error("failed to close WAL", zap.Error(err))
  523. } else {
  524. plog.Errorf("failed to unlock during closing wal: %s", err)
  525. }
  526. }
  527. }
  528. return w.dirFile.Close()
  529. }
  530. func (w *WAL) saveEntry(e *raftpb.Entry) error {
  531. // TODO: add MustMarshalTo to reduce one allocation.
  532. b := pbutil.MustMarshal(e)
  533. rec := &walpb.Record{Type: entryType, Data: b}
  534. if err := w.encoder.encode(rec); err != nil {
  535. return err
  536. }
  537. w.enti = e.Index
  538. return nil
  539. }
  540. func (w *WAL) saveState(s *raftpb.HardState) error {
  541. if raft.IsEmptyHardState(*s) {
  542. return nil
  543. }
  544. w.state = *s
  545. b := pbutil.MustMarshal(s)
  546. rec := &walpb.Record{Type: stateType, Data: b}
  547. return w.encoder.encode(rec)
  548. }
  549. func (w *WAL) Save(st raftpb.HardState, ents []raftpb.Entry) error {
  550. w.mu.Lock()
  551. defer w.mu.Unlock()
  552. // short cut, do not call sync
  553. if raft.IsEmptyHardState(st) && len(ents) == 0 {
  554. return nil
  555. }
  556. mustSync := raft.MustSync(st, w.state, len(ents))
  557. // TODO(xiangli): no more reference operator
  558. for i := range ents {
  559. if err := w.saveEntry(&ents[i]); err != nil {
  560. return err
  561. }
  562. }
  563. if err := w.saveState(&st); err != nil {
  564. return err
  565. }
  566. curOff, err := w.tail().Seek(0, io.SeekCurrent)
  567. if err != nil {
  568. return err
  569. }
  570. if curOff < SegmentSizeBytes {
  571. if mustSync {
  572. return w.sync()
  573. }
  574. return nil
  575. }
  576. return w.cut()
  577. }
  578. func (w *WAL) SaveSnapshot(e walpb.Snapshot) error {
  579. b := pbutil.MustMarshal(&e)
  580. w.mu.Lock()
  581. defer w.mu.Unlock()
  582. rec := &walpb.Record{Type: snapshotType, Data: b}
  583. if err := w.encoder.encode(rec); err != nil {
  584. return err
  585. }
  586. // update enti only when snapshot is ahead of last index
  587. if w.enti < e.Index {
  588. w.enti = e.Index
  589. }
  590. return w.sync()
  591. }
  592. func (w *WAL) saveCrc(prevCrc uint32) error {
  593. return w.encoder.encode(&walpb.Record{Type: crcType, Crc: prevCrc})
  594. }
  595. func (w *WAL) tail() *fileutil.LockedFile {
  596. if len(w.locks) > 0 {
  597. return w.locks[len(w.locks)-1]
  598. }
  599. return nil
  600. }
  601. func (w *WAL) seq() uint64 {
  602. t := w.tail()
  603. if t == nil {
  604. return 0
  605. }
  606. seq, _, err := parseWalName(filepath.Base(t.Name()))
  607. if err != nil {
  608. if w.lg != nil {
  609. w.lg.Fatal("failed to parse WAL name", zap.String("name", t.Name()), zap.Error(err))
  610. } else {
  611. plog.Fatalf("bad wal name %s (%v)", t.Name(), err)
  612. }
  613. }
  614. return seq
  615. }
  616. func closeAll(rcs ...io.ReadCloser) error {
  617. for _, f := range rcs {
  618. if err := f.Close(); err != nil {
  619. return err
  620. }
  621. }
  622. return nil
  623. }