file_pipeline.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2016 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. "fmt"
  17. "os"
  18. "path/filepath"
  19. "go.etcd.io/etcd/pkg/fileutil"
  20. "go.uber.org/zap"
  21. )
  22. // filePipeline pipelines allocating disk space
  23. type filePipeline struct {
  24. lg *zap.Logger
  25. // dir to put files
  26. dir string
  27. // size of files to make, in bytes
  28. size int64
  29. // count number of files generated
  30. count int
  31. filec chan *fileutil.LockedFile
  32. errc chan error
  33. donec chan struct{}
  34. }
  35. func newFilePipeline(lg *zap.Logger, dir string, fileSize int64) *filePipeline {
  36. fp := &filePipeline{
  37. lg: lg,
  38. dir: dir,
  39. size: fileSize,
  40. filec: make(chan *fileutil.LockedFile),
  41. errc: make(chan error, 1),
  42. donec: make(chan struct{}),
  43. }
  44. go fp.run()
  45. return fp
  46. }
  47. // Open returns a fresh file for writing. Rename the file before calling
  48. // Open again or there will be file collisions.
  49. func (fp *filePipeline) Open() (f *fileutil.LockedFile, err error) {
  50. select {
  51. case f = <-fp.filec:
  52. case err = <-fp.errc:
  53. }
  54. return f, err
  55. }
  56. func (fp *filePipeline) Close() error {
  57. close(fp.donec)
  58. return <-fp.errc
  59. }
  60. func (fp *filePipeline) alloc() (f *fileutil.LockedFile, err error) {
  61. // count % 2 so this file isn't the same as the one last published
  62. fpath := filepath.Join(fp.dir, fmt.Sprintf("%d.tmp", fp.count%2))
  63. if f, err = fileutil.LockFile(fpath, os.O_CREATE|os.O_WRONLY, fileutil.PrivateFileMode); err != nil {
  64. return nil, err
  65. }
  66. if err = fileutil.Preallocate(f.File, fp.size, true); err != nil {
  67. if fp.lg != nil {
  68. fp.lg.Warn("failed to preallocate space when creating a new WAL", zap.Int64("size", fp.size), zap.Error(err))
  69. } else {
  70. plog.Errorf("failed to allocate space when creating new wal file (%v)", err)
  71. }
  72. f.Close()
  73. return nil, err
  74. }
  75. fp.count++
  76. return f, nil
  77. }
  78. func (fp *filePipeline) run() {
  79. defer close(fp.errc)
  80. for {
  81. f, err := fp.alloc()
  82. if err != nil {
  83. fp.errc <- err
  84. return
  85. }
  86. select {
  87. case fp.filec <- f:
  88. case <-fp.donec:
  89. os.Remove(f.Name())
  90. f.Close()
  91. return
  92. }
  93. }
  94. }