candidate_manager.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 data_manager
  15. import (
  16. "fmt"
  17. "time"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/pkg/utils"
  20. "yunion.io/x/onecloud/pkg/scheduler/cache"
  21. candidatecache "yunion.io/x/onecloud/pkg/scheduler/cache/candidate"
  22. "yunion.io/x/onecloud/pkg/scheduler/core"
  23. )
  24. type CandidateGetArgs struct {
  25. // ResType is candidate host_type
  26. ResType string
  27. RegionID string
  28. ZoneID string
  29. ManagerID string
  30. HostTypes []string
  31. }
  32. type DataManager struct {
  33. SyncCacheGroup cache.CacheGroup
  34. CandidateGroup cache.CacheGroup
  35. }
  36. func NewDataManager(stopCh <-chan struct{}) *DataManager {
  37. m := new(DataManager)
  38. //m.SyncCacheGroup = synccache.NewSyncManager(stopCh)
  39. m.CandidateGroup = candidatecache.NewCandidateManager(stopCh)
  40. return m
  41. }
  42. func (m *DataManager) Run() {
  43. //go m.SyncCacheGroup.Run()
  44. go m.CandidateGroup.Run()
  45. }
  46. type CandidateManagerImplProvider interface {
  47. LoadCandidates() ([]interface{}, error)
  48. ReloadCandidates(ids []string) ([]interface{}, error)
  49. ReloadAllCandidates() ([]interface{}, error)
  50. GetCandidate(id string) (interface{}, error)
  51. }
  52. type HostCandidateManagerImplProvider struct {
  53. dataManager *DataManager
  54. }
  55. func getCache(dataManager *DataManager, name string) (cache.Cache, error) {
  56. candidate_cache, err := dataManager.CandidateGroup.Get(name)
  57. if err != nil {
  58. return nil, err
  59. }
  60. candidate_cache.WaitForReady()
  61. return candidate_cache, nil
  62. }
  63. func (p *HostCandidateManagerImplProvider) LoadCandidates() ([]interface{}, error) {
  64. candidate_cache, err := getCache(p.dataManager, candidatecache.HostCandidateCache)
  65. if err != nil {
  66. return nil, err
  67. }
  68. return candidate_cache.List(), nil
  69. }
  70. func (p *HostCandidateManagerImplProvider) ReloadCandidates(
  71. ids []string) ([]interface{}, error) {
  72. candidate_cache, err := getCache(p.dataManager, candidatecache.HostCandidateCache)
  73. if err != nil {
  74. return nil, err
  75. }
  76. return candidate_cache.Reload(ids)
  77. }
  78. func (p *HostCandidateManagerImplProvider) ReloadAllCandidates() ([]interface{}, error) {
  79. candidate_cache, err := getCache(p.dataManager, candidatecache.HostCandidateCache)
  80. if err != nil {
  81. return nil, err
  82. }
  83. return candidate_cache.ReloadAll()
  84. }
  85. func (p *HostCandidateManagerImplProvider) GetCandidate(id string) (interface{}, error) {
  86. candidate_cache, err := getCache(p.dataManager, candidatecache.HostCandidateCache)
  87. if err != nil {
  88. return nil, err
  89. }
  90. return candidate_cache.Get(id)
  91. }
  92. type BaremetalCandidateManagerImplProvider struct {
  93. dataManager *DataManager
  94. }
  95. func (p *BaremetalCandidateManagerImplProvider) LoadCandidates() ([]interface{}, error) {
  96. candidate_cache, err := getCache(p.dataManager, candidatecache.BaremetalCandidateCache)
  97. if err != nil {
  98. return nil, err
  99. }
  100. return candidate_cache.List(), nil
  101. }
  102. func (p *BaremetalCandidateManagerImplProvider) ReloadCandidates(
  103. ids []string) ([]interface{}, error) {
  104. candidate_cache, err := getCache(p.dataManager, candidatecache.BaremetalCandidateCache)
  105. if err != nil {
  106. return nil, err
  107. }
  108. return candidate_cache.Reload(ids)
  109. }
  110. func (p *BaremetalCandidateManagerImplProvider) ReloadAllCandidates() ([]interface{}, error) {
  111. candidate_cache, err := getCache(p.dataManager, candidatecache.BaremetalCandidateCache)
  112. if err != nil {
  113. return nil, err
  114. }
  115. return candidate_cache.ReloadAll()
  116. }
  117. func (p *BaremetalCandidateManagerImplProvider) GetCandidate(id string) (interface{}, error) {
  118. candidate_cache, err := getCache(p.dataManager, candidatecache.BaremetalCandidateCache)
  119. if err != nil {
  120. return nil, err
  121. }
  122. return candidate_cache.Get(id)
  123. }
  124. type CandidateManagerImpl struct {
  125. provider CandidateManagerImplProvider
  126. dataMap map[string][]interface{}
  127. stopCh <-chan struct{}
  128. lastLoadTime time.Time
  129. }
  130. func NewCandidateManagerImpl(provider CandidateManagerImplProvider, stopCh <-chan struct{},
  131. ) *CandidateManagerImpl {
  132. return &CandidateManagerImpl{
  133. provider: provider,
  134. dataMap: make(map[string][]interface{}),
  135. stopCh: stopCh,
  136. }
  137. }
  138. func (impl *CandidateManagerImpl) GetCandidates() ([]interface{}, error) {
  139. return impl.provider.LoadCandidates()
  140. }
  141. func (impl *CandidateManagerImpl) GetCandidate(id string) (interface{}, error) {
  142. return impl.provider.GetCandidate(id)
  143. }
  144. func (impl *CandidateManagerImpl) Reload(ids []string) ([]interface{}, error) {
  145. return impl.provider.ReloadCandidates(ids)
  146. }
  147. func (impl *CandidateManagerImpl) ReloadAll() ([]interface{}, error) {
  148. return impl.provider.ReloadAllCandidates()
  149. }
  150. func (impl *CandidateManagerImpl) Run() {
  151. }
  152. type CandidateManager struct {
  153. stopCh <-chan struct{}
  154. dataManager *DataManager
  155. impls map[string]*CandidateManagerImpl
  156. }
  157. func (cm *CandidateManager) GetCandidates(args CandidateGetArgs) ([]core.Candidater, error) {
  158. impl, err := cm.getImpl(args.ResType)
  159. if err != nil {
  160. return nil, errors.Wrapf(err, "GetCandidates implement by resource type %s", args.ResType)
  161. }
  162. candidates, err := impl.GetCandidates()
  163. if err != nil {
  164. return nil, errors.Wrapf(err, "GetCandidates from implement")
  165. }
  166. result := []core.Candidater{}
  167. matchZone := func(r core.Candidater, zoneId string) bool {
  168. if zoneId != "" {
  169. if r.Getter().Zone().GetId() == zoneId {
  170. return true
  171. }
  172. return false
  173. }
  174. return true
  175. }
  176. matchRegion := func(r core.Candidater, regionId string) bool {
  177. if regionId != "" {
  178. region := r.Getter().Region()
  179. if region == nil {
  180. return false
  181. }
  182. if region.GetId() == regionId {
  183. return true
  184. }
  185. return false
  186. }
  187. return true
  188. }
  189. matchCloudprovider := func(r core.Candidater, managerId string) bool {
  190. if managerId != "" {
  191. cloudProvier := r.Getter().Cloudprovider()
  192. // r who belongs to Provider Onecloud doesn't have cloudprovider
  193. if cloudProvier != nil && cloudProvier.GetId() == managerId {
  194. return true
  195. }
  196. return false
  197. }
  198. return true
  199. }
  200. matchHostTypes := func(c core.Candidater, hostTypes []string) bool {
  201. if len(hostTypes) == 0 {
  202. return true
  203. }
  204. return utils.IsInStringArray(c.Getter().HostType(), hostTypes)
  205. }
  206. for _, c := range candidates {
  207. r := c.(core.Candidater)
  208. if !matchRegion(r, args.RegionID) {
  209. continue
  210. }
  211. if !matchZone(r, args.ZoneID) {
  212. continue
  213. }
  214. if !matchCloudprovider(r, args.ManagerID) {
  215. continue
  216. }
  217. if !matchHostTypes(r, args.HostTypes) {
  218. continue
  219. }
  220. result = append(result, r)
  221. }
  222. return result, nil
  223. }
  224. func (cm *CandidateManager) GetCandidatesByIds(resType string, ids []string) ([]core.Candidater, error) {
  225. impl, err := cm.getImpl(resType)
  226. if err != nil {
  227. return nil, err
  228. }
  229. candidates := []core.Candidater{}
  230. for _, id := range ids {
  231. c, err2 := impl.GetCandidate(id)
  232. if err2 != nil {
  233. return nil, err2
  234. }
  235. candidates = append(candidates, c.(core.Candidater))
  236. }
  237. return candidates, nil
  238. }
  239. func (cm *CandidateManager) GetCandidate(id string, resType string) (interface{}, error) {
  240. impl, err := cm.getImpl(resType)
  241. if err != nil {
  242. return nil, err
  243. }
  244. c, err := impl.GetCandidate(id)
  245. if err != nil {
  246. return nil, err
  247. }
  248. return c.(core.Candidater), nil
  249. }
  250. func (cm *CandidateManager) getImpl(resType string) (*CandidateManagerImpl, error) {
  251. var (
  252. impl *CandidateManagerImpl
  253. ok bool
  254. )
  255. if impl, ok = cm.impls[resType]; !ok {
  256. return nil, fmt.Errorf("Resource Type \"%v\" not supported", resType)
  257. }
  258. return impl, nil
  259. }
  260. func (cm *CandidateManager) AddImpl(name string, impl *CandidateManagerImpl) {
  261. cm.impls[name] = impl
  262. }
  263. const (
  264. CANDIDATE_MANAGER_IMPL_HOST = "host"
  265. CANDIDATE_MANAGER_IMPL_BAREMETAL = "baremetal"
  266. )
  267. func NewCandidateManager(dataManager *DataManager, stopCh <-chan struct{}) *CandidateManager {
  268. candidateManager := &CandidateManager{
  269. stopCh: stopCh,
  270. impls: make(map[string]*CandidateManagerImpl),
  271. dataManager: dataManager,
  272. //dirtyPool: ttlpool.NewCountPool(),
  273. }
  274. candidateManager.AddImpl(CANDIDATE_MANAGER_IMPL_HOST, NewCandidateManagerImpl(
  275. &HostCandidateManagerImplProvider{dataManager: dataManager}, stopCh))
  276. candidateManager.AddImpl(CANDIDATE_MANAGER_IMPL_BAREMETAL, NewCandidateManagerImpl(
  277. &BaremetalCandidateManagerImplProvider{dataManager: dataManager}, stopCh))
  278. return candidateManager
  279. }
  280. func (cm *CandidateManager) Run() {
  281. for _, impl := range cm.impls {
  282. impl.Run()
  283. }
  284. }
  285. func (cm *CandidateManager) ReloadHosts(ids []string) ([]interface{}, error) {
  286. return cm.Reload(CANDIDATE_MANAGER_IMPL_HOST, ids)
  287. }
  288. func (cm *CandidateManager) Reload(resType string, candidateIds []string) ([]interface{}, error) {
  289. if len(candidateIds) == 0 {
  290. return []interface{}{}, nil
  291. }
  292. impl, err := cm.getImpl(resType)
  293. if err != nil {
  294. return nil, err
  295. }
  296. return impl.Reload(candidateIds)
  297. }
  298. func (cm *CandidateManager) ReloadAll(resType string) ([]interface{}, error) {
  299. impl, err := cm.getImpl(resType)
  300. if err != nil {
  301. return nil, err
  302. }
  303. return impl.ReloadAll()
  304. }
  305. //type IDirtyPoolItem interface {
  306. //ttlpool.Item
  307. //GetCount() uint64
  308. //}
  309. //func (cm *CandidateManager) SetCandidateDirty(item IDirtyPoolItem) {
  310. //cm.dirtyPool.Add(item, item.GetCount())
  311. //}
  312. //func (cm *CandidateManager) CleanDirtyCandidatesOnce(keys []string, sessionId string) {
  313. //for _, key := range keys {
  314. //cm.dirtyPool.DeleteByKey(key)
  315. //}
  316. //}
  317. /*func ToHostCandidate(c interface{}) (*candidatecache.HostDesc, error) {
  318. h, ok := c.(*candidatecache.HostDesc)
  319. if !ok {
  320. return nil, fmt.Errorf("can't convert %#v to *candidatecache.HostDesc", c)
  321. }
  322. return h, nil
  323. }
  324. func ToHostCandidates(cs []core.Candidater) ([]*candidatecache.HostDesc, error) {
  325. hs := make([]*candidatecache.HostDesc, 0)
  326. for _, c := range cs {
  327. h, err := ToHostCandidate(c)
  328. if err != nil {
  329. return nil, err
  330. }
  331. hs = append(hs, h)
  332. }
  333. return hs, nil
  334. }*/