Skip to content

Commit 9ec4480

Browse files
author
iwysiu
committed
GODRIVER-1099 Change RetryType to Type
Change-Id: Ib04da47943a4113b20190c021c7cd921b58519be
1 parent a38aab5 commit 9ec4480

File tree

12 files changed

+65
-66
lines changed

12 files changed

+65
-66
lines changed

x/mongo/driver/connstring/connstring_spec_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,20 @@ func runTestsInFile(t *testing.T, dirname string, filename string, warningsError
9696
}
9797

9898
var skipTest = map[string]struct{}{
99-
"tlsAllowInvalidHostnames and tlsInsecure both present (and false) raises an error": struct{}{},
100-
"tlsAllowInvalidHostnames and tlsInsecure both present (and true) raises an error": struct{}{},
101-
"tlsInsecure and tlsAllowInvalidHostnames both present (and false) raises an error": struct{}{},
102-
"tlsInsecure and tlsAllowInvalidHostnames both present (and true) raises an error": struct{}{},
103-
"tlsAllowInvalidCertificates and tlsInsecure both present (and false) raises an error": struct{}{},
104-
"tlsAllowInvalidCertificates and tlsInsecure both present (and true) raises an error": struct{}{},
105-
"tlsInsecure and tlsAllowInvalidCertificates both present (and false) raises an error": struct{}{},
106-
"tlsInsecure and tlsAllowInvalidCertificates both present (and true) raises an error": struct{}{},
107-
"Invalid tlsAllowInvalidHostnames causes a warning": struct{}{},
108-
"tlsAllowInvalidHostnames is parsed correctly": struct{}{},
109-
"Invalid tlsAllowInvalidCertificates causes a warning": struct{}{},
110-
"tlsAllowInvalidCertificates is parsed correctly": struct{}{},
111-
"Invalid serverSelectionTryOnce causes a warning": struct{}{},
112-
"Valid options specific to single-threaded drivers are parsed correctly": struct{}{},
99+
"tlsAllowInvalidHostnames and tlsInsecure both present (and false) raises an error": {},
100+
"tlsAllowInvalidHostnames and tlsInsecure both present (and true) raises an error": {},
101+
"tlsInsecure and tlsAllowInvalidHostnames both present (and false) raises an error": {},
102+
"tlsInsecure and tlsAllowInvalidHostnames both present (and true) raises an error": {},
103+
"tlsAllowInvalidCertificates and tlsInsecure both present (and false) raises an error": {},
104+
"tlsAllowInvalidCertificates and tlsInsecure both present (and true) raises an error": {},
105+
"tlsInsecure and tlsAllowInvalidCertificates both present (and false) raises an error": {},
106+
"tlsInsecure and tlsAllowInvalidCertificates both present (and true) raises an error": {},
107+
"Invalid tlsAllowInvalidHostnames causes a warning": {},
108+
"tlsAllowInvalidHostnames is parsed correctly": {},
109+
"Invalid tlsAllowInvalidCertificates causes a warning": {},
110+
"tlsAllowInvalidCertificates is parsed correctly": {},
111+
"Invalid serverSelectionTryOnce causes a warning": {},
112+
"Valid options specific to single-threaded drivers are parsed correctly": {},
113113
}
114114

115115
func runTest(t *testing.T, filename string, test *testCase, warningsError bool) {

x/mongo/driver/driver.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,20 @@ type nopCloserConnection struct{ Connection }
120120

121121
func (ncc nopCloserConnection) Close() error { return nil }
122122

123-
// TODO(GODRUVER-617): We can likely use 1 type for both the RetryType and the RetryMode by using
123+
// TODO(GODRIVER-617): We can likely use 1 type for both the Type and the RetryMode by using
124124
// 2 bits for the mode and 1 bit for the type. Although in the practical sense, we might not want to
125125
// do that since the type of retryability is tied to the operation itself and isn't going change,
126126
// e.g. and insert operation will always be a write, however some operations are both reads and
127127
// writes, for instance aggregate is a read but with a $out parameter it's a write.
128128

129-
// RetryType specifies whether a retry is a read, write, or disabled.
130-
type RetryType uint
129+
// Type specifies whether an operation is a read, write, or unknown.
130+
type Type uint
131131

132-
// THese are the availables types of retry.
132+
// THese are the availables types of Type.
133133
const (
134-
_ RetryType = iota
135-
RetryWrite
136-
RetryRead
134+
_ Type = iota
135+
Write
136+
Read
137137
)
138138

139139
// RetryMode specifies the way that retries are handled for retryable operations.

x/mongo/driver/drivergen/drivergen.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,17 @@ func (op Operation) CommandMethod() (string, error) {
154154
return "", fmt.Errorf("unknown request field type %s", field.Type)
155155
}
156156
var rf struct {
157-
ShortName string
158-
Name string
159-
ParameterName string
157+
ShortName string
158+
Name string
159+
ParameterName string
160+
MinWireVersion int
161+
MinWireVersionRequired int
160162
}
161163
rf.ShortName = op.ShortName()
162164
rf.Name = op.Command.Parameter
163165
rf.ParameterName = op.Command.Name
166+
rf.MinWireVersion = field.MinWireVersion
167+
rf.MinWireVersionRequired = field.MinWireVersionRequired
164168
err := tmpl.Execute(&buf, rf)
165169
if err != nil {
166170
return "", err
@@ -173,7 +177,7 @@ func (op Operation) CommandMethod() (string, error) {
173177
sort.Strings(names)
174178
for _, name := range names {
175179
field := op.Request[name]
176-
if name == op.Properties.Batches {
180+
if name == op.Properties.Batches || field.Skip {
177181
continue
178182
}
179183
var tmpl *template.Template
@@ -538,6 +542,7 @@ type RequestField struct {
538542
Slice bool
539543
Constructor bool
540544
Variadic bool
545+
Skip bool
541546
Documentation string
542547
MinWireVersion int
543548
MinWireVersionRequired int

x/mongo/driver/drivergen/templates/operation.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func ({{$.ShortName}} *{{$.Name}}) Execute(ctx context.Context) error {
179179
{{- end -}}
180180

181181
{{- if eq $.Properties.Retryable.Type "writes"}}
182-
RetryType: driver.RetryWrite,
182+
Type: driver.Write,
183183
{{- end -}}
184184

185185
{{- range $b := $.Properties.ExecuteBuiltins}}

x/mongo/driver/operation.go

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,13 @@ type Operation struct {
148148

149149
// RetryMode specifies how to retry. There are three modes that enable retry: RetryOnce,
150150
// RetryOncePerCommand, and RetryContext. For more information about what these modes do, please
151-
// refer to their definitions. Both RetryMode and RetryType must be set for retryability to be
152-
// enabled.
151+
// refer to their definitions. Both RetryMode and Type must be set for retryability to be enabled.
153152
RetryMode *RetryMode
154153

155-
// RetryType specifies the kinds of operations that can be retried. There is only one mode that
156-
// enables retry: RetryWrites. For more information about what this mode does, please refer to
157-
// it's definition. Both RetryType and RetryMode must be set for retryability to be enabled.
158-
RetryType RetryType
154+
// Type specifies the kind of operation this is. There is only one mode that enables retry: Write.
155+
// For more information about what this mode does, please refer to it's definition. Both Type and
156+
// RetryMode must be set for retryability to be enabled.
157+
Type Type
159158

160159
// Batches contains the documents that are split when executing a write command that potentially
161160
// has more documents than can fit in a single command. This should only be specified for
@@ -264,7 +263,7 @@ func (op Operation) Execute(ctx context.Context, scratch []byte) error {
264263
var retries int
265264
// TODO(GODRIVER-617): Add support for retryable reads.
266265
retryable := op.retryable(desc.Server)
267-
if retryable == RetryWrite && op.Client != nil && op.RetryMode != nil {
266+
if retryable == Write && op.Client != nil && op.RetryMode != nil {
268267
op.Client.RetryWrite = false
269268
if *op.RetryMode > RetryNone {
270269
op.Client.RetryWrite = true
@@ -350,7 +349,7 @@ func (op Operation) Execute(ctx context.Context, scratch []byte) error {
350349

351350
switch tt := err.(type) {
352351
case WriteCommandError:
353-
if retryable == RetryWrite && tt.Retryable() && retries != 0 {
352+
if retryable == Write && tt.Retryable() && retries != 0 {
354353
retries--
355354
original, err = err, nil
356355
conn.Close() // Avoid leaking the connection.
@@ -359,7 +358,7 @@ func (op Operation) Execute(ctx context.Context, scratch []byte) error {
359358
return original
360359
}
361360
conn, err = srvr.Connection(ctx)
362-
if err != nil || conn == nil || op.retryable(conn.Description()) != RetryWrite {
361+
if err != nil || conn == nil || op.retryable(conn.Description()) != Write {
363362
if conn != nil {
364363
conn.Close()
365364
}
@@ -396,7 +395,7 @@ func (op Operation) Execute(ctx context.Context, scratch []byte) error {
396395
if tt.HasErrorLabel(TransientTransactionError) || tt.HasErrorLabel(UnknownTransactionCommitResult) {
397396
op.Client.ClearPinnedServer()
398397
}
399-
if retryable == RetryWrite && tt.Retryable() && retries != 0 {
398+
if retryable == Write && tt.Retryable() && retries != 0 {
400399
retries--
401400
original, err = err, nil
402401
conn.Close() // Avoid leaking the connection.
@@ -405,7 +404,7 @@ func (op Operation) Execute(ctx context.Context, scratch []byte) error {
405404
return original
406405
}
407406
conn, err = srvr.Connection(ctx)
408-
if err != nil || conn == nil || op.retryable(conn.Description()) != RetryWrite {
407+
if err != nil || conn == nil || op.retryable(conn.Description()) != Write {
409408
if conn != nil {
410409
conn.Close()
411410
}
@@ -436,7 +435,7 @@ func (op Operation) Execute(ctx context.Context, scratch []byte) error {
436435
}
437436

438437
if batching && len(op.Batches.Documents) > 0 {
439-
if retryable == RetryWrite && op.Client != nil && op.RetryMode != nil {
438+
if retryable == Write && op.Client != nil && op.RetryMode != nil {
440439
if *op.RetryMode > RetryNone {
441440
op.Client.IncrementTxnNumber()
442441
}
@@ -457,20 +456,20 @@ func (op Operation) Execute(ctx context.Context, scratch []byte) error {
457456

458457
// Retryable writes are supported if the server supports sessions, the operation is not
459458
// within a transaction, and the write is acknowledged
460-
func (op Operation) retryable(desc description.Server) RetryType {
461-
switch op.RetryType {
462-
case RetryWrite:
459+
func (op Operation) retryable(desc description.Server) Type {
460+
switch op.Type {
461+
case Write:
463462
if op.Client != nil && (op.Client.Committing || op.Client.Aborting) {
464-
return RetryWrite
463+
return Write
465464
}
466465
if op.Deployment.SupportsRetry() &&
467466
description.SessionsSupported(desc.WireVersion) &&
468467
op.Client != nil && !(op.Client.TransactionInProgress() || op.Client.TransactionStarting()) &&
469468
writeconcern.AckWrite(op.WriteConcern) {
470-
return RetryWrite
469+
return Write
471470
}
472471
}
473-
return RetryType(0)
472+
return Type(0)
474473
}
475474

476475
// roundTrip writes a wiremessage to the connection and then reads a wiremessage. The wm parameter
@@ -811,7 +810,7 @@ func (op Operation) addSession(dst []byte, desc description.SelectedServer) ([]b
811810
dst = bsoncore.AppendDocumentElement(dst, "lsid", lsid)
812811

813812
var addedTxnNumber bool
814-
if op.RetryType == RetryWrite && client != nil && client.RetryWrite {
813+
if op.Type == Write && client != nil && client.RetryWrite {
815814
addedTxnNumber = true
816815
dst = bsoncore.AppendInt64Element(dst, "txnNumber", op.Client.TxnNumber)
817816
}
@@ -896,11 +895,6 @@ func (op Operation) updateOperationTime(response bsoncore.Document) {
896895
}
897896

898897
func (op Operation) getReadPrefBasedOnTransaction() (*readpref.ReadPref, error) {
899-
// don't validate read preference for write commands
900-
if op.RetryType == RetryWrite {
901-
return op.ReadPreference, nil
902-
}
903-
904898
if op.Client != nil && op.Client.TransactionRunning() {
905899
// Transaction's read preference always takes priority
906900
rp := op.Client.CurrentRp
@@ -915,7 +909,7 @@ func (op Operation) getReadPrefBasedOnTransaction() (*readpref.ReadPref, error)
915909
}
916910

917911
func (op Operation) createReadPref(serverKind description.ServerKind, topologyKind description.TopologyKind, isOpQuery bool) (bsoncore.Document, error) {
918-
if serverKind == description.Standalone || (isOpQuery && serverKind != description.Mongos) {
912+
if serverKind == description.Standalone || (isOpQuery && serverKind != description.Mongos) || op.Type == Write {
919913
// Don't send read preference for non-mongos when using OP_QUERY or for all standalones
920914
return nil, nil
921915
}

x/mongo/driver/operation/abort_transaction.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x/mongo/driver/operation/commit_transaction.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x/mongo/driver/operation/delete.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x/mongo/driver/operation/find_and_modify.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x/mongo/driver/operation/insert.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)