wal_unix.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. // +build !windows
  15. package wal
  16. import (
  17. "os"
  18. "github.com/coreos/etcd/pkg/fileutil"
  19. )
  20. func (w *WAL) renameWal(tmpdirpath string) (*WAL, error) {
  21. // On non-Windows platforms, hold the lock while renaming. Releasing
  22. // the lock and trying to reacquire it quickly can be flaky because
  23. // it's possible the process will fork to spawn a process while this is
  24. // happening. The fds are set up as close-on-exec by the Go runtime,
  25. // but there is a window between the fork and the exec where another
  26. // process holds the lock.
  27. if err := os.RemoveAll(w.dir); err != nil {
  28. return nil, err
  29. }
  30. if err := os.Rename(tmpdirpath, w.dir); err != nil {
  31. return nil, err
  32. }
  33. w.fp = newFilePipeline(w.dir, SegmentSizeBytes)
  34. df, err := fileutil.OpenDir(w.dir)
  35. w.dirFile = df
  36. return w, err
  37. }