PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/dir_plan9.go

https://gitlab.com/suo/badger
Go | 150 lines | 81 code | 17 blank | 52 comment | 24 complexity | d7d1be9f414a59f2f39841cdad1c4817 MD5 | raw file
  1. /*
  2. * Copyright 2020 Dgraph Labs, Inc. and Contributors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package badger
  17. import (
  18. "fmt"
  19. "os"
  20. "path/filepath"
  21. "strings"
  22. "github.com/dgraph-io/badger/v3/y"
  23. )
  24. // directoryLockGuard holds a lock on a directory and a pid file inside. The pid file isn't part
  25. // of the locking mechanism, it's just advisory.
  26. type directoryLockGuard struct {
  27. // File handle on the directory, which we've locked.
  28. f *os.File
  29. // The absolute path to our pid file.
  30. path string
  31. }
  32. // acquireDirectoryLock gets a lock on the directory.
  33. // It will also write our pid to dirPath/pidFileName for convenience.
  34. // readOnly is not supported on Plan 9.
  35. func acquireDirectoryLock(dirPath string, pidFileName string, readOnly bool) (
  36. *directoryLockGuard, error) {
  37. if readOnly {
  38. return nil, ErrPlan9NotSupported
  39. }
  40. // Convert to absolute path so that Release still works even if we do an unbalanced
  41. // chdir in the meantime.
  42. absPidFilePath, err := filepath.Abs(filepath.Join(dirPath, pidFileName))
  43. if err != nil {
  44. return nil, y.Wrap(err, "cannot get absolute path for pid lock file")
  45. }
  46. // If the file was unpacked or created by some other program, it might not
  47. // have the ModeExclusive bit set. Set it before we call OpenFile, so that we
  48. // can be confident that a successful OpenFile implies exclusive use.
  49. //
  50. // OpenFile fails if the file ModeExclusive bit set *and* the file is already open.
  51. // So, if the file is closed when the DB crashed, we're fine. When the process
  52. // that was managing the DB crashes, the OS will close the file for us.
  53. //
  54. // This bit of code is copied from Go's lockedfile internal package:
  55. // https://github.com/golang/go/blob/go1.15rc1/src/cmd/go/internal/lockedfile/lockedfile_plan9.go#L58
  56. if fi, err := os.Stat(absPidFilePath); err == nil {
  57. if fi.Mode()&os.ModeExclusive == 0 {
  58. if err := os.Chmod(absPidFilePath, fi.Mode()|os.ModeExclusive); err != nil {
  59. return nil, y.Wrapf(err, "could not set exclusive mode bit")
  60. }
  61. }
  62. } else if !os.IsNotExist(err) {
  63. return nil, err
  64. }
  65. f, err := os.OpenFile(absPidFilePath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666|os.ModeExclusive)
  66. if err != nil {
  67. if isLocked(err) {
  68. return nil, y.Wrapf(err,
  69. "Cannot open pid lock file %q. Another process is using this Badger database",
  70. absPidFilePath)
  71. }
  72. return nil, y.Wrapf(err, "Cannot open pid lock file %q", absPidFilePath)
  73. }
  74. if _, err = fmt.Fprintf(f, "%d\n", os.Getpid()); err != nil {
  75. f.Close()
  76. return nil, y.Wrapf(err, "could not write pid")
  77. }
  78. return &directoryLockGuard{f, absPidFilePath}, nil
  79. }
  80. // Release deletes the pid file and releases our lock on the directory.
  81. func (guard *directoryLockGuard) release() error {
  82. // It's important that we remove the pid file first.
  83. err := os.Remove(guard.path)
  84. if closeErr := guard.f.Close(); err == nil {
  85. err = closeErr
  86. }
  87. guard.path = ""
  88. guard.f = nil
  89. return err
  90. }
  91. // openDir opens a directory for syncing.
  92. func openDir(path string) (*os.File, error) { return os.Open(path) }
  93. // When you create or delete a file, you have to ensure the directory entry for the file is synced
  94. // in order to guarantee the file is visible (if the system crashes). (See the man page for fsync,
  95. // or see https://github.com/coreos/etcd/issues/6368 for an example.)
  96. func syncDir(dir string) error {
  97. f, err := openDir(dir)
  98. if err != nil {
  99. return y.Wrapf(err, "While opening directory: %s.", dir)
  100. }
  101. err = f.Sync()
  102. closeErr := f.Close()
  103. if err != nil {
  104. return y.Wrapf(err, "While syncing directory: %s.", dir)
  105. }
  106. return y.Wrapf(closeErr, "While closing directory: %s.", dir)
  107. }
  108. // Opening an exclusive-use file returns an error.
  109. // The expected error strings are:
  110. //
  111. // - "open/create -- file is locked" (cwfs, kfs)
  112. // - "exclusive lock" (fossil)
  113. // - "exclusive use file already open" (ramfs)
  114. //
  115. // See https://github.com/golang/go/blob/go1.15rc1/src/cmd/go/internal/lockedfile/lockedfile_plan9.go#L16
  116. var lockedErrStrings = [...]string{
  117. "file is locked",
  118. "exclusive lock",
  119. "exclusive use file already open",
  120. }
  121. // Even though plan9 doesn't support the Lock/RLock/Unlock functions to
  122. // manipulate already-open files, IsLocked is still meaningful: os.OpenFile
  123. // itself may return errors that indicate that a file with the ModeExclusive bit
  124. // set is already open.
  125. func isLocked(err error) bool {
  126. s := err.Error()
  127. for _, frag := range lockedErrStrings {
  128. if strings.Contains(s, frag) {
  129. return true
  130. }
  131. }
  132. return false
  133. }