Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyang-hu committed Sep 28, 2023
1 parent 3ce688d commit 5b1da45
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 5 deletions.
2 changes: 2 additions & 0 deletions mongo/integration/cmd_monitoring_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ func checkExpectations(mt *mtest.T, expectations *[]*expectation, id0, id1 bson.
var err error

if expectation.CommandStartedEvent != nil {
fmt.Println(expectation.CommandStartedEvent)
err = compareStartedEvent(mt, expectation, id0, id1)
}
if expectation.CommandSucceededEvent != nil {
Expand Down Expand Up @@ -356,6 +357,7 @@ func compareStartedEvent(mt *mtest.T, expectation *expectation, id0, id1 bson.Ra
actualID)
}
default:
fmt.Println(key, val.String(), actualVal.String())
if err := compareValues(mt, key, val, actualVal); err != nil {
return newMatchError(mt, expected.Command, evt.Command, "%s", err)
}
Expand Down
2 changes: 2 additions & 0 deletions mongo/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package mongo
import (
"context"
"errors"
"fmt"
"time"

"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -357,6 +358,7 @@ func (s *sessionImpl) CommitTransaction(ctx context.Context) error {
commitErr := s.clientSession.CommitTransaction()

// We set the write concern to majority for subsequent calls to CommitTransaction.
fmt.Println("========== CommitTransaction", s.clientSession.CurrentWc, "==========")
s.clientSession.UpdateCommitTransactionWriteConcern()

if err != nil {
Expand Down
130 changes: 130 additions & 0 deletions mongo/writeconcern/writeconcern.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,133 @@ func (wc *WriteConcern) IsValid() bool {
return false
}
}

// Option is an option to provide when creating a WriteConcern.
//
// Deprecated: Use the WriteConcern convenience functions or define a struct literal instead.
// For example:
//
// writeconcern.Majority()
//
// or
//
// journal := true
// &writeconcern.WriteConcern{
// W: 2,
// Journal: &journal,
// }
type Option func(concern *WriteConcern)

// New constructs a new WriteConcern.
//
// Deprecated: Use the WriteConcern convenience functions or define a struct literal instead.
// For example:
//
// writeconcern.Majority()
//
// or
//
// journal := true
// &writeconcern.WriteConcern{
// W: 2,
// Journal: &journal,
// }
func New(options ...Option) *WriteConcern {
concern := &WriteConcern{}

for _, option := range options {
option(concern)
}

return concern
}

// W requests acknowledgement that write operations propagate to the specified number of mongod
// instances.
//
// Deprecated: Use the Unacknowledged or W1 functions or define a struct literal instead.
// For example:
//
// writeconcern.Unacknowledged()
//
// or
//
// journal := true
// &writeconcern.WriteConcern{
// W: 2,
// Journal: &journal,
// }
func W(w int) Option {
return func(concern *WriteConcern) {
concern.W = w
}
}

// WMajority requests acknowledgement that write operations propagate to the majority of mongod
// instances.
//
// Deprecated: Use [Majority] instead.
func WMajority() Option {
return func(concern *WriteConcern) {
concern.W = majority
}
}

// WithOptions returns a copy of this WriteConcern with the options set.
//
// Deprecated: Use the WriteConcern convenience functions or define a struct literal instead.
// For example:
//
// writeconcern.Majority()
//
// or
//
// journal := true
// &writeconcern.WriteConcern{
// W: 2,
// Journal: &journal,
// }
func (wc *WriteConcern) WithOptions(options ...Option) *WriteConcern {
if wc == nil {
return New(options...)
}
newWC := &WriteConcern{}
*newWC = *wc

for _, option := range options {
option(newWC)
}

return newWC
}

// WTimeout specifies a time limit for the write concern.
//
// It is only applicable for "w" values greater than 1. Using a WTimeout and setting Timeout on the
// Client at the same time will result in undefined behavior.
//
// Deprecated: Use the WriteConcern convenience functions or define a struct literal instead.
// For example:
//
// wc := writeconcern.W1()
// wc.WTimeout = 30 * time.Second
//
// or
//
// journal := true
// &writeconcern.WriteConcern{
// W: "majority",
// WTimeout: 30 * time.Second,
// }
func WTimeout(d time.Duration) Option {
return func(concern *WriteConcern) {
concern.WTimeout = d
}
}

// GetWTimeout returns the write concern timeout.
//
// Deprecated: Use the WriteConcern.WTimeout field instead.
func (wc *WriteConcern) GetWTimeout() time.Duration {
return wc.WTimeout
}
17 changes: 12 additions & 5 deletions x/mongo/driver/session/client_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,13 +465,20 @@ func (c *Client) CommitTransaction() error {
// w timeout of 10 seconds. This should be called after a commit transaction operation fails with a
// retryable error or after a successful commit transaction operation.
func (c *Client) UpdateCommitTransactionWriteConcern() {
if c.CurrentWc == nil {
c.CurrentWc = &writeconcern.WriteConcern{}
wc := c.CurrentWc
timeout := 10 * time.Second
if wc != nil && wc.WTimeout != 0 {
timeout = wc.WTimeout
}
c.CurrentWc.W = "majority"
if c.CurrentWc.WTimeout == 0 {
c.CurrentWc.WTimeout = 10 * time.Second
if wc == nil {
wc = &writeconcern.WriteConcern{}
}
newWC := &writeconcern.WriteConcern{}
*newWC = *wc

newWC.W = "majority"
newWC.WTimeout = timeout
c.CurrentWc = newWC
}

// CheckAbortTransaction checks to see if allowed to abort transaction and returns
Expand Down

0 comments on commit 5b1da45

Please sign in to comment.