@@ -16,12 +16,9 @@ package healthcheck
1616
1717import (
1818 "context"
19- "encoding/json"
2019
21- v "github.com/hashicorp/go-version"
2220 "github.com/pkg/errors"
2321 "go.mongodb.org/mongo-driver/bson"
24- "go.mongodb.org/mongo-driver/bson/primitive"
2522 logf "sigs.k8s.io/controller-runtime/pkg/log"
2623
2724 "github.com/percona/percona-server-mongodb-operator/cmd/mongodb-healthcheck/db"
@@ -32,6 +29,7 @@ var ErrNoReplsetConfigStr = "(NotYetInitialized) no replset config has been rece
3229
3330func HealthCheckMongosLiveness (ctx context.Context , cnf * db.Config ) (err error ) {
3431 log := logf .FromContext (ctx ).WithName ("HealthCheckMongosLiveness" )
32+ ctx = logf .IntoContext (ctx , log )
3533
3634 client , err := db .Dial (ctx , cnf )
3735 if err != nil {
@@ -58,6 +56,7 @@ func HealthCheckMongosLiveness(ctx context.Context, cnf *db.Config) (err error)
5856
5957func HealthCheckMongodLiveness (ctx context.Context , cnf * db.Config , startupDelaySeconds int64 ) (_ * mongo.MemberState , err error ) {
6058 log := logf .FromContext (ctx ).WithName ("HealthCheckMongodLiveness" )
59+ ctx = logf .IntoContext (ctx , log )
6160
6261 client , err := db .Dial (ctx , cnf )
6362 if err != nil {
@@ -74,50 +73,14 @@ func HealthCheckMongodLiveness(ctx context.Context, cnf *db.Config, startupDelay
7473 return nil , errors .Wrap (err , "get isMaster response" )
7574 }
7675
77- buildInfo , err := client .RSBuildInfo (ctx )
76+ rsStatus , err := client .RSStatus (ctx )
7877 if err != nil {
79- return nil , errors .Wrap (err , "get buildInfo response" )
80- }
81-
82- replSetStatusCommand := bson.D {{Key : "replSetGetStatus" , Value : 1 }}
83- mongoVersion := v .Must (v .NewVersion (buildInfo .Version ))
84- if mongoVersion .Compare (v .Must (v .NewVersion ("4.2.1" ))) < 0 {
85- // https://docs.mongodb.com/manual/reference/command/replSetGetStatus/#syntax
86- replSetStatusCommand = append (replSetStatusCommand , primitive.E {Key : "initialSync" , Value : 1 })
87- }
88-
89- res := client .Database ("admin" ).RunCommand (ctx , replSetStatusCommand )
90- if res .Err () != nil {
91- // if we come this far, it means db connection was successful
92- // standalone mongod nodes in an unmanaged cluster doesn't need
93- // to die before they added to a replset
94- if res .Err ().Error () == ErrNoReplsetConfigStr {
78+ if err .Error () == ErrNoReplsetConfigStr {
9579 state := mongo .MemberStateUnknown
96- log .V (1 ).Info ("replSetGetStatus failed" , "err" , res . Err () .Error (), "state" , state )
80+ log .V (1 ).Info ("replSetGetStatus failed" , "err" , err .Error (), "state" , state )
9781 return & state , nil
9882 }
99- return nil , errors .Wrap (res .Err (), "get replsetGetStatus response" )
100- }
101-
102- // this is a workaround to fix decoding of empty interfaces
103- // https://jira.mongodb.org/browse/GODRIVER-988
104- rsStatus := ReplSetStatus {}
105- tempResult := bson.M {}
106- err = res .Decode (& tempResult )
107- if err != nil {
108- return nil , errors .Wrap (err , "decode replsetGetStatus response" )
109- }
110-
111- if err == nil {
112- result , err := json .Marshal (tempResult )
113- if err != nil {
114- return nil , errors .Wrap (err , "marshal temp result" )
115- }
116-
117- err = json .Unmarshal (result , & rsStatus )
118- if err != nil {
119- return nil , errors .Wrap (err , "unmarshal temp result" )
120- }
83+ return nil , errors .Wrap (err , "get replSetGetStatus response" )
12184 }
12285
12386 oplogRs := OplogRs {}
@@ -156,15 +119,12 @@ type OplogRs struct {
156119 StorageSize int64 `bson:"storageSize" json:"storageSize"`
157120}
158121
159- type ReplSetStatus struct {
160- InitialSyncStatus InitialSyncStatus `bson:"initialSyncStatus" json:"initialSyncStatus"`
161- mongo.Status `bson:",inline"`
162- }
163-
164- type InitialSyncStatus interface {}
165-
166- func CheckState (rs ReplSetStatus , startupDelaySeconds int64 , oplogSize int64 ) error {
167- uptime := rs .GetSelf ().Uptime
122+ func CheckState (rs mongo.Status , startupDelaySeconds int64 , oplogSize int64 ) error {
123+ self := rs .GetSelf ()
124+ if self == nil {
125+ return errors .New ("self member is not found" )
126+ }
127+ uptime := self .Uptime
168128
169129 switch rs .MyState {
170130 case mongo .MemberStatePrimary , mongo .MemberStateSecondary , mongo .MemberStateArbiter :
0 commit comments