network_manager.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. "sync"
  17. "yunion.io/x/log"
  18. "yunion.io/x/onecloud/pkg/scheduler/cache"
  19. synccache "yunion.io/x/onecloud/pkg/scheduler/cache/sync"
  20. networks_db "yunion.io/x/onecloud/pkg/scheduler/cache/sync/networks/db"
  21. )
  22. // ---------------------------------------------------
  23. type CandidateIdMap map[string]int
  24. type VpcNetwork struct {
  25. Data CandidateIdMap
  26. Network *synccache.SchedNetworkBuildResult
  27. }
  28. func NewVpcNetwork() *VpcNetwork {
  29. return &VpcNetwork{Data: make(CandidateIdMap)}
  30. }
  31. // ---------------------------------------------------
  32. type VpcNetworks struct {
  33. Data map[string]*VpcNetwork
  34. vpcNetworkLock sync.Mutex
  35. }
  36. func NewVpcNetworks() *VpcNetworks {
  37. return &VpcNetworks{
  38. Data: make(map[string]*VpcNetwork),
  39. vpcNetworkLock: sync.Mutex{},
  40. }
  41. }
  42. func (vns *VpcNetworks) Append(candidateId string,
  43. networks []*synccache.SchedNetworkBuildResult) {
  44. vns.vpcNetworkLock.Lock()
  45. defer vns.vpcNetworkLock.Unlock()
  46. for _, network := range networks {
  47. for _, idx := range []string{network.ID, network.Name} {
  48. vpcNetwork, ok := vns.Data[idx]
  49. if !ok {
  50. vpcNetwork = NewVpcNetwork()
  51. vns.Data[idx] = vpcNetwork
  52. }
  53. vpcNetwork.Network = network
  54. vpcNetwork.Data[candidateId] = 0
  55. }
  56. }
  57. }
  58. func (vns *VpcNetworks) Exists(networkId, candidateId string,
  59. ) *synccache.SchedNetworkBuildResult {
  60. vns.vpcNetworkLock.Lock()
  61. defer vns.vpcNetworkLock.Unlock()
  62. if vpcNetwork, ok := vns.Data[networkId]; ok {
  63. if _, ok := vpcNetwork.Data[candidateId]; ok {
  64. return vpcNetwork.Network
  65. }
  66. }
  67. return nil
  68. }
  69. func (vns *VpcNetworks) Get(networkId string) *VpcNetwork {
  70. vns.vpcNetworkLock.Lock()
  71. defer vns.vpcNetworkLock.Unlock()
  72. if vpcNetwork, ok := vns.Data[networkId]; ok {
  73. return vpcNetwork
  74. }
  75. return nil
  76. }
  77. // ---------------------------------------------------
  78. type NetworkManager struct {
  79. dataManager *DataManager
  80. vpcNetworks *VpcNetworks
  81. networksPool *ReservedPool
  82. }
  83. func NewNetworkManager(dataManager *DataManager, reservedPoolManager *ReservedPoolManager) *NetworkManager {
  84. networksPool, err := reservedPoolManager.GetPool("networks")
  85. if err != nil {
  86. log.Errorln(err)
  87. }
  88. return &NetworkManager{
  89. dataManager: dataManager,
  90. vpcNetworks: NewVpcNetworks(),
  91. networksPool: networksPool,
  92. }
  93. }
  94. func (m *NetworkManager) CleanVpc() {
  95. m.vpcNetworks = NewVpcNetworks()
  96. }
  97. func (m *NetworkManager) IsUnknown(id string) bool {
  98. return false
  99. }
  100. func (m *NetworkManager) GetReservecPorts(id string) int64 {
  101. // TODO: impl reserve network resource
  102. //if m.networksPool.GetReservedItem(id) != nil {
  103. //return m.networksPool.GetReservedItem(id).Get("Ports", int64(0)).(int64)
  104. //} else {
  105. return 0
  106. //}
  107. }
  108. func (m *NetworkManager) LoadUnknownNetworks(ids []string) {
  109. if len(ids) == 0 {
  110. return
  111. }
  112. builder, err := m.getHostNetworkDBDescBuilder()
  113. if err != nil {
  114. log.Errorf("Reload network error: %v", err)
  115. return
  116. }
  117. builder.Load(ids)
  118. }
  119. func (m *NetworkManager) GetVpcNetwork(networkId string) *VpcNetwork {
  120. return m.vpcNetworks.Get(networkId)
  121. }
  122. func (m *NetworkManager) ExistsVpcNetwork(networkId, candidateId string,
  123. ) *synccache.SchedNetworkBuildResult {
  124. return m.vpcNetworks.Exists(networkId, candidateId)
  125. }
  126. func (m *NetworkManager) getHostNetworkDBDescBuilder() (networks_db.NetworkDescBuilder, error) {
  127. cache, err := m.getNetworkCache()
  128. if err != nil {
  129. return nil, err
  130. }
  131. builder, err := cache.Get(synccache.HostNetworkDescBuilderCache)
  132. if err != nil {
  133. return nil, err
  134. }
  135. return builder.(networks_db.NetworkDescBuilder), nil
  136. }
  137. func (m *NetworkManager) getNetworkCache() (cache.Cache, error) {
  138. cache, err := m.dataManager.SyncCacheGroup.Get(synccache.NetworkSyncCache)
  139. if err != nil {
  140. return nil, err
  141. }
  142. cache.WaitForReady()
  143. return cache, nil
  144. }*/