collection.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. package mon
  2. import (
  3. "context"
  4. "errors"
  5. "time"
  6. "github.com/wuntsong-org/go-zero-plus/core/breaker"
  7. "github.com/wuntsong-org/go-zero-plus/core/timex"
  8. "go.mongodb.org/mongo-driver/mongo"
  9. mopt "go.mongodb.org/mongo-driver/mongo/options"
  10. "go.mongodb.org/mongo-driver/x/mongo/driver/session"
  11. )
  12. const (
  13. defaultSlowThreshold = time.Millisecond * 500
  14. // spanName is the span name of the mongo calls.
  15. spanName = "mongo"
  16. // mongodb method names
  17. aggregate = "Aggregate"
  18. bulkWrite = "BulkWrite"
  19. countDocuments = "CountDocuments"
  20. deleteMany = "DeleteMany"
  21. deleteOne = "DeleteOne"
  22. distinct = "Distinct"
  23. estimatedDocumentCount = "EstimatedDocumentCount"
  24. find = "Find"
  25. findOne = "FindOne"
  26. findOneAndDelete = "FindOneAndDelete"
  27. findOneAndReplace = "FindOneAndReplace"
  28. findOneAndUpdate = "FindOneAndUpdate"
  29. insertMany = "InsertMany"
  30. insertOne = "InsertOne"
  31. replaceOne = "ReplaceOne"
  32. updateByID = "UpdateByID"
  33. updateMany = "UpdateMany"
  34. updateOne = "UpdateOne"
  35. )
  36. // ErrNotFound is an alias of mongo.ErrNoDocuments
  37. var ErrNotFound = mongo.ErrNoDocuments
  38. type (
  39. // Collection defines a MongoDB collection.
  40. Collection interface {
  41. // Aggregate executes an aggregation pipeline.
  42. Aggregate(ctx context.Context, pipeline any, opts ...*mopt.AggregateOptions) (
  43. *mongo.Cursor, error)
  44. // BulkWrite performs a bulk write operation.
  45. BulkWrite(ctx context.Context, models []mongo.WriteModel, opts ...*mopt.BulkWriteOptions) (
  46. *mongo.BulkWriteResult, error)
  47. // Clone creates a copy of this collection with the same settings.
  48. Clone(opts ...*mopt.CollectionOptions) (*mongo.Collection, error)
  49. // CountDocuments returns the number of documents in the collection that match the filter.
  50. CountDocuments(ctx context.Context, filter any, opts ...*mopt.CountOptions) (int64, error)
  51. // Database returns the database that this collection is a part of.
  52. Database() *mongo.Database
  53. // DeleteMany deletes documents from the collection that match the filter.
  54. DeleteMany(ctx context.Context, filter any, opts ...*mopt.DeleteOptions) (
  55. *mongo.DeleteResult, error)
  56. // DeleteOne deletes at most one document from the collection that matches the filter.
  57. DeleteOne(ctx context.Context, filter any, opts ...*mopt.DeleteOptions) (
  58. *mongo.DeleteResult, error)
  59. // Distinct returns a list of distinct values for the given key across the collection.
  60. Distinct(ctx context.Context, fieldName string, filter any,
  61. opts ...*mopt.DistinctOptions) ([]any, error)
  62. // Drop drops this collection from database.
  63. Drop(ctx context.Context) error
  64. // EstimatedDocumentCount returns an estimate of the count of documents in a collection
  65. // using collection metadata.
  66. EstimatedDocumentCount(ctx context.Context, opts ...*mopt.EstimatedDocumentCountOptions) (int64, error)
  67. // Find finds the documents matching the provided filter.
  68. Find(ctx context.Context, filter any, opts ...*mopt.FindOptions) (*mongo.Cursor, error)
  69. // FindOne returns up to one document that matches the provided filter.
  70. FindOne(ctx context.Context, filter any, opts ...*mopt.FindOneOptions) (
  71. *mongo.SingleResult, error)
  72. // FindOneAndDelete returns at most one document that matches the filter. If the filter
  73. // matches multiple documents, only the first document is deleted.
  74. FindOneAndDelete(ctx context.Context, filter any, opts ...*mopt.FindOneAndDeleteOptions) (
  75. *mongo.SingleResult, error)
  76. // FindOneAndReplace returns at most one document that matches the filter. If the filter
  77. // matches multiple documents, FindOneAndReplace returns the first document in the
  78. // collection that matches the filter.
  79. FindOneAndReplace(ctx context.Context, filter, replacement any,
  80. opts ...*mopt.FindOneAndReplaceOptions) (*mongo.SingleResult, error)
  81. // FindOneAndUpdate returns at most one document that matches the filter. If the filter
  82. // matches multiple documents, FindOneAndUpdate returns the first document in the
  83. // collection that matches the filter.
  84. FindOneAndUpdate(ctx context.Context, filter, update any,
  85. opts ...*mopt.FindOneAndUpdateOptions) (*mongo.SingleResult, error)
  86. // Indexes returns the index view for this collection.
  87. Indexes() mongo.IndexView
  88. // InsertMany inserts the provided documents.
  89. InsertMany(ctx context.Context, documents []any, opts ...*mopt.InsertManyOptions) (
  90. *mongo.InsertManyResult, error)
  91. // InsertOne inserts the provided document.
  92. InsertOne(ctx context.Context, document any, opts ...*mopt.InsertOneOptions) (
  93. *mongo.InsertOneResult, error)
  94. // ReplaceOne replaces at most one document that matches the filter.
  95. ReplaceOne(ctx context.Context, filter, replacement any,
  96. opts ...*mopt.ReplaceOptions) (*mongo.UpdateResult, error)
  97. // UpdateByID updates a single document matching the provided filter.
  98. UpdateByID(ctx context.Context, id, update any,
  99. opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
  100. // UpdateMany updates the provided documents.
  101. UpdateMany(ctx context.Context, filter, update any,
  102. opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
  103. // UpdateOne updates a single document matching the provided filter.
  104. UpdateOne(ctx context.Context, filter, update any,
  105. opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
  106. // Watch returns a change stream cursor used to receive notifications of changes to the collection.
  107. Watch(ctx context.Context, pipeline any, opts ...*mopt.ChangeStreamOptions) (
  108. *mongo.ChangeStream, error)
  109. }
  110. decoratedCollection struct {
  111. *mongo.Collection
  112. name string
  113. brk breaker.Breaker
  114. }
  115. keepablePromise struct {
  116. promise breaker.Promise
  117. log func(error)
  118. }
  119. )
  120. func newCollection(collection *mongo.Collection, brk breaker.Breaker) Collection {
  121. return &decoratedCollection{
  122. Collection: collection,
  123. name: collection.Name(),
  124. brk: brk,
  125. }
  126. }
  127. func (c *decoratedCollection) Aggregate(ctx context.Context, pipeline any,
  128. opts ...*mopt.AggregateOptions) (cur *mongo.Cursor, err error) {
  129. ctx, span := startSpan(ctx, aggregate)
  130. defer func() {
  131. endSpan(span, err)
  132. }()
  133. err = c.brk.DoWithAcceptable(func() error {
  134. starTime := timex.Now()
  135. defer func() {
  136. c.logDurationSimple(ctx, aggregate, starTime, err)
  137. }()
  138. cur, err = c.Collection.Aggregate(ctx, pipeline, opts...)
  139. return err
  140. }, acceptable)
  141. return
  142. }
  143. func (c *decoratedCollection) BulkWrite(ctx context.Context, models []mongo.WriteModel,
  144. opts ...*mopt.BulkWriteOptions) (res *mongo.BulkWriteResult, err error) {
  145. ctx, span := startSpan(ctx, bulkWrite)
  146. defer func() {
  147. endSpan(span, err)
  148. }()
  149. err = c.brk.DoWithAcceptable(func() error {
  150. startTime := timex.Now()
  151. defer func() {
  152. c.logDurationSimple(ctx, bulkWrite, startTime, err)
  153. }()
  154. res, err = c.Collection.BulkWrite(ctx, models, opts...)
  155. return err
  156. }, acceptable)
  157. return
  158. }
  159. func (c *decoratedCollection) CountDocuments(ctx context.Context, filter any,
  160. opts ...*mopt.CountOptions) (count int64, err error) {
  161. ctx, span := startSpan(ctx, countDocuments)
  162. defer func() {
  163. endSpan(span, err)
  164. }()
  165. err = c.brk.DoWithAcceptable(func() error {
  166. startTime := timex.Now()
  167. defer func() {
  168. c.logDurationSimple(ctx, countDocuments, startTime, err)
  169. }()
  170. count, err = c.Collection.CountDocuments(ctx, filter, opts...)
  171. return err
  172. }, acceptable)
  173. return
  174. }
  175. func (c *decoratedCollection) DeleteMany(ctx context.Context, filter any,
  176. opts ...*mopt.DeleteOptions) (res *mongo.DeleteResult, err error) {
  177. ctx, span := startSpan(ctx, deleteMany)
  178. defer func() {
  179. endSpan(span, err)
  180. }()
  181. err = c.brk.DoWithAcceptable(func() error {
  182. startTime := timex.Now()
  183. defer func() {
  184. c.logDurationSimple(ctx, deleteMany, startTime, err)
  185. }()
  186. res, err = c.Collection.DeleteMany(ctx, filter, opts...)
  187. return err
  188. }, acceptable)
  189. return
  190. }
  191. func (c *decoratedCollection) DeleteOne(ctx context.Context, filter any,
  192. opts ...*mopt.DeleteOptions) (res *mongo.DeleteResult, err error) {
  193. ctx, span := startSpan(ctx, deleteOne)
  194. defer func() {
  195. endSpan(span, err)
  196. }()
  197. err = c.brk.DoWithAcceptable(func() error {
  198. startTime := timex.Now()
  199. defer func() {
  200. c.logDuration(ctx, deleteOne, startTime, err, filter)
  201. }()
  202. res, err = c.Collection.DeleteOne(ctx, filter, opts...)
  203. return err
  204. }, acceptable)
  205. return
  206. }
  207. func (c *decoratedCollection) Distinct(ctx context.Context, fieldName string, filter any,
  208. opts ...*mopt.DistinctOptions) (val []any, err error) {
  209. ctx, span := startSpan(ctx, distinct)
  210. defer func() {
  211. endSpan(span, err)
  212. }()
  213. err = c.brk.DoWithAcceptable(func() error {
  214. startTime := timex.Now()
  215. defer func() {
  216. c.logDurationSimple(ctx, distinct, startTime, err)
  217. }()
  218. val, err = c.Collection.Distinct(ctx, fieldName, filter, opts...)
  219. return err
  220. }, acceptable)
  221. return
  222. }
  223. func (c *decoratedCollection) EstimatedDocumentCount(ctx context.Context,
  224. opts ...*mopt.EstimatedDocumentCountOptions) (val int64, err error) {
  225. ctx, span := startSpan(ctx, estimatedDocumentCount)
  226. defer func() {
  227. endSpan(span, err)
  228. }()
  229. err = c.brk.DoWithAcceptable(func() error {
  230. startTime := timex.Now()
  231. defer func() {
  232. c.logDurationSimple(ctx, estimatedDocumentCount, startTime, err)
  233. }()
  234. val, err = c.Collection.EstimatedDocumentCount(ctx, opts...)
  235. return err
  236. }, acceptable)
  237. return
  238. }
  239. func (c *decoratedCollection) Find(ctx context.Context, filter any,
  240. opts ...*mopt.FindOptions) (cur *mongo.Cursor, err error) {
  241. ctx, span := startSpan(ctx, find)
  242. defer func() {
  243. endSpan(span, err)
  244. }()
  245. err = c.brk.DoWithAcceptable(func() error {
  246. startTime := timex.Now()
  247. defer func() {
  248. c.logDuration(ctx, find, startTime, err, filter)
  249. }()
  250. cur, err = c.Collection.Find(ctx, filter, opts...)
  251. return err
  252. }, acceptable)
  253. return
  254. }
  255. func (c *decoratedCollection) FindOne(ctx context.Context, filter any,
  256. opts ...*mopt.FindOneOptions) (res *mongo.SingleResult, err error) {
  257. ctx, span := startSpan(ctx, findOne)
  258. defer func() {
  259. endSpan(span, err)
  260. }()
  261. err = c.brk.DoWithAcceptable(func() error {
  262. startTime := timex.Now()
  263. defer func() {
  264. c.logDuration(ctx, findOne, startTime, err, filter)
  265. }()
  266. res = c.Collection.FindOne(ctx, filter, opts...)
  267. err = res.Err()
  268. return err
  269. }, acceptable)
  270. return
  271. }
  272. func (c *decoratedCollection) FindOneAndDelete(ctx context.Context, filter any,
  273. opts ...*mopt.FindOneAndDeleteOptions) (res *mongo.SingleResult, err error) {
  274. ctx, span := startSpan(ctx, findOneAndDelete)
  275. defer func() {
  276. endSpan(span, err)
  277. }()
  278. err = c.brk.DoWithAcceptable(func() error {
  279. startTime := timex.Now()
  280. defer func() {
  281. c.logDuration(ctx, findOneAndDelete, startTime, err, filter)
  282. }()
  283. res = c.Collection.FindOneAndDelete(ctx, filter, opts...)
  284. err = res.Err()
  285. return err
  286. }, acceptable)
  287. return
  288. }
  289. func (c *decoratedCollection) FindOneAndReplace(ctx context.Context, filter any,
  290. replacement any, opts ...*mopt.FindOneAndReplaceOptions) (
  291. res *mongo.SingleResult, err error) {
  292. ctx, span := startSpan(ctx, findOneAndReplace)
  293. defer func() {
  294. endSpan(span, err)
  295. }()
  296. err = c.brk.DoWithAcceptable(func() error {
  297. startTime := timex.Now()
  298. defer func() {
  299. c.logDuration(ctx, findOneAndReplace, startTime, err, filter, replacement)
  300. }()
  301. res = c.Collection.FindOneAndReplace(ctx, filter, replacement, opts...)
  302. err = res.Err()
  303. return err
  304. }, acceptable)
  305. return
  306. }
  307. func (c *decoratedCollection) FindOneAndUpdate(ctx context.Context, filter, update any,
  308. opts ...*mopt.FindOneAndUpdateOptions) (res *mongo.SingleResult, err error) {
  309. ctx, span := startSpan(ctx, findOneAndUpdate)
  310. defer func() {
  311. endSpan(span, err)
  312. }()
  313. err = c.brk.DoWithAcceptable(func() error {
  314. startTime := timex.Now()
  315. defer func() {
  316. c.logDuration(ctx, findOneAndUpdate, startTime, err, filter, update)
  317. }()
  318. res = c.Collection.FindOneAndUpdate(ctx, filter, update, opts...)
  319. err = res.Err()
  320. return err
  321. }, acceptable)
  322. return
  323. }
  324. func (c *decoratedCollection) InsertMany(ctx context.Context, documents []any,
  325. opts ...*mopt.InsertManyOptions) (res *mongo.InsertManyResult, err error) {
  326. ctx, span := startSpan(ctx, insertMany)
  327. defer func() {
  328. endSpan(span, err)
  329. }()
  330. err = c.brk.DoWithAcceptable(func() error {
  331. startTime := timex.Now()
  332. defer func() {
  333. c.logDurationSimple(ctx, insertMany, startTime, err)
  334. }()
  335. res, err = c.Collection.InsertMany(ctx, documents, opts...)
  336. return err
  337. }, acceptable)
  338. return
  339. }
  340. func (c *decoratedCollection) InsertOne(ctx context.Context, document any,
  341. opts ...*mopt.InsertOneOptions) (res *mongo.InsertOneResult, err error) {
  342. ctx, span := startSpan(ctx, insertOne)
  343. defer func() {
  344. endSpan(span, err)
  345. }()
  346. err = c.brk.DoWithAcceptable(func() error {
  347. startTime := timex.Now()
  348. defer func() {
  349. c.logDuration(ctx, insertOne, startTime, err, document)
  350. }()
  351. res, err = c.Collection.InsertOne(ctx, document, opts...)
  352. return err
  353. }, acceptable)
  354. return
  355. }
  356. func (c *decoratedCollection) ReplaceOne(ctx context.Context, filter, replacement any,
  357. opts ...*mopt.ReplaceOptions) (res *mongo.UpdateResult, err error) {
  358. ctx, span := startSpan(ctx, replaceOne)
  359. defer func() {
  360. endSpan(span, err)
  361. }()
  362. err = c.brk.DoWithAcceptable(func() error {
  363. startTime := timex.Now()
  364. defer func() {
  365. c.logDuration(ctx, replaceOne, startTime, err, filter, replacement)
  366. }()
  367. res, err = c.Collection.ReplaceOne(ctx, filter, replacement, opts...)
  368. return err
  369. }, acceptable)
  370. return
  371. }
  372. func (c *decoratedCollection) UpdateByID(ctx context.Context, id, update any,
  373. opts ...*mopt.UpdateOptions) (res *mongo.UpdateResult, err error) {
  374. ctx, span := startSpan(ctx, updateByID)
  375. defer func() {
  376. endSpan(span, err)
  377. }()
  378. err = c.brk.DoWithAcceptable(func() error {
  379. startTime := timex.Now()
  380. defer func() {
  381. c.logDuration(ctx, updateByID, startTime, err, id, update)
  382. }()
  383. res, err = c.Collection.UpdateByID(ctx, id, update, opts...)
  384. return err
  385. }, acceptable)
  386. return
  387. }
  388. func (c *decoratedCollection) UpdateMany(ctx context.Context, filter, update any,
  389. opts ...*mopt.UpdateOptions) (res *mongo.UpdateResult, err error) {
  390. ctx, span := startSpan(ctx, updateMany)
  391. defer func() {
  392. endSpan(span, err)
  393. }()
  394. err = c.brk.DoWithAcceptable(func() error {
  395. startTime := timex.Now()
  396. defer func() {
  397. c.logDurationSimple(ctx, updateMany, startTime, err)
  398. }()
  399. res, err = c.Collection.UpdateMany(ctx, filter, update, opts...)
  400. return err
  401. }, acceptable)
  402. return
  403. }
  404. func (c *decoratedCollection) UpdateOne(ctx context.Context, filter, update any,
  405. opts ...*mopt.UpdateOptions) (res *mongo.UpdateResult, err error) {
  406. ctx, span := startSpan(ctx, updateOne)
  407. defer func() {
  408. endSpan(span, err)
  409. }()
  410. err = c.brk.DoWithAcceptable(func() error {
  411. startTime := timex.Now()
  412. defer func() {
  413. c.logDuration(ctx, updateOne, startTime, err, filter, update)
  414. }()
  415. res, err = c.Collection.UpdateOne(ctx, filter, update, opts...)
  416. return err
  417. }, acceptable)
  418. return
  419. }
  420. func (c *decoratedCollection) logDuration(ctx context.Context, method string,
  421. startTime time.Duration, err error, docs ...any) {
  422. logDurationWithDocs(ctx, c.name, method, startTime, err, docs...)
  423. }
  424. func (c *decoratedCollection) logDurationSimple(ctx context.Context, method string,
  425. startTime time.Duration, err error) {
  426. logDuration(ctx, c.name, method, startTime, err)
  427. }
  428. func (p keepablePromise) accept(err error) error {
  429. p.promise.Accept()
  430. p.log(err)
  431. return err
  432. }
  433. func (p keepablePromise) keep(err error) error {
  434. if acceptable(err) {
  435. p.promise.Accept()
  436. } else {
  437. p.promise.Reject(err.Error())
  438. }
  439. p.log(err)
  440. return err
  441. }
  442. func acceptable(err error) bool {
  443. return err == nil ||
  444. errors.Is(err, mongo.ErrNoDocuments) ||
  445. errors.Is(err, mongo.ErrNilValue) ||
  446. errors.Is(err, mongo.ErrNilDocument) ||
  447. errors.Is(err, mongo.ErrNilCursor) ||
  448. errors.Is(err, mongo.ErrEmptySlice) ||
  449. // session errors
  450. errors.Is(err, session.ErrSessionEnded) ||
  451. errors.Is(err, session.ErrNoTransactStarted) ||
  452. errors.Is(err, session.ErrTransactInProgress) ||
  453. errors.Is(err, session.ErrAbortAfterCommit) ||
  454. errors.Is(err, session.ErrAbortTwice) ||
  455. errors.Is(err, session.ErrCommitAfterAbort) ||
  456. errors.Is(err, session.ErrUnackWCUnsupported) ||
  457. errors.Is(err, session.ErrSnapshotTransaction)
  458. }