You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

session_manager.go 6.0 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package holder
  2. import (
  3. "errors"
  4. "github.com/dk-lockdown/seata-golang/logging"
  5. "github.com/dk-lockdown/seata-golang/meta"
  6. "github.com/dk-lockdown/seata-golang/tc/model"
  7. "github.com/dk-lockdown/seata-golang/tc/session"
  8. )
  9. type ISessionManager interface {
  10. /**
  11. * Add global session.
  12. *
  13. * @param session the session
  14. * @throws TransactionException the transaction exception
  15. */
  16. AddGlobalSession(session *session.GlobalSession) error
  17. /**
  18. * Find global session global session.
  19. *
  20. * @param xid the xid
  21. * @return the global session
  22. */
  23. FindGlobalSession(xid string) *session.GlobalSession
  24. /**
  25. * Find global session global session.
  26. *
  27. * @param xid the xid
  28. * @param withBranchSessions the withBranchSessions
  29. * @return the global session
  30. */
  31. FindGlobalSessionWithBranchSessions(xid string, withBranchSessions bool) *session.GlobalSession
  32. /**
  33. * Update global session status.
  34. *
  35. * @param session the session
  36. * @param status the status
  37. * @throws TransactionException the transaction exception
  38. */
  39. UpdateGlobalSessionStatus(session *session.GlobalSession, status meta.GlobalStatus) error
  40. /**
  41. * Remove global session.
  42. *
  43. * @param session the session
  44. * @throws TransactionException the transaction exception
  45. */
  46. RemoveGlobalSession(session *session.GlobalSession) error
  47. /**
  48. * Add branch session.
  49. *
  50. * @param globalSession the global session
  51. * @param session the session
  52. * @throws TransactionException the transaction exception
  53. */
  54. AddBranchSession(globalSession *session.GlobalSession, session *session.BranchSession) error
  55. /**
  56. * Update branch session status.
  57. *
  58. * @param session the session
  59. * @param status the status
  60. * @throws TransactionException the transaction exception
  61. */
  62. UpdateBranchSessionStatus(session *session.BranchSession, status meta.BranchStatus) error
  63. /**
  64. * Remove branch session.
  65. *
  66. * @param globalSession the global session
  67. * @param session the session
  68. * @throws TransactionException the transaction exception
  69. */
  70. RemoveBranchSession(globalSession *session.GlobalSession, session *session.BranchSession) error
  71. /**
  72. * All sessions collection.
  73. *
  74. * @return the collection
  75. */
  76. AllSessions() []*session.GlobalSession
  77. /**
  78. * Find global sessions list.
  79. *
  80. * @param condition the condition
  81. * @return the list
  82. */
  83. FindGlobalSessions(condition model.SessionCondition) []*session.GlobalSession
  84. }
  85. type AbstractSessionManager struct {
  86. TransactionStoreManager ITransactionStoreManager
  87. Name string
  88. }
  89. func (sessionManager *AbstractSessionManager) AddGlobalSession(session *session.GlobalSession) error{
  90. logging.Logger.Debugf("MANAGER[%s] SESSION[%v] %s",sessionManager.Name, session, LogOperationGlobalAdd.String())
  91. sessionManager.writeSession(LogOperationGlobalAdd,session)
  92. return nil
  93. }
  94. func (sessionManager *AbstractSessionManager) UpdateGlobalSessionStatus(session *session.GlobalSession, status meta.GlobalStatus) error {
  95. logging.Logger.Debugf("MANAGER[%s] SESSION[%v] %s",sessionManager.Name, session, LogOperationGlobalUpdate.String())
  96. sessionManager.writeSession(LogOperationGlobalUpdate,session)
  97. return nil
  98. }
  99. func (sessionManager *AbstractSessionManager) RemoveGlobalSession(session *session.GlobalSession) error{
  100. logging.Logger.Debugf("MANAGER[%s] SESSION[%v] %s",sessionManager.Name, session, LogOperationGlobalRemove.String())
  101. sessionManager.writeSession(LogOperationGlobalRemove,session)
  102. return nil
  103. }
  104. func (sessionManager *AbstractSessionManager) AddBranchSession(globalSession *session.GlobalSession, session *session.BranchSession) error{
  105. logging.Logger.Debugf("MANAGER[%s] SESSION[%v] %s",sessionManager.Name, session, LogOperationBranchAdd.String())
  106. sessionManager.writeSession(LogOperationBranchAdd,session)
  107. return nil
  108. }
  109. func (sessionManager *AbstractSessionManager) UpdateBranchSessionStatus(session *session.BranchSession, status meta.BranchStatus) error{
  110. logging.Logger.Debugf("MANAGER[%s] SESSION[%v] %s",sessionManager.Name, session, LogOperationBranchUpdate.String())
  111. sessionManager.writeSession(LogOperationBranchUpdate,session)
  112. return nil
  113. }
  114. func (sessionManager *AbstractSessionManager) RemoveBranchSession(globalSession *session.GlobalSession, session *session.BranchSession) error{
  115. logging.Logger.Debugf("MANAGER[%s] SESSION[%v] %s",sessionManager.Name, session, LogOperationBranchRemove.String())
  116. sessionManager.writeSession(LogOperationBranchRemove,session)
  117. return nil
  118. }
  119. func (sessionManager *AbstractSessionManager) writeSession(logOperation LogOperation, sessionStorable session.SessionStorable) error {
  120. result := sessionManager.TransactionStoreManager.WriteSession(logOperation,sessionStorable)
  121. if !result {
  122. if logOperation == LogOperationGlobalAdd {
  123. return &meta.TransactionException{
  124. Code: meta.TransactionExceptionCodeFailedWriteSession,
  125. Message: "Fail to holder global session",
  126. }
  127. }
  128. if logOperation == LogOperationGlobalUpdate {
  129. return &meta.TransactionException{
  130. Code: meta.TransactionExceptionCodeFailedWriteSession,
  131. Message: "Fail to update global session",
  132. }
  133. }
  134. if logOperation == LogOperationGlobalRemove {
  135. return &meta.TransactionException{
  136. Code: meta.TransactionExceptionCodeFailedWriteSession,
  137. Message: "Fail to remove global session",
  138. }
  139. }
  140. if logOperation == LogOperationBranchAdd {
  141. return &meta.TransactionException{
  142. Code: meta.TransactionExceptionCodeFailedWriteSession,
  143. Message: "Fail to holder branch session",
  144. }
  145. }
  146. if logOperation == LogOperationBranchUpdate {
  147. return &meta.TransactionException{
  148. Code: meta.TransactionExceptionCodeFailedWriteSession,
  149. Message: "Fail to update branch session",
  150. }
  151. }
  152. if logOperation == LogOperationBranchRemove {
  153. return &meta.TransactionException{
  154. Code: meta.TransactionExceptionCodeFailedWriteSession,
  155. Message: "Fail to remove branch session",
  156. }
  157. }
  158. return errors.New("Unknown LogOperation:"+logOperation.String())
  159. }
  160. return nil
  161. }

Go Implementation For Seata