changeman.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 cachesync
  15. import (
  16. "fmt"
  17. "strings"
  18. "sync"
  19. "time"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/log"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/syncman/watcher"
  23. "yunion.io/x/onecloud/pkg/mcclient/informer"
  24. identity_modules "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  25. )
  26. type SResourceChangeManager struct {
  27. watcher.SInformerSyncManager
  28. resMan informer.IResourceManager
  29. intervalSeconds int
  30. ids []string
  31. idsLock *sync.Mutex
  32. }
  33. func newResourceChangeManager(resMan informer.IResourceManager, intvalSecs int) *SResourceChangeManager {
  34. man := &SResourceChangeManager{
  35. resMan: resMan,
  36. intervalSeconds: intvalSecs,
  37. ids: make([]string, 0),
  38. idsLock: &sync.Mutex{},
  39. }
  40. man.InitSync(man)
  41. man.FirstSync()
  42. man.StartWatching(resMan)
  43. return man
  44. }
  45. func (man *SResourceChangeManager) DoSync(first bool, timeout bool) (time.Duration, error) {
  46. if first || timeout {
  47. // reset id list
  48. man.resetId()
  49. } else {
  50. log.Debugf("to do incremental sync ids %s", jsonutils.Marshal(man.ids))
  51. }
  52. switch man.resMan.KeyString() {
  53. case identity_modules.Projects.KeywordPlural:
  54. tenantCacheSyncWorkerMan.Run(&tenantCacheSyncWorker{
  55. ids: man.ids,
  56. }, nil, nil)
  57. case identity_modules.Domains.KeywordPlural:
  58. tenantCacheSyncWorkerMan.Run(&domainCacheSyncWorker{
  59. ids: man.ids,
  60. }, nil, nil)
  61. case identity_modules.UsersV3.KeywordPlural:
  62. tenantCacheSyncWorkerMan.Run(&userCacheSyncWorker{
  63. ids: man.ids,
  64. }, nil, nil)
  65. }
  66. man.resetId()
  67. log.Debugf("sync DONE, next sync %d seconds later...", man.intervalSeconds*8)
  68. return time.Second * time.Duration(man.intervalSeconds) * 8, nil
  69. }
  70. func (man *SResourceChangeManager) NeedSync(dat *jsonutils.JSONDict) bool {
  71. if dat != nil && dat.Contains("id") {
  72. idstr, _ := dat.GetString("id")
  73. idstr = strings.TrimSpace(idstr)
  74. if len(idstr) > 0 {
  75. man.addId(idstr)
  76. }
  77. }
  78. return true
  79. }
  80. func (man *SResourceChangeManager) addId(idstr string) {
  81. man.idsLock.Lock()
  82. defer man.idsLock.Unlock()
  83. man.ids = append(man.ids, idstr)
  84. }
  85. func (man *SResourceChangeManager) resetId() {
  86. man.idsLock.Lock()
  87. defer man.idsLock.Unlock()
  88. man.ids = man.ids[0:0]
  89. }
  90. func (man *SResourceChangeManager) Name() string {
  91. return fmt.Sprintf("ResourceChangeManager:%s", man.resMan.GetKeyword())
  92. }