wal_unix.go 1.3 KB

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