Skip to content

Commit

Permalink
Don't use cluster.err
Browse files Browse the repository at this point in the history
  • Loading branch information
deafgoat committed Dec 10, 2014
1 parent 4890377 commit c50bc8e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 35 deletions.
16 changes: 3 additions & 13 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,12 @@ type mongoCluster struct {
direct bool
failFast bool
syncCount uint
err error
setName string
cachedIndex map[string]bool
sync chan bool
dial dialer
}

var (
ErrNoReachableServers = errors.New("no reachable servers")
)

func newCluster(userSeeds []string, direct, failFast bool, dial dialer, setName string) *mongoCluster {
cluster := &mongoCluster{
userSeeds: userSeeds,
Expand Down Expand Up @@ -208,8 +203,7 @@ func (cluster *mongoCluster) syncServer(server *mongoServer) (info *mongoServerI

if cluster.setName != "" && result.SetName != cluster.setName {
log("SYNC Server ", addr, " not a member of replica set ", cluster.setName)
cluster.err = errors.New(addr + " is not part of " + cluster.setName + " replica set")
return nil, nil, cluster.err
return nil, nil, errors.New(addr + " is not part of " + cluster.setName + " replica set")
}

if result.IsMaster {
Expand Down Expand Up @@ -270,9 +264,8 @@ func (cluster *mongoCluster) addServer(server *mongoServer, info *mongoServerInf
cluster.Unlock()
server.Close()
return
} else {
cluster.servers.Add(server)
}
cluster.servers.Add(server)
if info.Master {
cluster.masters.Add(server)
log("SYNC Adding ", server.Addr, " to cluster as a master.")
Expand Down Expand Up @@ -561,10 +554,7 @@ func (cluster *mongoCluster) AcquireSocket(slaveOk bool, syncTimeout time.Durati
syncCount = cluster.syncCount
} else if syncTimeout != 0 && started.Before(time.Now().Add(-syncTimeout)) || cluster.failFast && cluster.syncCount != syncCount {
cluster.RUnlock()
if cluster.err != nil {
return nil, cluster.err
}
return nil, ErrNoReachableServers
return nil, errors.New("no reachable servers")
}
log("Waiting for servers to synchronize...")
cluster.syncServers()
Expand Down
42 changes: 20 additions & 22 deletions cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ func (s *S) TestSyncTimeout(c *C) {
// Do something.
result := struct{ Ok bool }{}
err = session.Run("getLastError", &result)
c.Assert(err, Equals, mgo.ErrNoReachableServers)
c.Assert(err, ErrorMatches, "no reachable servers")
c.Assert(started.Before(time.Now().Add(-timeout)), Equals, true)
c.Assert(started.After(time.Now().Add(-timeout*2)), Equals, true)
}
Expand All @@ -832,7 +832,7 @@ func (s *S) TestDialWithTimeout(c *C) {
if session != nil {
session.Close()
}
c.Assert(err, Equals, mgo.ErrNoReachableServers)
c.Assert(err, ErrorMatches, "no reachable servers")
c.Assert(session, IsNil)
c.Assert(started.Before(time.Now().Add(-timeout)), Equals, true)
c.Assert(started.After(time.Now().Add(-timeout*2)), Equals, true)
Expand Down Expand Up @@ -875,7 +875,7 @@ func (s *S) TestSocketTimeoutOnDial(c *C) {
started := time.Now()

session, err := mgo.DialWithTimeout("localhost:40001", timeout)
c.Assert(err, Equals, mgo.ErrNoReachableServers)
c.Assert(err, ErrorMatches, "no reachable servers")
c.Assert(session, IsNil)

c.Assert(started.Before(time.Now().Add(-timeout)), Equals, true)
Expand Down Expand Up @@ -963,8 +963,6 @@ func (s *S) TestDialWithKnownReplSecondary(c *C) {
runTest(session, err)
}

var foreignMemberErrorRegex = ".*not part of.*"

func (s *S) TestDialWithForeignReplPrimary(c *C) {
if *fast {
c.Skip("-fast")
Expand All @@ -977,19 +975,19 @@ func (s *S) TestDialWithForeignReplPrimary(c *C) {
ReplicaSetName: "rs1",
}
_, err := mgo.DialWithInfo(&info)
c.Assert(err, ErrorMatches, foreignMemberErrorRegex)
c.Assert(err, ErrorMatches, "no reachable servers")

info.Direct = true
_, err = mgo.DialWithInfo(&info)
c.Assert(err, ErrorMatches, foreignMemberErrorRegex)
c.Assert(err, ErrorMatches, "no reachable servers")

connectionUrl := "mongodb://localhost:40021/?replicaSet=rs1"
_, err = mgo.Dial(connectionUrl)
c.Assert(err, ErrorMatches, foreignMemberErrorRegex)
c.Assert(err, ErrorMatches, "no reachable servers")

connectionUrl += "&connect=direct"
_, err = mgo.Dial(connectionUrl)
c.Assert(err, ErrorMatches, foreignMemberErrorRegex)
c.Assert(err, ErrorMatches, "no reachable servers")
}

func (s *S) TestDialWithForeignReplSecondary(c *C) {
Expand All @@ -1004,19 +1002,19 @@ func (s *S) TestDialWithForeignReplSecondary(c *C) {
ReplicaSetName: "rs1",
}
_, err := mgo.DialWithInfo(&info)
c.Assert(err, ErrorMatches, foreignMemberErrorRegex)
c.Assert(err, ErrorMatches, "no reachable servers")

info.Direct = true
_, err = mgo.DialWithInfo(&info)
c.Assert(err, ErrorMatches, foreignMemberErrorRegex)
c.Assert(err, ErrorMatches, "no reachable servers")

connectionUrl := "mongodb://localhost:40022/?replicaSet=rs1"
_, err = mgo.Dial(connectionUrl)
c.Assert(err, ErrorMatches, foreignMemberErrorRegex)
c.Assert(err, ErrorMatches, "no reachable servers")

connectionUrl += "&connect=direct"
_, err = mgo.Dial(connectionUrl)
c.Assert(err, ErrorMatches, foreignMemberErrorRegex)
c.Assert(err, ErrorMatches, "no reachable servers")
}

func (s *S) TestDialWithMixedPrimaries(c *C) {
Expand Down Expand Up @@ -1091,19 +1089,19 @@ func (s *S) TestDialWithForeignSeeds(c *C) {
}

_, err := mgo.DialWithInfo(&info)
c.Assert(err, ErrorMatches, foreignMemberErrorRegex)
c.Assert(err, ErrorMatches, "no reachable servers")

info.Direct = true
_, err = mgo.DialWithInfo(&info)
c.Assert(err, ErrorMatches, foreignMemberErrorRegex)
c.Assert(err, ErrorMatches, "no reachable servers")

connectionUrl := "mongodb://localhost:40021,localhost:40022/?replicaSet=rs1"
_, err = mgo.Dial(connectionUrl)
c.Assert(err, ErrorMatches, foreignMemberErrorRegex)
c.Assert(err, ErrorMatches, "no reachable servers")

connectionUrl += "&connect=direct"
_, err = mgo.Dial(connectionUrl)
c.Assert(err, ErrorMatches, foreignMemberErrorRegex)
c.Assert(err, ErrorMatches, "no reachable servers")
}

func (s *S) TestDialWithUnknownSeeds(c *C) {
Expand All @@ -1118,11 +1116,11 @@ func (s *S) TestDialWithUnknownSeeds(c *C) {
}

_, err := mgo.DialWithInfo(&info)
c.Assert(err, Equals, mgo.ErrNoReachableServers)
c.Assert(err, ErrorMatches, "no reachable servers")

connectionUrl := "mongodb://localhost:54321,localhost:12345/?replicaSet=rs1"
_, err = mgo.Dial(connectionUrl)
c.Assert(err, Equals, mgo.ErrNoReachableServers)
c.Assert(err, ErrorMatches, "no reachable servers")
}

func (s *S) TestDirect(c *C) {
Expand All @@ -1148,7 +1146,7 @@ func (s *S) TestDirect(c *C) {

coll := session.DB("mydb").C("mycoll")
err = coll.Insert(M{"test": 1})
c.Assert(err, Equals, mgo.ErrNoReachableServers)
c.Assert(err, ErrorMatches, "no reachable servers")

// Writing to the local database is okay.
coll = session.DB("local").C("mycoll")
Expand Down Expand Up @@ -1186,7 +1184,7 @@ func (s *S) TestDirectToUnknownStateMember(c *C) {

coll := session.DB("mydb").C("mycoll")
err = coll.Insert(M{"test": 1})
c.Assert(err, Equals, mgo.ErrNoReachableServers)
c.Assert(err, ErrorMatches, "no reachable servers")

// Slave is still reachable.
result.Host = ""
Expand All @@ -1205,7 +1203,7 @@ func (s *S) TestFailFast(c *C) {
started := time.Now()

_, err := mgo.DialWithInfo(&info)
c.Assert(err, Equals, mgo.ErrNoReachableServers)
c.Assert(err, ErrorMatches, "no reachable servers")

c.Assert(started.After(time.Now().Add(-time.Second)), Equals, true)
}
Expand Down

0 comments on commit c50bc8e

Please sign in to comment.