expire_queue.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 manager
  15. import (
  16. "sync"
  17. "time"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/util/sets"
  20. u "yunion.io/x/pkg/utils"
  21. "yunion.io/x/onecloud/pkg/scheduler/api"
  22. o "yunion.io/x/onecloud/pkg/scheduler/options"
  23. )
  24. type ExpireManager struct {
  25. expireChannel chan *api.ExpireArgs
  26. stopCh <-chan struct{}
  27. mergeLock *sync.Mutex
  28. }
  29. func NewExpireManager(stopCh <-chan struct{}) *ExpireManager {
  30. return &ExpireManager{
  31. expireChannel: make(chan *api.ExpireArgs, o.Options.ExpireQueueMaxLength),
  32. stopCh: stopCh,
  33. mergeLock: new(sync.Mutex),
  34. }
  35. }
  36. func (e *ExpireManager) Add(expireArgs *api.ExpireArgs) {
  37. e.expireChannel <- expireArgs
  38. }
  39. func (e *ExpireManager) Trigger() {
  40. e.batchMergeExpire()
  41. }
  42. type expireHost struct {
  43. Id string
  44. SessionId string
  45. }
  46. func newExpireHost(id string, sid string) *expireHost {
  47. return &expireHost{
  48. Id: id,
  49. SessionId: sid,
  50. }
  51. }
  52. func (e *ExpireManager) Run() {
  53. t := time.Tick(u.ToDuration(o.Options.ExpireQueueConsumptionPeriod))
  54. // Watching the expires.
  55. for {
  56. select {
  57. case <-t:
  58. e.batchMergeExpire()
  59. case <-e.stopCh:
  60. // update all the expire before return
  61. e.batchMergeExpire()
  62. close(e.expireChannel)
  63. e.expireChannel = nil
  64. log.Errorln("expire manager EXIT!")
  65. return
  66. default:
  67. // if expire number is bigger then 80 then update
  68. if len(e.expireChannel) >= o.Options.ExpireQueueDealLength {
  69. e.batchMergeExpire()
  70. } else {
  71. time.Sleep(1 * time.Second)
  72. }
  73. }
  74. }
  75. }
  76. func (e *ExpireManager) batchMergeExpire() {
  77. e.mergeLock.Lock()
  78. defer e.mergeLock.Unlock()
  79. expireRequestNumber := len(e.expireChannel)
  80. // If the expireRequestNumber then return right now.
  81. if expireRequestNumber <= 0 {
  82. return
  83. }
  84. dirtyHostSets := sets.NewString()
  85. dirtyBaremetalSets := sets.NewString()
  86. dirtyHosts := make([]*expireHost, 0)
  87. dirtyBaremetals := make([]*expireHost, 0)
  88. // Merge all same host.
  89. for i := 0; i < expireRequestNumber; i++ {
  90. expireArgs := <-e.expireChannel
  91. log.V(4).Infof("Get expireArgs from channel: %#v", expireArgs)
  92. dirtyHostSets.Insert(expireArgs.DirtyHosts...)
  93. for _, host := range expireArgs.DirtyHosts {
  94. dirtyHosts = append(dirtyHosts, newExpireHost(host, expireArgs.SessionId))
  95. }
  96. dirtyBaremetalSets.Insert(expireArgs.DirtyBaremetals...)
  97. for _, baremetal := range expireArgs.DirtyBaremetals {
  98. dirtyBaremetals = append(dirtyBaremetals, newExpireHost(baremetal, expireArgs.SessionId))
  99. }
  100. }
  101. log.V(4).Infof("batchMergeExpire dirtyHosts: %v, dirtyBaremetals: %v", dirtyHosts, dirtyBaremetals)
  102. wg := &sync.WaitGroup{}
  103. wg.Add(2)
  104. go func() {
  105. defer wg.Done()
  106. //dirtyHosts = notInSession(dirtyHosts, "host")
  107. if len(dirtyHosts) > 0 {
  108. log.V(10).Debugf("CleanDirty Hosts: %v\n", dirtyHosts)
  109. if _, err := schedManager.CandidateManager.Reload("host", dirtyHostSets.List()); err != nil {
  110. log.Errorf("Clean dirty hosts %v: %v", dirtyHosts, err)
  111. }
  112. schedManager.HistoryManager.CancelCandidatesPendingUsage(dirtyHosts)
  113. }
  114. }()
  115. go func() {
  116. defer wg.Done()
  117. //dirtyBaremetals = notInSession(dirtyBaremetals, "baremetal")
  118. if len(dirtyBaremetals) > 0 {
  119. log.V(10).Debugf("CleanDirty Baremetals: %v\n", dirtyBaremetals)
  120. if _, err := schedManager.CandidateManager.Reload("baremetal", dirtyBaremetalSets.List()); err != nil {
  121. log.Errorf("Clean dirty baremetals %v: %v", dirtyBaremetals, err)
  122. }
  123. schedManager.HistoryManager.CancelCandidatesPendingUsage(dirtyBaremetals)
  124. }
  125. }()
  126. if ok := e.waitTimeOut(wg, u.ToDuration(o.Options.ExpireQueueConsumptionTimeout)); !ok {
  127. log.Errorln("time out reload data.")
  128. }
  129. }
  130. func (e *ExpireManager) waitTimeOut(wg *sync.WaitGroup, timeout time.Duration) bool {
  131. ch := make(chan struct{})
  132. go func() {
  133. wg.Wait()
  134. close(ch)
  135. }()
  136. select {
  137. case <-ch:
  138. return true
  139. case <-time.After(timeout):
  140. return false
  141. }
  142. }