manager.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 options
  15. import (
  16. "reflect"
  17. "strings"
  18. "time"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/util/reflectutils"
  22. identity_api "yunion.io/x/onecloud/pkg/apis/identity"
  23. "yunion.io/x/onecloud/pkg/appsrv"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/syncman/watcher"
  26. "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  27. )
  28. const (
  29. MIN_REFRESH_INTERVAL_SECONDS = 30
  30. )
  31. type TOptionsChangeFunc func(oldOpts, newOpts interface{}) bool
  32. type SOptionManager struct {
  33. watcher.SInformerSyncManager
  34. serviceType string
  35. serviceVersion string
  36. options interface{}
  37. session IServiceConfigSession
  38. onOptionsChange TOptionsChangeFunc
  39. refreshInterval time.Duration
  40. }
  41. var (
  42. OptionManager *SOptionManager
  43. )
  44. func StartOptionManager(option interface{}, refreshSeconds int, serviceType, serviceVersion string, onChange TOptionsChangeFunc) {
  45. StartOptionManagerWithSessionDriver(option, refreshSeconds, serviceType, serviceVersion, onChange, newServiceConfigSession())
  46. }
  47. func StartOptionManagerWithSessionDriver(options interface{}, refreshSeconds int, serviceType, serviceVersion string, onChange TOptionsChangeFunc, session IServiceConfigSession) {
  48. if refreshSeconds <= MIN_REFRESH_INTERVAL_SECONDS {
  49. // a minimal 30 seconds refresh interval
  50. refreshSeconds = MIN_REFRESH_INTERVAL_SECONDS
  51. }
  52. refreshInterval := time.Duration(refreshSeconds) * time.Second
  53. log.Infof("OptionManager start to fetch service configs with interval %s ...", refreshInterval)
  54. OptionManager = &SOptionManager{
  55. serviceType: serviceType,
  56. serviceVersion: serviceVersion,
  57. options: options,
  58. session: session,
  59. onOptionsChange: onChange,
  60. refreshInterval: refreshInterval,
  61. }
  62. OptionManager.InitSync(OptionManager)
  63. if err := OptionManager.FirstSync(); err != nil {
  64. log.Errorf("OptionManager.FirstSync error: %v", err)
  65. }
  66. if session.IsRemote() {
  67. var opts *CommonOptions
  68. err := reflectutils.FindAnonymouStructPointer(options, &opts)
  69. if err != nil {
  70. log.Errorf("cannot find CommonOptions in options")
  71. } else if opts.SessionEndpointType == identity_api.EndpointInterfaceInternal {
  72. OptionManager.StartWatching(&identity.ServicesV3)
  73. }
  74. }
  75. }
  76. func (manager *SOptionManager) newOptions() interface{} {
  77. optType := reflect.ValueOf(manager.options).Elem().Type()
  78. return reflect.New(optType).Interface()
  79. }
  80. func copyOptions(dst, src interface{}) {
  81. dstValue := reflect.ValueOf(dst).Elem()
  82. dstValue.Set(reflect.ValueOf(src).Elem())
  83. }
  84. func optionsEquals(newOpts interface{}, oldOpts interface{}) bool {
  85. newOptsDict := jsonutils.Marshal(newOpts).(*jsonutils.JSONDict)
  86. oldOptsDict := jsonutils.Marshal(oldOpts).(*jsonutils.JSONDict)
  87. deleted, diff, _, added := jsonutils.Diff(oldOptsDict, newOptsDict)
  88. if deleted.Length() > 0 {
  89. log.Infof("Options removed: %s", deleted)
  90. return false
  91. }
  92. if diff.Length() > 0 {
  93. log.Infof("Options changed: %s", diff)
  94. return false
  95. }
  96. if added.Length() > 0 {
  97. log.Infof("Options added: %s", added)
  98. return false
  99. }
  100. return true
  101. }
  102. func (manager *SOptionManager) DoSync(first bool, timeout bool) (time.Duration, error) {
  103. newOpts := manager.newOptions()
  104. copyOptions(newOpts, manager.options)
  105. merged := manager.session.Merge(newOpts, manager.serviceType, manager.serviceVersion)
  106. if merged && !optionsEquals(newOpts, manager.options) {
  107. if manager.onOptionsChange != nil && manager.onOptionsChange(manager.options, newOpts) && !first {
  108. log.Infof("Option changes detected and going to restart the program...")
  109. appsrv.SetExitFlag()
  110. }
  111. copyOptions(manager.options, newOpts)
  112. // if first {
  113. // upload config
  114. manager.session.Upload()
  115. // }
  116. }
  117. return manager.refreshInterval, nil
  118. }
  119. func (manager *SOptionManager) NeedSync(dat *jsonutils.JSONDict) bool {
  120. serviceType, _ := dat.GetString("type")
  121. if strings.HasPrefix(serviceType, manager.serviceType) || serviceType == consts.COMMON_SERVICE {
  122. return true
  123. }
  124. return false
  125. }
  126. func (manager *SOptionManager) Name() string {
  127. return "ServiceConfigManager"
  128. }