blob: cde5bf5e6ccad297afdad4fe766a54c6b0b1677e [file] [log] [blame]
Don Newton379ae252019-04-01 12:17:06 -04001// 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
7package driver
8
9import (
10 "context"
11
12 "time"
13
14 "github.com/mongodb/mongo-go-driver/bson/bsoncodec"
15 "github.com/mongodb/mongo-go-driver/mongo/options"
16 "github.com/mongodb/mongo-go-driver/mongo/writeconcern"
17 "github.com/mongodb/mongo-go-driver/x/bsonx"
18 "github.com/mongodb/mongo-go-driver/x/mongo/driver/session"
19 "github.com/mongodb/mongo-go-driver/x/mongo/driver/topology"
20 "github.com/mongodb/mongo-go-driver/x/mongo/driver/uuid"
21 "github.com/mongodb/mongo-go-driver/x/network/command"
22 "github.com/mongodb/mongo-go-driver/x/network/description"
23 "github.com/mongodb/mongo-go-driver/x/network/result"
24)
25
26// FindOneAndReplace handles the full cycle dispatch and execution of a FindOneAndReplace command against the provided
27// topology.
28func FindOneAndReplace(
29 ctx context.Context,
30 cmd command.FindOneAndReplace,
31 topo *topology.Topology,
32 selector description.ServerSelector,
33 clientID uuid.UUID,
34 pool *session.Pool,
35 retryWrite bool,
36 registry *bsoncodec.Registry,
37 opts ...*options.FindOneAndReplaceOptions,
38) (result.FindAndModify, error) {
39
40 ss, err := topo.SelectServer(ctx, selector)
41 if err != nil {
42 return result.FindAndModify{}, err
43 }
44
45 // If no explicit session and deployment supports sessions, start implicit session.
46 if cmd.Session == nil && topo.SupportsSessions() {
47 cmd.Session, err = session.NewClientSession(pool, clientID, session.Implicit)
48 if err != nil {
49 return result.FindAndModify{}, err
50 }
51 defer cmd.Session.EndSession()
52 }
53
54 ro := options.MergeFindOneAndReplaceOptions(opts...)
55 if ro.BypassDocumentValidation != nil {
56 cmd.Opts = append(cmd.Opts, bsonx.Elem{"byapssDocumentValidation", bsonx.Boolean(*ro.BypassDocumentValidation)})
57 }
58 if ro.Collation != nil {
59 if ss.Description().WireVersion.Max < 5 {
60 return result.FindAndModify{}, ErrCollation
61 }
62 cmd.Opts = append(cmd.Opts, bsonx.Elem{"collation", bsonx.Document(ro.Collation.ToDocument())})
63 }
64 if ro.MaxTime != nil {
65 cmd.Opts = append(cmd.Opts, bsonx.Elem{"maxTimeMS", bsonx.Int64(int64(*ro.MaxTime / time.Millisecond))})
66 }
67 if ro.Projection != nil {
68 maxElem, err := interfaceToElement("fields", ro.Projection, registry)
69 if err != nil {
70 return result.FindAndModify{}, err
71 }
72
73 cmd.Opts = append(cmd.Opts, maxElem)
74 }
75 if ro.ReturnDocument != nil {
76 cmd.Opts = append(cmd.Opts, bsonx.Elem{"new", bsonx.Boolean(*ro.ReturnDocument == options.After)})
77 }
78 if ro.Sort != nil {
79 sortElem, err := interfaceToElement("sort", ro.Sort, registry)
80 if err != nil {
81 return result.FindAndModify{}, err
82 }
83
84 cmd.Opts = append(cmd.Opts, sortElem)
85 }
86 if ro.Upsert != nil {
87 cmd.Opts = append(cmd.Opts, bsonx.Elem{"upsert", bsonx.Boolean(*ro.Upsert)})
88 }
89
90 // Execute in a single trip if retry writes not supported, or retry not enabled
91 if !retrySupported(topo, ss.Description(), cmd.Session, cmd.WriteConcern) || !retryWrite {
92 if cmd.Session != nil {
93 cmd.Session.RetryWrite = false // explicitly set to false to prevent encoding transaction number
94 }
95 return findOneAndReplace(ctx, cmd, ss, nil)
96 }
97
98 cmd.Session.RetryWrite = retryWrite
99 cmd.Session.IncrementTxnNumber()
100
101 res, originalErr := findOneAndReplace(ctx, cmd, ss, nil)
102
103 // Retry if appropriate
104 if cerr, ok := originalErr.(command.Error); ok && cerr.Retryable() {
105 ss, err := topo.SelectServer(ctx, selector)
106
107 // Return original error if server selection fails or new server does not support retryable writes
108 if err != nil || !retrySupported(topo, ss.Description(), cmd.Session, cmd.WriteConcern) {
109 return result.FindAndModify{}, originalErr
110 }
111
112 return findOneAndReplace(ctx, cmd, ss, cerr)
113 }
114
115 return res, originalErr
116}
117
118func findOneAndReplace(
119 ctx context.Context,
120 cmd command.FindOneAndReplace,
121 ss *topology.SelectedServer,
122 oldErr error,
123) (result.FindAndModify, error) {
124 desc := ss.Description()
125 conn, err := ss.Connection(ctx)
126 if err != nil {
127 if oldErr != nil {
128 return result.FindAndModify{}, oldErr
129 }
130 return result.FindAndModify{}, err
131 }
132
133 if !writeconcern.AckWrite(cmd.WriteConcern) {
134 go func() {
135 defer func() { _ = recover() }()
136 defer conn.Close()
137
138 _, _ = cmd.RoundTrip(ctx, desc, conn)
139 }()
140
141 return result.FindAndModify{}, command.ErrUnacknowledgedWrite
142 }
143 defer conn.Close()
144
145 return cmd.RoundTrip(ctx, desc, conn)
146}