lockman.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 lockman
  15. import (
  16. "context"
  17. "fmt"
  18. "runtime/debug"
  19. "yunion.io/x/log"
  20. )
  21. type ILockedClass interface {
  22. Keyword() string
  23. }
  24. type ILockedObject interface {
  25. ILockedClass
  26. GetId() string
  27. }
  28. type ILockManager interface {
  29. LockKey(ctx context.Context, key string)
  30. UnlockKey(ctx context.Context, key string)
  31. LockClass(ctx context.Context, manager ILockedClass, projectId string)
  32. ReleaseClass(ctx context.Context, manager ILockedClass, projectId string)
  33. LockObject(ctx context.Context, model ILockedObject)
  34. ReleaseObject(ctx context.Context, model ILockedObject)
  35. LockRawObject(ctx context.Context, resName string, resId string)
  36. ReleaseRawObject(ctx context.Context, resName string, resId string)
  37. LockJointObject(ctx context.Context, model ILockedObject, model2 ILockedObject)
  38. ReleaseJointObject(ctx context.Context, model ILockedObject, model2 ILockedObject)
  39. }
  40. func getClassKey(manager ILockedClass, projectId string) string {
  41. // assert(getattr(cls, '_resource_name_', None) is not None)
  42. // return '%s-%s' % (cls._resource_name_, user_cred.tenant_id)
  43. return fmt.Sprintf("%s-%s", manager.Keyword(), projectId)
  44. }
  45. func getObjectKey(model ILockedObject) string {
  46. // assert(getattr(obj, '_resource_name_', None) is not None)
  47. // assert(getattr(obj, 'id', None) is not None)
  48. // return '%s-%s' % (obj._resource_name_, obj.id)
  49. return getRawObjectKey(model.Keyword(), model.GetId())
  50. }
  51. func getRawObjectKey(resName string, resId string) string {
  52. return fmt.Sprintf("%s-%s", resName, resId)
  53. }
  54. func getJointObjectKey(model ILockedObject, model2 ILockedObject) string {
  55. // def _get_joint_object_key(self, obj1, obj2, user_cred):
  56. // return '%s-%s' % (self._get_object_key(obj1, user_cred),
  57. // self._get_object_key(obj2, user_cred))
  58. return fmt.Sprintf("%s-%s", getObjectKey(model), getObjectKey(model2))
  59. }
  60. var _lockman ILockManager
  61. func Init(man ILockManager) {
  62. _lockman = man
  63. }
  64. func checkContext(ctx context.Context) {
  65. if ctx == context.Background() {
  66. log.Warningf("[!!!WARNING!!!] lock context of Background!")
  67. debug.PrintStack()
  68. } else if ctx == context.TODO() {
  69. log.Warningf("[!!!WARNING!!!] lock context of TODO!")
  70. debug.PrintStack()
  71. }
  72. }
  73. func LockClass(ctx context.Context, manager ILockedClass, projectId string) {
  74. checkContext(ctx)
  75. _lockman.LockClass(ctx, manager, projectId)
  76. }
  77. func ReleaseClass(ctx context.Context, manager ILockedClass, projectId string) {
  78. checkContext(ctx)
  79. _lockman.ReleaseClass(ctx, manager, projectId)
  80. }
  81. func LockObject(ctx context.Context, model ILockedObject) {
  82. checkContext(ctx)
  83. _lockman.LockObject(ctx, model)
  84. }
  85. func ReleaseObject(ctx context.Context, model ILockedObject) {
  86. checkContext(ctx)
  87. _lockman.ReleaseObject(ctx, model)
  88. }
  89. func LockRawObject(ctx context.Context, resName string, resId string) {
  90. checkContext(ctx)
  91. _lockman.LockRawObject(ctx, resName, resId)
  92. }
  93. func ReleaseRawObject(ctx context.Context, resName string, resId string) {
  94. checkContext(ctx)
  95. _lockman.ReleaseRawObject(ctx, resName, resId)
  96. }
  97. func LockJointObject(ctx context.Context, model ILockedObject, model2 ILockedObject) {
  98. checkContext(ctx)
  99. _lockman.LockJointObject(ctx, model, model2)
  100. }
  101. func ReleaseJointObject(ctx context.Context, model ILockedObject, model2 ILockedObject) {
  102. checkContext(ctx)
  103. _lockman.ReleaseJointObject(ctx, model, model2)
  104. }