syncworkers.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 models
  15. import (
  16. "context"
  17. "fmt"
  18. "runtime/debug"
  19. "github.com/serialx/hashring"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/log"
  22. "yunion.io/x/pkg/util/stringutils"
  23. "yunion.io/x/pkg/util/version"
  24. api "yunion.io/x/onecloud/pkg/apis/notify"
  25. "yunion.io/x/onecloud/pkg/appsrv"
  26. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  27. "yunion.io/x/onecloud/pkg/mcclient/modules/yunionconf"
  28. )
  29. var (
  30. syncAccountWorker *appsrv.SWorkerManager
  31. syncWorkers []*appsrv.SWorkerManager
  32. syncWorkerRing *hashring.HashRing
  33. indexMap map[string]int
  34. )
  35. func InitSyncWorkers(count int) {
  36. syncWorkers = make([]*appsrv.SWorkerManager, count)
  37. syncWorkerIndexes := make([]string, count)
  38. indexMap = map[string]int{}
  39. for i := range syncWorkers {
  40. syncWorkers[i] = appsrv.NewWorkerManager(
  41. fmt.Sprintf("syncWorkerManager-%d", i+1),
  42. 1,
  43. 2048,
  44. true,
  45. )
  46. syncWorkerIndexes[i] = stringutils.UUID4()
  47. indexMap[syncWorkerIndexes[i]] = i
  48. }
  49. syncWorkerRing = hashring.New(syncWorkerIndexes)
  50. syncAccountWorker = appsrv.NewWorkerManager(
  51. "cloudAccountProbeWorkerManager",
  52. 10,
  53. 2048,
  54. true,
  55. )
  56. }
  57. type resSyncTask struct {
  58. syncFunc func()
  59. key string
  60. }
  61. func (t *resSyncTask) Run() {
  62. t.syncFunc()
  63. }
  64. func (t *resSyncTask) Dump() string {
  65. return fmt.Sprintf("key: %s", t.key)
  66. }
  67. func RunSyncCloudproviderRegionTask(ctx context.Context, key string, syncFunc func()) {
  68. nodeIdxStr, _ := syncWorkerRing.GetNode(key)
  69. task := resSyncTask{
  70. syncFunc: syncFunc,
  71. key: key,
  72. }
  73. log.Debugf("run sync task at %d len %d", indexMap[nodeIdxStr], len(syncWorkers))
  74. syncWorkers[indexMap[nodeIdxStr]].Run(&task, nil, func(err error) {
  75. data := jsonutils.NewDict()
  76. data.Add(jsonutils.NewString("SyncCloudproviderRegion"), "task_name")
  77. data.Add(jsonutils.NewString(key), "task_id")
  78. data.Add(jsonutils.NewString(string(debug.Stack())), "stack")
  79. data.Add(jsonutils.NewString(err.Error()), "error")
  80. notifyclient.SystemExceptionNotify(context.TODO(), api.ActionSystemPanic, api.TOPIC_RESOURCE_TASK, data)
  81. yunionconf.BugReport.SendBugReport(ctx, version.GetShortString(), string(debug.Stack()), err)
  82. })
  83. }
  84. func RunSyncCloudAccountTask(ctx context.Context, probeFunc func()) {
  85. task := resSyncTask{
  86. syncFunc: probeFunc,
  87. key: "AccountProb",
  88. }
  89. syncAccountWorker.Run(&task, nil, func(err error) {
  90. data := jsonutils.NewDict()
  91. data.Add(jsonutils.NewString("SyncCloudAccountTask"), "task_name")
  92. data.Add(jsonutils.NewString(task.key), "task_id")
  93. data.Add(jsonutils.NewString(string(debug.Stack())), "stack")
  94. data.Add(jsonutils.NewString(err.Error()), "error")
  95. notifyclient.SystemExceptionNotify(context.TODO(), api.ActionSystemPanic, api.TOPIC_RESOURCE_TASK, data)
  96. yunionconf.BugReport.SendBugReport(ctx, version.GetShortString(), string(debug.Stack()), err)
  97. })
  98. }