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

PBM-1308: Improve read/write concern validation #946

Merged
merged 2 commits into from
May 15, 2024
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
3 changes: 3 additions & 0 deletions cmd/pbm-agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ func (a *Agent) Start(ctx context.Context) error {
logger := log.FromContext(ctx)
logger.Printf("pbm-agent:\n%s", version.Current().All(""))
logger.Printf("node: %s/%s", a.brief.SetName, a.brief.Me)
logger.Printf("conn level ReadConcern: %v; WriteConcern: %v",
a.leadConn.MongoOptions().ReadConcern.Level,
a.leadConn.MongoOptions().WriteConcern.W)

c, cerr := ctrl.ListenCmd(ctx, a.leadConn, a.closeCMD)

Expand Down
22 changes: 20 additions & 2 deletions pbm/connect/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,25 @@ func Direct(direct bool) MongoOption {
// If the option is not specified the default is: [readconcern.Majority].
func ReadConcern(readConcern *readconcern.ReadConcern) MongoOption {
return func(opts *options.ClientOptions) error {
if readConcern == nil {
return errors.New("ReadConcern not specified")
if err := validateReadConcern(readConcern); err != nil {
return err
}
opts.SetReadConcern(readConcern)
return nil
}
}

func validateReadConcern(readConcern *readconcern.ReadConcern) error {
if readConcern == nil {
return errors.New("ReadConcern not specified")
}
if readConcern.Level != readconcern.Local().Level &&
readConcern.Level != readconcern.Majority().Level {
return errors.New("ReadConcern level is not allowed")
}
return nil
}

// WriteConcern option sets level of acknowledgment for write operation.
// For PBM typically use: [writeconcern.W1] or [writeconcern.Majority].
// If the option is not specified the default is: [writeconcern.Majority].
Expand Down Expand Up @@ -147,6 +158,13 @@ func validateConnStringOpts(opts *options.ClientOptions) error {
if err = validateWriteConcern(opts.WriteConcern); err != nil {
return err
}
if err = validateReadConcern(opts.ReadConcern); err != nil {
return err
}
if opts.ReadConcern.Level == readconcern.Majority().Level &&
opts.WriteConcern.W == writeconcern.W1().W {
return errors.New("ReadConcern majority and WriteConcern 1 is not allowed")
}

return nil
}
Expand Down
Loading