mem.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 balancer
  15. import (
  16. "yunion.io/x/jsonutils"
  17. "yunion.io/x/log"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/pkg/util/sets"
  20. "yunion.io/x/onecloud/pkg/apis/monitor"
  21. "yunion.io/x/onecloud/pkg/monitor/tsdb"
  22. )
  23. type memAvailable struct{}
  24. func newMemAvailable() IMetricDriver {
  25. return &memAvailable{}
  26. }
  27. func (m *memAvailable) GetType() monitor.MigrationAlertMetricType {
  28. return monitor.MigrationAlertMetricTypeMemAvailable
  29. }
  30. func (ma *memAvailable) GetTsdbQuery() *TsdbQuery {
  31. return &TsdbQuery{
  32. Database: monitor.METRIC_DATABASE_TELE,
  33. Measurement: "mem",
  34. Fields: []string{"total", "free", "available"},
  35. }
  36. }
  37. func (ma *memAvailable) GetCandidate(obj jsonutils.JSONObject, host IHost, _ *tsdb.DataSource) (ICandidate, error) {
  38. return newMemCandidate(obj, host)
  39. }
  40. func (ma *memAvailable) SetHostCurrent(host IHost, vals map[string]float64) error {
  41. return setHostCurrent(host, vals, "free")
  42. }
  43. func (ma *memAvailable) GetTarget(host jsonutils.JSONObject) (ITarget, error) {
  44. return newTargetMemHost(host)
  45. }
  46. func (ma *memAvailable) GetCondition(s *monitor.AlertSetting) (ICondition, error) {
  47. t, err := GetAlertSettingThreshold(s)
  48. if err != nil {
  49. return nil, errors.Wrap(err, "GetAlertSettingThreshold")
  50. }
  51. return newMemoryCond(t), nil
  52. }
  53. // memCondition implements ICondition
  54. type memCondition struct {
  55. value float64
  56. }
  57. func newMemoryCond(value float64) ICondition {
  58. return &memCondition{
  59. value: value,
  60. }
  61. }
  62. func (m *memCondition) GetThreshold() float64 {
  63. return m.value
  64. }
  65. func (m *memCondition) GetSourceThresholdDelta(threshold float64, host IHost) float64 {
  66. // mem.available
  67. return threshold - host.GetCurrent()
  68. }
  69. func (m *memCondition) IsFitTarget(settings *monitor.MigrationAlertSettings, t ITarget, c ICandidate) error {
  70. tScore := t.GetCurrent() - c.GetScore()
  71. gtThreshold := tScore > m.GetThreshold()
  72. if gtThreshold {
  73. return nil
  74. }
  75. // 1G = 1 * 1024 * 1024 * 1024 (bytes)
  76. MAX_THRESHOLD := float64(1 * 1024 * 1024 * 1024)
  77. src := settings.Source
  78. srcHostIds := []string{}
  79. if src != nil {
  80. srcHostIds = src.HostIds
  81. }
  82. // only when srcHostIds isn't empty
  83. if len(srcHostIds) != 0 {
  84. if !gtThreshold && !sets.NewString(srcHostIds...).Has(t.GetId()) && tScore > MAX_THRESHOLD {
  85. // if target host is not in source specified hosts and calculated score is greater than MAX_THRESHOLD
  86. log.Infof("let host:%s:current(%f) + guest:%s:score(%f) > MAX_THRESHOLD(%f) to fit target, because it's not in source specified hosts", t.GetName(), t.GetCurrent(), c.GetName(), c.GetScore(), MAX_THRESHOLD)
  87. return nil
  88. }
  89. }
  90. return errors.Errorf("host:%s:current(%f) - guest:%s:score(%f) <= threshold(%f)", t.GetName(), t.GetCurrent(), c.GetName(), c.GetScore(), m.GetThreshold())
  91. }
  92. // memCandidate implements ICandidate
  93. type memCandidate struct {
  94. *guestResource
  95. score float64
  96. }
  97. func newMemCandidate(gst jsonutils.JSONObject, host IHost) (ICandidate, error) {
  98. res, err := newGuestResource(gst, host.GetName())
  99. if err != nil {
  100. return nil, errors.Wrap(err, "newGuestResource")
  101. }
  102. memSizeMB, err := gst.Int("vmem_size")
  103. if err != nil {
  104. return nil, errors.Wrap(err, "get vmem_size")
  105. }
  106. /* unit of influxdb query is byte
  107. > select free, available, total from mem where host_id = 'eda7c6f5-f714-4d59-8d6a-16b658712b07' limit 1;
  108. name: mem
  109. time free available total
  110. ---- ---- --------- -----
  111. 2022-05-02T00:00:00Z 15399550976 94193070080 270276599808
  112. */
  113. return &memCandidate{
  114. guestResource: res,
  115. score: float64(memSizeMB * 1024 * 1024),
  116. }, nil
  117. }
  118. func (m *memCandidate) GetScore() float64 {
  119. return m.score
  120. }
  121. type memHost struct {
  122. *HostResource
  123. availableMemSize float64
  124. }
  125. func newMemHost(obj jsonutils.JSONObject) (IHost, error) {
  126. host, err := newHostResource(obj)
  127. if err != nil {
  128. return nil, errors.Wrap(err, "newHostResource")
  129. }
  130. return &memHost{
  131. HostResource: host,
  132. }, nil
  133. }
  134. func (ts *memHost) GetCurrent() float64 {
  135. return ts.availableMemSize
  136. }
  137. func (ts *memHost) SetCurrent(val float64) IHost {
  138. ts.availableMemSize = val
  139. return ts
  140. }
  141. func (ts *memHost) Compare(oh IHost) bool {
  142. return ts.GetCurrent() > oh.GetCurrent()
  143. }
  144. type targetMemHost struct {
  145. IHost
  146. }
  147. func newTargetMemHost(obj jsonutils.JSONObject) (ITarget, error) {
  148. host, err := newMemHost(obj)
  149. if err != nil {
  150. return nil, errors.Wrap(err, "newMemHost")
  151. }
  152. ts := &targetMemHost{
  153. IHost: host,
  154. }
  155. return ts, nil
  156. }
  157. func (ts *targetMemHost) Selected(c ICandidate) ITarget {
  158. ts.SetCurrent(ts.GetCurrent() - c.GetScore())
  159. return ts
  160. }