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.go 8.7 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // Copyright (C) MongoDB, Inc. 2017-present.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  6. package mongo
  7. import (
  8. "context"
  9. "errors"
  10. "time"
  11. "go.mongodb.org/mongo-driver/bson"
  12. "go.mongodb.org/mongo-driver/bson/primitive"
  13. "go.mongodb.org/mongo-driver/mongo/options"
  14. "go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
  15. "go.mongodb.org/mongo-driver/x/mongo/driver"
  16. "go.mongodb.org/mongo-driver/x/mongo/driver/description"
  17. "go.mongodb.org/mongo-driver/x/mongo/driver/operation"
  18. "go.mongodb.org/mongo-driver/x/mongo/driver/session"
  19. "go.mongodb.org/mongo-driver/x/mongo/driver/topology"
  20. )
  21. // ErrWrongClient is returned when a user attempts to pass in a session created by a different client than
  22. // the method call is using.
  23. var ErrWrongClient = errors.New("session was not created by this client")
  24. var withTransactionTimeout = 120 * time.Second
  25. // SessionContext is a hybrid interface. It combines a context.Context with
  26. // a mongo.Session. This type can be used as a regular context.Context or
  27. // Session type. It is not goroutine safe and should not be used in multiple goroutines concurrently.
  28. type SessionContext interface {
  29. context.Context
  30. Session
  31. }
  32. type sessionContext struct {
  33. context.Context
  34. Session
  35. }
  36. type sessionKey struct {
  37. }
  38. // Session is the interface that represents a sequential set of operations executed.
  39. // Instances of this interface can be used to use transactions against the server
  40. // and to enable causally consistent behavior for applications.
  41. type Session interface {
  42. EndSession(context.Context)
  43. WithTransaction(ctx context.Context, fn func(sessCtx SessionContext) (interface{}, error), opts ...*options.TransactionOptions) (interface{}, error)
  44. StartTransaction(...*options.TransactionOptions) error
  45. AbortTransaction(context.Context) error
  46. CommitTransaction(context.Context) error
  47. ClusterTime() bson.Raw
  48. AdvanceClusterTime(bson.Raw) error
  49. OperationTime() *primitive.Timestamp
  50. AdvanceOperationTime(*primitive.Timestamp) error
  51. Client() *Client
  52. session()
  53. }
  54. // sessionImpl represents a set of sequential operations executed by an application that are related in some way.
  55. type sessionImpl struct {
  56. clientSession *session.Client
  57. client *Client
  58. topo *topology.Topology
  59. didCommitAfterStart bool // true if commit was called after start with no other operations
  60. }
  61. // EndSession ends the session.
  62. func (s *sessionImpl) EndSession(ctx context.Context) {
  63. if s.clientSession.TransactionInProgress() {
  64. // ignore all errors aborting during an end session
  65. _ = s.AbortTransaction(ctx)
  66. }
  67. s.clientSession.EndSession()
  68. }
  69. // WithTransaction creates a transaction on this session and runs the given callback, retrying for
  70. // TransientTransactionError and UnknownTransactionCommitResult errors. The only way to provide a
  71. // session to a CRUD method is to invoke that CRUD method with the mongo.SessionContext within the
  72. // callback. The mongo.SessionContext can be used as a regular context, so methods like
  73. // context.WithDeadline and context.WithTimeout are supported.
  74. //
  75. // If the context.Context already has a mongo.Session attached, that mongo.Session will be replaced
  76. // with the one provided.
  77. //
  78. // The callback may be run multiple times due to retry attempts. Non-retryable and timed out errors
  79. // are returned from this function.
  80. func (s *sessionImpl) WithTransaction(ctx context.Context, fn func(sessCtx SessionContext) (interface{}, error), opts ...*options.TransactionOptions) (interface{}, error) {
  81. timeout := time.NewTimer(withTransactionTimeout)
  82. defer timeout.Stop()
  83. var err error
  84. for {
  85. err = s.StartTransaction(opts...)
  86. if err != nil {
  87. return nil, err
  88. }
  89. res, err := fn(contextWithSession(ctx, s))
  90. if err != nil {
  91. if s.clientSession.TransactionRunning() {
  92. _ = s.AbortTransaction(ctx)
  93. }
  94. select {
  95. case <-timeout.C:
  96. return nil, err
  97. default:
  98. }
  99. if cerr, ok := err.(CommandError); ok {
  100. if cerr.HasErrorLabel(driver.TransientTransactionError) {
  101. continue
  102. }
  103. }
  104. return res, err
  105. }
  106. err = s.clientSession.CheckAbortTransaction()
  107. if err != nil {
  108. return res, nil
  109. }
  110. CommitLoop:
  111. for {
  112. err = s.CommitTransaction(ctx)
  113. if err == nil {
  114. return res, nil
  115. }
  116. select {
  117. case <-timeout.C:
  118. return res, err
  119. default:
  120. }
  121. if cerr, ok := err.(CommandError); ok {
  122. if cerr.HasErrorLabel(driver.UnknownTransactionCommitResult) && !cerr.IsMaxTimeMSExpiredError() {
  123. continue
  124. }
  125. if cerr.HasErrorLabel(driver.TransientTransactionError) {
  126. break CommitLoop
  127. }
  128. }
  129. return res, err
  130. }
  131. }
  132. }
  133. // StartTransaction starts a transaction for this session.
  134. func (s *sessionImpl) StartTransaction(opts ...*options.TransactionOptions) error {
  135. err := s.clientSession.CheckStartTransaction()
  136. if err != nil {
  137. return err
  138. }
  139. s.didCommitAfterStart = false
  140. topts := options.MergeTransactionOptions(opts...)
  141. coreOpts := &session.TransactionOptions{
  142. ReadConcern: topts.ReadConcern,
  143. ReadPreference: topts.ReadPreference,
  144. WriteConcern: topts.WriteConcern,
  145. MaxCommitTime: topts.MaxCommitTime,
  146. }
  147. return s.clientSession.StartTransaction(coreOpts)
  148. }
  149. // AbortTransaction aborts the session's transaction, returning any errors and error codes
  150. func (s *sessionImpl) AbortTransaction(ctx context.Context) error {
  151. err := s.clientSession.CheckAbortTransaction()
  152. if err != nil {
  153. return err
  154. }
  155. // Do not run the abort command if the transaction is in starting state
  156. if s.clientSession.TransactionStarting() || s.didCommitAfterStart {
  157. return s.clientSession.AbortTransaction()
  158. }
  159. selector := makePinnedSelector(s.clientSession, description.WriteSelector())
  160. s.clientSession.Aborting = true
  161. err = operation.NewAbortTransaction().Session(s.clientSession).ClusterClock(s.client.clock).Database("admin").
  162. Deployment(s.topo).WriteConcern(s.clientSession.CurrentWc).ServerSelector(selector).
  163. Retry(driver.RetryOncePerCommand).CommandMonitor(s.client.monitor).RecoveryToken(bsoncore.Document(s.clientSession.RecoveryToken)).Execute(ctx)
  164. s.clientSession.Aborting = false
  165. _ = s.clientSession.AbortTransaction()
  166. return replaceErrors(err)
  167. }
  168. // CommitTransaction commits the sesson's transaction.
  169. func (s *sessionImpl) CommitTransaction(ctx context.Context) error {
  170. err := s.clientSession.CheckCommitTransaction()
  171. if err != nil {
  172. return err
  173. }
  174. // Do not run the commit command if the transaction is in started state
  175. if s.clientSession.TransactionStarting() || s.didCommitAfterStart {
  176. s.didCommitAfterStart = true
  177. return s.clientSession.CommitTransaction()
  178. }
  179. if s.clientSession.TransactionCommitted() {
  180. s.clientSession.RetryingCommit = true
  181. }
  182. selector := makePinnedSelector(s.clientSession, description.WriteSelector())
  183. s.clientSession.Committing = true
  184. op := operation.NewCommitTransaction().
  185. Session(s.clientSession).ClusterClock(s.client.clock).Database("admin").Deployment(s.topo).
  186. WriteConcern(s.clientSession.CurrentWc).ServerSelector(selector).Retry(driver.RetryOncePerCommand).
  187. CommandMonitor(s.client.monitor).RecoveryToken(bsoncore.Document(s.clientSession.RecoveryToken))
  188. if s.clientSession.CurrentMct != nil {
  189. op.MaxTimeMS(int64(*s.clientSession.CurrentMct / time.Millisecond))
  190. }
  191. err = op.Execute(ctx)
  192. s.clientSession.Committing = false
  193. commitErr := s.clientSession.CommitTransaction()
  194. // We set the write concern to majority for subsequent calls to CommitTransaction.
  195. s.clientSession.UpdateCommitTransactionWriteConcern()
  196. if err != nil {
  197. return replaceErrors(err)
  198. }
  199. return commitErr
  200. }
  201. func (s *sessionImpl) ClusterTime() bson.Raw {
  202. return s.clientSession.ClusterTime
  203. }
  204. func (s *sessionImpl) AdvanceClusterTime(d bson.Raw) error {
  205. return s.clientSession.AdvanceClusterTime(d)
  206. }
  207. func (s *sessionImpl) OperationTime() *primitive.Timestamp {
  208. return s.clientSession.OperationTime
  209. }
  210. func (s *sessionImpl) AdvanceOperationTime(ts *primitive.Timestamp) error {
  211. return s.clientSession.AdvanceOperationTime(ts)
  212. }
  213. func (s *sessionImpl) Client() *Client {
  214. return s.client
  215. }
  216. func (*sessionImpl) session() {
  217. }
  218. // sessionFromContext checks for a sessionImpl in the argued context and returns the session if it
  219. // exists
  220. func sessionFromContext(ctx context.Context) *session.Client {
  221. s := ctx.Value(sessionKey{})
  222. if ses, ok := s.(*sessionImpl); ses != nil && ok {
  223. return ses.clientSession
  224. }
  225. return nil
  226. }
  227. func contextWithSession(ctx context.Context, sess Session) SessionContext {
  228. return &sessionContext{
  229. Context: context.WithValue(ctx, sessionKey{}, sess),
  230. Session: sess,
  231. }
  232. }