@@ -21,6 +21,7 @@ import (
2121 "github.com/PeerDB-io/peerdb/flow/model"
2222 "github.com/PeerDB-io/peerdb/flow/otel_metrics"
2323 "github.com/PeerDB-io/peerdb/flow/shared"
24+ peerdb_mongo "github.com/PeerDB-io/peerdb/flow/shared/mongo"
2425 "github.com/PeerDB-io/peerdb/flow/shared/types"
2526)
2627
@@ -45,7 +46,7 @@ func NewMongoConnector(ctx context.Context, config *protos.MongoConfig) (*MongoC
4546
4647 client , err := mongo .Connect (options .Client ().
4748 SetAppName ("PeerDB Mongo Connector" ).
48- SetReadPreference (readpref .Primary ()).
49+ SetReadPreference (readpref .SecondaryPreferred ()).
4950 SetCompressors ([]string {"zstd" , "snappy" }).
5051 ApplyURI (config .Uri ))
5152 if err != nil {
@@ -80,47 +81,11 @@ func (c *MongoConnector) ConnectionActive(ctx context.Context) error {
8081}
8182
8283func (c * MongoConnector ) GetVersion (ctx context.Context ) (string , error ) {
83- db := c .client .Database ("admin" )
84-
85- var buildInfoDoc bson.M
86- if err := db .RunCommand (ctx , bson.D {bson.E {Key : "buildInfo" , Value : 1 }}).Decode (& buildInfoDoc ); err != nil {
87- return "" , fmt .Errorf ("failed to run buildInfo command: %w" , err )
88- }
89-
90- version , ok := buildInfoDoc ["version" ].(string )
91- if ! ok {
92- return "" , fmt .Errorf ("buildInfo.version is not a string, but %T" , buildInfoDoc ["version" ])
93- }
94- return version , nil
95- }
96-
97- func (c * MongoConnector ) ValidateMirrorSource (ctx context.Context , cfg * protos.FlowConnectionConfigs ) error {
98- if cfg .DoInitialSnapshot && cfg .InitialSnapshotOnly {
99- return nil
100- }
101-
102- // Check if MongoDB is configured as a replica set
103- var result bson.M
104- if err := c .client .Database ("admin" ).RunCommand (ctx , bson.D {
105- {Key : "replSetGetStatus" , Value : 1 },
106- }).Decode (& result ); err != nil {
107- return fmt .Errorf ("failed to get replica set status: %w" , err )
108- }
109-
110- // Check if this is a replica set
111- if _ , ok := result ["set" ]; ! ok {
112- return errors .New ("MongoDB is not configured as a replica set, which is required for CDC" )
113- }
114-
115- if myState , ok := result ["myState" ]; ! ok {
116- return errors .New ("myState not found in response" )
117- } else if myStateInt , ok := myState .(int32 ); ! ok {
118- return fmt .Errorf ("failed to convert myState %v to int32" , myState )
119- } else if myStateInt != 1 {
120- return fmt .Errorf ("MongoDB is not the primary node in the replica set, current state: %d" , myState )
84+ buildInfo , err := peerdb_mongo .GetBuildInfo (ctx , c .client )
85+ if err != nil {
86+ return "" , err
12187 }
122-
123- return nil
88+ return buildInfo .Version , nil
12489}
12590
12691func (c * MongoConnector ) GetTableSchema (
0 commit comments