| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415 |
- // Copyright 2019 Yunion
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package data_manager
- import (
- "fmt"
- "time"
- "yunion.io/x/pkg/errors"
- "yunion.io/x/pkg/utils"
- "yunion.io/x/onecloud/pkg/scheduler/cache"
- candidatecache "yunion.io/x/onecloud/pkg/scheduler/cache/candidate"
- "yunion.io/x/onecloud/pkg/scheduler/core"
- )
- type CandidateGetArgs struct {
- // ResType is candidate host_type
- ResType string
- RegionID string
- ZoneID string
- ManagerID string
- HostTypes []string
- }
- type DataManager struct {
- SyncCacheGroup cache.CacheGroup
- CandidateGroup cache.CacheGroup
- }
- func NewDataManager(stopCh <-chan struct{}) *DataManager {
- m := new(DataManager)
- //m.SyncCacheGroup = synccache.NewSyncManager(stopCh)
- m.CandidateGroup = candidatecache.NewCandidateManager(stopCh)
- return m
- }
- func (m *DataManager) Run() {
- //go m.SyncCacheGroup.Run()
- go m.CandidateGroup.Run()
- }
- type CandidateManagerImplProvider interface {
- LoadCandidates() ([]interface{}, error)
- ReloadCandidates(ids []string) ([]interface{}, error)
- ReloadAllCandidates() ([]interface{}, error)
- GetCandidate(id string) (interface{}, error)
- }
- type HostCandidateManagerImplProvider struct {
- dataManager *DataManager
- }
- func getCache(dataManager *DataManager, name string) (cache.Cache, error) {
- candidate_cache, err := dataManager.CandidateGroup.Get(name)
- if err != nil {
- return nil, err
- }
- candidate_cache.WaitForReady()
- return candidate_cache, nil
- }
- func (p *HostCandidateManagerImplProvider) LoadCandidates() ([]interface{}, error) {
- candidate_cache, err := getCache(p.dataManager, candidatecache.HostCandidateCache)
- if err != nil {
- return nil, err
- }
- return candidate_cache.List(), nil
- }
- func (p *HostCandidateManagerImplProvider) ReloadCandidates(
- ids []string) ([]interface{}, error) {
- candidate_cache, err := getCache(p.dataManager, candidatecache.HostCandidateCache)
- if err != nil {
- return nil, err
- }
- return candidate_cache.Reload(ids)
- }
- func (p *HostCandidateManagerImplProvider) ReloadAllCandidates() ([]interface{}, error) {
- candidate_cache, err := getCache(p.dataManager, candidatecache.HostCandidateCache)
- if err != nil {
- return nil, err
- }
- return candidate_cache.ReloadAll()
- }
- func (p *HostCandidateManagerImplProvider) GetCandidate(id string) (interface{}, error) {
- candidate_cache, err := getCache(p.dataManager, candidatecache.HostCandidateCache)
- if err != nil {
- return nil, err
- }
- return candidate_cache.Get(id)
- }
- type BaremetalCandidateManagerImplProvider struct {
- dataManager *DataManager
- }
- func (p *BaremetalCandidateManagerImplProvider) LoadCandidates() ([]interface{}, error) {
- candidate_cache, err := getCache(p.dataManager, candidatecache.BaremetalCandidateCache)
- if err != nil {
- return nil, err
- }
- return candidate_cache.List(), nil
- }
- func (p *BaremetalCandidateManagerImplProvider) ReloadCandidates(
- ids []string) ([]interface{}, error) {
- candidate_cache, err := getCache(p.dataManager, candidatecache.BaremetalCandidateCache)
- if err != nil {
- return nil, err
- }
- return candidate_cache.Reload(ids)
- }
- func (p *BaremetalCandidateManagerImplProvider) ReloadAllCandidates() ([]interface{}, error) {
- candidate_cache, err := getCache(p.dataManager, candidatecache.BaremetalCandidateCache)
- if err != nil {
- return nil, err
- }
- return candidate_cache.ReloadAll()
- }
- func (p *BaremetalCandidateManagerImplProvider) GetCandidate(id string) (interface{}, error) {
- candidate_cache, err := getCache(p.dataManager, candidatecache.BaremetalCandidateCache)
- if err != nil {
- return nil, err
- }
- return candidate_cache.Get(id)
- }
- type CandidateManagerImpl struct {
- provider CandidateManagerImplProvider
- dataMap map[string][]interface{}
- stopCh <-chan struct{}
- lastLoadTime time.Time
- }
- func NewCandidateManagerImpl(provider CandidateManagerImplProvider, stopCh <-chan struct{},
- ) *CandidateManagerImpl {
- return &CandidateManagerImpl{
- provider: provider,
- dataMap: make(map[string][]interface{}),
- stopCh: stopCh,
- }
- }
- func (impl *CandidateManagerImpl) GetCandidates() ([]interface{}, error) {
- return impl.provider.LoadCandidates()
- }
- func (impl *CandidateManagerImpl) GetCandidate(id string) (interface{}, error) {
- return impl.provider.GetCandidate(id)
- }
- func (impl *CandidateManagerImpl) Reload(ids []string) ([]interface{}, error) {
- return impl.provider.ReloadCandidates(ids)
- }
- func (impl *CandidateManagerImpl) ReloadAll() ([]interface{}, error) {
- return impl.provider.ReloadAllCandidates()
- }
- func (impl *CandidateManagerImpl) Run() {
- }
- type CandidateManager struct {
- stopCh <-chan struct{}
- dataManager *DataManager
- impls map[string]*CandidateManagerImpl
- }
- func (cm *CandidateManager) GetCandidates(args CandidateGetArgs) ([]core.Candidater, error) {
- impl, err := cm.getImpl(args.ResType)
- if err != nil {
- return nil, errors.Wrapf(err, "GetCandidates implement by resource type %s", args.ResType)
- }
- candidates, err := impl.GetCandidates()
- if err != nil {
- return nil, errors.Wrapf(err, "GetCandidates from implement")
- }
- result := []core.Candidater{}
- matchZone := func(r core.Candidater, zoneId string) bool {
- if zoneId != "" {
- if r.Getter().Zone().GetId() == zoneId {
- return true
- }
- return false
- }
- return true
- }
- matchRegion := func(r core.Candidater, regionId string) bool {
- if regionId != "" {
- region := r.Getter().Region()
- if region == nil {
- return false
- }
- if region.GetId() == regionId {
- return true
- }
- return false
- }
- return true
- }
- matchCloudprovider := func(r core.Candidater, managerId string) bool {
- if managerId != "" {
- cloudProvier := r.Getter().Cloudprovider()
- // r who belongs to Provider Onecloud doesn't have cloudprovider
- if cloudProvier != nil && cloudProvier.GetId() == managerId {
- return true
- }
- return false
- }
- return true
- }
- matchHostTypes := func(c core.Candidater, hostTypes []string) bool {
- if len(hostTypes) == 0 {
- return true
- }
- return utils.IsInStringArray(c.Getter().HostType(), hostTypes)
- }
- for _, c := range candidates {
- r := c.(core.Candidater)
- if !matchRegion(r, args.RegionID) {
- continue
- }
- if !matchZone(r, args.ZoneID) {
- continue
- }
- if !matchCloudprovider(r, args.ManagerID) {
- continue
- }
- if !matchHostTypes(r, args.HostTypes) {
- continue
- }
- result = append(result, r)
- }
- return result, nil
- }
- func (cm *CandidateManager) GetCandidatesByIds(resType string, ids []string) ([]core.Candidater, error) {
- impl, err := cm.getImpl(resType)
- if err != nil {
- return nil, err
- }
- candidates := []core.Candidater{}
- for _, id := range ids {
- c, err2 := impl.GetCandidate(id)
- if err2 != nil {
- return nil, err2
- }
- candidates = append(candidates, c.(core.Candidater))
- }
- return candidates, nil
- }
- func (cm *CandidateManager) GetCandidate(id string, resType string) (interface{}, error) {
- impl, err := cm.getImpl(resType)
- if err != nil {
- return nil, err
- }
- c, err := impl.GetCandidate(id)
- if err != nil {
- return nil, err
- }
- return c.(core.Candidater), nil
- }
- func (cm *CandidateManager) getImpl(resType string) (*CandidateManagerImpl, error) {
- var (
- impl *CandidateManagerImpl
- ok bool
- )
- if impl, ok = cm.impls[resType]; !ok {
- return nil, fmt.Errorf("Resource Type \"%v\" not supported", resType)
- }
- return impl, nil
- }
- func (cm *CandidateManager) AddImpl(name string, impl *CandidateManagerImpl) {
- cm.impls[name] = impl
- }
- const (
- CANDIDATE_MANAGER_IMPL_HOST = "host"
- CANDIDATE_MANAGER_IMPL_BAREMETAL = "baremetal"
- )
- func NewCandidateManager(dataManager *DataManager, stopCh <-chan struct{}) *CandidateManager {
- candidateManager := &CandidateManager{
- stopCh: stopCh,
- impls: make(map[string]*CandidateManagerImpl),
- dataManager: dataManager,
- //dirtyPool: ttlpool.NewCountPool(),
- }
- candidateManager.AddImpl(CANDIDATE_MANAGER_IMPL_HOST, NewCandidateManagerImpl(
- &HostCandidateManagerImplProvider{dataManager: dataManager}, stopCh))
- candidateManager.AddImpl(CANDIDATE_MANAGER_IMPL_BAREMETAL, NewCandidateManagerImpl(
- &BaremetalCandidateManagerImplProvider{dataManager: dataManager}, stopCh))
- return candidateManager
- }
- func (cm *CandidateManager) Run() {
- for _, impl := range cm.impls {
- impl.Run()
- }
- }
- func (cm *CandidateManager) ReloadHosts(ids []string) ([]interface{}, error) {
- return cm.Reload(CANDIDATE_MANAGER_IMPL_HOST, ids)
- }
- func (cm *CandidateManager) Reload(resType string, candidateIds []string) ([]interface{}, error) {
- if len(candidateIds) == 0 {
- return []interface{}{}, nil
- }
- impl, err := cm.getImpl(resType)
- if err != nil {
- return nil, err
- }
- return impl.Reload(candidateIds)
- }
- func (cm *CandidateManager) ReloadAll(resType string) ([]interface{}, error) {
- impl, err := cm.getImpl(resType)
- if err != nil {
- return nil, err
- }
- return impl.ReloadAll()
- }
- //type IDirtyPoolItem interface {
- //ttlpool.Item
- //GetCount() uint64
- //}
- //func (cm *CandidateManager) SetCandidateDirty(item IDirtyPoolItem) {
- //cm.dirtyPool.Add(item, item.GetCount())
- //}
- //func (cm *CandidateManager) CleanDirtyCandidatesOnce(keys []string, sessionId string) {
- //for _, key := range keys {
- //cm.dirtyPool.DeleteByKey(key)
- //}
- //}
- /*func ToHostCandidate(c interface{}) (*candidatecache.HostDesc, error) {
- h, ok := c.(*candidatecache.HostDesc)
- if !ok {
- return nil, fmt.Errorf("can't convert %#v to *candidatecache.HostDesc", c)
- }
- return h, nil
- }
- func ToHostCandidates(cs []core.Candidater) ([]*candidatecache.HostDesc, error) {
- hs := make([]*candidatecache.HostDesc, 0)
- for _, c := range cs {
- h, err := ToHostCandidate(c)
- if err != nil {
- return nil, err
- }
- hs = append(hs, h)
- }
- return hs, nil
- }*/
|