dir_aix.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //go:build aix
  2. // +build aix
  3. /*
  4. * SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc.
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. package badger
  8. import (
  9. "errors"
  10. "fmt"
  11. "os"
  12. "path/filepath"
  13. "sync"
  14. "golang.org/x/sys/unix"
  15. "github.com/dgraph-io/badger/v4/y"
  16. )
  17. // AIX flock locks files, not descriptors. So, multiple descriptors cannot
  18. // be used in the same file. The first to close removals the lock on the
  19. // file.
  20. type directoryLockGuard struct {
  21. // The absolute path to our pid file.
  22. path string
  23. // Was this a shared lock for a read-only database?
  24. readOnly bool
  25. }
  26. // AIX flocking is file x process, not fd x file x process like linux. We can
  27. // only hold one descriptor with a lock open at any given time.
  28. type aixFlock struct {
  29. file *os.File
  30. count int
  31. readOnly bool
  32. }
  33. // Keep a map of locks synchronized by a mutex.
  34. var aixFlockMap = map[string]*aixFlock{}
  35. var aixFlockMapLock sync.Mutex
  36. // acquireDirectoryLock gets a lock on the directory (using flock). If
  37. // this is not read-only, it will also write our pid to
  38. // dirPath/pidFileName for convenience.
  39. func acquireDirectoryLock(dirPath string, pidFileName string, readOnly bool) (
  40. *directoryLockGuard, error) {
  41. // Convert to absolute path so that Release still works even if we do an unbalanced
  42. // chdir in the meantime.
  43. absPidFilePath, err := filepath.Abs(filepath.Join(dirPath, pidFileName))
  44. if err != nil {
  45. return nil, y.Wrapf(err, "cannot get absolute path for pid lock file")
  46. }
  47. aixFlockMapLock.Lock()
  48. defer aixFlockMapLock.Unlock()
  49. lg := &directoryLockGuard{absPidFilePath, readOnly}
  50. if lock, fnd := aixFlockMap[absPidFilePath]; fnd {
  51. if !readOnly || lock.readOnly != readOnly {
  52. return nil, fmt.Errorf(
  53. "Cannot acquire directory lock on %q. Another process is using this Badger database.", dirPath)
  54. }
  55. lock.count++
  56. } else {
  57. // This is the first acquirer, set up a lock file and register it.
  58. f, err := os.OpenFile(absPidFilePath, os.O_RDWR|os.O_CREATE, 0666)
  59. if err != nil {
  60. return nil, y.Wrapf(err, "cannot create/open pid file %q", absPidFilePath)
  61. }
  62. opts := unix.F_WRLCK
  63. if readOnly {
  64. opts = unix.F_RDLCK
  65. }
  66. flckt := unix.Flock_t{int16(opts), 0, 0, 0, 0, 0, 0}
  67. err = unix.FcntlFlock(uintptr(f.Fd()), unix.F_SETLK, &flckt)
  68. if err != nil {
  69. f.Close()
  70. return nil, y.Wrapf(err,
  71. "Cannot acquire directory lock on %q. Another process is using this Badger database.", dirPath)
  72. }
  73. if !readOnly {
  74. f.Truncate(0)
  75. // Write our pid to the file.
  76. _, err = f.Write([]byte(fmt.Sprintf("%d\n", os.Getpid())))
  77. if err != nil {
  78. f.Close()
  79. return nil, y.Wrapf(err,
  80. "Cannot write pid file %q", absPidFilePath)
  81. }
  82. }
  83. aixFlockMap[absPidFilePath] = &aixFlock{f, 1, readOnly}
  84. }
  85. return lg, nil
  86. }
  87. // Release deletes the pid file and releases our lock on the directory.
  88. func (guard *directoryLockGuard) release() error {
  89. var err error
  90. aixFlockMapLock.Lock()
  91. defer aixFlockMapLock.Unlock()
  92. if lock, fnd := aixFlockMap[guard.path]; fnd {
  93. lock.count--
  94. if lock.count == 0 {
  95. if !lock.readOnly {
  96. // Try to clear the PID if we succeed.
  97. lock.file.Truncate(0)
  98. os.Remove(guard.path)
  99. }
  100. if closeErr := lock.file.Close(); err == nil {
  101. err = closeErr
  102. }
  103. delete(aixFlockMap, guard.path)
  104. guard.path = ""
  105. }
  106. } else {
  107. err = errors.New(fmt.Sprintf("unknown lock %v", guard.path))
  108. }
  109. return err
  110. }
  111. // openDir opens a directory for syncing.
  112. func openDir(path string) (*os.File, error) { return os.Open(path) }
  113. // When you create or delete a file, you have to ensure the directory entry for the file is synced
  114. // in order to guarantee the file is visible (if the system crashes). (See the man page for fsync,
  115. // or see https://github.com/coreos/etcd/issues/6368 for an example.)
  116. func syncDir(dir string) error {
  117. var err error
  118. // AIX does not support fsync on a directory.
  119. // Data durability on crash may be affected.
  120. return err
  121. }