Skip to content
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
7 changes: 7 additions & 0 deletions internal/cluster/is_master.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func NewIsMaster(session *mgo.Session) (*IsMaster, error) {
return &i, err
}

// IsMasterDoc returns the raw response from the MongoDB 'isMaster' server command.
func (i *IsMaster) IsMasterDoc() *mdbstructs.IsMaster {
return i.isMaster
}
Expand Down Expand Up @@ -74,3 +75,9 @@ func (i *IsMaster) IsShardedCluster() bool {
}
return false
}

// LastWrite returns the last write to the MongoDB database as a
// bson.MongoTimestamp
func (i *IsMaster) LastWrite() bson.MongoTimestamp {
return i.isMaster.LastWrite.OpTime.Ts
}
18 changes: 18 additions & 0 deletions internal/cluster/is_master_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"testing"

"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"github.com/percona/mongodb-backup/internal/testutils"
"github.com/percona/mongodb-backup/mdbstructs"
)

func TestNewIsMaster(t *testing.T) {
Expand Down Expand Up @@ -151,3 +153,19 @@ func TestIsMasterIsShardServer(t *testing.T) {
t.Fatalf("Expected false from .IsShardServer()")
}
}

func TestIsMasterLastWrite(t *testing.T) {
ts, _ := bson.NewMongoTimestamp(bson.Now(), 0)
isMaster := &IsMaster{
isMaster: &mdbstructs.IsMaster{
LastWrite: mdbstructs.IsMasterLastWrite{
OpTime: &mdbstructs.OpTime{
Ts: ts,
},
},
},
}
if isMaster.LastWrite() != ts {
t.Fatalf("Got invalid timestamp from .LastWrite()! Expected %v, got %v", ts, isMaster.LastWrite())
}
}
2 changes: 1 addition & 1 deletion internal/oplog/oplog_tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func (ot *OplogTail) tailQuery() bson.M {
return query
}

query["ts"] = bson.M{"$gt": isMaster.IsMasterDoc().LastWrite.OpTime.Ts}
query["ts"] = bson.M{"$gt": isMaster.LastWrite()}
return query
}

Expand Down
59 changes: 31 additions & 28 deletions mdbstructs/is_master.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,37 @@ import (
"github.com/globalsign/mgo/bson"
)

// IsMasterLastWrite represents the last write to the MongoDB server
type IsMasterLastWrite struct {
OpTime *OpTime `bson:"opTime"`
LastWriteDate time.Time `bson:"lastWriteDate"`
MajorityOpTime *OpTime `bson:"majorityTime"`
MajorityWriteDate time.Time `bson:"majorityWriteDate"`
}

// IsMaster represents the document returned by db.runCommand( { isMaster: 1 } )
type IsMaster struct {
Hosts []string `bson:"hosts,omitempty"`
IsMaster bool `bson:"ismaster"`
Msg string `bson:"msg"`
MaxBsonObjectSise int64 `bson:"maxBsonObjectSize"`
MaxMessageSizeBytes int64 `bson:"maxMessageSizeBytes"`
MaxWriteBatchSize int64 `bson:"maxWriteBatchSize"`
LocalTime time.Time `bson:"localTime"`
LogicalSessionTimeoutMinutes int64 `bson:"logicalSessionTimeoutMinutes"`
MaxWireVersion int64 `bson:"maxWireVersion"`
MinWireVersion int64 `bson:"minWireVersion"`
Ok int `bson:"ok"`
SetName string `bson:"setName,omitempty"`
SetVersion string `bson:"setVersion,omitempty"`
Primary string `bson:"primary,omitempty"`
Secondary bool `bson:"secondary,omitempty"`
Hidden bool `bson:"hidden,omitempty"`
ConfigSvr int `bson:"configsvr,omitempty"`
Me string `bson:"me"`
LastWrite struct {
OpTime *OpTime `bson:"opTime"`
LastWriteDate time.Time `bson:"lastWriteDate"`
MajorityOpTime *OpTime `bson:"majorityTime"`
MajorityWriteDate time.Time `bson:"majorityWriteDate"`
} `bson:"lastWrite"`
ClusterTime *ClusterTime `bson:"$clusterTime,omitempty"`
ConfigServerState *ConfigServerState `bson:"$configServerState,omitempty"`
GleStats *GleStats `bson:"$gleStats,omitempty"`
OperationTime *bson.MongoTimestamp `bson:"operationTime,omitempty"`
Hosts []string `bson:"hosts,omitempty"`
IsMaster bool `bson:"ismaster"`
Msg string `bson:"msg"`
MaxBsonObjectSise int64 `bson:"maxBsonObjectSize"`
MaxMessageSizeBytes int64 `bson:"maxMessageSizeBytes"`
MaxWriteBatchSize int64 `bson:"maxWriteBatchSize"`
LocalTime time.Time `bson:"localTime"`
LogicalSessionTimeoutMinutes int64 `bson:"logicalSessionTimeoutMinutes"`
MaxWireVersion int64 `bson:"maxWireVersion"`
MinWireVersion int64 `bson:"minWireVersion"`
Ok int `bson:"ok"`
SetName string `bson:"setName,omitempty"`
SetVersion string `bson:"setVersion,omitempty"`
Primary string `bson:"primary,omitempty"`
Secondary bool `bson:"secondary,omitempty"`
Hidden bool `bson:"hidden,omitempty"`
ConfigSvr int `bson:"configsvr,omitempty"`
Me string `bson:"me"`
LastWrite IsMasterLastWrite `bson:"lastWrite"`
ClusterTime *ClusterTime `bson:"$clusterTime,omitempty"`
ConfigServerState *ConfigServerState `bson:"$configServerState,omitempty"`
GleStats *GleStats `bson:"$gleStats,omitempty"`
OperationTime *bson.MongoTimestamp `bson:"operationTime,omitempty"`
}