manager.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright 2019 Yunion
  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 libguestfs
  15. import (
  16. "sync"
  17. "time"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/onecloud/pkg/hostman/diskutils/libguestfs/guestfish"
  21. "yunion.io/x/onecloud/pkg/util/procutils"
  22. )
  23. type ErrorFish string
  24. func (e ErrorFish) Error() string {
  25. return string(e)
  26. }
  27. const (
  28. ErrFishsMismatch = ErrorFish("fishs mismatch")
  29. ErrFishsWorking = ErrorFish("fishs working")
  30. ErrFishsDied = ErrorFish("fishs died")
  31. )
  32. var guestfsManager *GuestfsManager
  33. func Init(count int) error {
  34. if guestfsManager == nil {
  35. if err := procutils.NewRemoteCommandAsFarAsPossible("modprobe", "dm-mod").Run(); err != nil {
  36. return errors.Wrap(err, "modprobe dm-mod")
  37. }
  38. log.Infof("guestfish count %v", count)
  39. guestfsManager = NewGuestfsManager(count)
  40. time.AfterFunc(time.Minute*3, guestfsManager.fishsRecycle)
  41. }
  42. return nil
  43. }
  44. type GuestfsManager struct {
  45. fishMaximum int
  46. happyFishCount int
  47. workingFishCount int
  48. fishs map[*guestfish.Guestfish]bool
  49. fishChan chan *guestfish.Guestfish
  50. fishlock sync.Mutex
  51. lastTimeFishing time.Time
  52. }
  53. func NewGuestfsManager(count int) *GuestfsManager {
  54. if count < 1 {
  55. count = 1
  56. }
  57. return &GuestfsManager{
  58. fishMaximum: count,
  59. fishs: make(map[*guestfish.Guestfish]bool, count),
  60. fishChan: make(chan *guestfish.Guestfish, count),
  61. }
  62. }
  63. func (m *GuestfsManager) AcquireFish() (*guestfish.Guestfish, error) {
  64. fisher := func() (*guestfish.Guestfish, error) {
  65. m.fishlock.Lock()
  66. defer m.fishlock.Unlock()
  67. m.lastTimeFishing = time.Now()
  68. fish, err := m.acquireFish()
  69. if err == ErrFishsWorking {
  70. return nil, err
  71. } else if err != nil {
  72. return nil, err
  73. }
  74. if !fish.IsAlive() {
  75. go func() {
  76. if e := m.ReleaseFish(fish); e != nil {
  77. log.Errorf("release fish failed %s", e)
  78. }
  79. }()
  80. return nil, nil
  81. }
  82. return fish, nil
  83. }
  84. for i := 0; i < 3; i++ {
  85. fish, err := fisher()
  86. if err == ErrFishsWorking {
  87. return m.waitingFishFinish(), nil
  88. }
  89. if err != nil {
  90. return nil, err
  91. }
  92. if fish != nil {
  93. return fish, nil
  94. }
  95. }
  96. return nil, ErrFishsDied
  97. }
  98. func (m *GuestfsManager) acquireFish() (*guestfish.Guestfish, error) {
  99. if m.happyFishCount == 0 {
  100. if m.workingFishCount < m.fishMaximum {
  101. fish, err := guestfish.NewGuestfish()
  102. if err != nil {
  103. return nil, err
  104. }
  105. m.fishs[fish] = true
  106. m.workingFishCount++
  107. return fish, nil
  108. } else {
  109. return nil, ErrFishsWorking
  110. }
  111. } else {
  112. for fish, working := range m.fishs {
  113. if !working {
  114. m.fishs[fish] = true
  115. m.workingFishCount++
  116. m.happyFishCount--
  117. return fish, nil
  118. }
  119. }
  120. return nil, ErrFishsMismatch
  121. }
  122. }
  123. func (m *GuestfsManager) waitingFishFinish() *guestfish.Guestfish {
  124. select {
  125. case fish := <-m.fishChan:
  126. return fish
  127. }
  128. }
  129. func (m *GuestfsManager) ReleaseFish(fish *guestfish.Guestfish) error {
  130. err := m.washfish(fish)
  131. m.fishlock.Lock()
  132. defer m.fishlock.Unlock()
  133. if err != nil {
  134. errQ := fish.Quit()
  135. if errQ != nil {
  136. log.Errorf("fish quit failed: %s", errQ)
  137. }
  138. delete(m.fishs, fish)
  139. m.workingFishCount--
  140. }
  141. m.fishChan <- fish
  142. return err
  143. }
  144. func (m *GuestfsManager) washfish(fish *guestfish.Guestfish) error {
  145. return fish.RemoveDrive()
  146. }
  147. func (m *GuestfsManager) fishsRecycle() {
  148. defer time.AfterFunc(time.Minute*10, m.fishsRecycle)
  149. m.fishlock.Lock()
  150. defer m.fishlock.Unlock()
  151. if m.lastTimeFishing.IsZero() || time.Now().Sub(m.lastTimeFishing) < time.Minute*10 {
  152. return
  153. }
  154. log.Infof("start release fishs ...")
  155. Loop:
  156. for {
  157. select {
  158. case fish := <-m.fishChan:
  159. m.fishs[fish] = false
  160. m.happyFishCount++
  161. m.workingFishCount--
  162. default:
  163. break Loop
  164. }
  165. }
  166. for fish, working := range m.fishs {
  167. if !working {
  168. err := fish.Quit()
  169. if err != nil {
  170. log.Errorf("fish quit failed: %s", err)
  171. }
  172. m.happyFishCount--
  173. delete(m.fishs, fish)
  174. }
  175. }
  176. }