Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add read concern configuration to Safe{} #2

Merged
merged 2 commits into from
Apr 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: go

go_import_path: github.com/domodwyer/mgo
go_import_path: github.com/globalsign/mgo

addons:
apt:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Build Status](https://travis-ci.org/domodwyer/mgo.svg?branch=master)](https://travis-ci.org/domodwyer/mgo) [![GoDoc](https://godoc.org/github.com/domodwyer/mgo?status.svg)](https://godoc.org/github.com/domodwyer/mgo)
[![Build Status](https://travis-ci.org/domodwyer/mgo.svg?branch=master)](https://travis-ci.org/domodwyer/mgo) [![GoDoc](https://godoc.org/github.com/globalsign/mgo?status.svg)](https://godoc.org/github.com/globalsign/mgo)

The MongoDB driver for Go
-------------------------
Expand Down
4 changes: 2 additions & 2 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import (
"fmt"
"sync"

"github.com/domodwyer/mgo/bson"
"github.com/domodwyer/mgo/internal/scram"
"github.com/globalsign/mgo/bson"
"github.com/globalsign/mgo/internal/scram"
)

type authCmd struct {
Expand Down
2 changes: 1 addition & 1 deletion auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
"sync"
"time"

mgo "github.com/domodwyer/mgo"
mgo "github.com/globalsign/mgo"
. "gopkg.in/check.v1"
)

Expand Down
2 changes: 1 addition & 1 deletion bson/bson_corpus_spec_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions bson/bson_corpus_spec_test_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"path/filepath"
"strings"

"github.com/domodwyer/mgo/internal/json"
"github.com/globalsign/mgo/internal/json"
)

func main() {
Expand Down Expand Up @@ -162,7 +162,7 @@ import (
"time"

. "gopkg.in/check.v1"
"github.com/domodwyer/mgo/bson"
"github.com/globalsign/mgo/bson"
)

func testValid(c *C, in []byte, expected []byte, result interface{}) {
Expand Down
2 changes: 1 addition & 1 deletion bson/bson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
"testing"
"time"

"github.com/domodwyer/mgo/bson"
"github.com/globalsign/mgo/bson"
. "gopkg.in/check.v1"
)

Expand Down
2 changes: 1 addition & 1 deletion bson/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"regexp"
"strings"

"github.com/domodwyer/mgo/bson"
"github.com/globalsign/mgo/bson"

. "gopkg.in/check.v1"
)
Expand Down
2 changes: 1 addition & 1 deletion bson/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strconv"
"time"

"github.com/domodwyer/mgo/internal/json"
"github.com/globalsign/mgo/internal/json"
)

// UnmarshalJSON unmarshals a JSON value that may hold non-standard
Expand Down
2 changes: 1 addition & 1 deletion bson/json_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package bson_test

import (
"github.com/domodwyer/mgo/bson"
"github.com/globalsign/mgo/bson"

"reflect"
"strings"
Expand Down
2 changes: 1 addition & 1 deletion bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"sort"

"github.com/domodwyer/mgo/bson"
"github.com/globalsign/mgo/bson"
)

// Bulk represents an operation that can be prepared with several
Expand Down
2 changes: 1 addition & 1 deletion bulk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
package mgo_test

import (
mgo "github.com/domodwyer/mgo"
mgo "github.com/globalsign/mgo"
. "gopkg.in/check.v1"
)

Expand Down
2 changes: 1 addition & 1 deletion cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
"sync"
"time"

"github.com/domodwyer/mgo/bson"
"github.com/globalsign/mgo/bson"
)

// ---------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import (
"sync"
"time"

mgo "github.com/domodwyer/mgo"
"github.com/domodwyer/mgo/bson"
mgo "github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
. "gopkg.in/check.v1"
)

Expand Down
2 changes: 1 addition & 1 deletion dbtest/dbserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strconv"
"time"

mgo "github.com/domodwyer/mgo"
mgo "github.com/globalsign/mgo"
"gopkg.in/tomb.v2"
)

Expand Down
4 changes: 2 additions & 2 deletions dbtest/dbserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"testing"
"time"

mgo "github.com/domodwyer/mgo"
mgo "github.com/globalsign/mgo"
. "gopkg.in/check.v1"

"github.com/domodwyer/mgo/dbtest"
"github.com/globalsign/mgo/dbtest"
)

type M map[string]interface{}
Expand Down
2 changes: 1 addition & 1 deletion gridfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
"sync"
"time"

"github.com/domodwyer/mgo/bson"
"github.com/globalsign/mgo/bson"
)

type GridFS struct {
Expand Down
4 changes: 2 additions & 2 deletions gridfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import (
"os"
"time"

mgo "github.com/domodwyer/mgo"
"github.com/domodwyer/mgo/bson"
mgo "github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
. "gopkg.in/check.v1"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/scram/scram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"strings"

"github.com/domodwyer/mgo/internal/scram"
"github.com/globalsign/mgo/internal/scram"
. "gopkg.in/check.v1"
)

Expand Down
2 changes: 1 addition & 1 deletion saslimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package mgo

import (
"github.com/domodwyer/mgo/internal/sasl"
"github.com/globalsign/mgo/internal/sasl"
)

func saslNew(cred Credential, host string) (saslStepper, error) {
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"sync"
"time"

"github.com/domodwyer/mgo/bson"
"github.com/globalsign/mgo/bson"
)

// ---------------------------------------------------------------------------
Expand Down
24 changes: 21 additions & 3 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
"sync"
"time"

"github.com/domodwyer/mgo/bson"
"github.com/globalsign/mgo/bson"
)

type Mode int
Expand Down Expand Up @@ -1889,6 +1889,7 @@ func (s *Session) SetPrefetch(p float64) {
type Safe struct {
W int // Min # of servers to ack before success
WMode string // Write mode for MongoDB 2.0+ (e.g. "majority")
RMode string // Read mode for MonogDB 3.2+ ("majority", "local", "linearizable")
WTimeout int // Milliseconds to wait for W before timing out
FSync bool // Sync via the journal if present, or via data files sync otherwise
J bool // Sync via the journal if present
Expand All @@ -1900,7 +1901,7 @@ func (s *Session) Safe() (safe *Safe) {
defer s.m.Unlock()
if s.safeOp != nil {
cmd := s.safeOp.query.(*getLastError)
safe = &Safe{WTimeout: cmd.WTimeout, FSync: cmd.FSync, J: cmd.J}
safe = &Safe{WTimeout: cmd.WTimeout, FSync: cmd.FSync, J: cmd.J, RMode: s.queryConfig.op.readConcern}
switch w := cmd.W.(type) {
case string:
safe.WMode = w
Expand Down Expand Up @@ -1980,6 +1981,7 @@ func (s *Session) Safe() (safe *Safe) {
//
// Relevant documentation:
//
// https://docs.mongodb.com/manual/reference/read-concern/
// http://www.mongodb.org/display/DOCS/getLastError+Command
// http://www.mongodb.org/display/DOCS/Verifying+Propagation+of+Writes+with+getLastError
// http://www.mongodb.org/display/DOCS/Data+Center+Awareness
Expand All @@ -1998,6 +2000,7 @@ func (s *Session) SetSafe(safe *Safe) {
// That is:
//
// - safe.WMode is always used if set.
// - safe.RMode is always used if set.
// - safe.W is used if larger than the current W and WMode is empty.
// - safe.FSync is always used if true.
// - safe.J is used if FSync is false.
Expand Down Expand Up @@ -2036,6 +2039,13 @@ func (s *Session) ensureSafe(safe *Safe) {
w = safe.W
}

// Set the read concern
switch safe.RMode {
case "majority", "local", "linearizable":
s.queryConfig.op.readConcern = safe.RMode
default:
}

var cmd getLastError
if s.safeOp == nil {
cmd = getLastError{1, w, safe.WTimeout, safe.FSync, safe.J}
Expand Down Expand Up @@ -3284,7 +3294,9 @@ func prepareFindOp(socket *mongoSocket, op *queryOp, limit int32) bool {
Snapshot: op.options.Snapshot,
OplogReplay: op.flags&flagLogReplay != 0,
Collation: op.options.Collation,
ReadConcern: readLevel{level: op.readConcern},
}

if op.limit < 0 {
find.BatchSize = -op.limit
find.SingleBatch = true
Expand Down Expand Up @@ -3334,7 +3346,7 @@ type findCmd struct {
Comment string `bson:"comment,omitempty"`
MaxScan int `bson:"maxScan,omitempty"`
MaxTimeMS int `bson:"maxTimeMS,omitempty"`
ReadConcern interface{} `bson:"readConcern,omitempty"`
ReadConcern readLevel `bson:"readConcern,omitempty"`
Max interface{} `bson:"max,omitempty"`
Min interface{} `bson:"min,omitempty"`
ReturnKey bool `bson:"returnKey,omitempty"`
Expand All @@ -3348,6 +3360,12 @@ type findCmd struct {
Collation *Collation `bson:"collation,omitempty"`
}

// readLevel provides the nested "level: majority" serialisation needed for the
// query read concern.
type readLevel struct {
level string `bson:"level,omitempty"`
}

// getMoreCmd holds the command used for requesting more query results on MongoDB 3.2+.
//
// Relevant documentation:
Expand Down
24 changes: 22 additions & 2 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ import (
"testing"
"time"

mgo "github.com/domodwyer/mgo"
"github.com/domodwyer/mgo/bson"
mgo "github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
. "gopkg.in/check.v1"
)

Expand Down Expand Up @@ -2781,6 +2781,7 @@ func (s *S) TestSafeSetting(c *C) {
safe := session.Safe()
c.Assert(safe.W, Equals, 0)
c.Assert(safe.WMode, Equals, "")
c.Assert(safe.RMode, Equals, "")
c.Assert(safe.WTimeout, Equals, 0)
c.Assert(safe.FSync, Equals, false)
c.Assert(safe.J, Equals, false)
Expand All @@ -2790,6 +2791,7 @@ func (s *S) TestSafeSetting(c *C) {
safe = session.Safe()
c.Assert(safe.W, Equals, 1)
c.Assert(safe.WMode, Equals, "")
c.Assert(safe.RMode, Equals, "")
c.Assert(safe.WTimeout, Equals, 2)
c.Assert(safe.FSync, Equals, true)
c.Assert(safe.J, Equals, false)
Expand All @@ -2799,6 +2801,7 @@ func (s *S) TestSafeSetting(c *C) {
safe = session.Safe()
c.Assert(safe.W, Equals, 0)
c.Assert(safe.WMode, Equals, "")
c.Assert(safe.RMode, Equals, "")
c.Assert(safe.WTimeout, Equals, 0)
c.Assert(safe.FSync, Equals, false)
c.Assert(safe.J, Equals, false)
Expand All @@ -2808,6 +2811,7 @@ func (s *S) TestSafeSetting(c *C) {
safe = session.Safe()
c.Assert(safe.W, Equals, 5)
c.Assert(safe.WMode, Equals, "")
c.Assert(safe.RMode, Equals, "")
c.Assert(safe.WTimeout, Equals, 6)
c.Assert(safe.FSync, Equals, false)
c.Assert(safe.J, Equals, true)
Expand All @@ -2817,6 +2821,7 @@ func (s *S) TestSafeSetting(c *C) {
safe = session.Safe()
c.Assert(safe.W, Equals, 5)
c.Assert(safe.WMode, Equals, "")
c.Assert(safe.RMode, Equals, "")
c.Assert(safe.WTimeout, Equals, 6)
c.Assert(safe.FSync, Equals, false)
c.Assert(safe.J, Equals, true)
Expand All @@ -2826,6 +2831,7 @@ func (s *S) TestSafeSetting(c *C) {
safe = session.Safe()
c.Assert(safe.W, Equals, 6)
c.Assert(safe.WMode, Equals, "")
c.Assert(safe.RMode, Equals, "")
c.Assert(safe.WTimeout, Equals, 4)
c.Assert(safe.FSync, Equals, true)
c.Assert(safe.J, Equals, false)
Expand All @@ -2835,6 +2841,17 @@ func (s *S) TestSafeSetting(c *C) {
safe = session.Safe()
c.Assert(safe.W, Equals, 0)
c.Assert(safe.WMode, Equals, "majority")
c.Assert(safe.RMode, Equals, "")
c.Assert(safe.WTimeout, Equals, 2)
c.Assert(safe.FSync, Equals, true)
c.Assert(safe.J, Equals, false)

// Read concern
session.EnsureSafe(&mgo.Safe{RMode: "majority"})
safe = session.Safe()
c.Assert(safe.W, Equals, 0)
c.Assert(safe.WMode, Equals, "majority")
c.Assert(safe.RMode, Equals, "majority")
c.Assert(safe.WTimeout, Equals, 2)
c.Assert(safe.FSync, Equals, true)
c.Assert(safe.J, Equals, false)
Expand All @@ -2844,6 +2861,7 @@ func (s *S) TestSafeSetting(c *C) {
safe = session.Safe()
c.Assert(safe.W, Equals, 0)
c.Assert(safe.WMode, Equals, "something")
c.Assert(safe.RMode, Equals, "majority")
c.Assert(safe.WTimeout, Equals, 2)
c.Assert(safe.FSync, Equals, true)
c.Assert(safe.J, Equals, false)
Expand All @@ -2853,6 +2871,7 @@ func (s *S) TestSafeSetting(c *C) {
safe = session.Safe()
c.Assert(safe.W, Equals, 0)
c.Assert(safe.WMode, Equals, "something")
c.Assert(safe.RMode, Equals, "majority")
c.Assert(safe.WTimeout, Equals, 2)
c.Assert(safe.FSync, Equals, true)
c.Assert(safe.J, Equals, false)
Expand All @@ -2863,6 +2882,7 @@ func (s *S) TestSafeSetting(c *C) {
clone.EnsureSafe(&mgo.Safe{WMode: "foo"})
safe = session.Safe()
c.Assert(safe.WMode, Equals, "something")
c.Assert(safe.RMode, Equals, "majority")
}

func (s *S) TestSafeInsert(c *C) {
Expand Down
Loading